使用结构图注册通用类型
我是结构图新手。我试图让 Structuremap 自动注册
public void RegisterAllEventHandlers()
{
Scan(cfg =>
{
cfg.TheCallingAssembly();
//cfg.IncludeNamespaceContainingType<NewCustomerCreated>();
cfg.IncludeNamespace("ParentNameSpace");
cfg.AddAllTypesOf(typeof (IHandle<NewCustomerCreated>));
});
//For(typeof (IHandle<>)).Use(typeof (NewCustomerCreated));
}
NewCustomerCreated
是事件,我想注册该事件的所有处理程序,即使用 IHandle
以下代码正在工作但我确信可以通过扫描来完成:-
ObjectFactory.Initialize(x =>
{
x.For(typeof(IHandle<NewCustomerCreated>))
.Add(new NewCustomerCreatedHandler());
x.For(typeof(IHandle<NewCustomerCreated>))
.Add(new SendWelcomeEmailToNewCustomer());
});
我正在尝试使用 DomainEvent raiser http://blog.robustsoftware.co.uk/2009 /08/better-domain-event-raiser.html
** 如果有人可以编辑问题以更好地反映我的要求,我将不胜感激 **
谢谢,
Mar
编辑 1:从博客添加代码
public interface IEventDispatcher
{
void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent;
}
public static class DomainEventDispatcher
{
public static IEventDispatcher Dispatcher { get; set; }
public static void Raise<TEvent>(TEvent eventToRaise) where TEvent : IDomainEvent
{
Dispatcher.Dispatch(eventToRaise);
}
}
public class StructureMapEventDispatcher : IEventDispatcher
{
public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
{
foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
{
handler.Handle(eventToDispatch);
}
}
从我的测试项目中,我调用注册表类,它将扫描程序集
public void RegisterAllEventHandlers()
{
Scan(cfg =>
{
cfg.TheCallingAssembly();
cfg.IncludeNamespace("Project1");
//cfg.AddAllTypesOf(typeof(IHandle<NewCustomerCreated>));
cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));
});
// This initializes correctly
//ObjectFactory.Initialize(x =>
//{
// x.For(typeof(IHandle<NewCustomerCreated>)).Add(new NewCustomerCreatedHandler());
//});
// Handler returns 0 count
var handler =ObjectFactory.GetAllInstances<IHandle<NewCustomerCreated>>();
}
,然后
var eventDispatcher = new StructureMapEventDispatcher();
DomainEventDispatcher.Dispatcher = eventDispatcher;
I am new to structuremap. I am trying to get Structuremap to auto-register
public void RegisterAllEventHandlers()
{
Scan(cfg =>
{
cfg.TheCallingAssembly();
//cfg.IncludeNamespaceContainingType<NewCustomerCreated>();
cfg.IncludeNamespace("ParentNameSpace");
cfg.AddAllTypesOf(typeof (IHandle<NewCustomerCreated>));
});
//For(typeof (IHandle<>)).Use(typeof (NewCustomerCreated));
}
NewCustomerCreated
is the event and I want to register all Handlers for this event ie ones using IHandle<NewCustomerCreated>
The following code is working but I am sure there it can be done by scanning :-
ObjectFactory.Initialize(x =>
{
x.For(typeof(IHandle<NewCustomerCreated>))
.Add(new NewCustomerCreatedHandler());
x.For(typeof(IHandle<NewCustomerCreated>))
.Add(new SendWelcomeEmailToNewCustomer());
});
I am trying to use DomainEvent raiser from
http://blog.robustsoftware.co.uk/2009/08/better-domain-event-raiser.html
** I would appreciate if someone can edit the question to reflect what I am asking in a better way **
Thank you,
Mar
Edit 1: Adding Code from blog
public interface IEventDispatcher
{
void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent;
}
public static class DomainEventDispatcher
{
public static IEventDispatcher Dispatcher { get; set; }
public static void Raise<TEvent>(TEvent eventToRaise) where TEvent : IDomainEvent
{
Dispatcher.Dispatch(eventToRaise);
}
}
public class StructureMapEventDispatcher : IEventDispatcher
{
public void Dispatch<TEvent>(TEvent eventToDispatch) where TEvent : IDomainEvent
{
foreach (var handler in ObjectFactory.GetAllInstances<IHandle<TEvent>>())
{
handler.Handle(eventToDispatch);
}
}
From my test project I am calling registry class which will scan the assembly
public void RegisterAllEventHandlers()
{
Scan(cfg =>
{
cfg.TheCallingAssembly();
cfg.IncludeNamespace("Project1");
//cfg.AddAllTypesOf(typeof(IHandle<NewCustomerCreated>));
cfg.ConnectImplementationsToTypesClosing(typeof(IHandle<>));
});
// This initializes correctly
//ObjectFactory.Initialize(x =>
//{
// x.For(typeof(IHandle<NewCustomerCreated>)).Add(new NewCustomerCreatedHandler());
//});
// Handler returns 0 count
var handler =ObjectFactory.GetAllInstances<IHandle<NewCustomerCreated>>();
}
and then
var eventDispatcher = new StructureMapEventDispatcher();
DomainEventDispatcher.Dispatcher = eventDispatcher;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
然后你的容器将完成剩下的工作:
如果你需要更多的控制,请看一下:
https://stackoverflow.com/questions/516892/使用扫描的通用类型的结构图自动注册
Try this:
Then your container will do the rest:
If you need more control take a look at this:
https://stackoverflow.com/questions/516892/structuremap-auto-registration-for-generic-types-using-scan