java ddd 事件总线_【DDD-Apwork框架】事件总线和事件聚合器

本文介绍了Java领域驱动设计(DDD)中事件总线的实现,使用了Apwork框架。通过代码详细展示了如何订阅、发布事件,以及事件处理的注册和取消注册过程。此外,还涉及了反射和泛型在事件总线中的应用。
摘要由CSDN通过智能技术生成

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;namespaceKeasy5.Events

{public classEventAggregator : IEventAggregator

{#region private property

private readonly object sync = new object();private readonly Dictionary> eventHandlers = new Dictionary>();private readonlyMethodInfo registerEventHandlerMethod;private readonly Func eventHandlerEquals = (o1, o2) =>{var o1Type =o1.GetType();var o2Type =o2.GetType();if (o1Type.IsGenericType &&o1Type.GetGenericTypeDefinition()== typeof(ActionDelegatedEventHandler<>) &&o2Type.IsGenericType&&o2Type.GetGenericTypeDefinition()== typeof(ActionDelegatedEventHandler<>))returno1.Equals(o2);return o1Type ==o2Type;

};//checks if the two event handlers are equal. if the event handler is an action-delegated, just simply//compare the two with the object.Equals override (since it was overriden by comparing the two delegates. Otherwise,//the type of the event handler will be used because we don't need to register the same type of the event handler//more than once for each specific event.

#endregion

#region Ctor

publicEventAggregator()

{

registerEventHandlerMethod= (from p in this.GetType().GetMethods()

let methodName=p.Name

let parameters=p.GetParameters()where methodName == "Subscribe" &&parameters!= null &&parameters.Length== 1 &&parameters[0].ParameterType.GetGenericTypeDefinition() == typeof(IEventHandler<>)selectp).First();

}///

///

///

///

///

///1.相关资料:C# 反射泛型/// http://www.cnblogs.com/easy5weikai/p/3790589.html

///2. 依赖注入:///

///

///

///

///

///

///

///

///

///

///

///

public EventAggregator(object[] handlers)

:this()

{foreach (var obj inhandlers)

{var type =obj.GetType();var implementedInterfaces =type.GetInterfaces();foreach (var implementedInterface inimplementedInterfaces)

{if (implementedInterface.IsGenericType &&implementedInterface.GetGenericTypeDefinition()== typeof(IEventHandler<>))

{var eventType =implementedInterface.GetGenericArguments().First();var method =registerEventHandlerMethod.MakeGenericMethod(eventType);

method.Invoke(this, new object[] { obj });

}

}

}

}#endregion

#region interface IEventAggregator members

public void Subscribe(IEventHandlereventHandler)where TEvent : class, IEvent

{lock(sync)

{var eventType = typeof(TEvent);if(eventHandlers.ContainsKey(eventType))

{var handlers =eventHandlers[eventType];if (handlers != null)

{if (!handlers.Exists(deh =>eventHandlerEquals(deh, eventHandler)))

handlers.Add(eventHandler);

}else{

handlers= new List();

handlers.Add(eventHandler);

}

}elseeventHandlers.Add(eventType,new List{ eventHandler });

}

}public void Subscribe(IEnumerable>eventHandlers)where TEvent : class, IEvent

{foreach (var eventHandler ineventHandlers)

Subscribe(eventHandler);

}public void Subscribe(params IEventHandler[] eventHandlers)where TEvent : class, IEvent

{foreach (var eventHandler ineventHandlers)

Subscribe(eventHandler);

}public void Subscribe(ActioneventHandlerFunc)where TEvent : class, IEvent

{

Subscribe(new ActionDelegatedEventHandler(eventHandlerFunc));

}public void Subscribe(IEnumerable>eventHandlerFuncs)where TEvent : class, IEvent

{foreach (var eventHandlerFunc ineventHandlerFuncs)

Subscribe(eventHandlerFunc);

}public void Subscribe(params Func[] eventHandlerFuncs)where TEvent : class, IEvent

{foreach (var eventHandlerFunc ineventHandlerFuncs)

Subscribe(eventHandlerFunc);

}public void Unsubscribe(IEventHandlereventHandler)where TEvent : class, IEvent

{lock(sync)

{var eventType = typeof(TEvent);if(eventHandlers.ContainsKey(eventType))

{var handlers =eventHandlers[eventType];if (handlers != null &&handlers.Exists(deh=>eventHandlerEquals(deh, eventHandler)))

{var handlerToRemove = handlers.First(deh =>eventHandlerEquals(deh, eventHandler));

handlers.Remove(handlerToRemove);

}

}

}

}public void Unsubscribe(IEnumerable>eventHandlers)where TEvent : class, IEvent

{foreach (var eventHandler ineventHandlers)

Unsubscribe(eventHandler);

}public void Unsubscribe(params IEventHandler[] eventHandlers)where TEvent : class, IEvent

{foreach (var eventHandler ineventHandlers)

Unsubscribe(eventHandler);

}public void Unsubscribe(ActioneventHandlerFunc)where TEvent : class, IEvent

{

Unsubscribe(new ActionDelegatedEventHandler(eventHandlerFunc));

}public void Unsubscribe(IEnumerable>eventHandlerFuncs)where TEvent : class, IEvent

{foreach (var eventHandlerFunc ineventHandlerFuncs)

Unsubscribe(eventHandlerFunc);

}public void Unsubscribe(params Func[] eventHandlerFuncs)where TEvent : class, IEvent

{foreach (var eventHandlerFunc ineventHandlerFuncs)

Unsubscribe(eventHandlerFunc);

}public void UnsubscribeAll()where TEvent : class, IEvent

{lock(sync)

{var eventType = typeof(TEvent);if(eventHandlers.ContainsKey(eventType))

{var handlers =eventHandlers[eventType];if (handlers != null)

handlers.Clear();

}

}

}public voidUnsubscribeAll()

{lock(sync)

{

eventHandlers.Clear();

}

}public IEnumerable> GetSubscriptions()where TEvent : class, IEvent

{var eventType = typeof(TEvent);if(eventHandlers.ContainsKey(eventType))

{var handlers =eventHandlers[eventType];if (handlers != null)return handlers.Select(p => p as IEventHandler).ToList();else

return null;

}else

return null;

}public void Publish(TEvent evnt)where TEvent : class, IEvent

{if (evnt == null)throw new ArgumentNullException("evnt");var eventType =evnt.GetType();if (eventHandlers.ContainsKey(eventType) &&eventHandlers[eventType]!= null &&eventHandlers[eventType].Count> 0)

{var handlers =eventHandlers[eventType];foreach (var handler inhandlers)

{var eventHandler = handler as IEventHandler;if (eventHandler == null)throw new ArgumentNullException("eventHandler");if (eventHandler.GetType().IsDefined(typeof(HandlesAsynchronouslyAttribute), false))

{

Task.Factory.StartNew((o)=>eventHandler.Handle((TEvent)o), evnt);

}else{

eventHandler.Handle(evnt);

}

}

}

}public void Publish(TEvent evnt,

Actioncallback,

TimeSpan? timeout = null)where TEvent : class, IEvent

{if (evnt == null)throw new ArgumentNullException("evnt");var eventType =evnt.GetType();if (eventHandlers.ContainsKey(eventType) &&eventHandlers[eventType]!= null &&eventHandlers[eventType].Count> 0)

{var handlers =eventHandlers[eventType];

List tasks = new List();try{foreach (var handler inhandlers)

{var eventHandler = handler as IEventHandler;if (eventHandler.GetType().IsDefined(typeof(HandlesAsynchronouslyAttribute), false))

{

tasks.Add(Task.Factory.StartNew((o)=>eventHandler.Handle((TEvent)o), evnt));

}else{

eventHandler.Handle(evnt);

}

}if (tasks.Count > 0)

{if (timeout == null)

Task.WaitAll(tasks.ToArray());elseTask.WaitAll(tasks.ToArray(), timeout.Value);

}

callback(evnt,true, null);

}catch(Exception ex)

{

callback(evnt,false, ex);

}

}elsecallback(evnt,false, null);

}#endregion}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值