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.
DataPortalTarget.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="DataPortalTarget.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Encapsulates server-side data portal invocations</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Concurrent;
10using System.Threading.Tasks;
11using Csla.Reflection;
12
13namespace Csla.Server
14{
15 internal class DataPortalTarget : LateBoundObject
16 {
17 private static readonly ConcurrentDictionary<Type, DataPortalMethodNames> _methodNameList =
18 new ConcurrentDictionary<Type, DataPortalMethodNames>();
19 private readonly IDataPortalTarget _target;
20 private readonly DataPortalMethodNames _methodNames;
21
22 public DataPortalTarget(object obj)
23 : base(obj)
24 {
25 _target = obj as IDataPortalTarget;
26 _methodNames = _methodNameList.GetOrAdd(obj.GetType(),
27 (t) => DataPortalMethodNames.Default);
28 }
29
30 public void OnDataPortalInvoke(DataPortalEventArgs eventArgs)
31 {
32 if (_target != null)
33 _target.DataPortal_OnDataPortalInvoke(eventArgs);
34 else
35 CallMethodIfImplemented(_methodNames.OnDataPortalInvoke, eventArgs);
36 }
37
38 public void Child_OnDataPortalInvoke(DataPortalEventArgs eventArgs)
39 {
40 if (_target != null)
41 _target.Child_OnDataPortalInvoke(eventArgs);
42 else
43 CallMethodIfImplemented("Child_OnDataPortalInvoke", eventArgs);
44 }
45
46
47 public void OnDataPortalInvokeComplete(DataPortalEventArgs eventArgs)
48 {
49 if (_target != null)
50 _target.DataPortal_OnDataPortalInvokeComplete(eventArgs);
51 else
52 CallMethodIfImplemented(_methodNames.OnDataPortalInvokeComplete, eventArgs);
53 }
54
55 internal void Child_OnDataPortalInvokeComplete(DataPortalEventArgs eventArgs)
56 {
57 if (_target != null)
58 _target.Child_OnDataPortalInvokeComplete(eventArgs);
59 else
60 CallMethodIfImplemented("Child_OnDataPortalInvokeComplete", eventArgs);
61 }
62
63 internal void OnDataPortalException(DataPortalEventArgs eventArgs, Exception ex)
64 {
65 if (_target != null)
66 _target.DataPortal_OnDataPortalException(eventArgs, ex);
67 else
68 CallMethodIfImplemented(_methodNames.OnDataPortalException, eventArgs, ex);
69 }
70
71
72 internal void Child_OnDataPortalException(DataPortalEventArgs eventArgs, Exception ex)
73 {
74 if (_target != null)
75 _target.Child_OnDataPortalException(eventArgs, ex);
76 else
77 CallMethodIfImplemented("Child_OnDataPortalException", eventArgs, ex);
78 }
79
80 public void ThrowIfBusy()
81 {
82 if (Instance is Csla.Core.ITrackStatus busy && busy.IsBusy)
83 throw new InvalidOperationException(string.Format("{0}.IsBusy == true", Instance.GetType().Name));
84 }
85
86 public void MarkNew()
87 {
88 if (_target != null)
89 _target.MarkNew();
90 else
91 CallMethodIfImplemented("MarkNew");
92 }
93
94 public void MarkAsChild()
95 {
96 if (_target != null)
97 _target.MarkAsChild();
98 else
99 CallMethodIfImplemented("MarkAsChild");
100 }
101
102 internal void MarkOld()
103 {
104 if (_target != null)
105 _target.MarkOld();
106 else
107 CallMethodIfImplemented("MarkOld");
108 }
109
110 private async Task InvokeOperationAsync<T>(object criteria, bool isSync)
111 where T : DataPortalOperationAttribute
112 {
113 object[] parameters = DataPortal.GetCriteriaArray(criteria);
114 await CallMethodTryAsyncDI<T>(isSync, parameters).ConfigureAwait(false);
115 }
116
117 public async Task CreateAsync(object criteria, bool isSync)
118 {
119 await InvokeOperationAsync<CreateAttribute>(criteria, isSync).ConfigureAwait(false);
120 }
121
122 public async Task CreateChildAsync(params object[] parameters)
123 {
124 await CallMethodTryAsyncDI<CreateChildAttribute>(false, parameters).ConfigureAwait(false);
125 }
126
127 public async Task FetchAsync(object criteria, bool isSync)
128 {
129 await InvokeOperationAsync<FetchAttribute>(criteria, isSync).ConfigureAwait(false);
130 }
131
132 public async Task FetchChildAsync(params object[] parameters)
133 {
134 await CallMethodTryAsyncDI<FetchChildAttribute>(false, parameters).ConfigureAwait(false);
135 }
136
137
138 public async Task UpdateAsync(bool isSync)
139 {
140 if (Instance is Core.BusinessBase busObj)
141 {
142 if (busObj.IsDeleted)
143 {
144 if (!busObj.IsNew)
145 {
146 // tell the object to delete itself
147 await InvokeOperationAsync<DeleteSelfAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(false);
148 }
149 MarkNew();
150 }
151 else
152 {
153 if (busObj.IsNew)
154 {
155 // tell the object to insert itself
156 await InvokeOperationAsync<InsertAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(false);
157 }
158 else
159 {
160 // tell the object to update itself
161 await InvokeOperationAsync<UpdateAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(false);
162 }
163 MarkOld();
164 }
165 }
166 else
167 {
168 // this is an updatable collection or some other
169 // non-BusinessBase type of object
170 // tell the object to update itself
171 await InvokeOperationAsync<UpdateAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(false);
172 MarkOld();
173 }
174 }
175
176 public async Task UpdateChildAsync(params object[] parameters)
177 {
178 // tell the business object to update itself
179 if (Instance is Core.BusinessBase busObj)
180 {
181 if (busObj.IsDeleted)
182 {
183 if (!busObj.IsNew)
184 {
185 // tell the object to delete itself
186 await CallMethodTryAsyncDI<DeleteSelfChildAttribute>(false, parameters).ConfigureAwait(false);
187 MarkNew();
188 }
189 }
190 else
191 {
192 if (busObj.IsNew)
193 {
194 // tell the object to insert itself
195 await CallMethodTryAsyncDI<InsertChildAttribute>(false, parameters).ConfigureAwait(false);
196 }
197 else
198 {
199 // tell the object to update itself
200 await CallMethodTryAsyncDI<UpdateChildAttribute>(false, parameters).ConfigureAwait(false);
201 }
202 MarkOld();
203 }
204
205 }
206 else if (Instance is Core.ICommandObject)
207 {
208 // tell the object to update itself
209 await CallMethodTryAsyncDI<ExecuteChildAttribute>(false, parameters).ConfigureAwait(false);
210 }
211 else
212 {
213 // this is an updatable collection or some other
214 // non-BusinessBase type of object
215 // tell the object to update itself
216 await CallMethodTryAsyncDI<UpdateChildAttribute>(false, parameters).ConfigureAwait(false);
217 MarkOld();
218 }
219 }
220
221 public async Task ExecuteAsync(bool isSync)
222 {
223 await InvokeOperationAsync<ExecuteAttribute>(EmptyCriteria.Instance, isSync).ConfigureAwait(false);
224 }
225
226 public async Task DeleteAsync(object criteria, bool isSync)
227 {
228 await InvokeOperationAsync<DeleteAttribute>(criteria, isSync).ConfigureAwait(false);
229 }
230 }
231
232 internal class DataPortalMethodNames
233 {
234 public static readonly DataPortalMethodNames Default = new DataPortalMethodNames();
235 public string OnDataPortalInvoke { get; set; } = "DataPortal_OnDataPortalInvoke";
236 public string OnDataPortalInvokeComplete { get; set; } = "DataPortal_OnDataPortalInvokeComplete";
237 public string OnDataPortalException { get; set; } = "DataPortal_OnDataPortalException";
238 }
239}
Enables simple invocation of methods against the contained object using late binding.
object CallMethodIfImplemented(string method)
Uses reflection to dynamically invoke a method if that method is implemented on the target object.
object Instance
Object instance managed by LateBoundObject.
bool IsBusy
Gets a value indicating whether the object, or any of the object's child objects, are busy running an...
Definition: INotifyBusy.cs:29
Defines the common properties required objects that track their own status.
Definition: ITrackStatus.cs:17
@ Default
Default value, rule can run in any context