0
说我有协议处理程序列表的字典列表,并在客户端服务知道使用基于一个枚举值的协议,这将是不错的选择的协议从传递的I/F的列表在structuremap注入的接口
我怎样才能在StructureMap做到这一点?:
public EmailTransportService(interfaces...,
IDictionary<EmailAccountType, IEmailTransportHandler> transportHandlers)
目前,我使用的ObjectFactory与获得命名实例,像这样:
_emailTransportHandlers = new Dictionary<EmailAccountType, string>
{
{EmailAccountType.Pop3, "Pop3Handler"},
{EmailAccountType.IMAP, "IMapHandler"}
};
然后像这样解决:
private IEmailTransportHandler GetTransportHandler(EmailAccountType accountType)
{
return ObjectFactory.GetNamedInstance<IEmailTransportHandler>(_emailTransportHandlers[accountType]);
}
但我不喜欢这个,因为它很难在我的单元测试中验证对处理程序的调用。
我的服务注册中心看起来像这样:
public EmailTransportServiceRegistry()
{
Scan(x =>
{
....
});
For<IEmailTransportHandler>().Use<ActiveUpPop3Handler>().Named("Pop3Handler");
For<IEmailTransportHandler>().Use<ActiveUpIMap4Handler>().Named("IMapHandler");
}
所以基本上我根据协议类型的字典列表上依靠命名实例。