[英]Castle Windsor Register by Convention - how to register generic Repository based on Entity?
這應該是一個普遍的問題,我已經搜索過,但仍未找到任何解決方案,如果是重復的,請給我指出適當的鏈接。
因此,我有一個通用的存儲庫來支持多個實體,有一段時間我像下面這樣注冊了它:
public class ExpensiveServiceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
Register<EntityA>(container);
Register<EntityB>(container);
//etc etc
//when there is a new one should register it manually
}
void Register<T>(IWindsorContainer container) where T : Entity
{
container.Register(Component.For<IRepository<T>>()
.ImplementedBy<Repository<T>>()
.LifeStyle.Transient);
}
}
注意:
位於Repositories
名稱空間上的Repositories
位於Entities
名稱空間上的Entities
,包括所有實體的Entity
基類
它工作正常,但是我認為應該是一種更好的方法,按照慣例使用注冊是更自動的方法,因此當我在項目中添加新實體時,windsor會自動識別它,並為其注冊存儲庫。 還有一個問題,如何忽略Entity
(基類),使其不在容器上注冊。
問候
你的意思是這樣的?
container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>))
.LifeStyle.Transient);
請試試這段代碼
var container = new WindsorContainer();
container.Register(Component.For(typeof(ICustomGenericRepository<>))
.ImplementedBy(typeof(CustomGenericRepository<>))
.LifeStyle.Transient);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.