예제 #1
0
        /// <summary>
        /// 找出当前的命令信息
        /// </summary>
        /// <typeparam name="TCommand">信息类型</typeparam>
        /// <param name="command">信息</param>
        /// <param name="communication">上下文通讯</param>
        /// <returns></returns>
        protected CommandExcutingElement FindCommandExcutingElement <TCommand>(TCommand command, HandlerCommunication communication) where TCommand : ICommand
        {
            var provider = new CommandHandlerProvider(this.serviceLocator.BeginLifetimeScope());
            var element  = CommandBusExcutingHelper.FindCommandExcutingElement(provider, command, communication);

            communication["BeginLifetimeScope"] = provider.Scope;
            return(element);
        }
예제 #2
0
        /// <summary>
        /// 找出当前的命令信息
        /// </summary>
        /// <typeparam name="TCommand">信息类型</typeparam>
        /// <param name="provider"></param>
        /// <param name="command">信息</param>
        /// <param name="communication">上下文通讯</param>
        /// <returns></returns>
        internal static CommandExcutingElement FindCommandExcutingElement <TCommand>(CommandHandlerProvider provider, TCommand command, HandlerCommunication communication) where TCommand : ICommand
        {
            /*查找事件监听者*/
            var commandListeners = provider.FindCommandHandler <TCommand>();

            if (commandListeners == null || commandListeners.Length <= 0)
            {
                return(new CommandExcutingElement());
            }

            if (commandListeners.Length >= 2)
            {
                throw new InvalidException(string.Format("the command {0} has more handler", typeof(TCommand).Name));
            }

            var helper = new CommandExcutingElement();
            /*命令上下文*/
            var commandContext = provider.FindCommandContext();
            var defaultContext = commandContext as ICommandContextInitable;

            if (defaultContext != null)
            {
                defaultContext.OnInit(communication, command);
            }

            helper.CommandContext = commandContext;

            /*发布命令*/
            var handler     = commandListeners[0];
            var handlerType = handler.GetType();

            helper.CommandHandler     = commandListeners[0];
            helper.CommandHandlerType = handler.GetType();

            /*handler所有属性*/
            var handlerAttributes = HandlerBehaviorStorager.Default.GetAttributes(handlerType);
            var excuteAttributes  = HandlerBehaviorStorager.Default.GetAttributes(handlerType, command.GetType());
            var handlerFilters    = ObjectExtension.GetAttributes <CommandHandlerFilterAttribute>(handlerAttributes) ?? new CommandHandlerFilterAttribute[] { };
            var excuteFilters     = ObjectExtension.GetAttributes <CommandHandlerFilterAttribute>(excuteAttributes) ?? new CommandHandlerFilterAttribute[] { };

            helper.HandlerFilters = handlerFilters;
            helper.ExcuteFilters  = excuteFilters;

            /*全局命令过滤器*/
            var authorizeAttributes = ObjectExtension.GetAttributes <CommandHandlerAuthorizeAttribute>(handlerAttributes) ?? new CommandHandlerAuthorizeAttribute[] { };

            helper.AuthorizeFilters = authorizeAttributes;

            /*日志预留接口*/
            var loggerAttribute = ObjectExtension.GetAttribute <LoggerAttribute>(handlerAttributes);

            if (loggerAttribute != null)
            {
                helper.LoggerAttribute = loggerAttribute;
                try
                {
                    var loggerBuilder = loggerAttribute.RegisterKey.IsNotNullOrEmpty() ? provider.Scope.Resolve <ILoggerBuilder>(loggerAttribute.RegisterKey) : provider.Scope.ResolveOptional <ILoggerBuilder>();
                    helper.LoggerBuilder = loggerBuilder ?? LoggerBuilder.Empty;
                }
                catch
                {
                }
            }

            return(helper);
        }