简体   繁体   English

温莎城堡温莎登记册组成部分

[英]Castle Windsor Register Component by Convention

I want to implement auto-registration for my repositories, which follow this convention: 我想为我的存储库实现自动注册,遵循以下约定:

  • Repositories implement IRepository 存储库实现IRepository
  • There is an abstract class which implements IRepository , for example: UserRepository 有一个实现IRepository的抽象类,例如: UserRepository
  • The actual implementation ends in the name of this abstract class, for instance: EntityFrameworkUserRepository 实际的实现以该抽象类的名称结尾,例如: EntityFrameworkUserRepository

I want to inject dependencies using castle into my controllers. 我想使用城堡将依赖项注入到我的控制器中。

public LinkService(LinkRepository linkRepository)
{
    if (linkRepository == null)
    {
        throw new ArgumentNullException("linkRepository");
    }
    this.linkRepository = linkRepository;
}

How can I map "components which implement IRepository " to "implementations which end with the same name as the component" 如何将“实现IRepository组件”映射到“以与组件相同名称结尾的实现”

container.Register(Component.For<LinkRepository>().ImplementedBy<EntityFrameworkLinkRepository>())

something like this, only auto-registering, so I don't have to add all repositories here manually each time I create a new one 像这样,只是自动注册,因此我不必在每次创建新存储库时在此处手动添加所有存储库

Something like this might do it: 这样的事情可能会做到:

var container = new WindsorContainer();
container.Register(
    AllTypes.FromAssemblyContaining<EntityFrameworkLinkRepository>()
        .BasedOn<IRepository>()
        .WithService.Select((type, types) => type.BaseType != null && type.Name.EndsWith(type.BaseType.Name)
                                                    ? new[] {type.BaseType}
                                                    : Enumerable.Empty<Type>()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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