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.
FactoryDataPortal.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="FactoryDataPortal.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Server-side data portal implementation that</summary>
7//-----------------------------------------------------------------------
8using System;
10using System.Threading.Tasks;
11using Csla.Properties;
12
13namespace Csla.Server
14{
21 {
22 #region Factory Loader
23
24 private static IObjectFactoryLoader _factoryLoader;
25
33 {
34 get
35 {
36 if (_factoryLoader == null)
37 {
38 string setting = ConfigurationManager.AppSettings["CslaObjectFactoryLoader"];
39 if (!string.IsNullOrEmpty(setting))
40 _factoryLoader =
41 (IObjectFactoryLoader)Reflection.MethodCaller.CreateInstance(Type.GetType(setting, true, true));
42 else
43 _factoryLoader = new ObjectFactoryLoader();
44 }
45 return _factoryLoader;
46 }
47 set
48 {
49 _factoryLoader = value;
50 }
51 }
52
53 #endregion
54
55 #region Method invokes
56
57 private async Task<DataPortalResult> InvokeMethod(string factoryTypeName, DataPortalOperations operation, string methodName, Type objectType, DataPortalContext context, bool isSync)
58 {
59 object factory = FactoryLoader.GetFactory(factoryTypeName);
60 var eventArgs = new DataPortalEventArgs(context, objectType, null, operation);
61
62 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "Invoke", eventArgs);
63 object result = null;
64 try
65 {
66 Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, factory, methodName);
67
68 result = await Csla.Reflection.MethodCaller.CallMethodTryAsync(factory, methodName).ConfigureAwait(false);
69 var error = result as Exception;
70 if (error != null)
71 throw error;
72
73 var busy = result as Csla.Core.ITrackStatus;
74 if (busy != null && busy.IsBusy)
75 throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name));
76
77 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeComplete", eventArgs);
78 }
79 catch (Exception ex)
80 {
81 Csla.Reflection.MethodCaller.CallMethodIfImplemented(
82 factory, "InvokeError", new DataPortalEventArgs(context, objectType, null, operation, ex));
83 throw;
84 }
85 return new DataPortalResult(result);
86 }
87
88 private async Task<DataPortalResult> InvokeMethod(string factoryTypeName, DataPortalOperations operation, string methodName, Type objectType, object e, DataPortalContext context, bool isSync)
89 {
90 object factory = FactoryLoader.GetFactory(factoryTypeName);
91 var eventArgs = new DataPortalEventArgs(context, objectType, e, operation);
92
93 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "Invoke", eventArgs);
94 object result = null;
95 try
96 {
97 Utilities.ThrowIfAsyncMethodOnSyncClient(isSync, factory, methodName, e);
98
99 result = await Csla.Reflection.MethodCaller.CallMethodTryAsync(factory, methodName, e).ConfigureAwait(false);
100 var error = result as Exception;
101 if (error != null)
102 throw error;
103
104 var busy = result as Csla.Core.ITrackStatus;
105 if (busy != null && busy.IsBusy)
106 throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name));
107
108 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeComplete", eventArgs);
109 }
110 catch (Exception ex)
111 {
112 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeError", new DataPortalEventArgs(context, objectType, e, operation, ex));
113 throw;
114 }
115 return new DataPortalResult(result);
116 }
117
118 #endregion
119
120 #region IDataPortalServer Members
121
131 public async Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
132 {
133 try
134 {
135 DataPortalResult result = null;
136 if (criteria is EmptyCriteria)
137 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Create, context.FactoryInfo.CreateMethodName, objectType, context, isSync).ConfigureAwait(false);
138 else
139 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Create, context.FactoryInfo.CreateMethodName, objectType, criteria, context, isSync).ConfigureAwait(false);
140 return result;
141 }
142 catch (Exception ex)
143 {
144 throw DataPortal.NewDataPortalException(
146 new DataPortalExceptionHandler().InspectException(objectType, criteria, context.FactoryInfo.CreateMethodName, ex),
147 null);
148 }
149 }
150
160 public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
161 {
162 try
163 {
164 DataPortalResult result = null;
165 if (criteria is EmptyCriteria)
166 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Fetch, context.FactoryInfo.FetchMethodName, objectType, context, isSync).ConfigureAwait(false);
167 else
168 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Fetch, context.FactoryInfo.FetchMethodName, objectType, criteria, context, isSync).ConfigureAwait(false);
169 return result;
170 }
171 catch (Exception ex)
172 {
173 throw DataPortal.NewDataPortalException(
175 new DataPortalExceptionHandler().InspectException(objectType, criteria, context.FactoryInfo.FetchMethodName, ex),
176 null);
177 }
178 }
179
188 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
189 {
190 string methodName = string.Empty;
191 try
192 {
193 DataPortalResult result = null;
194 if (obj is Core.ICommandObject)
195 methodName = context.FactoryInfo.ExecuteMethodName;
196 else
197 methodName = context.FactoryInfo.UpdateMethodName;
198
199 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Update, methodName, obj.GetType(), obj, context, isSync).ConfigureAwait(false);
200 return result;
201 }
202 catch (Exception ex)
203 {
204 throw DataPortal.NewDataPortalException(
205 methodName + " " + Resources.FailedOnServer,
206 new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, methodName, ex),
207 obj);
208
209 }
210 }
211
221 public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
222 {
223 try
224 {
225 DataPortalResult result = null;
226 if (criteria is EmptyCriteria)
227 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Delete, context.FactoryInfo.DeleteMethodName, objectType, context, isSync).ConfigureAwait(false);
228 else
229 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Delete, context.FactoryInfo.DeleteMethodName, objectType, criteria, context, isSync).ConfigureAwait(false);
230 return result;
231 }
232 catch (Exception ex)
233 {
234 throw DataPortal.NewDataPortalException(
236 new DataPortalExceptionHandler().InspectException(objectType, criteria, context.FactoryInfo.DeleteMethodName, ex),
237 null);
238 }
239 }
240
241 #endregion
242 }
243}
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.
ObjectFactoryAttribute FactoryInfo
Gets the current ObjectFactory attribute value (if any).
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.
Empty criteria used by the data portal as a placeholder for a create/fetch request that has no criter...
Server-side data portal implementation that invokes an object factory rather than directly interactin...
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
static IObjectFactoryLoader FactoryLoader
Gets or sets a delegate reference to the method called to create instances of factory objects as requ...
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
string FetchMethodName
Name of the method to call for a fetch operation.
string DeleteMethodName
Name of the method to call for a delete operation.
string ExecuteMethodName
Name of the method to call for a Execute operation.
string UpdateMethodName
Name of the method to call for a update operation.
string CreateMethodName
Name of the method to call for a create operation.
string FactoryTypeName
Assembly qualified type name of the factory object.
Class containing the default implementation for the FactoryLoader delegate used by the data portal ho...
Defines the common properties required objects that track their own status.
Definition: ITrackStatus.cs:17
Interface implemented by server-side data portal components.
Defines an interface to be implemented by a factory loader object that returns ObjectFactory objects ...
object GetFactory(string factoryName)
Returns an ObjectFactory object.
DataPortalOperations
List of data portal operations.