CSLA.NET 6.0.0
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;
10
11namespace Csla.Server
12{
17 public class ChildDataPortal
18 {
23 public ChildDataPortal(ApplicationContext applicationContext)
24 {
25 ApplicationContext = applicationContext;
26 }
27
31 private ApplicationContext ApplicationContext { get; set; }
32
37 public object Create(Type objectType)
38 {
39 try
40 {
41 return Create(objectType, false).Result;
42 }
43 catch (AggregateException ex)
44 {
45 throw ex.InnerException;
46 }
47 }
48
56 public object Create(Type objectType, params object[] parameters)
57 {
58 try
59 {
60 return Create(objectType, true, parameters).Result;
61 }
62 catch (AggregateException ex)
63 {
64 throw ex.InnerException;
65 }
66 }
67
71 public async Task<T> CreateAsync<T>()
72 {
73 return (T) await Create(typeof(T), false).ConfigureAwait(false);
74 }
75
82 public async Task<T> CreateAsync<T>(params object[] parameters)
83 {
84 return (T)await Create(typeof(T), true, parameters).ConfigureAwait(false);
85 }
86
87 private async Task<object> Create(Type objectType, bool hasParameters, params object[] parameters)
88 {
89 DataPortalTarget obj = null;
90 var eventArgs = new DataPortalEventArgs(null, objectType, parameters, DataPortalOperations.Create);
91 try
92 {
94 //ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
95 obj.Child_OnDataPortalInvoke(eventArgs);
96 obj.MarkAsChild();
97 obj.MarkNew();
98 await obj.CreateChildAsync(parameters).ConfigureAwait(false);
99 obj.OnDataPortalInvokeComplete(eventArgs);
100 return obj.Instance;
101
102 }
103 catch (Exception ex)
104 {
105 try
106 {
107 if (obj != null)
108 obj.Child_OnDataPortalException(eventArgs, ex);
109 }
110 catch
111 {
112 // ignore exceptions from the exception handler
113 }
114 object outval = null;
115 if (obj != null) outval = obj.Instance;
116 throw new Csla.DataPortalException(
117 "ChildDataPortal.Create " + Properties.Resources.FailedOnServer, ex, outval);
118 }
119 finally
120 {
121 object reference = null;
122 if (obj != null)
123 reference = obj.Instance;
124 //ApplicationContext.DataPortalActivator.FinalizeInstance(reference);
125 }
126 }
127
132 public object Fetch(Type objectType)
133 {
134 try
135 {
136 return Fetch(objectType, false, null).Result;
137 }
138 catch (AggregateException ex)
139 {
140 throw ex.InnerException;
141 }
142 }
143
151 public object Fetch(Type objectType, params object[] parameters)
152 {
153 try
154 {
155 return Fetch(objectType, true, parameters).Result;
156 }
157 catch (AggregateException ex)
158 {
159 throw ex.InnerException;
160 }
161 }
162
166 public async Task<T> FetchAsync<T>()
167 {
168 return (T)await Fetch(typeof(T), false).ConfigureAwait(false);
169 }
170
177 public async Task<T> FetchAsync<T>(params object[] parameters)
178 {
179 return (T)await Fetch(typeof(T), true, parameters).ConfigureAwait(false);
180 }
181
182 private async Task<object> Fetch(Type objectType, bool hasParameters, params object[] parameters)
183 {
184 DataPortalTarget obj = null;
185 var eventArgs = new DataPortalEventArgs(null, objectType, parameters, DataPortalOperations.Fetch);
186 try
187 {
188 // create an instance of the business object
189 obj = ApplicationContext.CreateInstanceDI<DataPortalTarget>(ApplicationContext.CreateInstanceDI(objectType));
190 //ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
191
192 obj.Child_OnDataPortalInvoke(eventArgs);
193 obj.MarkAsChild();
194 obj.MarkOld();
195 await obj.FetchChildAsync(parameters).ConfigureAwait(false);
196 obj.Child_OnDataPortalInvokeComplete(eventArgs);
197 return obj.Instance;
198 }
199 catch (Exception ex)
200 {
201 try
202 {
203 if (obj != null)
204 obj.Child_OnDataPortalException(eventArgs, ex);
205 }
206 catch
207 {
208 // ignore exceptions from the exception handler
209 }
210 object outval = null;
211 if (obj != null) outval = obj.Instance;
212 throw new Csla.DataPortalException(
213 "ChildDataPortal.Fetch " + Properties.Resources.FailedOnServer, ex, outval);
214 }
215 //finally
216 //{
217 // ApplicationContext.DataPortalActivator.FinalizeInstance(obj.Instance);
218 //}
219 }
220
225 public void Update(object obj)
226 {
227 try
228 {
229 Update(obj, false, false, null).Wait();
230 }
231 catch (AggregateException ex)
232 {
233 throw ex.InnerException;
234 }
235 }
236
244 public void Update(object obj, params object[] parameters)
245 {
246 try
247 {
248 Update(obj, true, false, parameters).Wait();
249 }
250 catch (AggregateException ex)
251 {
252 throw ex.InnerException;
253 }
254 }
255
260 public async Task UpdateAsync(object obj)
261 {
262 await Update(obj, false, false, null).ConfigureAwait(false);
263 }
264
272 public async Task UpdateAsync(object obj, params object[] parameters)
273 {
274 await Update(obj, true, false, parameters).ConfigureAwait(false);
275 }
276
281 public void UpdateAll(object obj)
282 {
283 Update(obj, false, true, null).Wait();
284 }
285
293 public void UpdateAll(object obj, params object[] parameters)
294 {
295 Update(obj, true, true, parameters).Wait();
296 }
297
302 public async Task UpdateAllAsync(object obj)
303 {
304 await Update(obj, false, true, null).ConfigureAwait(false);
305 }
306
314 public async Task UpdateAllAsync(object obj, params object[] parameters)
315 {
316 await Update(obj, true, true, parameters).ConfigureAwait(false);
317 }
318
319 private async Task Update(object obj, bool hasParameters, bool bypassIsDirtyTest, params object[] parameters)
320 {
321 if (obj == null)
322 return;
323
324 if (obj is Core.BusinessBase busObj && busObj.IsDirty == false && bypassIsDirtyTest == false)
325 {
326 // if the object isn't dirty, then just exit
327 return;
328 }
329
330 var operation = DataPortalOperations.Update;
331 Type objectType = obj.GetType();
332 DataPortalTarget lb = ApplicationContext.CreateInstanceDI<DataPortalTarget>(obj);
333 //ApplicationContext.DataPortalActivator.InitializeInstance(lb.Instance);
334
335 try
336 {
337 lb.Child_OnDataPortalInvoke(
338 new DataPortalEventArgs(null, objectType, obj, operation));
339 await lb.UpdateChildAsync(parameters).ConfigureAwait(false);
340 lb.Child_OnDataPortalInvokeComplete(
341 new DataPortalEventArgs(null, objectType, obj, operation));
342 }
343 catch (Exception ex)
344 {
345 try
346 {
347 if (lb != null)
348 lb.Child_OnDataPortalException(
349 new DataPortalEventArgs(null, objectType, obj, operation), ex);
350 }
351 catch
352 {
353 // ignore exceptions from the exception handler
354 }
355 throw new Csla.DataPortalException(
356 "ChildDataPortal.Update " + Properties.Resources.FailedOnServer, ex, obj);
357 }
358 //finally
359 //{
360 // ApplicationContext.DataPortalActivator.FinalizeInstance(lb.Instance);
361 //}
362 }
363 }
364}
Provides consistent context information between the client and server DataPortal objects.
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
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.
ChildDataPortal(ApplicationContext applicationContext)
Creates an instance of the type.
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.