Exemplo n.º 1
0
        public void MakeAwareOf(ActionExecutionContext context)
        {
            var targetType = context.Target.GetType();
            var guard      = targetType.GetMethod(MethodName);

            if (guard == null)
            {
                guard = targetType.GetMethod("get_" + MethodName);
            }

            if (guard == null)
            {
                return;
            }

            var oldCanExecute = context.CanExecute;

            context.CanExecute = () =>
            {
                if (oldCanExecute != null && !oldCanExecute())
                {
                    return(false);
                }

                return((bool)guard.Invoke(
                           context.Target,
                           MessageBinder.DetermineParameters(context, guard.GetParameters())
                           ));
            };
        }
Exemplo n.º 2
0
            internal static void InvokeAction(ActionExecutionContext context)
            {
                var decorators = context.GetFilters()
                                 .OfType <IDecorateCoroutineFilter>()
                                 .ToArray();

                // Use the default behaviour when no decorators are found
                if (!decorators.Any())
                {
                    BaseInvokeAction(context);
                    return;
                }

                var values      = MessageBinder.DetermineParameters(context, context.Method.GetParameters());
                var returnValue = context.Method.Invoke(context.Target, values);

                IEnumerable <IResult> coroutine;

                if (returnValue is IResult)
                {
                    coroutine = new[] { returnValue as IResult };
                }
                else if (returnValue is IEnumerable <IResult> )
                {
                    coroutine = returnValue as IEnumerable <IResult>;
                }
                else
                {
                    return;
                }

                coroutine = decorators.Aggregate(coroutine, (current, decorator) => decorator.Decorate(current, context));

                Coroutine.BeginExecute(coroutine.GetEnumerator(), context);
            }
Exemplo n.º 3
0
        public static void InvokeAction(ActionExecutionContext context)
        {
            var values = MessageBinder.DetermineParameters(context, context.Method.GetParameters());
            var result = Coroutine.CreateParentEnumerator(ExecuteActionWithParameters(values).GetEnumerator());

            var wrappers = FilterManager.GetFiltersFor(context).OfType <IExecutionWrapper>();
            var pipeline = result.WrapWith(wrappers);

            //if pipeline has error, action execution should throw!
            pipeline.Completed += (o, e) =>
            {
                Execute.OnUIThread(() =>
                {
                    if (e.Error != null)
                    {
                        throw new Exception(
                            string.Format("An error occurred while executing {0}.",
                                          context.Message),
                            e.Error
                            );
                    }
                });
            };

            pipeline.Execute(context);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Executes the specified this action on the specified target.
        /// </summary>
        /// <param name="actionMessage">The action message.</param>
        /// <param name="handlingNode">The node.</param>
        /// <param name="context">The context.</param>
        public override void Execute(ActionMessage actionMessage, IInteractionNode handlingNode, object context)
        {
            try
            {
                var parameters = MessageBinder.DetermineParameters(
                    actionMessage,
                    UnderlyingRequirements,
                    handlingNode,
                    context
                    );

                TryUpdateTrigger(actionMessage, handlingNode, true);

                foreach (var filter in UnderlyingFilters.PreProcessors)
                {
                    if (filter.Execute(actionMessage, handlingNode, parameters))
                    {
                        continue;
                    }

                    TryUpdateTrigger(actionMessage, handlingNode, false);
                    return;
                }

                var outcome = new MessageProcessingOutcome(
                    UnderlyingMethod.Invoke(handlingNode.MessageHandler.Unwrap(), parameters),
                    UnderlyingMethod.Info.ReturnType,
                    false
                    );

                foreach (var filter in UnderlyingFilters.PostProcessors)
                {
                    filter.Execute(actionMessage, handlingNode, outcome);
                }

                HandleOutcome(actionMessage, handlingNode, outcome);
            }
            catch (Exception ex)
            {
                TryUpdateTrigger(actionMessage, handlingNode, false);
                if (!TryApplyRescue(actionMessage, handlingNode, ex))
                {
                    Log.Error(ex);
                    throw;
                }
                OnCompleted();
            }
        }
        public static void Hook()
        {
            var oldPrepareContext = ActionMessage.PrepareContext;

            ActionMessage.PrepareContext = context =>
            {
                oldPrepareContext(context);
                FilterFrameworkCoreCustomization.PrepareContext(context);
            };

            ActionMessage.InvokeAction = context =>
            {
                var values = MessageBinder.DetermineParameters(context, context.Method.GetParameters());
                FilterFrameworkCoreCustomization.InvokeAction(context, values);
            };
        }
Exemplo n.º 6
0
        /// <summary>
        /// Executes the specified this action on the specified target.
        /// </summary>
        /// <param name="actionMessage">The action message.</param>
        /// <param name="handlingNode">The node.</param>
        /// <param name="context">The context.</param>
        public override void Execute(ActionMessage actionMessage, IInteractionNode handlingNode, object context)
        {
            try
            {
                var parameters = MessageBinder.DetermineParameters(
                    actionMessage,
                    UnderlyingRequirements,
                    handlingNode,
                    context
                    );

                TryUpdateTrigger(actionMessage, handlingNode, true);

                CurrentTask = UnderlyingMethod.CreateBackgroundTask(handlingNode.MessageHandler.Unwrap(), parameters);

                foreach (var filter in UnderlyingFilters.PreProcessors)
                {
                    if (filter.Execute(actionMessage, handlingNode, parameters))
                    {
                        continue;
                    }

                    TryUpdateTrigger(actionMessage, handlingNode, false);
                    return;
                }

                DoExecute(actionMessage, handlingNode, parameters);
            }
            catch (Exception ex)
            {
                TryUpdateTrigger(actionMessage, handlingNode, false);
                if (!TryApplyRescue(actionMessage, handlingNode, ex))
                {
                    Log.Error(ex);
                    throw;
                }
                OnCompleted();
            }
        }