Unity批量注册,按惯例 [英] Unity Batch Register by convention
本文介绍了Unity批量注册,按惯例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Unity IoC中执行以下Autofac代码的等效操作.
I'm trying to do the equivalent of the following Autofac code in Unity IoC.
builder.RegisterAssemblyTypes(typeof (DataRepository<>).Assembly)
.Where(t => t.Name.EndsWith("Repository"))
.AsImplementedInterfaces();
这基本上取代了单独注册以下内容:
This basically replaces individually registering the following:
DataSourceDataRepository : DataRepository<DataSource>, IDataSourceDataRepository
为清楚起见:这会将我所有存储库类型注册为它们的已实现接口,因此当我注入IDataSourceDataRepository时会得到一个DataSourceDataRepository,等等.
For clarity: This registers all of my Repository types as their implemented interfaces, so when i inject IDataSourceDataRepository I get a DataSourceDataRepository, etc.
团结一致,我无法超越一次手动完成一项任务. 测试代码仅显示注册失败.
In unity i've been unable to get beyond doing one at a time manually. Test code just shows a failure to register.
我的尝试:
container.RegisterType<RepositoryConnection>(new HierarchicalLifetimeManager());
container.RegisterTypes(
AllClasses.FromLoadedAssemblies().Where(t => typeof(IRepository).IsAssignableFrom(t)), getLifetimeManager: t => new TransientLifetimeManager(),
getInjectionMembers: t=> new InjectionConstructor[] {new InjectionConstructor(typeof(RepositoryConnection)) } );
推荐答案
这应该有效:
container.RegisterTypes(
AllClasses.FromAssemblies(typeof(DataRepository<>).Assembly)
.Where(t => t.Name.EndsWith("Repository")),
WithMappings.FromAllInterfaces);
这篇关于Unity批量注册,按惯例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文