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.
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 {
27 public SimpleDataPortal(ApplicationContext applicationContext, IDataPortalActivator activator, IDataPortalExceptionInspector exceptionInspector)
28 {
29 ApplicationContext = applicationContext;
30 Activator = activator;
31 ExceptionInspector = exceptionInspector;
32 }
33
34 private ApplicationContext ApplicationContext { get; set; }
35 private IDataPortalActivator Activator { get; set; }
36 private IDataPortalExceptionInspector ExceptionInspector { get; set; }
37
47 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
48 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
49 public async Task<DataPortalResult> Create(
50 Type objectType, object criteria, DataPortalContext context, bool isSync)
51 {
52 DataPortalTarget obj = null;
53 var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Create);
54 try
55 {
56 obj = ApplicationContext.CreateInstanceDI<DataPortalTarget>(ApplicationContext.CreateInstance(objectType));
57 Activator.InitializeInstance(obj.Instance);
58 obj.OnDataPortalInvoke(eventArgs);
59 obj.MarkNew();
60 await obj.CreateAsync(criteria, isSync);
61 obj.ThrowIfBusy();
62 obj.OnDataPortalInvokeComplete(eventArgs);
63 return new DataPortalResult(ApplicationContext, obj.Instance);
64 }
65 catch (Exception ex)
66 {
67 try
68 {
69 if (obj != null)
70 obj.OnDataPortalException(eventArgs, ex);
71 }
72 catch
73 {
74 // ignore exceptions from the exception handler
75 }
76 object outval = null;
77 if (obj != null) outval = obj.Instance;
78 throw DataPortal.NewDataPortalException(
79 ApplicationContext, "DataPortal.Create " + Resources.FailedOnServer,
80 new DataPortalExceptionHandler(ExceptionInspector).InspectException(objectType, outval, criteria, "DataPortal.Create", ex),
81 outval);
82 }
83 finally
84 {
85 object reference = null;
86 if (obj != null)
87 reference = obj.Instance;
88 Activator.FinalizeInstance(reference);
89 }
90 }
91
101 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
102 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
103 public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
104 {
105 DataPortalTarget obj = null;
106 var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Fetch);
107 try
108 {
109 obj = ApplicationContext.CreateInstanceDI<DataPortalTarget>(ApplicationContext.CreateInstance(objectType));
110 Activator.InitializeInstance(obj.Instance);
111 obj.OnDataPortalInvoke(eventArgs);
112 obj.MarkOld();
113 await obj.FetchAsync(criteria, isSync);
114 obj.ThrowIfBusy();
115 obj.OnDataPortalInvokeComplete(eventArgs);
116 return new DataPortalResult(ApplicationContext, obj.Instance);
117 }
118 catch (Exception ex)
119 {
120 try
121 {
122 if (obj != null)
123 obj.OnDataPortalException(eventArgs, ex);
124 }
125 catch
126 {
127 // ignore exceptions from the exception handler
128 }
129 object outval = null;
130 if (obj != null) outval = obj.Instance;
131 throw DataPortal.NewDataPortalException(
132 ApplicationContext, "DataPortal.Fetch " + Resources.FailedOnServer,
133 new DataPortalExceptionHandler(ExceptionInspector).InspectException(objectType, outval, criteria, "DataPortal.Fetch", ex),
134 outval);
135 }
136 finally
137 {
138 object reference = null;
139 if (obj != null)
140 reference = obj.Instance;
141 Activator.FinalizeInstance(reference);
142 }
143 }
144
153 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
154 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
155 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
156 {
158 Type objectType = obj.GetType();
159 var lb = ApplicationContext.CreateInstanceDI<DataPortalTarget>(obj);
160 if (lb.Instance is Core.ICommandObject)
161 return await Execute(lb, context, isSync);
162
163 var eventArgs = new DataPortalEventArgs(context, objectType, obj, operation);
164 try
165 {
166 Activator.InitializeInstance(lb.Instance);
167 lb.OnDataPortalInvoke(eventArgs);
168 await lb.UpdateAsync(isSync);
169 lb.ThrowIfBusy();
170 lb.OnDataPortalInvokeComplete(eventArgs);
171 return new DataPortalResult(ApplicationContext, lb.Instance);
172 }
173 catch (Exception ex)
174 {
175 try
176 {
177 lb.OnDataPortalException(eventArgs, ex);
178 }
179 catch
180 {
181 // ignore exceptions from the exception handler
182 }
183 throw DataPortal.NewDataPortalException(
184 ApplicationContext, "DataPortal.Update " + Resources.FailedOnServer,
185 new DataPortalExceptionHandler(ExceptionInspector).InspectException(obj.GetType(), obj, null, "DataPortal.Update", ex),
186 obj);
187 }
188 finally
189 {
190 object reference = null;
191 if (lb != null)
192 reference = lb.Instance;
193 Activator.FinalizeInstance(reference);
194 }
195 }
196
197 private async Task<DataPortalResult> Execute(DataPortalTarget obj, DataPortalContext context, bool isSync)
198 {
199 DataPortalOperations operation = DataPortalOperations.Execute;
200 Type objectType = obj.Instance.GetType();
201 var eventArgs = new DataPortalEventArgs(context, objectType, obj, operation);
202 try
203 {
204 Activator.InitializeInstance(obj.Instance);
205 obj.OnDataPortalInvoke(eventArgs);
206 await obj.ExecuteAsync(isSync);
207 obj.ThrowIfBusy();
208 obj.OnDataPortalInvokeComplete(eventArgs);
209 return new DataPortalResult(ApplicationContext, obj.Instance);
210 }
211 catch (Exception ex)
212 {
213 try
214 {
215 obj.OnDataPortalException(eventArgs, ex);
216 }
217 catch
218 {
219 // ignore exceptions from the exception handler
220 }
221 object reference = null;
222 reference = obj.Instance ?? obj;
223 throw DataPortal.NewDataPortalException(
224 ApplicationContext, "DataPortal.Execute " + Resources.FailedOnServer,
225 new DataPortalExceptionHandler(ExceptionInspector).InspectException(reference.GetType(), reference, null, "DataPortal.Execute", ex),
226 reference);
227 }
228 finally
229 {
230 object reference = null;
231 if (obj != null)
232 reference = obj.Instance;
233 Activator.FinalizeInstance(reference);
234 }
235 }
245 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "Csla.Server.DataPortalException.#ctor(System.String,System.Exception,Csla.Server.DataPortalResult)")]
246 public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
247 {
248 DataPortalTarget obj = null;
249 var eventArgs = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Delete);
250 try
251 {
252 obj = ApplicationContext.CreateInstanceDI<DataPortalTarget>(ApplicationContext.CreateInstance(objectType));
253 Activator.InitializeInstance(obj.Instance);
254 obj.OnDataPortalInvoke(eventArgs);
255 await obj.DeleteAsync(criteria, isSync);
256 obj.ThrowIfBusy();
257 obj.OnDataPortalInvokeComplete(eventArgs);
259 }
260 catch (Exception ex)
261 {
262 try
263 {
264 obj.OnDataPortalException(eventArgs, ex);
265 }
266 catch
267 {
268 // ignore exceptions from the exception handler
269 }
270 throw DataPortal.NewDataPortalException(
271 ApplicationContext, "DataPortal.Delete " + Resources.FailedOnServer,
272 new DataPortalExceptionHandler(ExceptionInspector).InspectException(objectType, obj, null, "DataPortal.Delete", ex),
273 null);
274 }
275 finally
276 {
277 object reference = null;
278 if (obj != null)
279 reference = obj.Instance;
280 Activator.FinalizeInstance(reference);
281 }
282 }
283 }
284}
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...
object CreateInstance(Type objectType, params object[] parameters)
Creates an object using Activator.
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 hook for developers to add custom error handling in the DataPortal.
Exception InspectException(Type objectType, object criteria, string methodName, Exception ex)
Transforms the exception in a Fetch, Create or Execute method.
Implements the server-side DataPortal message router as discussed in Chapter 4.
Definition: DataPortal.cs:24
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.
SimpleDataPortal(ApplicationContext applicationContext, IDataPortalActivator activator, IDataPortalExceptionInspector exceptionInspector)
Creates an instance of the type
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.
Defines a type used to activate concrete business instances.
void InitializeInstance(object obj)
Initializes an existing business object instance.
void FinalizeInstance(object obj)
Finalizes an existing business object instance.
Implement this interface to check a DataPortalException before returning Exception to the client.
Interface implemented by server-side data portal components.
DataPortalOperations
List of data portal operations.