public void can_execute_a_callback()
        {
            var method = Mock <IMethod>();
            var target = new MethodHost();
            var result = new object();

            var handlingNode = new InteractionNode(
                new ControlHost(),
                Mock <IRoutedMessageController>()
                );

            handlingNode.RegisterHandler(Mock <IRoutedMessageHandler>());

            handlingNode.MessageHandler.Stub(x => x.Unwrap())
            .Return(target);

            methodFactory.Expect(x => x.CreateFrom(info))
            .Return(method);

            attribute.Initialize(typeof(MethodHost), null, container);

            method.Expect(x => x.Invoke(target, result)).Return(typeof(string));
            method.Stub(x => x.Info).Return(typeof(object).GetMethod("ToString"));

            var outcome = new MessageProcessingOutcome(result, result.GetType(), false);

            attribute.Execute(null, handlingNode, outcome);
        }
        public void can_execute_a_callback()
        {
            var method = Mock<IMethod>();
            var target = new MethodHost();
            var result = new object();

            var handlingNode = new InteractionNode(
                new ControlHost(),
                Mock<IRoutedMessageController>()
                );

            handlingNode.RegisterHandler(Mock<IRoutedMessageHandler>());

            handlingNode.MessageHandler.Stub(x => x.Unwrap())
                .Return(target);

            methodFactory.Expect(x => x.CreateFrom(info))
                .Return(method);

            attribute.Initialize(typeof(MethodHost), null, container);

            method.Expect(x => x.Invoke(target, result)).Return(typeof(string));
            method.Stub(x => x.Info).Return(typeof(object).GetMethod("ToString"));

            var outcome = new MessageProcessingOutcome(result, result.GetType(), false);

            attribute.Execute(null, handlingNode, outcome);
        }
        /// <summary>
        /// Executes the filter.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="handlingNode">The handling node.</param>
        /// <param name="outcome">The outcome of processing the message</param>
        public virtual void Execute(IRoutedMessage message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
        {
            if(callback == null || outcome.WasCancelled)
                return;

            outcome.Result = callback.Invoke(handlingNode.MessageHandler.Unwrap(), outcome.Result);
            outcome.ResultType = callback.Info.ReturnType;
        }
Exemple #4
0
        public void Execute(IRoutedMessage message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
        {
            var content = LanguageKey.IsNullOrEmpty() ? Message : LanguageReader.GetValue(LanguageKey);

            ServiceLocator.Current.GetInstance <IAuditLogModel>().Write(new AuditLog {
                Action = content, CurrentUser = ApplicationCache.Get <string>(Global.LoggerId)
            });
        }
        public override void Execute(ActionMessage actionMessage, IInteractionNode handlingNode, object context)
        {
            try
            {
                var parameters = _messageBinder.DetermineParameters(actionMessage, _requirements, handlingNode, context);

                // IBeforeProcessor 继承 IPreProcessor 相当于一样
                var processors = _filters.PreProcessors.OfType <IBeforeProcessor>();


                // caliburn preProcessor (before) (针对 IBeforeProcessor)
                foreach (var filter in _filters.PreProcessors.Except(processors.Cast <IPreProcessor>()))
                {
                    if (!filter.Execute(actionMessage, handlingNode, parameters))
                    {
                        return;
                    }
                }

                // before custom aop hehavior (针对IPreProcessor)
                foreach (var before in processors.OrderByDescending(p => p.Priority))
                {
                    if (!before.BeforeExecute(actionMessage, handlingNode, parameters))
                    {
                        return;
                    }
                }

                object result = null;

                // the method will excute
                result = _method.Invoke(handlingNode.MessageHandler.Unwrap(), parameters);

                var outcome = new MessageProcessingOutcome(result, _method.Info.ReturnType, false);

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

                HandleOutcome(actionMessage, handlingNode, outcome);
            }
            catch (Exception ex)
            {
                if (!TryApplyRescue(actionMessage, handlingNode, ex))
                {
                    throw;
                }
                OnCompleted();
            }
        }
Exemple #6
0
        public void alters_return_with_post_execute_filters()
        {
            var handlingNode = Stub <IInteractionNode>();
            var sourceNode   = Stub <IInteractionNode>();
            var result       = Mock <IResult>();

            var message = new ActionMessage();

            message.Initialize(sourceNode);

            var target     = new object();
            var parameters = new object[] {
                5, "param"
            };
            var returnValue = new object();
            var filter      = MockRepository.GenerateMock <IPostProcessor>();
            var context     = EventArgs.Empty;

            var handler = Stub <IRoutedMessageHandler>();

            handler.Stub(x => x.Unwrap()).Return(new object());
            handlingNode.Stub(x => x.MessageHandler).Return(handler);

            messageBinder.Expect(x => x.DetermineParameters(message, null, handlingNode, context))
            .Return(parameters);

            filterManager.Stub(x => x.PreProcessors)
            .Return(new IPreProcessor[] {});

            method.Expect(x => x.Invoke(target, parameters))
            .Return(returnValue);

            filterManager.Stub(x => x.PostProcessors)
            .Return(new[] {
                filter
            });

            var outcome = new MessageProcessingOutcome(returnValue, returnValue.GetType(), false);

            filter.Expect(x => x.Execute(message, handlingNode, outcome));

            messageBinder.Expect(x => x.CreateResult(outcome))
            .IgnoreArguments()
            .Return(result);

            result.Expect(x => x.Execute(null)).IgnoreArguments();

            action.Execute(message, handlingNode, context);
        }
Exemple #7
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();
            }
        }
Exemple #8
0
        private void HandleOutcome(IRoutedMessageWithOutcome message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
        {
            var result = _messageBinder.CreateResult(outcome);

            result.Completed += (r, ex) =>{
                if(ex != null)
                {
                    if(!TryApplyRescue(message, handlingNode, ex))
                        throw ex;
                }

                OnCompleted();
            };

            result.Execute(message, handlingNode);
        }
        public void alters_return_with_post_execute_filters()
        {
            var handlingNode = Stub<IInteractionNode>();
            var sourceNode = Stub<IInteractionNode>();
            var result = Mock<IResult>();

            var message = new ActionMessage();
            message.Initialize(sourceNode);

            var target = new object();
            var parameters = new object[] {
                5, "param"
            };
            var returnValue = new object();
            var filter = MockRepository.GenerateMock<IPostProcessor>();
            var context = EventArgs.Empty;

            var handler = Stub<IRoutedMessageHandler>();
            handler.Stub(x => x.Unwrap()).Return(new object());
            handlingNode.Stub(x => x.MessageHandler).Return(handler);

            messageBinder.Expect(x => x.DetermineParameters(message, null, handlingNode, context))
                .Return(parameters);

            filterManager.Stub(x => x.PreProcessors)
                .Return(new IPreProcessor[] {});

            method.Expect(x => x.Invoke(target, parameters))
                .Return(returnValue);

            filterManager.Stub(x => x.PostProcessors)
                .Return(new[] {
                    filter
                });

            var outcome = new MessageProcessingOutcome(returnValue, returnValue.GetType(), false);

            filter.Expect(x => x.Execute(message, handlingNode, outcome));

            messageBinder.Expect(x => x.CreateResult(outcome))
                .IgnoreArguments()
                .Return(result);

            result.Expect(x => x.Execute(null)).IgnoreArguments();

            action.Execute(message, handlingNode, context);
        }
        /// <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,
                    _requirements,
                    handlingNode,
                    context
                    );

                TryUpdateTrigger(actionMessage, handlingNode, true);

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

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

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

                foreach (var filter in _filters.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();
            }
        }
Exemple #11
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,
                    _requirements,
                    handlingNode,
                    context
                    );

                foreach (var filter in _filters.PreProcessors)
                {
                    if (!filter.Execute(actionMessage, handlingNode, parameters))
                    {
                        return;
                    }
                }

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

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

                HandleOutcome(actionMessage, handlingNode, outcome);
            }
            catch (Exception ex)
            {
                if (!TryApplyRescue(actionMessage, handlingNode, ex))
                {
                    throw;
                }
                OnCompleted();
            }
        }
        private void HandleOutcome(ActionMessage message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
        {
            var result = _messageBinder.CreateResult(outcome);

            result.Completed += (s, e) =>{
                TryUpdateTrigger(message, handlingNode, false);

                if(e.Error != null)
                {
                    if(!TryApplyRescue(message, handlingNode, e.Error))
                    {
                        Log.Error(e.Error);
                        throw e.Error;
                    }
                }

                OnCompleted();
            };

            result.Execute(new ResultExecutionContext(serviceLocator, message, handlingNode));
        }
Exemple #13
0
        private void HandleOutcome(IRoutedMessageWithOutcome message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
        {
            var result = _messageBinder.CreateResult(outcome);

            result.Completed += (r, ex) => {
                if (ex != null)
                {
                    if (!TryApplyRescue(message, handlingNode, ex))
                    {
                        throw ex;
                    }
                }

                OnCompleted();
            };

            result.Execute(message, handlingNode);
        }
Exemple #14
0
        private void HandleOutcome(ActionMessage message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
        {
            var result = MessageBinder.CreateResult(outcome);

            result.Completed += (s, e) => {
                TryUpdateTrigger(message, handlingNode, false);

                if (e.Error != null)
                {
                    if (!TryApplyRescue(message, handlingNode, e.Error))
                    {
                        Log.Error(e.Error);
                        throw e.Error;
                    }
                }

                OnCompleted();
            };

            result.Execute(new ResultExecutionContext(serviceLocator, message, handlingNode));
        }
Exemple #15
0
 public IResult CreateResult(MessageProcessingOutcome outcome)
 {
     throw new NotImplementedException();
 }
        /// <summary>
        /// Executes the core logic, specific to the action type.
        /// </summary>
        /// <param name="actionMessage">The action message.</param>
        /// <param name="handlingNode">The node.</param>
        /// <param name="parameters">The parameters.</param>
        protected void DoExecute(ActionMessage actionMessage, IInteractionNode handlingNode, object[] parameters)
        {
            CurrentTask.Completed +=
                (s, e) => Invocation.Execute.OnUIThread(
                              () =>{
                                  Interlocked.Decrement(ref runningCount);
                                  if(e.Error != null)
                                  {
                                      TryUpdateTrigger(actionMessage, handlingNode, false);
                                      if(!TryApplyRescue(actionMessage, handlingNode, e.Error))
                                      {
                                          Log.Error(e.Error);
                                          throw e.Error;
                                      }
                                      OnCompleted();
                                  }
                                  else
                                  {
                                      try
                                      {
                                          var outcome = new MessageProcessingOutcome(
                                              e.Cancelled ? null : e.Result,
                                              _method.Info.ReturnType,
                                              e.Cancelled
                                              );

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

                                          var result = _messageBinder.CreateResult(outcome);

                                          result.Completed += (r, arg) =>{
                                              TryUpdateTrigger(actionMessage, handlingNode, false);

                                              if(arg.Error != null)
                                              {
                                                  if(!TryApplyRescue(actionMessage, handlingNode, arg.Error))
                                                  {
                                                      Log.Error(arg.Error);
                                                      throw arg.Error;
                                                  }
                                              }

                                              OnCompleted();
                                          };

                                          result.Execute(new ResultExecutionContext(serviceLocator, actionMessage, handlingNode));
                                      }
                                      catch(Exception ex)
                                      {
                                          TryUpdateTrigger(actionMessage, handlingNode, false);
                                          if(!TryApplyRescue(actionMessage, handlingNode, ex))
                                          {
                                              Log.Error(ex);
                                              throw;
                                          }
                                          OnCompleted();
                                      }
                                  }
                              });

			Interlocked.Increment(ref runningCount);
            CurrentTask.Start(this);
            CurrentTask = null;
        }
Exemple #17
0
 void IPostProcessor.Execute(IRoutedMessage message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
 {
     ServiceLocator.Current.GetInstance <IWindowManager>().ShowDialog(outcome.Result);
 }
 public IResult CreateResult(MessageProcessingOutcome outcome)
 {
     throw new NotImplementedException();
 }
Exemple #19
0
        /// <summary>
        /// Executes the core logic, specific to the action type.
        /// </summary>
        /// <param name="actionMessage">The action message.</param>
        /// <param name="handlingNode">The node.</param>
        /// <param name="parameters">The parameters.</param>
        protected void DoExecute(ActionMessage actionMessage, IInteractionNode handlingNode, object[] parameters)
        {
            CurrentTask.Completed +=
                (s, e) => Invocation.Execute.OnUIThread(
                    () => {
                Interlocked.Decrement(ref runningCount);
                if (e.Error != null)
                {
                    TryUpdateTrigger(actionMessage, handlingNode, false);
                    if (!TryApplyRescue(actionMessage, handlingNode, e.Error))
                    {
                        Log.Error(e.Error);
                        throw e.Error;
                    }
                    OnCompleted();
                }
                else
                {
                    try
                    {
                        var outcome = new MessageProcessingOutcome(
                            e.Cancelled ? null : e.Result,
                            UnderlyingMethod.Info.ReturnType,
                            e.Cancelled
                            );

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

                        var result = MessageBinder.CreateResult(outcome);

                        result.Completed += (r, arg) => {
                            TryUpdateTrigger(actionMessage, handlingNode, false);

                            if (arg.Error != null)
                            {
                                if (!TryApplyRescue(actionMessage, handlingNode, arg.Error))
                                {
                                    Log.Error(arg.Error);
                                    throw arg.Error;
                                }
                            }

                            OnCompleted();
                        };

                        result.Execute(new ResultExecutionContext(serviceLocator, actionMessage, handlingNode));
                    }
                    catch (Exception ex)
                    {
                        TryUpdateTrigger(actionMessage, handlingNode, false);
                        if (!TryApplyRescue(actionMessage, handlingNode, ex))
                        {
                            Log.Error(ex);
                            throw;
                        }
                        OnCompleted();
                    }
                }
            });

            Interlocked.Increment(ref runningCount);
            CurrentTask.Start(this);
            CurrentTask = null;
        }
Exemple #20
0
        /// <summary>
        /// Attaches the task complete.
        /// </summary>
        /// <param name="actionMessage">The action message.</param>
        /// <param name="handlingNode">The handling node.</param>
        /// <param name="parameters">The parameters.</param>
        protected void AttachTaskEvents(ActionMessage actionMessage, IInteractionNode handlingNode, object[] parameters)
        {
            if (CurrentBackgroundActionInfo.ShowBusyCursor)
            {
                CurrentBackgroundTask.Starting +=
                    (s, e) => Caliburn.Core.Invocation.Execute.OnUIThread(
                        () =>
                {
                    FrameworkElement element = handlingNode.UIElement as FrameworkElement;
                    element.Cursor           = Cursors.Wait;
                });

                CurrentBackgroundTask.Completed +=
                    (s, e) => Caliburn.Core.Invocation.Execute.OnUIThread(
                        () =>
                {
                    FrameworkElement element = handlingNode.UIElement as FrameworkElement;
                    element.Cursor           = Cursors.Arrow;
                });
            }
            CurrentBackgroundTask.Completed +=
                (s, e) => Caliburn.Core.Invocation.Execute.OnUIThread(
                    () =>
            {
                Interlocked.Decrement(ref _runningCount);
                if (e.Error != null)
                {
                    TryUpdateTrigger(actionMessage, handlingNode, false);
                    if (!TryApplyRescue(actionMessage, handlingNode, e.Error))
                    {
                        throw e.Error;
                    }
                    OnCompleted();
                }
                else
                {
                    try
                    {
                        var outcome = new MessageProcessingOutcome(
                            e.Result,
                            _method.Info.ReturnType,
                            e.Cancelled
                            );

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

                        var result = _messageBinder.CreateResult(outcome);

                        result.Completed += (r, ex) =>
                        {
                            TryUpdateTrigger(actionMessage, handlingNode, false);

                            if (ex != null)
                            {
                                if (!TryApplyRescue(actionMessage, handlingNode, ex))
                                {
                                    throw ex;
                                }
                            }

                            OnCompleted();
                        };

                        result.Execute(actionMessage, handlingNode);
                    }
                    catch (Exception ex)
                    {
                        TryUpdateTrigger(actionMessage, handlingNode, false);
                        if (!TryApplyRescue(actionMessage, handlingNode, ex))
                        {
                            throw;
                        }
                        OnCompleted();
                    }
                }
            });

            Interlocked.Increment(ref _runningCount);
        }
        /// <summary>
        /// Executes the filter.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="handlingNode">The handling node.</param>
        /// <param name="outcome">The outcome of processing the message</param>
        public virtual void Execute(IRoutedMessage message, IInteractionNode handlingNode, MessageProcessingOutcome outcome)
        {
            if (_callback == null || outcome.WasCancelled)
            {
                return;
            }

            outcome.Result     = _callback.Invoke(handlingNode.MessageHandler.Unwrap(), outcome.Result);
            outcome.ResultType = _callback.Info.ReturnType;
        }
Exemple #22
0
        /// <summary>
        /// Executes the core logic, specific to the action type.
        /// </summary>
        /// <param name="actionMessage">The action message.</param>
        /// <param name="handlingNode">The node.</param>
        /// <param name="parameters">The parameters.</param>
        protected void DoExecute(ActionMessage actionMessage, IInteractionNode handlingNode, object[] parameters)
        {
            CurrentTask.Completed +=
                (s, e) => Core.Invocation.Execute.OnUIThread(
                    () => {
                Interlocked.Decrement(ref _runningCount);
                if (e.Error != null)
                {
                    TryUpdateTrigger(actionMessage, handlingNode, false);
                    if (!TryApplyRescue(actionMessage, handlingNode, e.Error))
                    {
                        throw e.Error;
                    }
                    OnCompleted();
                }
                else
                {
                    try
                    {
                        var outcome = new MessageProcessingOutcome(
                            e.Result,
                            _method.Info.ReturnType,
                            e.Cancelled
                            );

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

                        var result = _messageBinder.CreateResult(outcome);

                        result.Completed += (r, ex) => {
                            TryUpdateTrigger(actionMessage, handlingNode, false);

                            if (ex != null)
                            {
                                if (!TryApplyRescue(actionMessage, handlingNode, ex))
                                {
                                    throw ex;
                                }
                            }

                            OnCompleted();
                        };

                        result.Execute(actionMessage, handlingNode);
                    }
                    catch (Exception ex)
                    {
                        TryUpdateTrigger(actionMessage, handlingNode, false);
                        if (!TryApplyRescue(actionMessage, handlingNode, ex))
                        {
                            throw;
                        }
                        OnCompleted();
                    }
                }
            });

            Interlocked.Increment(ref _runningCount);
            CurrentTask.Enqueue(this);
            CurrentTask = null;
        }