jer*_*erg 5 c# autofac autofac-module
该autofac文档状态:
当 Autofac 注入类型为构造函数的参数时,
IEnumerable<ITask>它不会查找提供IEnumerable<ITask>. 相反,容器将找到 ITask 的所有实现并注入所有这些实现。
但实际上,它添加每个已注册类型的次数与已注册的次数相同。因此,如果您按以下方式注册一个课程两次:
builder.RegisterType<A>();
builder.RegisterType<A>();
Run Code Online (Sandbox Code Playgroud)
然后你在枚举中得到两个项目!!在单个模块中,这不是问题,因为您显然会注意仅注册一次您的类型。但是如果你有一个被多个模块注册的共享模块(典型的菱形模块依赖关系图),那么你在枚举中得到的项与共享模块已被其他人注册的一样多......
这是一个错误吗?有没有办法强制枚举为每个实现提供单个项目,如文档中所述,不再有?
您必须将Autofac视为一种Dictionary<IComponentRegistration>. 注册可以与类型或委托或具有配置行为的其他一切相关联。
通过注册两次类型,您将拥有两个不同的类型IComponentRegistration,在您的情况下,注册将是相似的。如果您查看以下差异,您会发现您将有两个不同的注册,它们使用相同Type但具有不同的配置。
builder.RegisterType<A>().WithConstrusctorArgument("param1", "x");
builder.RegisterType<A>().WithConstrusctorArgument("param1", "y");
Run Code Online (Sandbox Code Playgroud)
在这种情况下,解析IEnumerable<A>将为您提供两个不同的实例,A并且它确实有意义。
有没有办法强制枚举为每个实现提供单个项目
我不建议这样做。最好重构您的应用程序以不允许这种情况,如果您需要,您将删除Autofac中的许多很棒的东西。如果您有一个Type可以在多个模块中注册的模块,那么Type在主模块中注册它可能是有意义的。
顺便说一句,如果你真的想这样做,你将必须找到一种方法来区分你的容器的所有注册。IEnumerable<>然后您可以通过实现您自己的来覆盖默认实现IRegistrationSource,以下示例将为您提供每种类型仅一个注册。
class CustomEnumerableRegistrationSource : IRegistrationSource
{
public Boolean IsAdapterForIndividualComponents
{
get
{
return false;
}
}
public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
{
IServiceWithType typedService = service as IServiceWithType;
if (typedService == null)
{
return Enumerable.Empty<IComponentRegistration>();
}
if (!(typedService.ServiceType.IsGenericType && typedService.ServiceType.GetGenericTypeDefinition() == typeof(IEnumerable<>)))
{
return Enumerable.Empty<IComponentRegistration>();
}
Type elementType = typedService.ServiceType.GetGenericArguments()[0];
Service elementService = typedService.ChangeType(elementType);
Type collectionType = typeof(List<>).MakeGenericType(elementType);
IComponentRegistration registration = RegistrationBuilder.ForDelegate(collectionType, (c, p) =>
{
IEnumerable<IComponentRegistration> registrations = c.ComponentRegistry.RegistrationsFor(elementService);
IEnumerable<Object> elements = registrations.Select(cr => c.ResolveComponent(cr, p));
// get distinct elements by type
Array array = elements.GroupBy(o => o.GetType()).Select(o => o.First()).ToArray();
Array array2 = Array.CreateInstance(elementType, array.Length);
array.CopyTo(array2, 0);
Object collection = Activator.CreateInstance(collectionType, new Object[] { array2 });
return collection;
}).As(service)
.CreateRegistration();
return new IComponentRegistration[] { registration };
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样使用它:
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterType<A1>().As<IA>();
builder.RegisterType<A1>().As<IA>();
builder.RegisterType<A2>().As<IA>();
builder.RegisterSource(new CustomEnumerableRegistrationSource());
IContainer container = builder.Build();
IEnumerable<IA> services = container.Resolve<IEnumerable<IA>>();
Console.WriteLine(services.Count());
Run Code Online (Sandbox Code Playgroud)
此注册是原始CollectionRegistrationSource的简化版本。请查看 CollectionRegistrationSource.cs 的源代码以获得更完整的注册版本。
| 归档时间: |
|
| 查看次数: |
687 次 |
| 最近记录: |