Пример #1
0
 /// <summary>
 /// Delete a business object.
 /// </summary>
 /// <param name="objectType">Type of business object to create.</param>
 /// <param name="criteria">Criteria object describing business object.</param>
 /// <param name="context">
 /// <see cref="Server.DataPortalContext" /> object passed to the server.
 /// </param>
 /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
 public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
 {
     try
     {
         DataPortalResult result = null;
         if (criteria is EmptyCriteria)
         {
             result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Delete, context.FactoryInfo.DeleteMethodName, objectType, context, isSync).ConfigureAwait(false);
         }
         else
         {
             result = await InvokeMethod(context.FactoryInfo.FactoryTypeName, DataPortalOperations.Delete, context.FactoryInfo.DeleteMethodName, objectType, criteria, context, isSync).ConfigureAwait(false);
         }
         return(result);
     }
     catch (Exception ex)
     {
         throw DataPortal.NewDataPortalException(
                   context.FactoryInfo.DeleteMethodName + " " + Resources.FailedOnServer,
                   new DataPortalExceptionHandler().InspectException(objectType, criteria, context.FactoryInfo.DeleteMethodName, ex),
                   null);
     }
 }
Пример #2
0
        /// <summary>
        /// Delete a business object.
        /// </summary>
        /// <param name="objectType">Type of business object to create.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
        public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            try
            {
                SetContext(context);

                Initialize(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Operation = DataPortalOperations.Delete, IsSync = isSync
                });

                AuthorizeRequest(new AuthorizeRequest(objectType, criteria, DataPortalOperations.Delete));
                DataPortalResult     result;
                DataPortalMethodInfo method;
                var factoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType);
                if (factoryInfo != null)
                {
                    var    factoryType = FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName);
                    string methodName  = factoryInfo.DeleteMethodName;
                    method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, criteria);
                }
                else
                {
                    method = DataPortalMethodCache.GetMethodInfo(objectType, "DataPortal_Delete", criteria);
                }

                IDataPortalServer portal;
#if !(ANDROID || IOS) && !NETFX_CORE
                switch (method.TransactionalAttribute.TransactionType)
                {
#if !MONO
                case TransactionalTypes.EnterpriseServices:
                    portal = GetServicedComponentPortal(method.TransactionalAttribute);
                    try
                    {
                        result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);
                    }
                    finally
                    {
                        ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
                    }
                    break;
#endif
                case TransactionalTypes.TransactionScope:
                    portal = new TransactionalDataPortal(method.TransactionalAttribute);
                    result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);

                    break;

                default:
                    portal = new DataPortalBroker();
                    result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);

                    break;
                }
#else
                portal = new DataPortalBroker();
                result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);
#endif
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Result = result, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                return(result);
            }
            catch (Csla.Server.DataPortalException ex)
            {
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = ex, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                throw;
            }
            catch (AggregateException ex)
            {
                Exception error = null;
                if (ex.InnerExceptions.Count > 0)
                {
                    error = ex.InnerExceptions[0].InnerException;
                }
                else
                {
                    error = ex;
                }
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Delete " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Delete", error),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                throw fex;
            }
            catch (Exception ex)
            {
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Delete " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Delete", ex),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                throw fex;
            }
            finally
            {
                ClearContext(context);
            }
        }
Пример #3
0
        public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
        {
            Type objectType = null;
            DataPortalOperations operation = DataPortalOperations.Update;

            try
            {
                SetContext(context);

                objectType = obj.GetType();

                if (obj is Core.ICommandObject)
                {
                    operation = DataPortalOperations.Execute;
                }
                Initialize(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Operation = operation, IsSync = isSync
                });

                AuthorizeRequest(new AuthorizeRequest(objectType, obj, operation));
                DataPortalResult     result;
                DataPortalMethodInfo method;
                var factoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType);
                if (factoryInfo != null)
                {
                    string methodName;
                    var    factoryType = FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName);
                    var    bbase       = obj as Core.BusinessBase;
                    if (bbase != null)
                    {
                        if (bbase.IsDeleted)
                        {
                            methodName = factoryInfo.DeleteMethodName;
                        }
                        else
                        {
                            methodName = factoryInfo.UpdateMethodName;
                        }
                    }
                    else if (obj is Core.ICommandObject)
                    {
                        methodName = factoryInfo.ExecuteMethodName;
                    }
                    else
                    {
                        methodName = factoryInfo.UpdateMethodName;
                    }
                    method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, new object[] { obj });
                }
                else
                {
                    string methodName;
                    var    bbase = obj as Core.BusinessBase;
                    if (bbase != null)
                    {
                        if (bbase.IsDeleted)
                        {
                            methodName = "DataPortal_DeleteSelf";
                        }
                        else
                        if (bbase.IsNew)
                        {
                            methodName = "DataPortal_Insert";
                        }
                        else
                        {
                            methodName = "DataPortal_Update";
                        }
                    }
                    else if (obj is Core.ICommandObject)
                    {
                        methodName = "DataPortal_Execute";
                    }
                    else
                    {
                        methodName = "DataPortal_Update";
                    }
                    method = DataPortalMethodCache.GetMethodInfo(obj.GetType(), methodName);
                }
#if !(ANDROID || IOS) && !NETFX_CORE
                context.TransactionalType = method.TransactionalAttribute.TransactionType;
#else
                context.TransactionalType = method.TransactionalType;
#endif
                IDataPortalServer portal;
#if !(ANDROID || IOS) && !NETFX_CORE
                switch (method.TransactionalAttribute.TransactionType)
                {
#if !MONO
                case TransactionalTypes.EnterpriseServices:
                    portal = GetServicedComponentPortal(method.TransactionalAttribute);
                    try
                    {
                        result = await portal.Update(obj, context, isSync).ConfigureAwait(false);
                    }
                    finally
                    {
                        ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
                    }
                    break;
#endif
                case TransactionalTypes.TransactionScope:
                    portal = new TransactionalDataPortal(method.TransactionalAttribute);
                    result = await portal.Update(obj, context, isSync).ConfigureAwait(false);

                    break;

                default:
                    portal = new DataPortalBroker();
                    result = await portal.Update(obj, context, isSync).ConfigureAwait(false);

                    break;
                }
#else
                portal = new DataPortalBroker();
                result = await portal.Update(obj, context, isSync).ConfigureAwait(false);
#endif
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Result = result, Operation = operation, IsSync = isSync
                });
                return(result);
            }
            catch (Csla.Server.DataPortalException ex)
            {
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Exception = ex, Operation = operation, IsSync = isSync
                });
                throw;
            }
            catch (AggregateException ex)
            {
                Exception error = null;
                if (ex.InnerExceptions.Count > 0)
                {
                    error = ex.InnerExceptions[0].InnerException;
                }
                else
                {
                    error = ex;
                }
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Update " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, "DataPortal.Update", error),
                    obj);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync
                });
                throw fex;
            }
            catch (Exception ex)
            {
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Update " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, "DataPortal.Update", ex),
                    obj);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync
                });
                throw fex;
            }
            finally
            {
                ClearContext(context);
            }
        }
Пример #4
0
        /// <summary>
        /// Get an existing business object.
        /// </summary>
        /// <param name="objectType">Type of business object to retrieve.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
        public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            try
            {
                SetContext(context);

                Initialize(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });

                AuthorizeRequest(new AuthorizeRequest(objectType, criteria, DataPortalOperations.Fetch));
                DataPortalResult result;

                DataPortalMethodInfo method = DataPortalMethodCache.GetFetchMethod(objectType, criteria);

                IDataPortalServer portal;
#if !SILVERLIGHT && !NETFX_CORE
                switch (method.TransactionalAttribute.TransactionType)
                {
#if !MONO
                case TransactionalTypes.EnterpriseServices:
                    portal = GetServicedComponentPortal(method.TransactionalAttribute);
                    try
                    {
                        result = await portal.Fetch(objectType, criteria, context, isSync);
                    }
                    finally
                    {
                        ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
                    }
                    break;
#endif
                case TransactionalTypes.TransactionScope:
                    portal = new TransactionalDataPortal(method.TransactionalAttribute);
                    result = await portal.Fetch(objectType, criteria, context, isSync);

                    break;

                default:
                    portal = new DataPortalSelector();
                    result = await portal.Fetch(objectType, criteria, context, isSync);

                    break;
                }
#else
                portal = new DataPortalSelector();
                result = await portal.Fetch(objectType, criteria, context, isSync);
#endif
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Result = result, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                return(result);
            }
            catch (Csla.Server.DataPortalException ex)
            {
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = ex, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                throw;
            }
            catch (AggregateException ex)
            {
                Exception error = null;
                if (ex.InnerExceptions.Count > 0)
                {
                    error = ex.InnerExceptions[0].InnerException;
                }
                else
                {
                    error = ex;
                }
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Fetch " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Fetch", error),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                throw fex;
            }
            catch (Exception ex)
            {
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Fetch " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Fetch", ex),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                throw fex;
            }
            finally
            {
                ClearContext(context);
            }
        }
Пример #5
0
        public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            LateBoundObject   obj    = null;
            IDataPortalTarget target = null;
            var eventArgs            = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Delete);

            try
            {
                // create an instance of the business objet
                obj = new LateBoundObject(ApplicationContext.DataPortalActivator.CreateInstance(objectType));
                ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);

                target = obj.Instance as IDataPortalTarget;

                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvoke(eventArgs);
                }
                else
                {
                    obj.CallMethodIfImplemented("DataPortal_OnDataPortalInvoke", eventArgs);
                }

                // tell the business object to delete itself
                await obj.CallMethodTryAsync("DataPortal_Delete", criteria).ConfigureAwait(false);

                var busy = obj.Instance as Csla.Core.ITrackStatus;
                if (busy != null && busy.IsBusy)
                {
                    throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name));
                }

                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvokeComplete(eventArgs);
                }
                else
                {
                    obj.CallMethodIfImplemented("DataPortal_OnDataPortalInvokeComplete", eventArgs);
                }

                return(new DataPortalResult());
            }
            catch (Exception ex)
            {
                try
                {
                    if (target != null)
                    {
                        target.DataPortal_OnDataPortalException(eventArgs, ex);
                    }
                    else if (obj != null)
                    {
                        obj.CallMethodIfImplemented("DataPortal_OnDataPortalException", eventArgs, ex);
                    }
                }
                catch
                {
                    // ignore exceptions from the exception handler
                }
                throw DataPortal.NewDataPortalException(
                          "DataPortal.Delete " + Resources.FailedOnServer,
                          new DataPortalExceptionHandler().InspectException(objectType, obj, null, "DataPortal.Delete", ex),
                          null);
            }
        }
Пример #6
0
        public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
        {
            DataPortalOperations operation = DataPortalOperations.Update;
            Type            objectType     = obj.GetType();
            var             target         = obj as IDataPortalTarget;
            LateBoundObject lb             = new LateBoundObject(obj);

            ApplicationContext.DataPortalActivator.InitializeInstance(lb.Instance);
            try
            {
                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvoke(
                        new DataPortalEventArgs(context, objectType, obj, operation));
                }
                else
                {
                    lb.CallMethodIfImplemented(
                        "DataPortal_OnDataPortalInvoke",
                        new DataPortalEventArgs(context, objectType, obj, operation));
                }

                // tell the business object to update itself
                var busObj = obj as Core.BusinessBase;
                if (busObj != null)
                {
                    if (busObj.IsDeleted)
                    {
                        if (!busObj.IsNew)
                        {
                            // tell the object to delete itself
                            await lb.CallMethodTryAsync("DataPortal_DeleteSelf").ConfigureAwait(false);
                        }
                        if (target != null)
                        {
                            target.MarkNew();
                        }
                        else
                        {
                            lb.CallMethodIfImplemented("MarkNew");
                        }
                    }
                    else
                    {
                        if (busObj.IsNew)
                        {
                            // tell the object to insert itself
                            await lb.CallMethodTryAsync("DataPortal_Insert").ConfigureAwait(false);
                        }
                        else
                        {
                            // tell the object to update itself
                            await lb.CallMethodTryAsync("DataPortal_Update").ConfigureAwait(false);
                        }
                        if (target != null)
                        {
                            target.MarkOld();
                        }
                        else
                        {
                            lb.CallMethodIfImplemented("MarkOld");
                        }
                    }
                }
                else if (obj is Core.ICommandObject)
                {
                    operation = DataPortalOperations.Execute;
                    // tell the object to update itself
                    await lb.CallMethodTryAsync("DataPortal_Execute").ConfigureAwait(false);
                }
                else
                {
                    // this is an updatable collection or some other
                    // non-BusinessBase type of object
                    // tell the object to update itself
                    await lb.CallMethodTryAsync("DataPortal_Update").ConfigureAwait(false);

                    if (target != null)
                    {
                        target.MarkOld();
                    }
                    else
                    {
                        lb.CallMethodIfImplemented("MarkOld");
                    }
                }

                var busy = busObj as Csla.Core.ITrackStatus;
                if (busy != null && busy.IsBusy)
                {
                    throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name));
                }

                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvokeComplete(
                        new DataPortalEventArgs(context, objectType, obj, operation));
                }
                else
                {
                    lb.CallMethodIfImplemented("DataPortal_OnDataPortalInvokeComplete",
                                               new DataPortalEventArgs(context, objectType, obj, operation));
                }

                return(new DataPortalResult(obj));
            }
            catch (Exception ex)
            {
                try
                {
                    if (target != null)
                    {
                        target.DataPortal_OnDataPortalException(
                            new DataPortalEventArgs(context, objectType, obj, operation), ex);
                    }
                    else
                    {
                        lb.CallMethodIfImplemented("DataPortal_OnDataPortalException", new DataPortalEventArgs(context, objectType, obj, operation), ex);
                    }
                }
                catch
                {
                    // ignore exceptions from the exception handler
                }
                throw DataPortal.NewDataPortalException(
                          "DataPortal.Update " + Resources.FailedOnServer,
                          new DataPortalExceptionHandler().InspectException(obj.GetType(), obj, null, "DataPortal.Update", ex),
                          obj);
            }
        }
Пример #7
0
        public async Task <DataPortalResult> Create(
            Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            LateBoundObject   obj    = null;
            IDataPortalTarget target = null;
            var eventArgs            = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Create);

            try
            {
                // create an instance of the business object.
                obj = new LateBoundObject(ApplicationContext.DataPortalActivator.CreateInstance(objectType));
                ApplicationContext.DataPortalActivator.InitializeInstance(obj.Instance);

                target = obj.Instance as IDataPortalTarget;

                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvoke(eventArgs);
                    target.MarkNew();
                }
                else
                {
                    obj.CallMethodIfImplemented("DataPortal_OnDataPortalInvoke", eventArgs);
                    obj.CallMethodIfImplemented("MarkNew");
                }

                // tell the business object to create its data
                if (criteria is EmptyCriteria)
                {
                    await obj.CallMethodTryAsync("DataPortal_Create").ConfigureAwait(false);
                }
                else
                {
                    await obj.CallMethodTryAsync("DataPortal_Create", criteria).ConfigureAwait(false);
                }

                var busy = obj.Instance as Csla.Core.ITrackStatus;
                if (busy != null && busy.IsBusy)
                {
                    throw new InvalidOperationException(string.Format("{0}.IsBusy == true", objectType.Name));
                }

                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvokeComplete(eventArgs);
                }
                else
                {
                    obj.CallMethodIfImplemented(
                        "DataPortal_OnDataPortalInvokeComplete", eventArgs);
                }

                // return the populated business object as a result
                return(new DataPortalResult(obj.Instance));
            }
            catch (Exception ex)
            {
                try
                {
                    if (target != null)
                    {
                        target.DataPortal_OnDataPortalException(eventArgs, ex);
                    }
                    else if (obj != null)
                    {
                        obj.CallMethodIfImplemented("DataPortal_OnDataPortalException", eventArgs, ex);
                    }
                }
                catch
                {
                    // ignore exceptions from the exception handler
                }
                object outval = null;
                if (obj != null)
                {
                    outval = obj.Instance;
                }
                throw DataPortal.NewDataPortalException(
                          "DataPortal.Create " + Resources.FailedOnServer,
                          new DataPortalExceptionHandler().InspectException(objectType, outval, criteria, "DataPortal.Create", ex),
                          outval);
            }
            finally
            {
                object reference = null;
                if (obj != null)
                {
                    reference = obj.Instance;
                }
                ApplicationContext.DataPortalActivator.FinalizeInstance(reference);
            }
        }
Пример #8
0
        /// <summary>
        /// Delete a business object.
        /// </summary>
        /// <param name="objectType">Type of business object to create.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
        public async Task <DataPortalResult> Delete(Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            try
            {
                SetContext(context);

                Initialize(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Operation = DataPortalOperations.Delete, IsSync = isSync
                });

                AuthorizeRequest(new AuthorizeRequest(ApplicationContext, objectType, criteria, DataPortalOperations.Delete));
                DataPortalResult     result;
                DataPortalMethodInfo method;
                var factoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType);
                if (factoryInfo != null)
                {
                    var    factoryType = FactoryDataPortal.FactoryLoader.GetFactoryType(factoryInfo.FactoryTypeName);
                    string methodName  = factoryInfo.DeleteMethodName;
                    method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, criteria);
                }
                else
                {
                    Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
                    if (criteria is EmptyCriteria)
                    {
                        serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod <DeleteAttribute>(objectType, null);
                    }
                    else
                    {
                        serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod <DeleteAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
                    }
                    serviceProviderMethodInfo.PrepForInvocation();
                    method = serviceProviderMethodInfo.DataPortalMethodInfo;
                }

                IDataPortalServer portal;
                switch (method.TransactionalAttribute.TransactionType)
                {
#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
                case TransactionalTypes.EnterpriseServices:
                    portal = GetServicedComponentPortal(method.TransactionalAttribute);
                    try
                    {
                        result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);
                    }
                    finally
                    {
                        ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
                    }
                    break;
#endif
                case TransactionalTypes.TransactionScope:
                    portal = new TransactionalDataPortal(method.TransactionalAttribute);
                    result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);

                    break;

                default:
                    portal = ApplicationContext.CreateInstanceDI <DataPortalBroker>();
                    result = await portal.Delete(objectType, criteria, context, isSync).ConfigureAwait(false);

                    break;
                }
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Result = result, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                return(result);
            }
            catch (Csla.Server.DataPortalException ex)
            {
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = ex, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                throw;
            }
            catch (AggregateException ex)
            {
                Exception error = null;
                if (ex.InnerExceptions.Count > 0)
                {
                    error = ex.InnerExceptions[0].InnerException;
                }
                else
                {
                    error = ex;
                }
                var fex = DataPortal.NewDataPortalException(
                    ApplicationContext, "DataPortal.Delete " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Delete", error),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                throw fex;
            }
            catch (Exception ex)
            {
                var fex = DataPortal.NewDataPortalException(
                    ApplicationContext, "DataPortal.Delete " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Delete", ex),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Delete, IsSync = isSync
                });
                throw fex;
            }
            finally
            {
                ClearContext(context);
            }
        }
Пример #9
0
        /// <summary>
        /// Get an existing business object.
        /// </summary>
        /// <param name="objectType">Type of business object to retrieve.</param>
        /// <param name="criteria">Criteria object describing business object.</param>
        /// <param name="context">
        /// <see cref="Server.DataPortalContext" /> object passed to the server.
        /// </param>
        /// <param name="isSync">True if the client-side proxy should synchronously invoke the server.</param>
        public async Task <DataPortalResult> Fetch(Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            try
            {
                SetContext(context);

                Initialize(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });

                AuthorizeRequest(new AuthorizeRequest(objectType, criteria, DataPortalOperations.Fetch));
                DataPortalResult     result;
                DataPortalMethodInfo method;

#if NET40
                method = DataPortalMethodCache.GetFetchMethod(objectType, criteria);
#else
                Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
                if (criteria is EmptyCriteria)
                {
                    serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod <FetchAttribute>(objectType, null);
                }
                else
                {
                    serviceProviderMethodInfo = Reflection.ServiceProviderMethodCaller.FindDataPortalMethod <FetchAttribute>(objectType, Server.DataPortal.GetCriteriaArray(criteria));
                }

                serviceProviderMethodInfo.PrepForInvocation();
                method = serviceProviderMethodInfo.DataPortalMethodInfo;
#endif

                IDataPortalServer portal;
#if !(ANDROID || IOS) && !NETFX_CORE
                switch (method.TransactionalAttribute.TransactionType)
                {
#if !MONO && !NETSTANDARD2_0 && !NET5_0
                case TransactionalTypes.EnterpriseServices:
                    portal = GetServicedComponentPortal(method.TransactionalAttribute);
                    try
                    {
                        result = await portal.Fetch(objectType, criteria, context, isSync).ConfigureAwait(false);
                    }
                    finally
                    {
                        ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
                    }
                    break;
#endif
                case TransactionalTypes.TransactionScope:
                    portal = new TransactionalDataPortal(method.TransactionalAttribute);
                    result = await portal.Fetch(objectType, criteria, context, isSync).ConfigureAwait(false);

                    break;

                default:
                    portal = new DataPortalBroker();
                    result = await portal.Fetch(objectType, criteria, context, isSync).ConfigureAwait(false);

                    break;
                }
#else
                portal = new DataPortalBroker();
                result = await portal.Fetch(objectType, criteria, context, isSync).ConfigureAwait(false);
#endif
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Result = result, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                return(result);
            }
            catch (Csla.Server.DataPortalException ex)
            {
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = ex, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                throw;
            }
            catch (AggregateException ex)
            {
                Exception error = null;
                if (ex.InnerExceptions.Count > 0)
                {
                    error = ex.InnerExceptions[0].InnerException;
                }
                else
                {
                    error = ex;
                }
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Fetch " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Fetch", error),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                throw fex;
            }
            catch (Exception ex)
            {
                var fex = DataPortal.NewDataPortalException(
                    "DataPortal.Fetch " + Resources.FailedOnServer,
                    new DataPortalExceptionHandler().InspectException(objectType, criteria, "DataPortal.Fetch", ex),
                    null);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = criteria, Exception = fex, Operation = DataPortalOperations.Fetch, IsSync = isSync
                });
                throw fex;
            }
            finally
            {
                ClearContext(context);
            }
        }
Пример #10
0
        public async Task <DataPortalResult> Update(object obj, DataPortalContext context, bool isSync)
        {
            Type objectType = null;
            DataPortalOperations operation = DataPortalOperations.Update;

            try
            {
                SetContext(context);

                objectType = obj.GetType();

                if (obj is Core.ICommandObject)
                {
                    operation = DataPortalOperations.Execute;
                }

                AuthorizeRequest(new AuthorizeRequest(objectType, obj, operation));

                Initialize(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Operation = operation, IsSync = isSync
                });

                DataPortalResult     result;
                DataPortalMethodInfo method;
                var factoryInfo = ObjectFactoryAttribute.GetObjectFactoryAttribute(objectType);
                if (factoryInfo != null)
                {
                    string methodName;
                    var    factoryLoader = ApplicationContext.CurrentServiceProvider.GetService(typeof(Server.IObjectFactoryLoader)) as Server.IObjectFactoryLoader;
                    var    factoryType   = factoryLoader?.GetFactoryType(factoryInfo.FactoryTypeName);
                    if (obj is Core.BusinessBase bbase)
                    {
                        if (bbase.IsDeleted)
                        {
                            methodName = factoryInfo.DeleteMethodName;
                        }
                        else
                        {
                            methodName = factoryInfo.UpdateMethodName;
                        }
                    }
                    else if (obj is Core.ICommandObject)
                    {
                        methodName = factoryInfo.ExecuteMethodName;
                    }
                    else
                    {
                        methodName = factoryInfo.UpdateMethodName;
                    }
                    method = Server.DataPortalMethodCache.GetMethodInfo(factoryType, methodName, new object[] { obj });
                }
                else
                {
                    Reflection.ServiceProviderMethodInfo serviceProviderMethodInfo;
                    if (obj is Core.BusinessBase bbase)
                    {
                        if (bbase.IsDeleted)
                        {
                            serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod <DeleteSelfAttribute>(objectType, null);
                        }
                        else
                        if (bbase.IsNew)
                        {
                            serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod <InsertAttribute>(objectType, null);
                        }
                        else
                        {
                            serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod <UpdateAttribute>(objectType, null);
                        }
                    }
                    else if (obj is Core.ICommandObject)
                    {
                        serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod <ExecuteAttribute>(objectType, null);
                    }
                    else
                    {
                        serviceProviderMethodInfo = ServiceProviderMethodCaller.FindDataPortalMethod <UpdateAttribute>(objectType, null);
                    }

                    serviceProviderMethodInfo.PrepForInvocation();
                    method = serviceProviderMethodInfo.DataPortalMethodInfo;
                }

                context.TransactionalType = method.TransactionalAttribute.TransactionType;
                IDataPortalServer portal;
                switch (method.TransactionalAttribute.TransactionType)
                {
#if !NETSTANDARD2_0 && !NET5_0 && !NET6_0
                case TransactionalTypes.EnterpriseServices:
                    portal = GetServicedComponentPortal(method.TransactionalAttribute);
                    try
                    {
                        result = await portal.Update(obj, context, isSync).ConfigureAwait(false);
                    }
                    finally
                    {
                        ((System.EnterpriseServices.ServicedComponent)portal).Dispose();
                    }
                    break;
#endif
                case TransactionalTypes.TransactionScope:
                    var broker = ApplicationContext.CreateInstanceDI <DataPortalBroker>();
                    portal = new TransactionalDataPortal(broker, method.TransactionalAttribute);
                    result = await portal.Update(obj, context, isSync).ConfigureAwait(false);

                    break;

                default:
                    portal = ApplicationContext.CreateInstanceDI <DataPortalBroker>();
                    result = await portal.Update(obj, context, isSync).ConfigureAwait(false);

                    break;
                }
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Result = result, Operation = operation, IsSync = isSync
                });
                return(result);
            }
            catch (Csla.Server.DataPortalException ex)
            {
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Exception = ex, Operation = operation, IsSync = isSync
                });
                throw;
            }
            catch (AggregateException ex)
            {
                Exception error = null;
                if (ex.InnerExceptions.Count > 0)
                {
                    error = ex.InnerExceptions[0].InnerException;
                }
                else
                {
                    error = ex;
                }
                var fex = DataPortal.NewDataPortalException(
                    ApplicationContext, "DataPortal.Update " + Resources.FailedOnServer,
                    DataPortalExceptionHandler.InspectException(obj.GetType(), obj, null, "DataPortal.Update", error),
                    obj);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync
                });
                throw fex;
            }
            catch (Exception ex)
            {
                var fex = DataPortal.NewDataPortalException(
                    ApplicationContext, "DataPortal.Update " + Resources.FailedOnServer,
                    DataPortalExceptionHandler.InspectException(obj.GetType(), obj, null, "DataPortal.Update", ex),
                    obj);
                Complete(new InterceptArgs {
                    ObjectType = objectType, Parameter = obj, Exception = fex, Operation = operation, IsSync = isSync
                });
                throw fex;
            }
            finally
            {
                ClearContext(context);
            }
        }
Пример #11
0
        public async Task <DataPortalResult> Create(
            Type objectType, object criteria, DataPortalContext context, bool isSync)
        {
            LateBoundObject   obj    = null;
            IDataPortalTarget target = null;
            var eventArgs            = new DataPortalEventArgs(context, objectType, criteria, DataPortalOperations.Create);

            try
            {
                // create an instance of the business object.
                obj = new LateBoundObject(objectType);

                target = obj.Instance as IDataPortalTarget;

                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvoke(eventArgs);
                    target.MarkNew();
                }
                else
                {
                    obj.CallMethodIfImplemented("DataPortal_OnDataPortalInvoke", eventArgs);
                    obj.CallMethodIfImplemented("MarkNew");
                }

                // tell the business object to create its data
                if (criteria is EmptyCriteria)
                {
                    await obj.CallMethodTryAsync("DataPortal_Create");
                }
                else
                {
                    await obj.CallMethodTryAsync("DataPortal_Create", criteria);
                }

                if (target != null)
                {
                    target.DataPortal_OnDataPortalInvokeComplete(eventArgs);
                }
                else
                {
                    obj.CallMethodIfImplemented(
                        "DataPortal_OnDataPortalInvokeComplete", eventArgs);
                }

                // return the populated business object as a result
                return(new DataPortalResult(obj.Instance));
            }
            catch (Exception ex)
            {
                try
                {
                    if (target != null)
                    {
                        target.DataPortal_OnDataPortalException(eventArgs, ex);
                    }
                    else if (obj != null)
                    {
                        obj.CallMethodIfImplemented("DataPortal_OnDataPortalException", eventArgs, ex);
                    }
                }
                catch
                {
                    // ignore exceptions from the exception handler
                }
                object outval = null;
                if (obj != null)
                {
                    outval = obj.Instance;
                }
                throw DataPortal.NewDataPortalException(
                          "DataPortal.Create " + Resources.FailedOnServer,
                          new DataPortalExceptionHandler().InspectException(objectType, outval, criteria, "DataPortal.Create", ex),
                          outval);
            }
        }