Recipe: CM with WCF RIA Services

Sep 17, 2010 at 2:43 PM

Hi!

I am building an app (SL) where I use CM and WCF RIA Services.  Anyone doing the same might find the following useful:

1) Load Data:

    public class LoadData<T> : IResult
        where T : Entity
    {
        private readonly DomainContext _ctx;
        private readonly EntityQuery<T> _qry;
        private readonly LoadBehavior _loadBehavior;

        private readonly Action<LoadOperation<T>> _completedAction;

        public LoadData(DomainContext ctx, EntityQuery<T> qry, LoadBehavior loadBehavior = LoadBehavior.MergeIntoCurrent, Action<LoadOperation<T>> completedAction = null)
        {
            _ctx = ctx;
            _qry = qry;
            _loadBehavior = loadBehavior;
            _completedAction = completedAction;
        }

        public void Execute(ActionExecutionContext context = null)
        {
            _ctx.Load(_qry, _loadBehavior, lo =>
            {
                if (lo.HasError) lo.MarkErrorAsHandled();
                if (_completedAction != null)
                    Caliburn.Micro.Execute.OnUIThread(() => _completedAction(lo));
                Completed(thisnew ResultCompletionEventArgs { Error = lo.Error, WasCancelled = false });
            }, null);
        }

        public event EventHandler<ResultCompletionEventArgs> Completed;
    }

    public static partial class Extensions
    {
        public static IResult LoadData<T>(this DomainContext ctx, EntityQuery<T> qry, LoadBehavior loadBehavior = LoadBehavior.MergeIntoCurrent, Action<LoadOperation<T>> completedAction = null)
            where T : Entity
        {
            return new LoadData<T>(ctx, qry, loadBehavior, completedAction);
        }
    }

Please observe that the provided action will be called even if an error is returned.  This is how I prefer it.  You could easily change this of course.

 

2) Saving Data:

    public class SaveData : IResult
    {
        private readonly DomainContext _ctx;

        private readonly Action<SubmitOperation> _completedAction;

        public SaveData(DomainContext ctx, Action<SubmitOperation> completedAction = null)
        {
            _ctx = ctx;
            _completedAction = completedAction;
        }

        public void Execute(ActionExecutionContext context = null)
        {
            _ctx.SubmitChanges(so =>
            {
                if (so.HasError) so.MarkErrorAsHandled();
                if (_completedAction != null)
                    Caliburn.Micro.Execute.OnUIThread(() => _completedAction(so));
                Completed(thisnew ResultCompletionEventArgs { Error = so.Error, WasCancelled = false });
            }, null);
        }

        public event EventHandler<ResultCompletionEventArgs> Completed;
    }

    public static partial class Extensions
    {
        public static IResult SubmitData(this DomainContext ctx, Action<SubmitOperation> completedAction = null)
        {
            return new SaveData(ctx, completedAction);
        }
    }
Same observation as above...
3) Loading a user from the Authentication service:
    public class LoadUserData : IResult
    {
        private readonly AuthenticationService _svc;

        public LoadUserData(AuthenticationService svc)
        {
            _svc = svc;
        }

        public void Execute(ActionExecutionContext context = null)
        {
            _svc.LoadUser(lo =>
            {
                if (lo.HasError) lo.MarkErrorAsHandled();
                Completed(thisnew ResultCompletionEventArgs { Error = lo.Error, WasCancelled = false });
            }, null);
        }

        public event EventHandler<ResultCompletionEventArgs> Completed;
    }

    public static partial class Extensions
    {
        public static IResult LoadUserData(this AuthenticationService svc)
        {
            return new LoadUserData(svc);
        }
    }
I will add more soon...
Coordinator
Sep 17, 2010 at 3:21 PM

Nice work! This might make a good recipe for the site. Would you consider writing a short article describing this and including these code samples along with it? If so, you can email it to me and I will get it into the recipes section.

Sep 17, 2010 at 3:27 PM

Thanks!  Let me get a bit further and I will try and put together an article and a sample.