繁体   English   中英

城堡温莎公约注册-如何基于实体注册通用存储库?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM