简单注入器:在基类中注入属性
几个星期以来,我一直在使用 Simple Injector 依赖注入容器,并取得了巨大成功。我喜欢它的简单配置。但现在我有一个设计,我不知道如何配置。我有一个基类,其中派生出许多类型,并且我想将依赖项注入到基类的属性中,但不必为每个派生类进行配置。我尝试使用属性来做到这一点,但简单注入器不支持属性。这是我的设计的精简版本。
public interface Handler<TMessage> where TMessage : Message
{
void Handle(TMessage message);
}
public abstract class BaseHandler
{
// This property I want to inject
public HandlerContext Context { get; set; }
}
// Derived type
public class NotifyCustomerHandler : BaseHandler,
Handler<NotifyCustomerMessage>
{
public NotifyCustomerHandler(SomeDependency dependency)
{
}
public void Handle(NotifyCustomerMessage message)
{
}
}
我的配置现在如下所示:
container.Register<HandlerContext, AspHandlerContext>();
container.Register<Handler<NotifyCustomerMessage>, NotifyCustomerHandler>();
// many other Handler<T> lines here
如何在 BaseHandler 中注入属性?
预先感谢您的任何帮助。
For several weeks I've been using the Simple Injector dependency injection container, with great success. I love the easy by which I can configure it. But now I have a design that I don't know how to configure. I have a base class where many types from derive, and I want to inject a dependency into a property of the base class, but without having to configure that for every derived class. I tried to do this with attributes, but Simple Injector does not support attributes. Here is a trimmed down version of my design.
public interface Handler<TMessage> where TMessage : Message
{
void Handle(TMessage message);
}
public abstract class BaseHandler
{
// This property I want to inject
public HandlerContext Context { get; set; }
}
// Derived type
public class NotifyCustomerHandler : BaseHandler,
Handler<NotifyCustomerMessage>
{
public NotifyCustomerHandler(SomeDependency dependency)
{
}
public void Handle(NotifyCustomerMessage message)
{
}
}
My configuration now looks like this:
container.Register<HandlerContext, AspHandlerContext>();
container.Register<Handler<NotifyCustomerMessage>, NotifyCustomerHandler>();
// many other Handler<T> lines here
How can I inject the property in the BaseHandler?
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单注入器关于属性注入的文档给出了非常清晰的解释关于这个。基本选项是:
PropertySelectionBehavior
。正如文档所解释的,不建议使用
RegisterInitializer
对依赖项进行属性注入;仅针对配置值。这让您可以重写 Simple Injector 的
PropertySelectionBehavior
,但拥有一个基类本身却像是一种 SOLID 违规。请查看以下文章。它描述了为什么拥有基类可能是一个坏主意,并且本文提出了一个解决方案。The Simple Injector documentation on property injection gives a very clear explanation about this. The basic options are:
RegisterInitializer
.PropertySelectionBehavior
.As the documentation explains, the use of
RegisterInitializer
is not advised for property injection on dependencies; only on configuration values.This leaves you with overriding Simple Injector's
PropertySelectionBehavior
, but having a base class by itself smells like a SOLID violation. Please take a look at the following article. It describes why having a base class might be a bad idea and the article presents a solution for this.