10using System.Security.Principal;
11using System.Threading.Tasks;
30 Dashboard = Server.Dashboard.DashboardFactory.GetDashboard();
38 : this(
"CslaAuthorizationProvider")
48 protected DataPortal(
string cslaAuthorizationProviderAppSettingName)
49 : this(GetAuthProviderType(cslaAuthorizationProviderAppSettingName))
59 if (
null == authProviderType)
65 if (
null == _authorizer)
69 if (
null == _authorizer)
76 if (_interceptor ==
null)
80 if (_interceptor ==
null)
87 private static Type GetAuthProviderType(
string cslaAuthorizationProviderAppSettingName)
89 if (cslaAuthorizationProviderAppSettingName ==
null)
93 if (
null == _authorizer)
95 var authProvider = ConfigurationManager.AppSettings[cslaAuthorizationProviderAppSettingName];
96 return string.IsNullOrEmpty(authProvider) ?
97 typeof(NullAuthorizer) :
98 Type.GetType(authProvider, true);
102 return _authorizer.GetType();
110#if !NETSTANDARD2_0 && !NET5_0
111 private IDataPortalServer GetServicedComponentPortal(TransactionalAttribute transactionalAttribute)
113 switch (transactionalAttribute.TransactionIsolationLevel)
116 return new ServicedDataPortalSerializable();
118 return new ServicedDataPortalRepeatableRead();
120 return new ServicedDataPortalReadCommitted();
122 return new ServicedDataPortalReadUncommitted();
124 throw new ArgumentOutOfRangeException(
"transactionalAttribute");
138 public async Task<DataPortalResult>
Create(
149 DataPortalMethodInfo method;
151 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
152 if (criteria is Server.EmptyCriteria)
153 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
CreateAttribute>(objectType,
null);
155 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
CreateAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
156 serviceProviderMethodInfo.PrepForInvocation();
157 method = serviceProviderMethodInfo.DataPortalMethodInfo;
160 switch (method.TransactionalAttribute.TransactionType)
162#if !NETSTANDARD2_0 && !NET5_0
164 portal = GetServicedComponentPortal(method.TransactionalAttribute);
167 result = await portal.
Create(objectType, criteria, context, isSync).ConfigureAwait(
false);
171 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
179 result = await portal.
Create(objectType, criteria, context, isSync).ConfigureAwait(
false);
184 result = await portal.
Create(objectType, criteria, context, isSync).ConfigureAwait(
false);
195 catch (AggregateException ex)
197 Exception error =
null;
198 if (ex.InnerExceptions.Count > 0)
199 error = ex.InnerExceptions[0].InnerException;
220 ClearContext(context);
243 DataPortalMethodInfo method;
245 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
247 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
FetchAttribute>(objectType,
null);
249 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
FetchAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
251 serviceProviderMethodInfo.PrepForInvocation();
252 method = serviceProviderMethodInfo.DataPortalMethodInfo;
255 switch (method.TransactionalAttribute.TransactionType)
257#if !NETSTANDARD2_0 && !NET5_0
259 portal = GetServicedComponentPortal(method.TransactionalAttribute);
262 result = await portal.
Fetch(objectType, criteria, context, isSync).ConfigureAwait(
false);
266 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
272 result = await portal.
Fetch(objectType, criteria, context, isSync).ConfigureAwait(
false);
276 result = await portal.
Fetch(objectType, criteria, context, isSync).ConfigureAwait(
false);
287 catch (AggregateException ex)
289 Exception error =
null;
290 if (ex.InnerExceptions.Count > 0)
291 error = ex.InnerExceptions[0].InnerException;
312 ClearContext(context);
324 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1062:ValidateArgumentsOfPublicMethods")]
327 Type objectType =
null;
333 objectType = obj.GetType();
335 if (obj is Core.ICommandObject)
337 Initialize(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Operation = operation, IsSync = isSync });
341 DataPortalMethodInfo method;
343 if (factoryInfo !=
null)
347 var bbase = obj as Core.BusinessBase;
351 methodName = factoryInfo.DeleteMethodName;
353 methodName = factoryInfo.UpdateMethodName;
355 else if (obj is Core.ICommandObject)
356 methodName = factoryInfo.ExecuteMethodName;
358 methodName = factoryInfo.UpdateMethodName;
359 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName,
new object[] { obj });
363 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
364 var bbase = obj as Core.BusinessBase;
368 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
DeleteSelfAttribute>(objectType,
null);
371 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
InsertAttribute>(objectType,
null);
373 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
UpdateAttribute>(objectType,
null);
375 else if (obj is Core.ICommandObject)
376 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
ExecuteAttribute>(objectType,
null);
378 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
UpdateAttribute>(objectType,
null);
380 serviceProviderMethodInfo.PrepForInvocation();
381 method = serviceProviderMethodInfo.DataPortalMethodInfo;
386 switch (method.TransactionalAttribute.TransactionType)
388#if !NETSTANDARD2_0 && !NET5_0
390 portal = GetServicedComponentPortal(method.TransactionalAttribute);
393 result = await portal.
Update(obj, context, isSync).ConfigureAwait(
false);
397 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
403 result = await portal.
Update(obj, context, isSync).ConfigureAwait(
false);
407 result = await portal.
Update(obj, context, isSync).ConfigureAwait(
false);
410 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Result = result, Operation = operation, IsSync = isSync });
415 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = ex, Operation = operation, IsSync = isSync });
418 catch (AggregateException ex)
420 Exception error =
null;
421 if (ex.InnerExceptions.Count > 0)
422 error = ex.InnerExceptions[0].InnerException;
429 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync });
438 Complete(
new InterceptArgs { ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync });
443 ClearContext(context);
466 DataPortalMethodInfo method;
468 if (factoryInfo !=
null)
471 string methodName = factoryInfo.DeleteMethodName;
472 method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, criteria);
476 Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
478 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
DeleteAttribute>(objectType,
null);
480 serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod<
DeleteAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
481 serviceProviderMethodInfo.PrepForInvocation();
482 method = serviceProviderMethodInfo.DataPortalMethodInfo;
486 switch (method.TransactionalAttribute.TransactionType)
488#if !NETSTANDARD2_0 && !NET5_0
490 portal = GetServicedComponentPortal(method.TransactionalAttribute);
493 result = await portal.
Delete(objectType, criteria, context, isSync).ConfigureAwait(
false);
497 ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
503 result = await portal.
Delete(objectType, criteria, context, isSync).ConfigureAwait(
false);
507 result = await portal.
Delete(objectType, criteria, context, isSync).ConfigureAwait(
false);
518 catch (AggregateException ex)
520 Exception error =
null;
521 if (ex.InnerExceptions.Count > 0)
522 error = ex.InnerExceptions[0].InnerException;
543 ClearContext(context);
548 private static Type _interceptorType =
null;
549 private static bool _InterceptorTypeSet =
false;
560 if (!_InterceptorTypeSet)
562 var typeName = ConfigurationManager.AppSettings[
"CslaDataPortalInterceptor"];
563 if (!
string.IsNullOrWhiteSpace(typeName))
565 _InterceptorTypeSet =
true;
567 return _interceptorType;
571 _interceptorType = value;
572 _InterceptorTypeSet =
true;
578 var timer = ApplicationContext.ClientContext.GetValueOrNull(
"__dataportaltimer");
581 var startTime = (DateTimeOffset)timer;
582 e.
Runtime = DateTimeOffset.Now - startTime;
586 if (_interceptor !=
null)
590 internal void Initialize(InterceptArgs e)
592 ApplicationContext.ClientContext[
"__dataportaltimer"] = DateTimeOffset.Now;
595 if (_interceptor !=
null)
603 ApplicationContext.LogicalExecutionLocations _oldLocation;
605 private void SetContext(DataPortalContext context)
608 ApplicationContext.SetLogicalExecutionLocation(ApplicationContext.LogicalExecutionLocations.Server);
610 if (!context.IsRemotePortal && ApplicationContext.WebContextManager !=
null && !ApplicationContext.WebContextManager.IsValid)
611 ApplicationContext.SetContext(context.ClientContext, context.GlobalContext);
615 if (!context.IsRemotePortal)
return;
619 ApplicationContext.SetExecutionLocation(ApplicationContext.ExecutionLocations.Server);
623 ApplicationContext.SetContext(context.ClientContext, context.GlobalContext);
626 System.Threading.Thread.CurrentThread.CurrentCulture =
627 new System.Globalization.CultureInfo(context.ClientCulture);
628 System.Threading.Thread.CurrentThread.CurrentUICulture =
629 new System.Globalization.CultureInfo(context.ClientUICulture);
631 if (ApplicationContext.AuthenticationType ==
"Windows")
634 if (context.Principal !=
null)
642 AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
647 if (context.Principal ==
null)
655 ApplicationContext.User = context.Principal;
659 private void ClearContext(DataPortalContext context)
661 ApplicationContext.SetLogicalExecutionLocation(_oldLocation);
664 if (!context.IsRemotePortal)
return;
665 ApplicationContext.Clear();
666 if (ApplicationContext.AuthenticationType !=
"Windows")
667 ApplicationContext.User =
null;
674 private static object _syncRoot =
new object();
675 private static IAuthorizeDataPortal _authorizer =
null;
682 get {
return _authorizer; }
683 set { _authorizer = value; }
691 private static void AuthorizeRequest(AuthorizeRequest clientRequest)
693 _authorizer.Authorize(clientRequest);
714 internal static DataPortalException NewDataPortalException(
string message, Exception innerException,
object businessObject)
716 if (!ApplicationContext.DataPortalReturnObjectOnException)
717 businessObject =
null;
733 if (criteria !=
null)
734 if (criteria.GetType().Equals(typeof(
object[])))
735 clength = criteria.GetLength(0);
739 if (criteria ==
null || (clength == 1 && criteria[0] ==
null))
741 else if (clength == 0)
743 else if (clength == 1)
746 return new Core.MobileList<
object>(criteria);
757 if (criteria ==
null)
760 return Array.Empty<
object>();
762 return new object[] {
null };
763 else if (criteria.GetType().Equals(typeof(
object[])))
765 var array = (
object[])criteria;
766 var clength = array.GetLength(0);
768 return Array.Empty<
object>();
772 else if (criteria is Core.MobileList<
object> list)
773 return list.ToArray();
775 return new object[] { criteria };
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 AuthenticationProviderDoesNotImplementIAuthorizeDataPortal
Looks up a localized string similar to Csla Authentication Provider specified does not implement IAut...
static string AuthorizationProviderNameNotSpecified
Looks up a localized string similar to Csla Authorization Provider App Setting name not specified.
static string BusinessPrincipalException
Looks up a localized string similar to Principal must be of type BusinessPrincipal,...
static string CslaAuthenticationProviderNotSet
Looks up a localized string similar to Csla Authentication Provider not set!.
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.
Default implementation of the authorizer that allows all data portal calls to pass.
void Authorize(AuthorizeRequest clientRequest)
Creates an instance of the type.
Allows the Data Portal call to be intercepted by a custom IDataPortalServer implementation.
Provides consistent context information between the client and server DataPortal objects.
TransactionalTypes TransactionalType
Gets the current transactional type.
This class provides a hoook for developers to add custom error handling in the DataPortal.
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(string cslaAuthorizationProviderAppSettingName)
This construcor accepts the App Setting name for the Csla Authorization Provider, therefore getting t...
DataPortal()
Default constructor
async Task< DataPortalResult > Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
Get an existing business object.
static Type InterceptorType
Gets or sets the type of interceptor invoked by the data portal for pre- and post-processing of each ...
static object[] GetCriteriaArray(object criteria)
Converts a single serializable criteria value into an array of type object.
DataPortal(Type authProviderType)
This constructor accepts the Authorization Provider Type as a parameter.
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.
static IAuthorizeDataPortal Authorizer
Gets or sets a reference to the current authorizer.
static Dashboard.IDashboard Dashboard
Gets the data portal dashboard instance.
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
Server-side data portal implementation that invokes an object factory rather than directly interactin...
static IObjectFactoryLoader FactoryLoader
Gets or sets a delegate reference to the method called to create instances of factory objects as requ...
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...
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.
Specifies a method used by the server-side data portal to update domain object data during an update ...
Interface to be implemented by a custom authorization provider.
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.
Implement this interface to create a data portal interceptor that is notified each time the data port...
void Complete(InterceptArgs e)
Invoked at the end of each server-side data portal invocation for success and exception scenarios.
void Initialize(InterceptArgs e)
Invoked at the start of each server-side data portal invocation, immediately after the context has be...
Type GetFactoryType(string factoryName)
Returns the type of the factory object.
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.