1#if !XAMARIN && !WINDOWS_UWP
10using System.Collections;
11using System.Collections.Generic;
12using System.Collections.ObjectModel;
13using System.ComponentModel;
14using System.Windows.Data;
15using System.Reflection;
34 _factoryParameters =
new ObservableCollection<object>();
35 _factoryParameters.CollectionChanged +=
36 new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_factoryParameters_CollectionChanged);
51 protected virtual void OnSaved(
object newObject, Exception error,
object userState)
57 void _factoryParameters_CollectionChanged(
58 object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
65 private Type _objectType =
null;
66 private bool _manageLifetime;
67 private string _factoryMethod =
string.Empty;
68 private ObservableCollection<object> _factoryParameters;
69 private bool _isAsynchronous;
70 private CslaDataProviderCommandManager _commandManager;
82 return _commandManager;
99 OnPropertyChanged(
new PropertyChangedEventArgs(
"ObjectType"));
113 return _manageLifetime;
117 _manageLifetime = value;
118 OnPropertyChanged(
new PropertyChangedEventArgs(
"ManageObjectLifetime"));
122 private object _dataChangedHandler;
137 return _dataChangedHandler;
141 _dataChangedHandler = value;
145 OnPropertyChanged(
new PropertyChangedEventArgs(
"DataChangedHandler"));
159 return _factoryMethod;
163 _factoryMethod = value;
164 OnPropertyChanged(
new PropertyChangedEventArgs(
"FactoryMethod"));
176 return _factoryParameters;
187 get {
return _isAsynchronous; }
188 set { _isAsynchronous = value; }
200 OnQueryFinished(value,
null,
null,
null);
201 OnPropertyChanged(
new PropertyChangedEventArgs(
"ObjectInstance"));
210 get {
return _isBusy; }
214 OnPropertyChanged(
new PropertyChangedEventArgs(
"IsBusy"));
233 private bool _firstRun =
true;
234 private bool _init =
false;
235 private bool _endInitCompete =
false;
236 private bool _endInitError =
false;
254 _endInitCompete =
true;
270 if (!IsInitialLoadEnabled)
280 _endInitError =
false;
281 OnQueryFinished(
null);
285 if (this.IsRefreshDeferred)
288 QueryRequest request =
new QueryRequest();
289 request.ObjectType = _objectType;
290 request.FactoryMethod = _factoryMethod;
291 request.FactoryParameters = _factoryParameters;
292 request.ManageObjectLifetime = _manageLifetime;
297 System.Threading.ThreadPool.QueueUserWorkItem(DoQuery, request);
302 private void DoQuery(
object state)
304 QueryRequest request = (QueryRequest)state;
305 object result =
null;
306 Exception exceptionResult =
null;
307 object[] parameters =
new List<object>(request.FactoryParameters).ToArray();
312 BindingFlags flags = BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy;
313 System.Reflection.MethodInfo factory = request.ObjectType.GetMethod(
314 request.FactoryMethod, flags,
null,
315 MethodCaller.GetParameterTypes(parameters),
null);
322 int parameterCount = parameters.Length;
323 System.Reflection.MethodInfo[] methods = request.ObjectType.GetMethods(flags);
324 foreach (System.Reflection.MethodInfo method in methods)
325 if (method.Name == request.FactoryMethod && method.GetParameters().Length == parameterCount)
336 throw new InvalidOperationException(
343 result = factory.Invoke(
null, parameters);
347 exceptionResult = ex.BusinessException;
349 catch (System.Reflection.TargetInvocationException ex)
351 if (ex.InnerException !=
null)
353 exceptionResult = ex.InnerException;
355 if (dpe !=
null && dpe.BusinessException !=
null)
359 exceptionResult = ex;
363 exceptionResult = ex;
368 exceptionResult = ex;
371 if (request.ManageObjectLifetime && result !=
null)
383 if (!_endInitCompete && exceptionResult !=
null)
384 _endInitError =
true;
387 OnQueryFinished(result, exceptionResult, (o) => {
IsBusy =
false;
return null; },
null);
390#region QueryRequest Class
392 private class QueryRequest
394 private Type _objectType;
398 get {
return _objectType; }
399 set { _objectType = value; }
402 private string _factoryMethod;
406 get {
return _factoryMethod; }
407 set { _factoryMethod = value; }
410 private ObservableCollection<object> _factoryParameters;
414 get {
return _factoryParameters; }
415 set { _factoryParameters =
416 new ObservableCollection<object>(
new List<object>(value)); }
418 private bool _manageLifetime;
422 get {
return _manageLifetime; }
423 set { _manageLifetime = value; }
432#region Cancel/Update/New/Remove
445 if (undo !=
null && _manageLifetime)
481 object result = savable;
482 Exception exceptionResult =
null;
488 ICloneable clonable = savable as ICloneable;
489 if (clonable !=
null)
494 if (undo !=
null && _manageLifetime)
499 result = savable.Save();
504 Core.ISavable original = this.Data as Core.ISavable;
505 if (original !=
null)
506 original.SaveComplete(result);
511 if (undo !=
null && _manageLifetime)
516 exceptionResult = ex;
519 OnQueryFinished(
null, exceptionResult,
null,
null);
521 OnQueryFinished(result,
null,
null,
null);
523 OnSaved(result, exceptionResult,
null);
536 IBindingList list = this.Data as IBindingList;
537 if (list !=
null && list.AllowNew)
538 return list.AddNew();
561 list = bb.
Parent as IBindingList;
563 list = this.Data as IBindingList;
564 if (list !=
null && list.AllowRemove)
This is the non-generic base class from which most business objects will be derived.
Core.IParent Parent
Provide access to the parent reference for use in child object code.
Event arguments containing a reference to the new object that was returned as a result of the Save() ...
This exception is returned for any errors occurring during the server-side DataPortal invocation.
Exception BusinessException
Gets the original server-side exception.
A strongly-typed resource class, for looking up localized strings, etc.
static string NoSuchFactoryMethod
Looks up a localized string similar to No such factory method:{0}.
Implements support for RoutedCommands that can be executed by the CslaDataProvider control.
Wraps and creates a CSLA .NET-style object that you can use as a binding source.
object AddNew()
Adds a new item to the object if the object implements IBindingList and AllowNew is true.
override void BeginQuery()
Overridden.
object ObjectInstance
Gets or sets a reference to the data object.
bool ManageObjectLifetime
Gets or sets a value indicating whether the data control should manage the lifetime of the business o...
bool IsAsynchronous
Gets or sets a value that indicates whether to perform object creation in a worker thread or in the a...
void Rebind()
Triggers WPF data binding to rebind to the data object.
IList FactoryParameters
Get the list of parameters to pass to the factory method.
override void BeginInit()
Indicates that the control is about to initialize.
Type ObjectType
Gets or sets the type of object to create an instance of.
string FactoryMethod
Gets or sets the name of the static (Shared in Visual Basic) factory method that should be called to ...
CslaDataProviderCommandManager CommandManager
Gets an object that can be used to execute Save and Undo commands on this CslaDataProvider through XA...
void Cancel()
Cancels changes to the business object, returning it to its previous state.
virtual void OnSaved(object newObject, Exception error, object userState)
Raise the Saved event when the object has been saved.
override void EndInit()
Indicates that the control has initialized.
CslaDataProvider()
Creates an instance of the object.
void RemoveItem(object sender, ExecuteEventArgs e)
Removes an item from the list if the object implements IBindingList and AllowRemove is true.
object DataChangedHandler
Gets or sets a reference to an object that will handle the DataChanged event raised by this data prov...
void Save()
Accepts changes to the business object, and commits them by calling the object's Save() method.
EventHandler< Csla.Core.SavedEventArgs > Saved
Event raised when the object has been saved.
bool IsBusy
Gets a value indicating if this object is busy.
Arguments passed to a method invoked by the Execute trigger action.
Specifies that the object can save itself.
Define the common methods used by the UI to interact with n-level undo.
void ApplyEdit()
Commits the current edit process.
void BeginEdit()
Starts a nested edit on the object.
Interface defining the interaction between a CslaDataSource and an error dialog control.
void Register(object source)
Method called by the CslaDataProvider when the error dialog should register any events it wishes to h...