sheng-jie / eventbus Goto Github PK
View Code? Open in Web Editor NEWC# 事件总线实现
Home Page: http://www.jianshu.com/p/22fbe7a7c120
License: MIT License
C# 事件总线实现
Home Page: http://www.jianshu.com/p/22fbe7a7c120
License: MIT License
圣杰兄请教一个问题下,我结合了Autofac实现了事件总线的注册和Trigger的解耦,但是唯独Action的注册和调用没法通过Autofac做到,后面尝试使用反射实现也没办法,原因是ActionEventHandler的构造函数就是一个Action,希望 圣杰兄帮忙指点下。这是我触发事件的那一段代码:
public void Trigger(TEventData eventData) where TEventData : IEventData
{
//从事件映射集合里获取匹配当前EventData(事件源数据)的Handler
List handlerTypes = _eventStore.GetHandlersForEvent(eventData.GetType()).ToList();
if (handlerTypes.Count <= 0) return;
//循环执行事件处理函数
foreach (var handlerType in handlerTypes)
{
var handlerInterface = handlerType.GetInterface("IEventHandler`1");
var eventHandler = ContainerManager.Current.Container.Resolve(handlerInterface);
if (eventHandler.GetType().FullName == handlerType.FullName)
{
var handler = eventHandler as IEventHandler;
handler?.HandleEvent(eventData);
}
else
{
//如果Ioc容器里没有则需要反射生成
//因为ActionEventHandler的构造函数需要有指定的入参:Action,这里没法反射!!
MethodInfo methodInfo = handlerType.GetMethod("HandleEvent").MakeGenericMethod();
if (methodInfo != null)
{
object obj = Activator.CreateInstance(handlerType);
methodInfo.Invoke(obj, new object[] { eventData });
}
}
}
}
看了下,和微软eshop实现原理一样,现在遇到数据一致性,和同步等待的问题,有没有计划实现?
线程安全字典集合可以采用 类似这样的 ConcurrentDictionary<string, ConcurrentBag<ServiceResult>> 处理方式,,这样就可不用设置线程同步锁对象了
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.