码迷,mamicode.com
首页 > 其他好文 > 详细

EventBus

时间:2019-12-08 15:48:31      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:nts   info   arguments   sha   sign   locale   factory   执行   key   

一、AbpLocalEventBusOptions,AbpDistributedEventBusOptions

IEventHandler是一个空方法,分为ILocalEventHandler<TEvent>,IDistributedEventHandler<TEvent>方法,

具有HandleEventAsync(EventType eventData))方法

保存着IEventHandler的实现类型,它通过OnRegistered遍历IServiceCollection服务的委托方法,将ILocalEventHandler<EventType>方法保存在AbpLocalEventBusOptions下列表

IDistributedEventHandler<EventType>保存在AbpDistributedEventBusOptions下列表里

而ActionEventHandle<T> 是本地ILocalEventHandle的方法,它是 TransientDependency方法,它执行是委托方法。

  

二、IEventBus它执行的是发布事件,订阅和取消订阅,EventBus是抽象方法,它的实现有NullDistributedEventBus, 这个不注入容器的

它的实现方法将Option的IEventHandler列表方法,将IEventHandle<eventType>eventType以及EventHandlerFactory(多个实例),即从容器根据HandlerType获取IEventHandler,进行订阅在EventBus里 

(1)、Local

(1)LocalBus:ISingletonDependency
订阅方法:保存EventType,以及工厂方法保存在字典里

protected ConcurrentDictionary<Type, List<IEventHandlerFactory>> HandlerFactories { get; }

IEventHandleFactory工厂方法GetHandler(),IsInFactories(List(IEventHandlerFactory)),有以下3个
>>SingleInstanceHandlerFactory:
Subscribe(Type eventType, IEventHandler handler)
Subscribe<TEvent>(
TEvent委托,使用是的ActionEvent<TEvent>
 public IEventHandlerDisposeWrapper GetHandler()
        {
            return new EventHandlerDisposeWrapper(HandlerInstance);
        }

>>TransientEventHandlerFactory:
Subscribe<TEvent, THandler>()

 protected virtual IEventHandler CreateHandler()
        {
            return (IEventHandler) Activator.CreateInstance(HandlerType);
        }
        public virtual IEventHandlerDisposeWrapper GetHandler()
        {
            var handler = CreateHandler();
            return new EventHandlerDisposeWrapper(
                handler,
                () => (handler as IDisposable)?.Dispose()
            );
        }


>>IocEventHandlerFactory,Option的默认
Subscribe(Type eventType, IEventHandlerFactory factory)
     public IEventHandlerDisposeWrapper GetHandler()
        {
            var scope = ScopeFactory.CreateScope();
            return new EventHandlerDisposeWrapper(
                (IEventHandler) scope.ServiceProvider.GetRequiredService(HandlerType),
                () => scope.Dispose()
            );
        }

 

 
将Option的handler订阅在EventBus里
 protected virtual void SubscribeHandlers(ITypeList<IEventHandler> handlers)
        {
            foreach (var handler in handlers)
            {
                var interfaces = handler.GetInterfaces();
                foreach (var @interface in interfaces)
                {
                    if (!typeof(IEventHandler).GetTypeInfo().IsAssignableFrom(@interface))
                    {
                        continue;
                    }

                    var genericArgs = @interface.GetGenericArguments();
                    if (genericArgs.Length == 1)
                    {
                        Subscribe(genericArgs[0], new IocEventHandlerFactory(ServiceScopeFactory, handler));
                    }
                }
            }
        }

 

(2)Distributed

1)LocalDistributedEventBus,它是TryRegister方法,它是SingletonDependency方法,它暴露是的服务接口是IDistributedEventBus以LocalDistributedEventBus

 并没有继承EventBus抽象方法

 

三、Publish方法

Task PublishAsync(Type eventType, object eventData);
Task PublishAsync<TEvent>(TEvent eventData)
            where TEvent : class;

执行ILocalEventHandler和 IDistributedEventHandler方法

 

每个EventType有多个List<IEventHandlerFactory> EventHandlerFactories

    protected override IEnumerable<EventTypeWithEventHandlerFactories> GetHandlerFactories(Type eventType)
        {
            var handlerFactoryList = new List<EventTypeWithEventHandlerFactories>();

            foreach (var handlerFactory in HandlerFactories.Where(hf => ShouldTriggerEventForHandler(eventType, hf.Key)))
            {
                handlerFactoryList.Add(new EventTypeWithEventHandlerFactories(handlerFactory.Key, handlerFactory.Value));
            }

            return handlerFactoryList.ToArray();
        }

 

  private static bool ShouldTriggerEventForHandler(Type targetEventType, Type handlerEventType)
        {
            //Should trigger same type
            if (handlerEventType == targetEventType)
            {
                return true;
            }

            //Should trigger for inherited types
            if (handlerEventType.IsAssignableFrom(targetEventType))
            {
                return true;
            }

            return false;
        }

四、RabbbitMQ

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

EventBus

标签:nts   info   arguments   sha   sign   locale   factory   执行   key   

原文地址:https://www.cnblogs.com/cloudsu/p/12001337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!