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.
SimpleDataPortal.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="SimpleDataPortal.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Implements the server-side DataPortal as discussed</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Threading.Tasks;
10using Csla.Properties;
11using Csla.Reflection;
12
13namespace Csla.Server
14{
20 {
30 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
31 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
32 public async Task<DataPortalResult> Create(
33 Type objectType, object criteria, DataPortalContext context, bool isSync)
34 {
35 DataPortalTarget obj = null;
36 var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Create);
37 try
38 {
39 obj = new DataPortalTarget(ApplicationContext.DataPortalActivator.CreateInstance(objectType));
40 ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
41 obj.OnDataPortalInvoke(eventArgs);
42 obj.MarkNew();
43 await obj.CreateAsync(criteria, isSync);
44 obj.ThrowIfBusy();
45 obj.OnDataPortalInvokeComplete(eventArgs);
46 return new DataPortalResult(obj.Instance);
47 }
48 catch (Exception ex)
49 {
50 try
51 {
52 if (obj != null)
53 obj.OnDataPortalException(eventArgs, ex);
54 }
55 catch
56 {
57 // ignore exceptions from the exception handler
58 }
59 object outval = null;
60 if (obj != null) outval = obj.Instance;
61 throw DataPortal.NewDataPortalException(
62 "DataPortal.Create " + Resources.FailedOnServer,
63 new DataPortalExceptionHandler().InspectException(objectType, outval, criteria, "DataPortal.Create", ex),
64 outval);
65 }
66 finally
67 {
68 object reference = null;
69 if (obj != null)
70 reference = obj.Instance;
71 ApplicationContext.DataPortalActivator.FinalizeInstance(reference);
72 }
73 }
74
84 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
85 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
86 public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
87 {
88 DataPortalTarget obj = null;
89 var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Fetch);
90 try
91 {
92 obj = new DataPortalTarget(ApplicationContext.DataPortalActivator.CreateInstance(objectType));
93 ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
94 obj.OnDataPortalInvoke(eventArgs);
95 obj.MarkOld();
96 await obj.FetchAsync(criteria, isSync);
97 obj.ThrowIfBusy();
98 obj.OnDataPortalInvokeComplete(eventArgs);
99 return new DataPortalResult(obj.Instance);
100 }
101 catch (Exception ex)
102 {
103 try
104 {
105 if (obj != null)
106 obj.OnDataPortalException(eventArgs, ex);
107 }
108 catch
109 {
110 // ignore exceptions from the exception handler
111 }
112 object outval = null;
113 if (obj != null) outval = obj.Instance;
114 throw DataPortal.NewDataPortalException(
115 "DataPortal.Fetch " + Resources.FailedOnServer,
116 new DataPortalExceptionHandler().InspectException(objectType, outval, criteria, "DataPortal.Fetch", ex),
117 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
136 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
137 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
138 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
139 {
141 Type objectType = obj.GetType();
142 var lb = new DataPortalTarget(obj);
143 if (lb.Instance is Core.ICommandObject)
144 return await Execute(lb, context, isSync);
145
146 var eventArgs = new DataPortalEventArgs(context, objectType, obj, operation);
147 try
148 {
149 ApplicationContext.DataPortalActivator.InitializeInstance(lb.Instance);
150 lb.OnDataPortalInvoke(eventArgs);
151 await lb.UpdateAsync(isSync);
152 lb.ThrowIfBusy();
153 lb.OnDataPortalInvokeComplete(eventArgs);
154 return new DataPortalResult(lb.Instance);
155 }
156 catch (Exception ex)
157 {
158 try
159 {
160 lb.OnDataPortalException(eventArgs, ex);
161 }
162 catch
163 {
164 // ignore exceptions from the exception handler
165 }
166 throw DataPortal.NewDataPortalException(
167 "DataPortal.Update " + Resources.FailedOnServer,
168 new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, "DataPortal.Update", ex),
169 obj);
170 }
171 finally
172 {
173 object reference = null;
174 if (lb != null)
175 reference = lb.Instance;
176 ApplicationContext.DataPortalActivator.FinalizeInstance(reference);
177 }
178 }
179
180 private async Task<DataPortalResult> Execute(DataPortalTarget obj, DataPortalContext context, bool isSync)
181 {
182 DataPortalOperations operation = DataPortalOperations.Execute;
183 Type objectType = obj.Instance.GetType();
184 var eventArgs = new DataPortalEventArgs(context, objectType, obj, operation);
185 try
186 {
187 ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
188 obj.OnDataPortalInvoke(eventArgs);
189 await obj.ExecuteAsync(isSync);
190 obj.ThrowIfBusy();
191 obj.OnDataPortalInvokeComplete(eventArgs);
192 return new DataPortalResult(obj.Instance);
193 }
194 catch (Exception ex)
195 {
196 try
197 {
198 obj.OnDataPortalException(eventArgs, ex);
199 }
200 catch
201 {
202 // ignore exceptions from the exception handler
203 }
204 object reference = null;
205 reference = obj.Instance ?? obj;
206 throw DataPortal.NewDataPortalException(
207 "DataPortal.Execute " + Resources.FailedOnServer,
208 new DataPortalExceptionHandler().InspectException(reference.GetType(), reference, null, "DataPortal.Execute", ex),
209 reference);
210 }
211 finally
212 {
213 object reference = null;
214 if (obj != null)
215 reference = obj.Instance;
216 ApplicationContext.DataPortalActivator.FinalizeInstance(reference);
217 }
218 }
228 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
229 public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
230 {
231 DataPortalTarget obj = null;
232 var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Delete);
233 try
234 {
235 obj = new DataPortalTarget(ApplicationContext.DataPortalActivator.CreateInstance(objectType));
236 ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);
237 obj.OnDataPortalInvoke(eventArgs);
238 await obj.DeleteAsync(criteria, isSync);
239 obj.ThrowIfBusy();
240 obj.OnDataPortalInvokeComplete(eventArgs);
241 return new DataPortalResult();
242 }
243 catch (Exception ex)
244 {
245 try
246 {
247 obj.OnDataPortalException(eventArgs, ex);
248 }
249 catch
250 {
251 // ignore exceptions from the exception handler
252 }
253 throw DataPortal.NewDataPortalException(
254 "DataPortal.Delete " + Resources.FailedOnServer,
255 new DataPortalExceptionHandler().InspectException(objectType, obj, null, "DataPortal.Delete", ex),
256 null);
257 }
258 finally
259 {
260 object reference = null;
261 if (obj != null)
262 reference = obj.Instance;
263 ApplicationContext.DataPortalActivator.FinalizeInstance(reference);
264 }
265 }
266 }
267}
Provides information about the DataPortal call.
A strongly-typed resource class, for looking up localized strings, etc.
static string FailedOnServer
Looks up a localized string similar to failed on the server.
Provides consistent context information between the client and server DataPortal objects.
This class provides a hoook for developers to add custom error handling in the DataPortal.
Implements the server-side DataPortal message router as discussed in Chapter 4.
Returns data from the server-side DataPortal to the client-side DataPortal.
Implements the server-side DataPortal as discussed in Chapter 4.
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
Interface implemented by server-side data portal components.
DataPortalOperations
List of data portal operations.