BeginRefresh design

BeginRefresh design

Old forum URL: forums.lhotka.net/forums/t/8164.aspx


Cine posted on Tuesday, December 15, 2009

I am wondering why the VievModelBase.BeginRefresh was designed the way it was...

The current one requires me to make code like this:
BeginRefresh("GetList", 0);

Whereas I have a hard time accepting random string references, and would much rather have something like:
BeginRefresh(evt => mymodel.GetList(0, evt));
a little extra typing, but typesafe and rename safe

Or with no extra params its just
BeginRefresh(mymodel.GetList)

Index: ViewModelBase.cs
===================================================================
--- ViewModelBase.cs    (revision 4491)
+++ ViewModelBase.cs    (working copy)
@@ -555,6 +555,25 @@
     /// Creates or retrieves a new instance of the
     /// Model by invoking a static factory method.
     /// </summary>
+    /// <param name="action">A call to the static factory method.</param>
+    protected virtual void BeginRefresh(Action<EventHandler<DataPortalResult<T>>> action)
+    {
+       try
+       {
+          Error = null;
+          IsBusy = true;
+          action(QueryCompleted);
+       }
+       catch (Exception ex)
+       {
+          Error = ex;
+          IsBusy = false;
+       }
+    }
+    /// <summary>
+    /// Creates or retrieves a new instance of the
+    /// Model by invoking a static factory method.
+    /// </summary>
     /// <param name="factoryMethod">Name of the static factory method.</param>
     protected virtual void BeginRefresh(string factoryMethod)
     {
@@ -569,15 +588,13 @@
       return handler;
     }
 
-
-    private void QueryCompleted(object sender, EventArgs e)
+     private void QueryCompleted(object sender, DataPortalResult<T> e)
     {
       IsBusy = false;
-      var eventArgs = (IDataPortalResult)e;
-      if (eventArgs.Error == null)
-        Model = (T)eventArgs.Object;
+       if (e.Error == null)
+        Model = e.Object;
       else
-        Error = eventArgs.Error;
+        Error = e.Error;
       OnRefreshed();
     }
 

lukky replied on Tuesday, December 15, 2009

I like the version using lambda better.

My $0.02

triplea replied on Tuesday, December 15, 2009

That would be cool. A bit like the improvement in 3.6.2 where RegisterProperty overloads were added using Lambdas instead of strings.

RockfordLhotka replied on Tuesday, December 15, 2009

This is a good idea, I will add it to the wish list.

Copyright (c) Marimer LLC