[英]Auto Wiring Repository With Generic Type Parameters in Autofac
我想知道是否有一種基於約定的方法來在Autofac中注冊以下內容:
builder.RegisterType<BatchDocumentRepository>()
.As<IRepository<BatchDocument, IEnumerable<BatchDocument>, int>>();
builder.RegisterType<BatchRepository>()
.As<IRepository<Batch, IEnumerable<Batch>, int>>();
builder.RegisterType<DeploymentReleaseRepository>()
.As<IRepository<DeploymentRelease, IEnumerable<DeploymentRelease>, int>>();
builder.RegisterType<DeploymentRepository>()
.As<IRepository<Deployment, IEnumerable<Deployment>, int>>();
上面的方法工作正常,但我只是想知道是否有一種更干凈且重復性更小的方法。 我看了一下這篇文章: 使用Autofac解決通用接口 ,但是情況並不完全相同。
謝謝!
您可以將.AsImplementedInterfaces()
.As<...>()
替換為.AsImplementedInterfaces()
。 除此之外,您可以使用:
builder.RegisterAssemblyTypes()
.Where(t => t.GetInterfaces()
.Any(i => i.IsGenericType &&
i.GetGenericDefinition() == typeof(IRepository<>)))
.AsImplementedInterfaces();
或類似的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.