public async Task <bool> TryCallOperation(IPortalTarget target, PortalOperation operation)
        {
            await CheckAccess(operation.ToAuthorizationOperation());

            var methods = MethodsForOperation(operation) ?? new List <MethodInfo>();

            using (await target.StopAllActions())
            {
                var invoked = false;

                foreach (var method in methods)
                {
                    var success         = true;
                    var parameters      = method.GetParameters().ToList();
                    var parameterValues = new object[parameters.Count()];

                    for (var i = 0; i < parameterValues.Length; i++)
                    {
                        var parameter = parameters[i];
                        if (!Scope.IsRegistered(parameter.ParameterType))
                        {
                            // Assume it's a criteria not a dependency
                            success = false;
                            break;
                        }
                    }

                    if (success)
                    {
                        // No parameters or all of the parameters are dependencies
                        for (var i = 0; i < parameterValues.Length; i++)
                        {
                            var parameter = parameters[i];
                            parameterValues[i] = Scope.Resolve(parameter.ParameterType);
                        }

                        invoked = true;

                        var result = method.Invoke(target, parameterValues);
                        if (method.ReturnType == typeof(Task))
                        {
                            await(Task) result;
                        }

                        PostOperation(target, operation);

                        break;
                    }
                }

                return(invoked);
            }
        }
        public async Task <bool> TryCallOperation(IPortalTarget target, PortalOperation operation, object[] criteria, Type[] criteriaTypes)
        {
            await CheckAccess(operation.ToAuthorizationOperation(), criteria);

            using (await target.StopAllActions())
            {
                // The criteriaTypes need to be captured by Generic method definitions
                // in case the values sent in are null

                var method = MethodForOperation(operation, criteriaTypes);

                if (method != null)
                {
                    var parameters      = method.GetParameters().ToList();
                    var parameterValues = new object[parameters.Count()];

                    var criteriaE = criteria.GetEnumerator();

                    for (var i = 0; i < parameterValues.Length; i++)
                    {
                        if (criteriaE.MoveNext())
                        {
                            // Use up the criteria values first
                            // Assume MethodForOperation got the types right
                            parameterValues[i] = criteriaE.Current;
                        }
                        else
                        {
                            var parameter = parameters[i];
                            if (Scope.TryResolve(parameter.ParameterType, out var pv))
                            {
                                parameterValues[i] = pv;
                            }
                        }
                    }

                    var result = method.Invoke(target, parameterValues);

                    if (method.ReturnType == typeof(Task))
                    {
                        await(Task) result;
                    }

                    PostOperation(target, operation);

                    return(true);
                }

                return(false);
            }
        }
        protected virtual void PostOperation(IPortalTarget target, PortalOperation operation)
        {
            var editTarget = target as IPortalEditTarget;

            if (editTarget != null)
            {
                switch (operation)
                {
                case PortalOperation.Create:
                    editTarget.MarkNew();
                    break;

                case PortalOperation.CreateChild:
                    editTarget.MarkAsChild();
                    editTarget.MarkNew();
                    break;

                case PortalOperation.Fetch:
                    break;

                case PortalOperation.FetchChild:
                    editTarget.MarkAsChild();
                    break;

                case PortalOperation.Delete:
                    break;

                case PortalOperation.DeleteChild:
                    break;

                case PortalOperation.Insert:
                case PortalOperation.InsertChild:
                case PortalOperation.Update:
                case PortalOperation.UpdateChild:
                    editTarget.MarkUnmodified();
                    editTarget.MarkOld();
                    break;

                default:
                    break;
                }
            }
        }
示例#4
0
 public Stopped(IPortalTarget target)
 {
     this.Target = target;
 }