数週間、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)
{
}
}
私の構成は次のようになります。
container.Register<HandlerContext, AspHandlerContext>();
container.Register<Handler<NotifyCustomerMessage>, NotifyCustomerHandler>();
// many other Handler<T> lines here
BaseHandler にプロパティを挿入するにはどうすればよいですか?
助けてくれてありがとう。