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.
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 {
29 {
30 ApplicationContext = applicationContext;
31 FactoryLoader = factoryLoader;
32 ExceptionInspector = inspector;
33 }
34
35 private ApplicationContext ApplicationContext { get; set; }
36 private IObjectFactoryLoader FactoryLoader { get; set; }
37 private IDataPortalExceptionInspector ExceptionInspector { get; set; }
38
39 #region Method invokes
40
41 private async Task<DataPortalResult> InvokeMethod(string factoryTypeName, DataPortalOperations operation, string methodName, Type objectType, DataPortalContext context, bool isSync)
42 {
43 object factory = FactoryLoader.GetFactory(factoryTypeName);
44 var eventArgs = new DataPortalEventArgs(context, objectType, null, operation);
45
46 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "Invoke", eventArgs);
47 object result;
48 try
49 {
50 Utilities.ThrowIfAsyncMethodOnSyncClient(ApplicationContext, isSync, factory, methodName);
51
52 result = await Csla.Reflection.MethodCaller.CallMethodTryAsync(factory, methodName).ConfigureAwait(false);
53 if (result is Exception error)
54 throw error;
55
56 if (result is Csla.Core.ITrackStatus busy && busy.IsBusy)
57 throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name));
58
59 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeComplete", eventArgs);
60 }
61 catch (Exception ex)
62 {
63 Csla.Reflection.MethodCaller.CallMethodIfImplemented(
64 factory, "InvokeError", new DataPortalEventArgs(context, objectType, null, operation, ex));
65 throw;
66 }
67 return new DataPortalResult(ApplicationContext, result);
68 }
69
70 private async Task<DataPortalResult> InvokeMethod(string factoryTypeName, DataPortalOperations operation, string methodName, Type objectType, object e, DataPortalContext context, bool isSync)
71 {
72 object factory = FactoryLoader.GetFactory(factoryTypeName);
73 var eventArgs = new DataPortalEventArgs(context, objectType, e, operation);
74
75 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "Invoke", eventArgs);
76 object result;
77 try
78 {
79 Utilities.ThrowIfAsyncMethodOnSyncClient(ApplicationContext, isSync, factory, methodName, e);
80
81 result = await Csla.Reflection.MethodCaller.CallMethodTryAsync(factory, methodName, e).ConfigureAwait(false);
82 if (result is Exception error)
83 throw error;
84
85 if (result is Csla.Core.ITrackStatus busy && busy.IsBusy)
86 throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name));
87
88 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeComplete", eventArgs);
89 }
90 catch (Exception ex)
91 {
92 Csla.Reflection.MethodCaller.CallMethodIfImplemented(factory, "InvokeError", new DataPortalEventArgs(context, objectType, e, operation, ex));
93 throw;
94 }
95 return new DataPortalResult(ApplicationContext, result);
96 }
97
98 #endregion
99
100 #region IDataPortalServer Members
101
111 public async Task<DataPortalResult> Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
112 {
113 try
114 {
115 DataPortalResult result;
116 if (criteria is EmptyCriteria)
117 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Create, context.FactoryInfo.CreateMethodName, objectType, context, isSync).ConfigureAwait(false);
118 else
119 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Create, context.FactoryInfo.CreateMethodName, objectType, criteria, context, isSync).ConfigureAwait(false);
120 return result;
121 }
122 catch (Exception ex)
123 {
124 throw DataPortal.NewDataPortalException(
126 new DataPortalExceptionHandler(ExceptionInspector).InspectException(objectType, criteria, context.FactoryInfo.CreateMethodName, ex),
127 null);
128 }
129 }
130
140 public async Task<DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
141 {
142 try
143 {
144 DataPortalResult result;
145 if (criteria is EmptyCriteria)
146 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Fetch, context.FactoryInfo.FetchMethodName, objectType, context, isSync).ConfigureAwait(false);
147 else
148 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Fetch, context.FactoryInfo.FetchMethodName, objectType, criteria, context, isSync).ConfigureAwait(false);
149 return result;
150 }
151 catch (Exception ex)
152 {
153 throw DataPortal.NewDataPortalException(
155 new DataPortalExceptionHandler(ExceptionInspector).InspectException(objectType, criteria, context.FactoryInfo.FetchMethodName, ex),
156 null);
157 }
158 }
159
168 public async Task<DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
169 {
170 string methodName = string.Empty;
171 try
172 {
173 DataPortalResult result;
174 if (obj is Core.ICommandObject)
175 methodName = context.FactoryInfo.ExecuteMethodName;
176 else
177 methodName = context.FactoryInfo.UpdateMethodName;
178
179 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Update, methodName, obj.GetType(), obj, context, isSync).ConfigureAwait(false);
180 return result;
181 }
182 catch (Exception ex)
183 {
184 throw DataPortal.NewDataPortalException(
185 ApplicationContext, methodName + " " + Resources.FailedOnServer,
186 new DataPortalExceptionHandler(ExceptionInspector).InspectException(obj.GetType(), obj, null, methodName, ex),
187 obj);
188
189 }
190 }
191
201 public async Task<DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
202 {
203 try
204 {
205 DataPortalResult result;
206 if (criteria is EmptyCriteria)
207 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Delete, context.FactoryInfo.DeleteMethodName, objectType, context, isSync).ConfigureAwait(false);
208 else
209 result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Delete, context.FactoryInfo.DeleteMethodName, objectType, criteria, context, isSync).ConfigureAwait(false);
210 return result;
211 }
212 catch (Exception ex)
213 {
214 throw DataPortal.NewDataPortalException(
216 new DataPortalExceptionHandler(ExceptionInspector).InspectException(objectType, criteria, context.FactoryInfo.DeleteMethodName, ex),
217 null);
218 }
219 }
220
221 #endregion
222 }
223}
Csla.Server.DataPortalContext DataPortalContext
Provides consistent context information between the client and server DataPortal objects.
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 hook for developers to add custom error handling in the DataPortal.
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.
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.
FactoryDataPortal(ApplicationContext applicationContext, IObjectFactoryLoader factoryLoader, IDataPortalExceptionInspector inspector)
Creates an instance of the type.
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.
bool IsBusy
Gets a value indicating whether the object, or any of the object's child objects, are busy running an...
Definition: INotifyBusy.cs:29
Defines the common properties required objects that track their own status.
Definition: ITrackStatus.cs:17
Implement this interface to check a DataPortalException before returning Exception to the client.
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.