小编Smi*_*tha的帖子

Simple Injector:在基类中注入属性

几周以来,我一直在使用Simple Injector依赖注入容器,取得了巨大的成功.我喜欢简单的配置它.但现在我有一个我不知道如何配置的设计.我有一个基类,其中派生了许多类型,我想将依赖注入基类的属性,但不必为每个派生类配置.我尝试使用属性执行此操作,但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)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我的配置现在看起来像这样:

container.Register<HandlerContext, AspHandlerContext>();
container.Register<Handler<NotifyCustomerMessage>, NotifyCustomerHandler>();
// many other Handler<T> lines here
Run Code Online (Sandbox Code Playgroud)

如何在BaseHandler中注入属性?

在此先感谢您的帮助.

.net c# dependency-injection ioc-container simple-injector

4
推荐指数
1
解决办法
6741
查看次数