Exemplo n.º 1
0
 /// <summary>
 /// 查询执行元素
 /// </summary>
 /// <typeparam name="TEvent">事件类型</typeparam>
 /// <param name="commandContext">命令上下文</param>
 /// <param name="e">事件</param>
 /// <returns></returns>
 private List <EventExcutingElement> FindEventExcutingElement <TEvent>(ICommandContext commandContext, TEvent e)
     where TEvent : IEvent
 {
     try
     {
         var provider = new EventHandlerProvider(this.serviceLocator.BeginLifetimeScope());
         var elements = EventBusExcutingHelper.FindEventExcutingElement(provider, commandContext, e);
         commandContext.Items["BeginLifetimeScope"] = provider.Scope;
         return(elements);
     }
     catch
     {
         throw;
     }
 }
Exemplo n.º 2
0
        internal static List <EventExcutingElement> FindEventExcutingElement <TEvent>(EventHandlerProvider provider, ICommandContext commandContext, TEvent e) where TEvent : IEvent
        {
            var eventHandlers = provider.FindHandler <TEvent>();

            if (eventHandlers == null)
            {
                return new List <EventExcutingElement> {
                }
            }
            ;

            /*先获取到每个eventHandler的所有属性*/
            var elements = new List <EventExcutingElement>();

            foreach (var item in eventHandlers)
            {
                var handlerType = item.GetType();

                var            attributes      = HandlerBehaviorStorager.Default.GetAttributes(handlerType);
                var            sortAttribute   = ObjectExtension.GetAttribute <EventHandlerPriorityAttribute>(attributes);
                var            loggerAttribute = ObjectExtension.GetAttribute <Aop.LoggerAttribute>(attributes);
                ILoggerBuilder loggerBuilder   = null;
                if (loggerAttribute != null)
                {
                    try
                    {
                        loggerBuilder = loggerAttribute.RegisterKey.IsNotNullOrEmpty() ? provider.Scope.Resolve <ILoggerBuilder>(loggerAttribute.RegisterKey) : provider.Scope.ResolveOptional <ILoggerBuilder>();
                    }
                    catch
                    {
                    }
                }

                var eventContext = provider.FindEventContext();
                if (eventContext is DefaultEventContext)
                {
                    ((DefaultEventContext)eventContext).WorkTime = eventContext.WorkTime;
                }

                elements.Add(new EventExcutingElement()
                {
                    Order            = (sortAttribute == null || sortAttribute.Order < 0) ? 1 : sortAttribute.Order,
                    EventContext     = eventContext,
                    EventHandler     = item,
                    EventHandlerType = handlerType,
                    LoggerBuilder    = loggerBuilder ?? LoggerBuilder.Empty,
                    LoggerAttribute  = loggerAttribute,
                    HandlerFilters   = ObjectExtension.GetAttributes <EventHandlerFilterAttribute>(HandlerBehaviorStorager.Default.GetAttributes(handlerType, e.GetType()))
                });
            }

            elements.Sort((x, y) =>
            {
                if (x.Equals(y))
                {
                    return(1);
                }
                return(x.Order > y.Order ? 1 : -1);
            });

            return(elements);
        }