gpt4 book ai didi

c# - CaSTLe Windsor Register by Convention - 如何注册基于实体的通用存储库?

转载 作者:行者123 更新时间:2023-11-30 20:05:26 25 4
gpt4 key购买 nike

这应该是一个普遍的问题,我已经搜索过但还没有找到任何解决方案,如果是骗局,请指出适当的链接。

所以,我有一个通用存储库来支持多个实体,暂时我像下面这样注册它:

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 命名空间
位于Entities命名空间的实体,包括所有实体的Entity基类

它工作正常,但我认为它应该是一种更好的方法,使用约定注册的更自动的方法,所以当我在我的项目中添加一个新实体时,windsor 会自动识别它,并为它注册存储库。还有一个问题,如何忽略 Entity(基类)使其不在容器中注册。

问候

最佳答案

你的意思是这样的?

container.Register(Component.For(typeof(IRepository<>))
.ImplementedBy(typeof(Repository<>))
.LifeStyle.Transient);

关于c# - CaSTLe Windsor Register by Convention - 如何注册基于实体的通用存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11374102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com