简单注入器:在基类中注入属性

发布于 2024-11-13 01:45:17 字数 1023 浏览 1 评论 0原文

几个星期以来,我一直在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执妄 2024-11-20 01:45:18

简单注入器关于属性注入的文档给出了非常清晰的解释关于这个。基本选项是:

  • 使用RegisterInitializer 注册初始化委托。
  • 覆盖简单注入器的 PropertySelectionBehavior

正如文档所解释的,不建议使用 RegisterInitializer 对依赖项进行属性注入;仅针对配置值。

这让您可以重写 Simple Injector 的 PropertySelectionBehavior,但拥有一个基类本身却像是一种 SOLID 违规。请查看以下文章。它描述了为什么拥有基类可能是一个坏主意,并且本文提出了一个解决方案。

The Simple Injector documentation on property injection gives a very clear explanation about this. The basic options are:

  • Register an initialization delegate using RegisterInitializer.
  • Override Simple Injector's 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文