Simple injector: entering an object into a base class

For several weeks, I used the Simple Injector container with great success . I like it easily with which I can customize it. But now I have a design that I don’t know how to customize. I have a base class in which many types are inferred, and I want to attach a dependency to a property of the base class, but without the need to configure this for each derived class. I tried to do this using attributes, but Simple Injector does not support attributes. Here is a stripped 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)
    {
    }
}

Now my configuration is as follows:

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

How can I insert a property in a BaseHandler?

Thanks in advance for your help.

+4
1

. :

  • , RegisterInitializer.
  • PropertySelectionBehavior.

, RegisterInitializer ; .

Simple Injector PropertySelectionBehavior, SOLID. , . , , .

+9

All Articles