10using System.Security.Principal;
11using System.Threading.Tasks;
14using System.Collections.Generic;
67 Options = options.DataPortalServerOptions;
68 Authorizer = authorizer;
70 FactoryLoader = factoryLoader;
71 Activator = activator;
72 ExceptionInspector = exceptionInspector;
78#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
92 throw new ArgumentOutOfRangeException(
"transactionalAttribute");
97 private Reflection.ServiceProviderMethodCaller serviceProviderMethodCaller;
98 private Reflection.ServiceProviderMethodCaller ServiceProviderMethodCaller
102 if (serviceProviderMethodCaller ==
null)
103 serviceProviderMethodCaller = (Reflection.ServiceProviderMethodCaller)ApplicationContext.
CreateInstanceDI(typeof(Reflection.ServiceProviderMethodCaller));
104 return serviceProviderMethodCaller;
117 public async Task<DataPortalResult>
Create(
129 DataPortalMethodInfo method;
131 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
132 if (criteria is Server.EmptyCriteria)
133 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
CreateAttribute>(objectType,
null);
135 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
CreateAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
136 serviceProviderMethodInfo.PrepForInvocation();
137 method = serviceProviderMethodInfo.DataPortalMethodInfo;
140 switch (method.TransactionalAttribute.TransactionType)
142#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
144 portal = GetServicedComponentPortal(method.TransactionalAttribute);
147 result = await portal.
Create(objectType, criteria, context, isSync).ConfigureAwait(
false);
151 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
160 result = await portal.
Create(objectType, criteria, context, isSync).ConfigureAwait(
false);
165 result = await portal.
Create(objectType, criteria, context, isSync).ConfigureAwait(
false);
176 catch (AggregateException ex)
178 Exception error =
null;
179 if (ex.InnerExceptions.Count > 0)
180 error = ex.InnerExceptions[0].InnerException;
201 ClearContext(context);
225 DataPortalMethodInfo method;
227 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
229 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
FetchAttribute>(objectType,
null);
231 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
FetchAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
233 serviceProviderMethodInfo.PrepForInvocation();
234 method = serviceProviderMethodInfo.DataPortalMethodInfo;
237 switch (method.TransactionalAttribute.TransactionType)
239#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
241 portal = GetServicedComponentPortal(method.TransactionalAttribute);
244 result = await portal.
Fetch(objectType, criteria, context, isSync).ConfigureAwait(
false);
248 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
255 result = await portal.
Fetch(objectType, criteria, context, isSync).ConfigureAwait(
false);
259 result = await portal.
Fetch(objectType, criteria, context, isSync).ConfigureAwait(
false);
270 catch (AggregateException ex)
272 Exception error =
null;
273 if (ex.InnerExceptions.Count > 0)
274 error = ex.InnerExceptions[0].InnerException;
295 ClearContext(context);
307 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1062:ValidateArgumentsOfPublicMethods")]
310 Type objectType =
null;
316 objectType = obj.GetType();
318 if (obj is Core.ICommandObject)
323 Initialize(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Operation = operation, IsSync = isSync });
326 DataPortalMethodInfo method;
328 if (factoryInfo !=
null)
331 var factoryLoader =
ApplicationContext.CurrentServiceProvider.GetService(typeof(Server.IObjectFactoryLoader)) as Server.IObjectFactoryLoader;
332 var factoryType = factoryLoader?.GetFactoryType(factoryInfo.FactoryTypeName);
333 if (obj is Core.BusinessBase bbase)
336 methodName = factoryInfo.DeleteMethodName;
338 methodName = factoryInfo.UpdateMethodName;
340 else if (obj is Core.ICommandObject)
341 methodName = factoryInfo.ExecuteMethodName;
343 methodName = factoryInfo.UpdateMethodName;
344 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName,
new object[] { obj });
348 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
349 if (obj is Core.BusinessBase bbase)
352 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
DeleteSelfAttribute>(objectType,
null);
355 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
InsertAttribute>(objectType,
null);
357 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
UpdateAttribute>(objectType,
null);
359 else if (obj is Core.ICommandObject)
360 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
ExecuteAttribute>(objectType,
null);
362 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
UpdateAttribute>(objectType,
null);
364 serviceProviderMethodInfo.PrepForInvocation();
365 method = serviceProviderMethodInfo.DataPortalMethodInfo;
370 switch (method.TransactionalAttribute.TransactionType)
372#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
374 portal = GetServicedComponentPortal(method.TransactionalAttribute);
377 result = await portal.
Update(obj, context, isSync).ConfigureAwait(
false);
381 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
388 result = await portal.
Update(obj, context, isSync).ConfigureAwait(
false);
392 result = await portal.
Update(obj, context, isSync).ConfigureAwait(
false);
395 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Result = result, Operation = operation, IsSync = isSync });
400 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = ex, Operation = operation, IsSync = isSync });
403 catch (AggregateException ex)
405 Exception error =
null;
406 if (ex.InnerExceptions.Count > 0)
407 error = ex.InnerExceptions[0].InnerException;
414 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync });
423 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync });
428 ClearContext(context);
452 DataPortalMethodInfo method;
454 if (factoryInfo !=
null)
456 var factoryLoader =
ApplicationContext.CurrentServiceProvider.GetService(typeof(Server.IObjectFactoryLoader)) as Server.IObjectFactoryLoader;
457 var factoryType = factoryLoader?.GetFactoryType(factoryInfo.FactoryTypeName);
458 string methodName = factoryInfo.DeleteMethodName;
459 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, criteria);
463 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
465 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
DeleteAttribute>(objectType,
null);
467 serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod<
DeleteAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
468 serviceProviderMethodInfo.PrepForInvocation();
469 method = serviceProviderMethodInfo.DataPortalMethodInfo;
473 switch (method.TransactionalAttribute.TransactionType)
475#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
477 portal = GetServicedComponentPortal(method.TransactionalAttribute);
480 result = await portal.
Delete(objectType, criteria, context, isSync).ConfigureAwait(
false);
484 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
491 result = await portal.
Delete(objectType, criteria, context, isSync).ConfigureAwait(
false);
495 result = await portal.
Delete(objectType, criteria, context, isSync).ConfigureAwait(
false);
506 catch (AggregateException ex)
508 Exception error =
null;
509 if (ex.InnerExceptions.Count > 0)
510 error = ex.InnerExceptions[0].InnerException;
531 ClearContext(context);
540 if (timer ==
null)
return;
542 var startTime = (DateTimeOffset)timer;
543 e.
Runtime = DateTimeOffset.Now - startTime;
547 internal void Initialize(InterceptArgs e)
559 ApplicationContext.LogicalExecutionLocations _oldLocation;
566 if (context.IsRemotePortal)
569 ApplicationContext.SetExecutionLocation(ApplicationContext.
ExecutionLocations.Server);
573 ApplicationContext.SetContext(context.ClientContext);
579 SetPrincipal(context);
587 if (context.Principal !=
null)
595 AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
600 if (context.Principal ==
null)
608 ApplicationContext.
User = context.Principal;
615 System.Threading.Thread.CurrentThread.CurrentCulture =
616 new System.Globalization.CultureInfo(context.ClientCulture);
617 System.Threading.Thread.CurrentThread.CurrentUICulture =
618 new System.Globalization.CultureInfo(context.ClientUICulture);
623 ApplicationContext.SetLogicalExecutionLocation(_oldLocation);
626 if (!context.IsRemotePortal)
return;
627 ApplicationContext.
Clear();
629 ApplicationContext.
User =
null;
634 private void AuthorizeRequest(AuthorizeRequest clientRequest)
639 internal static DataPortalException NewDataPortalException(
640 ApplicationContext applicationContext,
string message, Exception innerException,
object businessObject)
643 businessObject =
null;
645 throw new DataPortalException(
647 innerException,
new DataPortalResult(applicationContext, businessObject));
659 if (criteria !=
null)
660 if (criteria.GetType().Equals(typeof(
object[])))
661 clength = criteria.GetLength(0);
665 if (criteria ==
null || (clength == 1 && criteria[0] ==
null))
667 else if (clength == 0)
669 else if (clength == 1)
672 return new Core.MobileList<
object>(criteria);
683 if (criteria ==
null)
686 return Array.Empty<
object>();
688 return new object[] {
null };
689 else if (criteria.GetType().Equals(typeof(
object[])))
691 var array = (
object[])criteria;
692 var clength = array.GetLength(0);
694 return Array.Empty<
object>();
698 else if (criteria is Core.MobileList<
object> list)
699 return list.ToArray();
701 return new object[] { criteria };
Csla.Server.DataPortalContext DataPortalContext
Provides consistent context information between the client and server DataPortal objects.
LogicalExecutionLocations LogicalExecutionLocation
Return Logical Execution Location - Client or Server This is applicable to Local mode as well
ExecutionLocations
Enum representing the locations code can execute.
LogicalExecutionLocations
Enum representing the logical execution location The setting is set to server when server is execting...
object CreateInstanceDI(Type objectType, params object[] parameters)
Creates an object using 'Activator.CreateInstance' using service provider (if one is available) to po...
void Clear()
Clears all context collections.
static bool DataPortalReturnObjectOnException
Gets a value indicating whether the server-side business object should be returned to the client as p...
static string AuthenticationType
Gets the authentication type being used by the CSLA .NET framework.
IPrincipal User
Get or set the current IPrincipal object representing the user's identity.
ContextDictionary ClientContext
Returns the application-specific context data provided by the client.
Use this type to configure the settings for CSLA .NET.
Server-side data portal options.
Specifies a method used by the server-side data portal to initialize a new domain object.
Specifies a method used by the server-side data portal to delete domain object data during an update ...
Specifies a method used by the server-side data portal to delete domain object data during an explici...
Specifies a method used by the server-side data portal to execute a command object.
Specifies a method used by the server-side data portal to load existing data into the domain object.
Specifies a method used by the server-side data portal to insert domain object data during an update ...
A strongly-typed resource class, for looking up localized strings, etc.
static string BusinessPrincipalException
Looks up a localized string similar to Principal must be of type BusinessPrincipal,...
static string FailedOnServer
Looks up a localized string similar to failed on the server.
static string NoPrincipalAllowedException
Looks up a localized string similar to No principal object should be passed to DataPortal when using ...
Object containing information about the client request to the data portal.
Data portal server dashboard.
Allows the Data Portal call to be intercepted by a custom IDataPortalServer implementation.
Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
Provides consistent context information between the client and server DataPortal objects.
TransactionalTypes TransactionalType
Gets the current transactional type.
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.
This exception is returned from the server-side DataPortal and contains the exception and context dat...
Implements the server-side DataPortal message router as discussed in Chapter 4.
DataPortal(ApplicationContext applicationContext, IDashboard dashboard, CslaOptions options, IAuthorizeDataPortal authorizer, InterceptorManager interceptors, IObjectFactoryLoader factoryLoader, IDataPortalActivator activator, IDataPortalExceptionInspector exceptionInspector, DataPortalExceptionHandler exceptionHandler)
Creates an instance of the type.
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
static object[] GetCriteriaArray(object criteria)
Converts a single serializable criteria value into an array of type object.
static object GetCriteriaFromArray(params object[] criteria)
Converts a params array to a single serializable criteria value.
async Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
async Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
async Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
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...
static EmptyCriteria Instance
Gets an instance of EmptyCriteria
Arguments parameter passed to the interceptor methods.
TimeSpan Runtime
Gets or sets a value containing the elapsed runtime for this operation (only valid at end of operatio...
Manage dataportal interception using DI-registered implementations
void Initialize(InterceptArgs e)
Cascade the initial interception request prior to the main DataPortal operation
void Complete(InterceptArgs e)
Cascade the final interception request after the main DataPortal operation has completed
Null criteria used by the data portal as a placeholder for a create/fetch request that has a single n...
static NullCriteria Instance
Gets an instance of NullCriteria
Specifies that the data portal should invoke a factory object rather than the business object.
Implements the server-side Serviced DataPortal described in Chapter 4.
Implements the server-side Serviced DataPortal described in Chapter 4.
Implements the server-side Serviced DataPortal described in Chapter 4.
Implements the server-side Serviced DataPortal described in Chapter 4.
Implements the server-side Serviced DataPortal described in Chapter 4.
Marks a DataPortal_XYZ method to run within the specified transactional context.
TransactionIsolationLevel TransactionIsolationLevel
Specifies override for transaction isolation level.
Specifies a method used by the server-side data portal to update domain object data during an update ...
Data portal server dashboard.
Interface to be implemented by a custom authorization provider.
void Authorize(AuthorizeRequest clientRequest)
Implement this method to perform custom authorization on every data portal call.
Defines a type used to activate concrete business instances.
Implement this interface to check a DataPortalException before returning Exception to the client.
Interface implemented by server-side data portal components.
Task< DataPortalResult > Update(object obj, DataPortalContext context, bool isSync)
Update a business object.
Task< DataPortalResult > Create(Type objectType, object criteria, DataPortalContext context, bool isSync)
Create a new business object.
Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
Task< DataPortalResult > Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
Delete a business object.
Defines an interface to be implemented by a factory loader object that returns ObjectFactory objects ...
TransactionalTypes
Provides a list of possible transactional technologies to be used by the server-side DataPortal.
TransactionIsolationLevel
Specifies an isolation level for transactions controlled by TransactionalAttribute
DataPortalOperations
List of data portal operations.