CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
DataPortalT.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataPortalT.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Client side data portal used for making asynchronous</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Globalization;
10using System.Linq;
11using System.Threading.Tasks;
12using Csla.Properties;
13
14namespace Csla
15{
23 public class DataPortal<T> : IDataPortal<T>
24 {
30
31 private class DataPortalAsyncRequest
32 {
33 public object Argument { get; set; }
34 public System.Security.Principal.IPrincipal Principal { get; set; }
35 public Csla.Core.ContextDictionary ClientContext { get; set; }
36 public Csla.Core.ContextDictionary GlobalContext { get; set; }
37 public object UserState { get; set; }
38 // passes CurrentCulture and CurrentUICulture to the async thread
39 public CultureInfo CurrentCulture;
40 public CultureInfo CurrentUICulture;
41
42 public DataPortalAsyncRequest(object argument, object userState)
43 {
44 this.Argument = argument;
45 this.Principal = Csla.ApplicationContext.User;
46 this.ClientContext = Csla.ApplicationContext.ClientContext;
47#pragma warning disable CS0618 // Type or member is obsolete
48 this.GlobalContext = Csla.ApplicationContext.GlobalContext;
49#pragma warning restore CS0618 // Type or member is obsolete
50 this.UserState = userState;
51 this.CurrentCulture = System.Globalization.CultureInfo.CurrentCulture;
52 this.CurrentUICulture = System.Globalization.CultureInfo.CurrentUICulture;
53 }
54 }
55
56 private class DataPortalAsyncResult
57 {
58 public T Result { get; set; }
59 public Csla.Core.ContextDictionary GlobalContext { get; set; }
60 public object UserState { get; set; }
61 public Exception Error { get; set; }
62
63 public DataPortalAsyncResult(T result, Csla.Core.ContextDictionary globalContext, Exception error, object userState)
64 {
65 this.Result = result;
66 this.GlobalContext = globalContext;
67 this.UserState = userState;
68 this.Error = error;
69 }
70 }
71
72 private async Task<object> DoCreateAsync(Type objectType, object criteria, bool isSync)
73 {
74 Server.DataPortalResult result = null;
75 Server.DataPortalContext dpContext = null;
76 try
77 {
78 DataPortal.OnDataPortalInitInvoke(null);
79
80 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, objectType, Server.DataPortal.GetCriteriaArray(criteria)))
81 throw new Csla.Security.SecurityException(string.Format(
83 "create",
84 objectType.Name));
85 Reflection.ServiceProviderMethodInfo method;
86 if (criteria is Server.EmptyCriteria)
87 method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<CreateAttribute>(objectType, null, false);
88 else
89 method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<CreateAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria), false);
90 var proxy = GetDataPortalProxy(objectType, method);
91
92 dpContext =
93 new Csla.Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
94
95 DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Create));
96
97 try
98 {
99 result = await proxy.Create(objectType, criteria, dpContext, isSync);
100 GlobalContext = result.GlobalContext;
101 if (isSync && proxy.IsServerRemote)
102 ApplicationContext.ContextManager.SetGlobalContext(GlobalContext);
103 }
104 catch (AggregateException ex)
105 {
106 if (ex.InnerExceptions.Count > 0)
107 {
108 if (ex.InnerExceptions[0] is Server.DataPortalException dpe)
109 HandleCreateDataPortalException(dpe, isSync, proxy);
110 }
111 throw new DataPortalException(
112 string.Format("DataPortal.Create {0}", Resources.Failed),
113 ex, null);
114 }
115 catch (Server.DataPortalException ex)
116 {
117 HandleCreateDataPortalException(ex, isSync, proxy);
118 }
119 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Create));
120 }
121 catch (Exception ex)
122 {
123 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Create, ex));
124 throw;
125 }
126 return result.ReturnObject;
127 }
128
129 private void HandleCreateDataPortalException(Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
130 {
131 HandleDataPortalException("Create", ex, isSync, proxy);
132 }
133
141 public T Create(params object[] criteria)
142 {
143 return (T)Create(typeof(T), Server.DataPortal.GetCriteriaFromArray(criteria));
144 }
145
146 internal static object Create(Type objectType, object criteria)
147 {
148 var dp = new DataPortal<object>();
149 try
150 {
151 return dp.DoCreateAsync(objectType, criteria, true).Result;
152 }
153 catch (AggregateException ex)
154 {
155 if (ex.InnerExceptions.Count > 0)
156 throw ex.InnerExceptions[0];
157 else
158 throw;
159 }
160 }
161
168 public async Task<T> CreateAsync(params object[] criteria)
169 {
170 return (T)await DoCreateAsync(typeof(T), Server.DataPortal.GetCriteriaFromArray(criteria), false);
171 }
172
191 [Obsolete]
192 public event EventHandler<DataPortalResult<T>> CreateCompleted;
193
215 [Obsolete]
216 protected virtual void OnCreateCompleted(DataPortalResult<T> e)
217 {
218 CreateCompleted?.Invoke(this, e);
219 }
220
226 [Obsolete]
227 public void BeginCreate()
228 {
229 BeginCreate(Server.EmptyCriteria.Instance);
230 }
231
238 [Obsolete]
239 public void BeginCreate(object criteria)
240 {
241 BeginCreate(criteria, null);
242 }
243
251 [Obsolete]
252 public async void BeginCreate(object criteria, object userState)
253 {
254 try
255 {
256 var obj = await CreateAsync(criteria);
257 OnCreateCompleted(new DataPortalResult<T>(obj, null, userState));
258 }
259 catch (AggregateException ex)
260 {
261 if (ex.InnerExceptions.Count > 0)
262 OnCreateCompleted(new DataPortalResult<T>(default(T), ex.Flatten().InnerExceptions[0], userState));
263 else
264 OnCreateCompleted(new DataPortalResult<T>(default(T), ex, userState));
265 }
266 catch (Exception ex)
267 {
268 OnCreateCompleted(new DataPortalResult<T>(default(T), ex, userState));
269 }
270 }
271
272 private async Task<object> DoFetchAsync(Type objectType, object criteria, bool isSync)
273 {
274 Server.DataPortalResult result = null;
275 Server.DataPortalContext dpContext = null;
276 try
277 {
278 DataPortal.OnDataPortalInitInvoke(null);
279
280 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.GetObject, objectType, Server.DataPortal.GetCriteriaArray(criteria)))
281 throw new Csla.Security.SecurityException(string.Format(
283 "get",
284 objectType.Name));
285
286 var method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<FetchAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria), false);
287 var proxy = GetDataPortalProxy(objectType, method);
288
289 dpContext =
290 new Csla.Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
291
292 DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Fetch));
293
294 try
295 {
296 result = await proxy.Fetch(objectType, criteria, dpContext, isSync);
297 GlobalContext = result.GlobalContext;
298 if (isSync && proxy.IsServerRemote)
299 ApplicationContext.ContextManager.SetGlobalContext(GlobalContext);
300 }
301 catch (AggregateException ex)
302 {
303 if (ex.InnerExceptions.Count > 0)
304 {
305 var dpe = ex.InnerExceptions[0] as Server.DataPortalException;
306 if (dpe != null)
307 HandleFetchDataPortalException(dpe, isSync, proxy);
308 }
309 throw new DataPortalException(
310 string.Format("DataPortal.Fetch {0}", Resources.Failed),
311 ex, null);
312 }
313 catch (Server.DataPortalException ex)
314 {
315 HandleFetchDataPortalException(ex, isSync, proxy);
316 }
317 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Fetch));
318 }
319 catch (Exception ex)
320 {
321 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Fetch, ex));
322 throw;
323 }
324 return result.ReturnObject;
325 }
326
327 private void HandleFetchDataPortalException(Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
328 {
329 HandleDataPortalException("Fetch", ex, isSync, proxy);
330 }
331
350 [Obsolete]
351 public event EventHandler<DataPortalResult<T>> FetchCompleted;
352
374 [Obsolete]
375 protected virtual void OnFetchCompleted(DataPortalResult<T> e)
376 {
377 FetchCompleted?.Invoke(this, e);
378 }
379
387 public T Fetch(params object[] criteria)
388 {
389 return (T)Fetch(typeof(T), Server.DataPortal.GetCriteriaFromArray(criteria));
390 }
391
392 internal static object Fetch(Type objectType, object criteria)
393 {
394 var dp = new DataPortal<object>();
395 try
396 {
397 return dp.DoFetchAsync(objectType, criteria, true).Result;
398 }
399 catch (AggregateException ex)
400 {
401 if (ex.InnerExceptions.Count > 0)
402 throw ex.InnerExceptions[0];
403 else
404 throw;
405 }
406 }
407
414 public async Task<T> FetchAsync(params object[] criteria)
415 {
416 return (T)await DoFetchAsync(typeof(T), Server.DataPortal.GetCriteriaFromArray(criteria), false);
417 }
418
424 [Obsolete]
425 public void BeginFetch()
426 {
427 BeginFetch(Server.EmptyCriteria.Instance);
428 }
429
436 [Obsolete]
437 public void BeginFetch(object criteria)
438 {
439 BeginFetch(criteria, null);
440 }
441
449 [Obsolete]
450 public async void BeginFetch(object criteria, object userState)
451 {
452 try
453 {
454 var obj = await FetchAsync(criteria);
455 OnFetchCompleted(new DataPortalResult<T>(obj, null, userState));
456 }
457 catch (AggregateException ex)
458 {
459 if (ex.InnerExceptions.Count > 0)
460 OnFetchCompleted(new DataPortalResult<T>(default(T), ex.Flatten().InnerExceptions[0], userState));
461 else
462 OnFetchCompleted(new DataPortalResult<T>(default(T), ex, userState));
463 }
464 catch (Exception ex)
465 {
466 OnFetchCompleted(new DataPortalResult<T>(default(T), ex, userState));
467 }
468 }
469
470 internal async Task<T> DoUpdateAsync(T obj, bool isSync)
471 {
472 Server.DataPortalResult result = null;
473 Server.DataPortalContext dpContext = null;
475 Type objectType = obj.GetType();
476 try
477 {
478 DataPortal.OnDataPortalInitInvoke(null);
479 DataPortalClient.IDataPortalProxy proxy = null;
480 var factoryInfo = Csla.Server.ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType);
481 if (factoryInfo != null)
482 {
483 Csla.Server.DataPortalMethodInfo method = null;
484 var factoryType = Csla.Server.FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName);
485
486 if (obj is Core.ICommandObject)
487 {
488 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj))
490 "execute",
491 objectType.Name));
492 if (factoryType != null)
493 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.ExecuteMethodName, new object[] { obj });
494 }
495 else
496 {
497 if (obj is Core.BusinessBase bbase)
498 {
499 if (bbase.IsDeleted)
500 {
501 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, obj))
503 "delete",
504 objectType.Name));
505 if (factoryType != null)
506 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.DeleteMethodName,
507 new object[] { obj });
508 }
509 // must check the same authorization rules as for DataPortal_XYZ methods
510 else if (bbase.IsNew)
511 {
512 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, obj))
514 "create",
515 objectType.Name));
516 if (factoryType != null)
517 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName,
518 new object[] { obj });
519 }
520 else
521 {
522 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj))
524 "save",
525 objectType.Name));
526 if (factoryType != null)
527 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName,
528 new object[] { obj });
529 }
530 }
531 else
532 {
533 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj))
535 "save",
536 objectType.Name));
537
538 if (factoryType != null)
539 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, factoryInfo.UpdateMethodName,
540 new object[] { obj });
541 }
542 }
543 if (method == null)
544 method = new Csla.Server.DataPortalMethodInfo();
545 proxy = GetDataPortalProxy(objectType, method.RunLocal);
546 }
547 else
548 {
549 Reflection.ServiceProviderMethodInfo method;
550 var criteria = Server.DataPortal.GetCriteriaArray(Server.EmptyCriteria.Instance);
551 if (obj is Core.ICommandObject)
552 {
553 operation = DataPortalOperations.Execute;
554 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj))
556 "execute",
557 objectType.Name));
558 method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<ExecuteAttribute>(objectType, criteria, false);
559 }
560 else
561 {
562 if (obj is Core.BusinessBase bbase)
563 {
564 if (bbase.IsDeleted)
565 {
566 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, obj))
568 "delete",
569 objectType.Name));
570 method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<DeleteSelfAttribute>(objectType, criteria, false);
571 }
572 else if (bbase.IsNew)
573 {
574 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, obj))
576 "create",
577 objectType.Name));
578 method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<InsertAttribute>(objectType, criteria, false);
579 }
580 else
581 {
582 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, obj))
584 "save",
585 objectType.Name));
586 method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<UpdateAttribute>(objectType, criteria, false);
587 }
588 }
589 else
590 {
591 method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<UpdateAttribute>(objectType, criteria, false);
592 }
593 }
594 proxy = GetDataPortalProxy(objectType, method);
595 }
596
597 dpContext =
598 new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
599
600 DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, obj, operation));
601
602 try
603 {
604 if (!proxy.IsServerRemote && ApplicationContext.AutoCloneOnUpdate)
605 {
606 // when using local data portal, automatically
607 // clone original object before saving
608 if (obj is ICloneable cloneable)
609 obj = (T)cloneable.Clone();
610 }
611 result = await proxy.Update(obj, dpContext, isSync);
612 }
613 catch (AggregateException ex)
614 {
615 if (ex.InnerExceptions.Count > 0)
616 {
617 if (ex.InnerExceptions[0] is Server.DataPortalException dpe)
618 HandleUpdateDataPortalException(dpe, isSync, proxy);
619 }
620 throw new DataPortalException(
621 string.Format("DataPortal.Update {0}", Resources.Failed),
622 ex, null);
623 }
624 catch (Server.DataPortalException ex)
625 {
626 HandleUpdateDataPortalException(ex, isSync, proxy);
627 }
628
629 GlobalContext = result.GlobalContext;
630 if (proxy.IsServerRemote && isSync)
631 ApplicationContext.ContextManager.SetGlobalContext(GlobalContext);
632
633 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, obj, operation));
634 }
635 catch (Exception ex)
636 {
637 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, obj, operation, ex));
638 throw;
639 }
640 return (T)result.ReturnObject;
641 }
642
643 private void HandleUpdateDataPortalException(Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
644 {
645 HandleDataPortalException("Update", ex, isSync, proxy);
646 }
647
648 private void HandleDataPortalException(string operation, Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
649 {
650 var result = ex.Result;
651 GlobalContext = result.GlobalContext;
652 if (proxy.IsServerRemote && isSync)
653 ApplicationContext.ContextManager.SetGlobalContext(GlobalContext);
654 var original = ex.InnerException;
655 if (original.InnerException != null)
656 original = original.InnerException;
657 throw new DataPortalException(
658 String.Format("DataPortal.{2} {0} ({1})", Resources.Failed, original.Message, operation),
659 ex.InnerException, result.ReturnObject);
660 }
661
680 [Obsolete]
681 public event EventHandler<DataPortalResult<T>> UpdateCompleted;
682
704 [Obsolete]
705 protected virtual void OnUpdateCompleted(DataPortalResult<T> e)
706 {
707 UpdateCompleted?.Invoke(this, e);
708 }
709
715 public T Update(T obj)
716 {
717 try
718 {
719 return DoUpdateAsync(obj, true).Result;
720 }
721 catch (AggregateException ex)
722 {
723 if (ex.InnerExceptions.Count > 0)
724 throw ex.InnerExceptions[0];
725 else
726 throw;
727 }
728 }
729
735 [Obsolete]
736 public void BeginUpdate(T obj)
737 {
738 BeginUpdate(obj, null);
739 }
740
747 [Obsolete]
748 public async void BeginUpdate(T obj, object userState)
749 {
750 try
751 {
752 var result = await UpdateAsync(obj);
753 OnUpdateCompleted(new DataPortalResult<T>(result, null, userState));
754 }
755 catch (AggregateException ex)
756 {
757 if (ex.InnerExceptions.Count > 0)
758 OnUpdateCompleted(new DataPortalResult<T>(default(T), ex.Flatten().InnerExceptions[0], userState));
759 else
760 OnUpdateCompleted(new DataPortalResult<T>(default(T), ex, userState));
761 }
762 catch (Exception ex)
763 {
764 OnUpdateCompleted(new DataPortalResult<T>(default(T), ex, userState));
765 }
766 }
767
773 public async Task<T> UpdateAsync(T obj)
774 {
775 return await DoUpdateAsync(obj, false);
776 }
777
778 internal async Task DoDeleteAsync(Type objectType, object criteria, bool isSync)
779 {
780 Server.DataPortalResult result = null;
781 Server.DataPortalContext dpContext = null;
782 try
783 {
784 DataPortal.OnDataPortalInitInvoke(null);
785
786 if (!Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, objectType, Server.DataPortal.GetCriteriaArray(criteria)))
788 "delete",
789 objectType.Name));
790
791 var method = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<DeleteAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria), false);
792 var proxy = GetDataPortalProxy(objectType, method);
793
794 dpContext = new Server.DataPortalContext(GetPrincipal(), proxy.IsServerRemote);
795
796 DataPortal.OnDataPortalInvoke(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Delete));
797
798 try
799 {
800 result = await proxy.Delete(objectType, criteria, dpContext, isSync);
801 }
802 catch (AggregateException ex)
803 {
804 if (ex.InnerExceptions.Count > 0)
805 {
806 var dpe = ex.InnerExceptions[0] as Server.DataPortalException;
807 if (dpe != null)
808 HandleDeleteDataPortalException(dpe, isSync, proxy);
809 }
810 throw new DataPortalException(
811 string.Format("DataPortal.Delete {0}", Resources.Failed),
812 ex, null);
813 }
814 catch (Server.DataPortalException ex)
815 {
816 HandleDeleteDataPortalException(ex, isSync, proxy);
817 }
818
819 GlobalContext = result.GlobalContext;
820 if (proxy.IsServerRemote && isSync)
821 ApplicationContext.ContextManager.SetGlobalContext(result.GlobalContext);
822
823 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Delete));
824 }
825 catch (Exception ex)
826 {
827 DataPortal.OnDataPortalInvokeComplete(new DataPortalEventArgs(dpContext, objectType, criteria, DataPortalOperations.Delete, ex));
828 throw;
829 }
830 }
831
832 private void HandleDeleteDataPortalException(Server.DataPortalException ex, bool isSync, Csla.DataPortalClient.IDataPortalProxy proxy)
833 {
834 HandleDataPortalException("Delete", ex, isSync, proxy);
835 }
836
855 [Obsolete]
856 public event EventHandler<DataPortalResult<T>> DeleteCompleted;
857
879 [Obsolete]
880 protected virtual void OnDeleteCompleted(DataPortalResult<T> e)
881 {
882 DeleteCompleted?.Invoke(this, e);
883 }
884
890 public void Delete(params object[] criteria)
891 {
892 Delete(typeof(T), Server.DataPortal.GetCriteriaFromArray(criteria));
893 }
894
895 internal static void Delete(Type objectType, object criteria)
896 {
897 var dp = new DataPortal<object>();
898 try
899 {
900 var task = dp.DoDeleteAsync(objectType, criteria, true);
901 if (!task.IsCompleted)
902 task.RunSynchronously();
903 if (task.Exception != null)
904 throw task.Exception;
905 }
906 catch (AggregateException ex)
907 {
908 if (ex.InnerExceptions.Count > 0)
909 throw ex.InnerExceptions[0];
910 else
911 throw;
912 }
913 }
914
920 [Obsolete]
921 public void BeginDelete(object criteria)
922 {
923 BeginDelete(criteria, null);
924 }
925
932 [Obsolete]
933 public async void BeginDelete(object criteria, object userState)
934 {
935 try
936 {
937 await DoDeleteAsync(typeof(T), criteria, true);
938 OnDeleteCompleted(new DataPortalResult<T>(default(T), null, userState));
939 }
940 catch (AggregateException ex)
941 {
942 if (ex.InnerExceptions.Count > 0)
943 OnDeleteCompleted(new DataPortalResult<T>(default(T), ex.Flatten().InnerExceptions[0], userState));
944 else
945 OnDeleteCompleted(new DataPortalResult<T>(default(T), ex, userState));
946 }
947 catch (Exception ex)
948 {
949 OnDeleteCompleted(new DataPortalResult<T>(default(T), ex, userState));
950 }
951 }
952
958 public async Task DeleteAsync(params object[] criteria)
959 {
960 await DoDeleteAsync(typeof(T), Server.DataPortal.GetCriteriaFromArray(criteria), false);
961 }
962
966 [Obsolete]
967 public event EventHandler<DataPortalResult<T>> ExecuteCompleted;
968
973 [Obsolete]
974 protected virtual void OnExecuteCompleted(DataPortalResult<T> e)
975 {
976 ExecuteCompleted?.Invoke(this, e);
977 }
978
984 public T Execute(T command)
985 {
986 return Update(command);
987 }
988
994 [Obsolete]
995 public void BeginExecute(T command)
996 {
997 BeginExecute(command, null);
998 }
999
1006 [Obsolete]
1007 public async void BeginExecute(T command, object userState)
1008 {
1009 try
1010 {
1011 var result = await UpdateAsync(command);
1012 OnExecuteCompleted(new DataPortalResult<T>(result, null, userState));
1013 }
1014 catch (AggregateException ex)
1015 {
1016 if (ex.InnerExceptions.Count > 0)
1017 OnExecuteCompleted(new DataPortalResult<T>(default(T), ex.Flatten().InnerExceptions[0], userState));
1018 else
1019 OnExecuteCompleted(new DataPortalResult<T>(default(T), ex, userState));
1020 }
1021 catch (Exception ex)
1022 {
1023 OnExecuteCompleted(new DataPortalResult<T>(default(T), ex, userState));
1024 }
1025 }
1026
1032 public async Task<T> ExecuteAsync(T command)
1033 {
1034 return await DoUpdateAsync(command, false);
1035 }
1036
1037 private static DataPortalClient.IDataPortalProxy GetDataPortalProxy(Type objectType, Reflection.ServiceProviderMethodInfo method)
1038 {
1039 if (method != null)
1040 return GetDataPortalProxy(objectType, method.MethodInfo.RunLocal());
1041 else
1042 return GetDataPortalProxy(objectType, false);
1043 }
1044
1045 private static DataPortalClient.IDataPortalProxy GetDataPortalProxy(Type objectType, bool forceLocal)
1046 {
1047 if (forceLocal || ApplicationContext.IsOffline)
1048 {
1049 return new DataPortalClient.LocalProxy();
1050 }
1051 else
1052 {
1053 // load dataportal factory if loaded
1054 if (DataPortal.ProxyFactory == null)
1055 DataPortal.LoadDataPortalProxyFactory();
1056
1057 return DataPortal.ProxyFactory.Create(objectType);
1058 }
1059 }
1060
1061 private static System.Security.Principal.IPrincipal GetPrincipal()
1062 {
1063 if (ApplicationContext.AuthenticationType == "Windows")
1064 {
1065 // Windows integrated security
1066 return null;
1067 }
1068 else
1069 {
1070 // we assume using the CSLA framework security
1071 return ApplicationContext.User;
1072 }
1073 }
1074 }
1075
1076 internal static class Extensions
1077 {
1078 internal static bool RunLocal(this System.Reflection.MethodInfo t)
1079 {
1080 return t.CustomAttributes.Count(a => a.AttributeType.Equals(typeof(RunLocalAttribute))) > 0;
1081 }
1082 }
1083}
Dictionary type that is serializable with the SerializationFormatterFactory.GetFormatter().
Provides information about the DataPortal call.
This is the client-side DataPortal.
Definition: DataPortalT.cs:24
void BeginCreate(object criteria)
Called by a factory method in a business class or by the UI to create a new object,...
Definition: DataPortalT.cs:239
async void BeginExecute(T command, object userState)
Called by a factory method in a business class or by the UI to execute a command object.
async void BeginFetch(object criteria, object userState)
Called by a factory method in a business class or by the UI to Fetch a new object,...
Definition: DataPortalT.cs:450
void Delete(params object[] criteria)
Called by a factory method in a business class or by the UI to delete an object.
Definition: DataPortalT.cs:890
virtual void OnCreateCompleted(DataPortalResult< T > e)
Raises the event.
Definition: DataPortalT.cs:216
T Execute(T command)
Called by a factory method in a business class or by the UI to execute a command object.
Definition: DataPortalT.cs:984
async void BeginUpdate(T obj, object userState)
Called by a factory method in a business class or by the UI to update an object.
Definition: DataPortalT.cs:748
virtual void OnUpdateCompleted(DataPortalResult< T > e)
Raises the event.
Definition: DataPortalT.cs:705
async Task< T > CreateAsync(params object[] criteria)
Called by a factory method in a business class or by the UI to create a new object,...
Definition: DataPortalT.cs:168
virtual void OnExecuteCompleted(DataPortalResult< T > e)
Raises the ExecuteCompleted event.
Definition: DataPortalT.cs:974
async Task DeleteAsync(params object[] criteria)
Called by a factory method in a business class or by the UI to delete an object.
Definition: DataPortalT.cs:958
EventHandler< DataPortalResult< T > > FetchCompleted
Event raised when the operation has completed.
Definition: DataPortalT.cs:351
EventHandler< DataPortalResult< T > > DeleteCompleted
Event raised when the operation has completed.
Definition: DataPortalT.cs:856
void BeginExecute(T command)
Called by a factory method in a business class or by the UI to execute a command object.
Definition: DataPortalT.cs:995
T Update(T obj)
Called by a factory method in a business class or by the UI to update an object.
Definition: DataPortalT.cs:715
void BeginUpdate(T obj)
Called by a factory method in a business class or by the UI to update an object.
Definition: DataPortalT.cs:736
T Create(params object[] criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Definition: DataPortalT.cs:141
virtual void OnDeleteCompleted(DataPortalResult< T > e)
Raises the event.
Definition: DataPortalT.cs:880
async Task< T > ExecuteAsync(T command)
Called by a factory method in a business class or by the UI to execute a command object.
virtual void OnFetchCompleted(DataPortalResult< T > e)
Raises the event.
Definition: DataPortalT.cs:375
EventHandler< DataPortalResult< T > > UpdateCompleted
Event raised when the operation has completed.
Definition: DataPortalT.cs:681
EventHandler< DataPortalResult< T > > ExecuteCompleted
Event indicating an execute operation is complete.
Definition: DataPortalT.cs:967
T Fetch(params object[] criteria)
Called by a factory method in a business class to Fetch a new object, which is loaded with default va...
Definition: DataPortalT.cs:387
void BeginFetch()
Called by a factory method in a business class or by the UI to Fetch a new object,...
Definition: DataPortalT.cs:425
Csla.Core.ContextDictionary GlobalContext
Gets a reference to the global context returned from the background thread and/or server.
Definition: DataPortalT.cs:29
async Task< T > UpdateAsync(T obj)
Called by a factory method in a business class or by the UI to update an object.
Definition: DataPortalT.cs:773
void BeginCreate()
Called by a factory method in a business class or by the UI to create a new object,...
Definition: DataPortalT.cs:227
EventHandler< DataPortalResult< T > > CreateCompleted
Event raised when the operation has completed.
Definition: DataPortalT.cs:192
static object Create(Type objectType, object criteria)
Called by a factory method in a business class to create a new object, which is loaded with default v...
Definition: DataPortal.cs:91
async void BeginCreate(object criteria, object userState)
Called by a factory method in a business class or by the UI to create a new object,...
Definition: DataPortalT.cs:252
async Task< T > FetchAsync(params object[] criteria)
Called by a factory method in a business class or by the UI to Fetch a new object,...
Definition: DataPortalT.cs:414
void BeginDelete(object criteria)
Called by a factory method in a business class or by the UI to delete an object.
Definition: DataPortalT.cs:921
void BeginFetch(object criteria)
Called by a factory method in a business class or by the UI to Fetch a new object,...
Definition: DataPortalT.cs:437
async void BeginDelete(object criteria, object userState)
Called by a factory method in a business class or by the UI to delete an object.
Definition: DataPortalT.cs:933
DataPortalResult defines the results of DataPortal operation.
Specifies a method used by the server-side data portal to delete domain object data during an update ...
Specifies a method used by the server-side data portal to load existing data into the domain object.
A strongly-typed resource class, for looking up localized strings, etc.
static string Failed
Looks up a localized string similar to failed.
static string UserNotAuthorizedException
Looks up a localized string similar to User not authorized to {0} object type {1}.
Tracks the business rules for a business object.
static bool HasPermission(AuthorizationActions action, Type objectType)
Checks per-type authorization rules.
Provides consistent context information between the client and server DataPortal objects.
Server-side data portal implementation that invokes an object factory rather than directly interactin...
static IObjectFactoryLoader FactoryLoader
Gets or sets a delegate reference to the method called to create instances of factory objects as requ...
Specifies that the data portal should invoke a factory object rather than the business object.
Interface implemented by client-side data portal proxy objects.
Interface defining the members of the data portal type.
Definition: IDataPortal.cs:21
Type GetFactoryType(string factoryName)
Returns the type of the factory object.
@ Error
Represents a serious business rule violation that should cause an object to be considered invalid.
DataPortalOperations
List of data portal operations.