SO でこの質問に答えている間、ジェネリック型の多くの実装をLifetimeScopeLifestyle
SimpleInjector 内のインスタンスに登録するための最良の手法がわかりませんでした。
この形式の登録に推奨される方法は、次のようなものです。
container.RegisterManyForOpenGeneric(typeof(IRepository<>),
typeof(IRepository<>).Assembly);
LifetimeScopeLifestyle
しかし、これは のインスタンスを渡すことを許可しません。
以下は私が思いついたものですが、具体的にではなく、一般的なインターフェイスをチェックしているため、十分な回復力がないことはわかっていますIRepository<>
。誰でもこれを行う方法を教えてもらえますか?
public static void Configure(Container container)
{
var lifetimeScope = new LifetimeScopeLifestyle();
container.Register<IUnitOfWork, UnitOfWork>(lifetimeScope);
//this query needs improvement
var registrations =
from type in typeof(IRepository<>).Assembly.GetExportedTypes()
where typeof(IRepository).IsAssignableFrom(type)
&& type.IsClass
&& !type.IsAbstract
from service in type.GetInterfaces()
where service.IsGenericType
select new { Service = service, Implementation = type };
foreach (var registration in registrations)
{
container.Register(registration.Service,
registration.Implementation, lifetimeScope);
}
}