StructureMap:在基类中注入原始属性
与此问题相关,但是这个问题是关于StructureMap的。
我有以下接口定义:
public interface ICommandHandler
{
ILogger Logger { get; set; }
bool SendAsync { get; set; }
}
我有多个实现 ICommandHandler
的实现,需要解析。我想在实现 ICommandHandler 的实例中自动注入这两个属性。
我找到了一种通过让所有实现继承基类型和 [SetterProperty] 属性来注入 ILogger 的方法:
public abstract class BaseCommandHandler : ICommandHandler
{
[SetterProperty]
public ILogger Logger { get; set; }
public bool SendAsync { get; set; }
}
但是,这并不能帮助我注入所有实现中的原始 bool
类型。
命令处理程序实现了一个从基本接口继承的通用接口:
public interface ICommandHandler<TCommand> : ICommandHandler
where TCommand : Command
{
void Handle(TCommand command);
}
这是我当前的配置:
var container = new Container();
container.Configure(r => r
.For<ICommandHandler<CustomerMovedCommand>>()
.Use<CustomerMovedHandler>()
.SetProperty(handler =>
{
handler.SendAsync = true;
}));
container.Configure(r => r
.For<ICommandHandler<ProcessOrderCommand>>()
.Use<ProcessOrderHandler>()
.SetProperty(handler =>
{
handler.SendAsync = true;
}));
container.Configure(r => r
.For<ICommandHandler<CustomerLeftCommand>>()
.Use<CustomerLeftHandler>()
.SetProperty(handler =>
{
handler.SendAsync = true;
}));
// etc. etc. etc.
如您所见,我为每个配置设置了属性,这非常麻烦(但是,>SetProperty
接受 Action
委托)。
有哪些方法可以使用 StructureMap 更有效地做到这一点?
Related to this question, but this question is about StructureMap.
I have got the following interface definition:
public interface ICommandHandler
{
ILogger Logger { get; set; }
bool SendAsync { get; set; }
}
I have multiple implementations that implement ICommandHandler
and need to be resolved. I want to automatically inject these two properties in instances that implement ICommandHandler
.
I found a way to do inject the ILogger
by letting all implementations inherit from a base type and the [SetterProperty]
attribute:
public abstract class BaseCommandHandler : ICommandHandler
{
[SetterProperty]
public ILogger Logger { get; set; }
public bool SendAsync { get; set; }
}
This however, doesn't help me with injecting the primitive bool
type in all implementations.
The command handlers implement a generic interface that inherits from the base interface:
public interface ICommandHandler<TCommand> : ICommandHandler
where TCommand : Command
{
void Handle(TCommand command);
}
Here is my current configuration:
var container = new Container();
container.Configure(r => r
.For<ICommandHandler<CustomerMovedCommand>>()
.Use<CustomerMovedHandler>()
.SetProperty(handler =>
{
handler.SendAsync = true;
}));
container.Configure(r => r
.For<ICommandHandler<ProcessOrderCommand>>()
.Use<ProcessOrderHandler>()
.SetProperty(handler =>
{
handler.SendAsync = true;
}));
container.Configure(r => r
.For<ICommandHandler<CustomerLeftCommand>>()
.Use<CustomerLeftHandler>()
.SetProperty(handler =>
{
handler.SendAsync = true;
}));
// etc. etc. etc.
As you can see, I set the property for each and every configuration, which is quite cumbersome (however, it is pretty nice that the SetProperty
accepts a Action<T>
delegate).
What ways are there to do this more efficiently with StructureMap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许你可以用这个?
Maybe you could use this?