[英]Castle Windsor Register Component by Convention
I want to implement auto-registration for my repositories, which follow this convention: 我想为我的存储库实现自动注册,遵循以下约定:
IRepository
IRepository
IRepository
, for example: UserRepository
IRepository
的抽象类,例如: UserRepository
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.