1#if !XAMARIN && !WINDOWS_UWP
10using System.Collections.Generic;
20 [Obsolete(
"Use ViewModelBase.RefreshAsync",
false)]
23 private CslaOperation<T> _lastOperation;
24 private Action<CslaOperation<T>> _nextOperationExecutor =
null;
47 [Obsolete(
"Use RefreshAsync",
false)]
50 ExecuteOperation(operation => operation.Execute(factoryMethod));
59 [Obsolete(
"Use RefreshAsync",
false)]
60 protected override void BeginRefresh(
string factoryMethod, params
object[] factoryParameters)
62 ExecuteOperation(operation => operation.Execute(factoryMethod, factoryParameters));
68 [Obsolete(
"Use RefreshAsync",
false)]
71 _nextOperationExecutor =
null;
72 if (_lastOperation !=
null)
74 _lastOperation.Cancel();
81 private void ExecuteOperation(Action<CslaOperation<T>> operationExecutor)
85 if (_lastOperation !=
null)
86 _lastOperation.Cancel();
90 _nextOperationExecutor = operationExecutor;
97 _lastOperation =
new CslaOperation<T>(QueryCompleted);
98 operationExecutor(_lastOperation);
107 private void QueryCompleted(DataPortalResult<T> result,
bool isCanceled)
109 _lastOperation =
null;
113 var executor = _nextOperationExecutor;
114 _nextOperationExecutor =
null;
115 ExecuteOperation(executor);
119 if (isCanceled)
return;
123 if (result.Error ==
null)
126 Model = result.Object;
129 Error = result.Error;
139 internal class CslaOperation<T>
141 private readonly Action<DataPortalResult<T>,
bool> _onCompletedQuery;
142 private bool _isCanceled;
144 public CslaOperation(Action<DataPortalResult<T>,
bool> onCompletedQuery)
146 if (onCompletedQuery ==
null)
throw new ArgumentNullException(
"onCompletedQuery");
148 _onCompletedQuery = onCompletedQuery;
156 public void Execute(Action<EventHandler<DataPortalResult<T>>> factoryMethod)
158 EventHandler<DataPortalResult<T>> handler = (sender, result) => _onCompletedQuery(result, _isCanceled);
159 factoryMethod(handler);
162 public void Execute(
string factoryMethod, params
object[] factoryParameters)
164 var parameters =
new List<object>(factoryParameters) { CreateHandler() };
165 MethodCaller.CallFactoryMethod(typeof(T), factoryMethod, parameters.ToArray());
168 private Delegate CreateHandler()
170 var args = typeof(DataPortalResult<>).MakeGenericType(typeof(T));
171 System.Reflection.MethodInfo method = MethodCaller.GetNonPublicMethod(GetType(),
"QueryCompleted");
172 Delegate handler = Delegate.CreateDelegate(typeof(EventHandler<>).MakeGenericType(args),
this, method);
176 private void QueryCompleted(
object sender, EventArgs e)
178 DataPortalResult<T> result = (DataPortalResult<T>)e;
179 _onCompletedQuery(result, _isCanceled);
DataPortalResult defines the results of DataPortal operation.
ViewModel without multithreading (concurrency) bugs.
bool IsConcurentRefreshesAllowed
Allows more than one refresh operations in one time.
new void BeginRefresh(Action< EventHandler< DataPortalResult< T > > > factoryMethod)
Creates or retrieves a new instance of the Model by invoking a static factory method.
virtual void CancelRefresh()
Cancel refresh operation.
override void BeginRefresh(string factoryMethod, params object[] factoryParameters)
Creates or retrieves a new instance of the Model by invoking a static factory method.
T Model
Gets or sets the Model object.
virtual void OnRefreshed()
Method called after a refresh operation has completed.
bool IsBusy
Gets a value indicating whether this object is executing an asynchronous process.
virtual void OnRefreshing(T model)
Method called after a refresh operation has completed and before the model is updated.
Exception Error
Gets the Error object corresponding to the last asynchronous operation.
Base class used to create ViewModel objects, with pre-existing verbs for use by InvokeMethod or Invok...
@ Execute
Execute operation.