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.
ChildDataPortal.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="ChildDataPortal.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Invoke data portal methods on child</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Threading.Tasks;
10using Csla.Reflection;
11
12namespace Csla.Server
13{
18 public class ChildDataPortal
19 {
24 public object Create(Type objectType)
25 {
26 try
27 {
28 return Create(objectType, false).Result;
29 }
30 catch (AggregateException ex)
31 {
32 throw ex.InnerException;
33 }
34 }
35
43 public object Create(Type objectType, params object[] parameters)
44 {
45 try
46 {
47 return Create(objectType, true, parameters).Result;
48 }
49 catch (AggregateException ex)
50 {
51 throw ex.InnerException;
52 }
53 }
54
58 public async Task<T> CreateAsync<T>()
59 {
60 return (T) await Create(typeof(T), false).ConfigureAwait(false);
61 }
62
69 public async Task<T> CreateAsync<T>(params object[] parameters)
70 {
71 return (T)await Create(typeof(T), true, parameters).ConfigureAwait(false);
72 }
73
74 private async Task<object> Create(Type objectType, bool hasParameters, params object[] parameters)
75 {
76 DataPortalTarget obj = null;
77 var eventArgs = new DataPortalEventArgs(null, objectType, parameters, DataPortalOperations.Create);
78 try
79 {
80 obj = new DataPortalTarget(ApplicationContext.DataPortalActivator.CreateInstance(objectType));
81 ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
82 obj.Child_OnDataPortalInvoke(eventArgs);
83 obj.MarkAsChild();
84 obj.MarkNew();
85 await obj.CreateChildAsync(parameters).ConfigureAwait(false);
86 obj.OnDataPortalInvokeComplete(eventArgs);
87 return obj.Instance;
88
89 }
90 catch (Exception ex)
91 {
92 try
93 {
94 if (obj != null)
95 obj.Child_OnDataPortalException(eventArgs, ex);
96 }
97 catch
98 {
99 // ignore exceptions from the exception handler
100 }
101 object outval = null;
102 if (obj != null) outval = obj.Instance;
103 throw new Csla.DataPortalException(
104 "ChildDataPortal.Create " + Properties.Resources.FailedOnServer, ex, outval);
105 }
106 finally
107 {
108 object reference = null;
109 if (obj != null)
110 reference = obj.Instance;
111 ApplicationContext.DataPortalActivator.FinalizeInstance(reference);
112 }
113 }
114
119 public object Fetch(Type objectType)
120 {
121 try
122 {
123 return Fetch(objectType, false, null).Result;
124 }
125 catch (AggregateException ex)
126 {
127 throw ex.InnerException;
128 }
129 }
130
138 public object Fetch(Type objectType, params object[] parameters)
139 {
140 try
141 {
142 return Fetch(objectType, true, parameters).Result;
143 }
144 catch (AggregateException ex)
145 {
146 throw ex.InnerException;
147 }
148 }
149
153 public async Task<T> FetchAsync<T>()
154 {
155 return (T)await Fetch(typeof(T), false).ConfigureAwait(false);
156 }
157
164 public async Task<T> FetchAsync<T>(params object[] parameters)
165 {
166 return (T)await Fetch(typeof(T), true, parameters).ConfigureAwait(false);
167 }
168
169 private async Task<object> Fetch(Type objectType, bool hasParameters, params object[] parameters)
170 {
171 DataPortalTarget obj = null;
172 var eventArgs = new DataPortalEventArgs(null, objectType, parameters, DataPortalOperations.Fetch);
173 try
174 {
175 // create an instance of the business object
176 obj = new DataPortalTarget(ApplicationContext.DataPortalActivator.CreateInstance(objectType));
177 ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
178
179 obj.Child_OnDataPortalInvoke(eventArgs);
180 obj.MarkAsChild();
181 obj.MarkOld();
182 await obj.FetchChildAsync(parameters).ConfigureAwait(false);
183 obj.Child_OnDataPortalInvokeComplete(eventArgs);
184 return obj.Instance;
185 }
186 catch (Exception ex)
187 {
188 try
189 {
190 if (obj != null)
191 obj.Child_OnDataPortalException(eventArgs, ex);
192 }
193 catch
194 {
195 // ignore exceptions from the exception handler
196 }
197 object outval = null;
198 if (obj != null) outval = obj.Instance;
199 throw new Csla.DataPortalException(
200 "ChildDataPortal.Fetch " + Properties.Resources.FailedOnServer, ex, outval);
201 }
202 finally
203 {
204 ApplicationContext.DataPortalActivator.FinalizeInstance(obj.Instance);
205 }
206 }
207
212 public void Update(object obj)
213 {
214 try
215 {
216 Update(obj, false, false, null).Wait();
217 }
218 catch (AggregateException ex)
219 {
220 throw ex.InnerException;
221 }
222 }
223
231 public void Update(object obj, params object[] parameters)
232 {
233 try
234 {
235 Update(obj, true, false, parameters).Wait();
236 }
237 catch (AggregateException ex)
238 {
239 throw ex.InnerException;
240 }
241 }
242
247 public async Task UpdateAsync(object obj)
248 {
249 await Update(obj, false, false, null).ConfigureAwait(false);
250 }
251
259 public async Task UpdateAsync(object obj, params object[] parameters)
260 {
261 await Update(obj, true, false, parameters).ConfigureAwait(false);
262 }
263
268 public void UpdateAll(object obj)
269 {
270 Update(obj, false, true, null).Wait();
271 }
272
280 public void UpdateAll(object obj, params object[] parameters)
281 {
282 Update(obj, true, true, parameters).Wait();
283 }
284
289 public async Task UpdateAllAsync(object obj)
290 {
291 await Update(obj, false, true, null).ConfigureAwait(false);
292 }
293
301 public async Task UpdateAllAsync(object obj, params object[] parameters)
302 {
303 await Update(obj, true, true, parameters).ConfigureAwait(false);
304 }
305
306 private async Task Update(object obj, bool hasParameters, bool bypassIsDirtyTest, params object[] parameters)
307 {
308 if (obj == null)
309 return;
310
311 if (obj is Core.BusinessBase busObj && busObj.IsDirty == false && bypassIsDirtyTest == false)
312 {
313 // if the object isn't dirty, then just exit
314 return;
315 }
316
317 var operation = DataPortalOperations.Update;
318 Type objectType = obj.GetType();
319 DataPortalTarget lb = new DataPortalTarget(obj);
320 ApplicationContext.DataPortalActivator.InitializeInstance(lb.Instance);
321
322 try
323 {
324 lb.Child_OnDataPortalInvoke(
325 new DataPortalEventArgs(null, objectType, obj, operation));
326 await lb.UpdateChildAsync(parameters).ConfigureAwait(false);
327 lb.Child_OnDataPortalInvokeComplete(
328 new DataPortalEventArgs(null, objectType, obj, operation));
329 }
330 catch (Exception ex)
331 {
332 try
333 {
334 if (lb != null)
335 lb.Child_OnDataPortalException(
336 new DataPortalEventArgs(null, objectType, obj, operation), ex);
337 }
338 catch
339 {
340 // ignore exceptions from the exception handler
341 }
342 throw new Csla.DataPortalException(
343 "ChildDataPortal.Update " + Properties.Resources.FailedOnServer, ex, obj);
344 }
345 finally
346 {
347 ApplicationContext.DataPortalActivator.FinalizeInstance(lb.Instance);
348 }
349 }
350 }
351}
Provides information about the DataPortal call.
This exception is returned for any errors occurring during the server-side DataPortal invocation.
Invoke data portal methods on child objects.
void Update(object obj, params object[] parameters)
Update a business object.
async Task UpdateAllAsync(object obj, params object[] parameters)
Update a business object.
void Update(object obj)
Update a business object.
async Task UpdateAsync(object obj, params object[] parameters)
Update a business object.
async Task< T > FetchAsync< T >()
Get an existing business object.
object Create(Type objectType)
Create a new business object.
object Fetch(Type objectType)
Get an existing business object.
async Task UpdateAllAsync(object obj)
Update a business object.
void UpdateAll(object obj, params object[] parameters)
Update a business object.
object Create(Type objectType, params object[] parameters)
Create a new business object.
void UpdateAll(object obj)
Update a business object.
object Fetch(Type objectType, params object[] parameters)
Get an existing business object.
async Task< T > CreateAsync< T >()
Create a new business object.
async Task UpdateAsync(object obj)
Update a business object.
DataPortalOperations
List of data portal operations.