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.
MobileRequestProcessor.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="MobileRequestProcessor.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Object that processes all the requests from a client</summary>
7//-----------------------------------------------------------------------
8using System;
10using System.Globalization;
11using System.Threading;
12using Csla.Properties;
13using System.Threading.Tasks;
14
16{
21 {
22
23#region Factory Loader
24
25 private static IMobileFactoryLoader _factoryLoader;
33 {
34 get
35 {
36 if (_factoryLoader == null)
37 {
38 string setting = ConfigurationManager.AppSettings["CslaMobileFactoryLoader"];
39 if (!string.IsNullOrEmpty(setting))
40 _factoryLoader =
41 (IMobileFactoryLoader)Reflection.MethodCaller.CreateInstance(Type.GetType(setting, true, true));
42 else
43 _factoryLoader = new MobileFactoryLoader();
44 }
45 return _factoryLoader;
46 }
47 set
48 {
49 _factoryLoader = value;
50 }
51 }
52
53#endregion
54
55#region Operations
56
62 public async Task<MobileResponse> Create(MobileCriteriaRequest request)
63 {
64 var serverDataPortal = new Csla.Server.DataPortal();
65 var result = new MobileResponse();
66 Type businessObjectType = null;
67 object criteria = null;
68 try
69 {
70 criteria = request.Criteria;
71 // load type for business object
72 businessObjectType = Type.GetType(request.TypeName);
73 if (businessObjectType == null)
74 throw new InvalidOperationException(
76
77 SetContext(request);
78
79 serverDataPortal.Initialize(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Create });
80 serverDataPortal.Authorize(new AuthorizeRequest(businessObjectType, criteria, DataPortalOperations.Create));
81
82 object newObject = null;
83 var factoryInfo = GetMobileFactoryAttribute(businessObjectType);
84 if (factoryInfo == null)
85 {
86 if (criteria != null)
87 newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "CreateAsync", new Type[] { businessObjectType }, true, criteria).ConfigureAwait(false);
88 else
89 newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "CreateAsync", new Type[] { businessObjectType }, false, null).ConfigureAwait(false);
90 }
91 else
92 {
93 if (string.IsNullOrEmpty(factoryInfo.CreateMethodName))
94 throw new InvalidOperationException(Resources.CreateMethodNameNotSpecified);
95
96 object f = FactoryLoader.GetFactory(factoryInfo.FactoryTypeName);
97 if (criteria != null)
98 newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.CreateMethodName, criteria).ConfigureAwait(false);
99 else
100 newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.CreateMethodName).ConfigureAwait(false);
101 }
102 result.Object = newObject;
103#pragma warning disable CS0618 // Type or member is obsolete
104 result.GlobalContext = ApplicationContext.GlobalContext;
105#pragma warning restore CS0618 // Type or member is obsolete
106 }
108 {
109 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Create", ex);
110 result.Error = inspected.InnerException;
111 }
112 catch (Exception ex)
113 {
114 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Create", ex);
115 result.Error = inspected;
116 }
117 finally
118 {
119 if (result.Error != null)
120 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Create, Exception = result.Error, Result = new DataPortalResult(result.Object, result.Error, result.GlobalContext) });
121 else
122 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Create, Result = new DataPortalResult(result.Object, result.GlobalContext) });
123 ClearContext();
124 }
125 return result;
126 }
127
133 public async Task<MobileResponse> Fetch(MobileCriteriaRequest request)
134 {
135 var serverDataPortal = new Csla.Server.DataPortal();
136 var result = new MobileResponse();
137 Type businessObjectType = null;
138 object criteria = null;
139 try
140 {
141 // unpack criteria data into object
142 criteria = request.Criteria;
143
144 // load type for business object
145 businessObjectType = Type.GetType(request.TypeName);
146 if (businessObjectType == null)
147 throw new InvalidOperationException(
148 string.Format(Resources.ObjectTypeCouldNotBeLoaded, request.TypeName));
149
150 SetContext(request);
151
152 serverDataPortal.Initialize(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Fetch });
153 serverDataPortal.Authorize(new AuthorizeRequest(businessObjectType, criteria, DataPortalOperations.Fetch));
154
155 object newObject = null;
156 var factoryInfo = GetMobileFactoryAttribute(businessObjectType);
157 if (factoryInfo == null)
158 {
159 if (criteria != null)
160 newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "FetchAsync", new Type[] { businessObjectType }, true, criteria).ConfigureAwait(false);
161 else
162 newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "FetchAsync", new Type[] { businessObjectType }, false, null).ConfigureAwait(false);
163 }
164 else
165 {
166 if (string.IsNullOrEmpty(factoryInfo.FetchMethodName))
167 throw new InvalidOperationException(Resources.FetchMethodNameNotSpecified);
168
169 object f = FactoryLoader.GetFactory(factoryInfo.FactoryTypeName);
170 if (criteria != null)
171 newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.FetchMethodName, criteria).ConfigureAwait(false);
172 else
173 newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.FetchMethodName).ConfigureAwait(false);
174 }
175 result.Object = newObject;
176#pragma warning disable CS0618 // Type or member is obsolete
177 result.GlobalContext = ApplicationContext.GlobalContext;
178#pragma warning restore CS0618 // Type or member is obsolete
179 }
181 {
182 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Fetch", ex);
183 result.Error = inspected.InnerException;
184 }
185 catch (Exception ex)
186 {
187 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Fetch", ex);
188 result.Error = inspected;
189 }
190 finally
191 {
192 if (result.Error != null)
193 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Fetch, Exception = result.Error, Result = new DataPortalResult(result.Object, result.Error, result.GlobalContext) });
194 else
195 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Fetch, Result = new DataPortalResult(result.Object, result.GlobalContext) });
196 ClearContext();
197 }
198 return result;
199 }
200
206 public async Task<MobileResponse> Update(MobileUpdateRequest request)
207 {
208 var serverDataPortal = new Csla.Server.DataPortal();
209 var result = new MobileResponse();
210 Type businessObjectType = null;
211 object obj = null;
212 var operation = DataPortalOperations.Update;
213 try
214 {
215 // unpack object
216 obj = request.ObjectToUpdate;
217
218 if (obj is Csla.Core.ICommandObject)
219 operation = DataPortalOperations.Execute;
220
221 // load type for business object
222 businessObjectType = obj.GetType();
223
224 SetContext(request);
225
226 serverDataPortal.Initialize(new InterceptArgs { ObjectType = businessObjectType, Parameter = obj, Operation = operation });
227 serverDataPortal.Authorize(new AuthorizeRequest(businessObjectType, obj, operation));
228
229 object newObject = null;
230 var factoryInfo = GetMobileFactoryAttribute(businessObjectType);
231 if (factoryInfo == null)
232 {
233 newObject = await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "UpdateAsync", new Type[] { businessObjectType }, true, obj).ConfigureAwait(false);
234 }
235 else
236 {
237 if (string.IsNullOrEmpty(factoryInfo.UpdateMethodName))
238 throw new InvalidOperationException(Resources.UpdateMethodNameNotSpecified);
239
240 object f = FactoryLoader.GetFactory(factoryInfo.FactoryTypeName);
241 newObject = await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.UpdateMethodName, obj).ConfigureAwait(false);
242 }
243 result.Object = newObject;
244#pragma warning disable CS0618 // Type or member is obsolete
245 result.GlobalContext = ApplicationContext.GlobalContext;
246#pragma warning restore CS0618 // Type or member is obsolete
247 }
249 {
250 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, obj, "DataPortal.Update", ex);
251 result.Error = inspected.InnerException;
252 }
253 catch (Exception ex)
254 {
255 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, obj, "DataPortal.Update", ex);
256 result.Error = inspected;
257 }
258 finally
259 {
260 if (result.Error != null)
261 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = obj, Operation = operation, Exception = result.Error, Result = new DataPortalResult(result.Object, result.Error, result.GlobalContext) });
262 else
263 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = obj, Operation = operation, Result = new DataPortalResult(result.Object, result.GlobalContext) });
264 ClearContext();
265 }
266 return result;
267 }
268
274 public async Task<MobileResponse> Delete(MobileCriteriaRequest request)
275 {
276 var serverDataPortal = new Csla.Server.DataPortal();
277 var result = new MobileResponse();
278 Type businessObjectType = null;
279 object criteria = null;
280 try
281 {
282 // unpack criteria data into object
283 criteria = request.Criteria;
284
285 // load type for business object
286 businessObjectType = Type.GetType(request.TypeName);
287 if (businessObjectType == null)
288 throw new InvalidOperationException(
289 string.Format(Resources.ObjectTypeCouldNotBeLoaded, request.TypeName));
290
291 SetContext(request);
292
293 serverDataPortal.Initialize(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Delete });
294 serverDataPortal.Authorize(new AuthorizeRequest(businessObjectType, criteria, DataPortalOperations.Delete));
295
296 var factoryInfo = GetMobileFactoryAttribute(businessObjectType);
297 if (factoryInfo == null)
298 {
299 await Csla.Reflection.MethodCaller.CallGenericStaticMethodAsync(typeof(Csla.DataPortal), "DeleteAsync", new Type[] { businessObjectType }, true, criteria).ConfigureAwait(false);
300 }
301 else
302 {
303 if (string.IsNullOrEmpty(factoryInfo.DeleteMethodName))
304 throw new InvalidOperationException(Resources.DeleteMethodNameNotSpecified);
305
306 object f = FactoryLoader.GetFactory(factoryInfo.FactoryTypeName);
307 await Csla.Reflection.MethodCaller.CallMethodTryAsync(f, factoryInfo.DeleteMethodName, criteria).ConfigureAwait(false);
308 }
309#pragma warning disable CS0618 // Type or member is obsolete
310 result.GlobalContext = ApplicationContext.GlobalContext;
311#pragma warning restore CS0618 // Type or member is obsolete
312 }
314 {
315 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Delete", ex);
316 result.Error = inspected.InnerException;
317 }
318 catch (Exception ex)
319 {
320 var inspected = new DataPortalExceptionHandler().InspectException(businessObjectType, criteria, "DataPortal.Delete", ex);
321 result.Error = inspected;
322 }
323 finally
324 {
325 if (result.Error != null)
326 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Delete, Exception = result.Error, Result = new DataPortalResult(result.Object, result.Error, result.GlobalContext) });
327 else
328 serverDataPortal.Complete(new InterceptArgs { ObjectType = businessObjectType, Parameter = criteria, Operation = DataPortalOperations.Delete, Result = new DataPortalResult(result.Object, result.GlobalContext) });
329 ClearContext();
330 }
331 return result;
332 }
333
334#endregion
335
336#region Mobile Factory
337
338 private static MobileFactoryAttribute GetMobileFactoryAttribute(Type objectType)
339 {
340 var result = objectType.GetCustomAttributes(typeof(MobileFactoryAttribute), true);
341 if (result != null && result.Length > 0)
342 return result[0] as MobileFactoryAttribute;
343 else
344 return null;
345 }
346
347#endregion
348
349#region Context and Criteria
350 private void SetContext(IMobileRequest request)
351 {
352 ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server);
353 ApplicationContext.SetLogicalExecutionLocation(ApplicationContext.LogicalExecutionLocations.Server);
354
355 ApplicationContext.SetContext(request.ClientContext, request.GlobalContext);
356 if (ApplicationContext.AuthenticationType != "Windows")
357 ApplicationContext.User = request.Principal;
358 SetClientCultures(request);
359 }
360
364 public static void ClearContext()
365 {
366 ApplicationContext.Clear();
367 if (ApplicationContext.AuthenticationType != "Windows")
368 ApplicationContext.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(string.Empty), new string[] { });
369 }
370
371#region client culture
376 private void SetClientCultures(IMobileRequest request)
377 {
378 CultureInfo culture = null;
379 // clientCulture
380 if (TryGetCulture(request.ClientCulture, ref culture))
381 {
382 Thread.CurrentThread.CurrentCulture = culture;
383 }
384 // clientUICulture
385 if (TryGetCulture(request.ClientUICulture, ref culture))
386 {
387 Thread.CurrentThread.CurrentUICulture = culture;
388 }
389 }
390
391
398 private bool TryGetCulture(string clientCulture, ref CultureInfo culture)
399 {
400 if (string.IsNullOrWhiteSpace(clientCulture)) return false;
401
402 try
403 {
404 culture = CultureInfo.GetCultureInfo(clientCulture);
405 return true;
406 }
407 catch (CultureNotFoundException)
408 {
409 return false;
410 }
411 }
412
413#endregion
414
415#endregion
416 }
417}
This is the client-side DataPortal.
Definition: DataPortalT.cs:24
A strongly-typed resource class, for looking up localized strings, etc.
static string CreateMethodNameNotSpecified
Looks up a localized string similar to Create method name not specified in MobileFactory attribute.
static string DeleteMethodNameNotSpecified
Looks up a localized string similar to Delete method name not specified in MobileFactory attribute.
static string ObjectTypeCouldNotBeLoaded
Looks up a localized string similar to Object type or assembly could not be loaded ({0}).
static string UpdateMethodNameNotSpecified
Looks up a localized string similar to Update method name not specified in MobileFactory attribute.
static string FetchMethodNameNotSpecified
Looks up a localized string similar to Fetch method name not specified in MobileFactory attribute.
This exception is returned from the CallMethod method in the server-side DataPortal and contains the ...
Object containing information about the client request to the data portal.
This class provides a hoook 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.
Returns data from the server-side DataPortal to the client-side DataPortal.
Class that will be used to execute a request from a client.
string TypeName
Type of object that is the target of the request
Class containing the default implementation for the FactoryLoader delegate used by the data portal ho...
Object that processes all the requests from a client
async Task< MobileResponse > Create(MobileCriteriaRequest request)
Create a new business object.
static IMobileFactoryLoader FactoryLoader
Gets or sets a delegate reference to the method called to create instances of factory objects as requ...
async Task< MobileResponse > Update(MobileUpdateRequest request)
Update a business object.
async Task< MobileResponse > Delete(MobileCriteriaRequest request)
Delete a business object.
static void ClearContext()
Clears the application context and current principal.
async Task< MobileResponse > Fetch(MobileCriteriaRequest request)
Get an existing business object.
Object that encompasses the resut of the request from a client
Class that will be used to execute an Update request from a client.
object ObjectToUpdate
Business object that will be updated.
Arguments parameter passed to the interceptor methods.
Specifies that the WCF data portal should invoke a factory object rather than the business object.
This interface is implemented by all Command objects.
Defines an interface to be implemented by a factory object that returns MobileFactory objects based o...
object GetFactory(string factoryName)
Returns a MobileFactory object.
Interface for all requests from client
string ClientCulture
The client culture.
string ClientUICulture
The client UI culture.
DataPortalOperations
List of data portal operations.