我有一个问题,关于自动注册的IEventHandler<>与RegisterManyForOpenGeneric,这会导致一次注册(我做错了一些事情,我肯定)。
在我的应用程序中,由外部输入引发一个事件,然后将该输入分派到实现传入事件类型的IEventHandler<>的所有类:
// during bootstrap, the below code is called
resolver.RegisterManyForOpenGeneric(typeof(IEventHandler<>), AppDomain.CurrentDomain.GetAssemblies());
// when the app is running, an external event is published below:
public void PublishEvent(object evnt)
{
var handlerType = (typeof(IEventHandler<>)).MakeGenericType(evnt.getType());
var handlers = resolver.GetAllInstances(handlerType).ToList();
foreach(var handler in handlers)
{
/* do some reflection to get method */
method.Invoke(handler, new[] { evnt } });
}
}
// the below is an event handlers that get automatically wired up OK
class TopicVoteStatisticsProjection : IEventHandler<AdminActedOnTopic>
{
public void Handle(AdminActedOnTopic evnt)
{
// for every event, this gets called once and all is good
}
}上述所有IEventHandler<>类都是自动连接的,通过上述代码发布的所有事件都被发送到处理程序OK (对于上面的场景,我只有一个接口IEventHandler<AdminActedOnTopic>)。
但是,如果我在同一个实现类中注册了多个事件处理程序接口:
class TopicVoteStatisticsProjection : IEventHandler<UserVotedOnTopic>, IEventHandler<AdminActedOnTopic>
{
public void Handle(UserVotedOnTopic evnt)
{
}
public void Handle(AdminActedOnTopic evnt)
{
// for every event, this gets called twice!!
}
}然后,IEventHandler的相同处理程序将被返回两倍于:
handlers = resolver.GetAllInstances(typeof(IEventHandler<AdminActedOnTopic>)).ToList();
/* above query returns the following instances:
Projections.TopicVoteStatistics.TopicVoteStatisticsProjection
Projections.TopicVoteStatistics.TopicVoteStatisticsProjection
*/这太糟糕了,因为我的计算结果加倍了!在这种情况下,这两个事件是相关的,理想情况下,我希望将它们放在同一个处理程序实现中。
我的问题是--如何自动注册处理程序,其中有些处理程序实现了多个IEventHandler<> 接口,而没有重复注册?。
发布于 2015-02-13 20:21:56
我认为你所看到的行为是一个错误,它是在2.7版中引入的。详细描述如下:https://simpleinjector.codeplex.com/workitem/20996
它应该从2.7.2版本中修复。
https://stackoverflow.com/questions/28507879
复制相似问题