5

autofac文档指出:

当 Autofac 注入类型的构造函数参数时,IEnumerable<ITask>它不会寻找提供IEnumerable<ITask>. 相反,容器将找到 ITask 的所有实现并将它们全部注入。

但实际上,它添加每个注册类型的次数与注册的次数一样多。因此,如果您按以下方式注册课程两次:

builder.RegisterType<A>(); 
builder.RegisterType<A>(); 

然后你得到两个项目在枚举中在单个模块中,这不是问题,因为您显然只需要注册一次您的类型。但是,如果您有一个由多个模块注册的共享模块(典型的菱形模块依赖关系图),那么您在枚举中获得的项目与其他人注册的共享模块一样多......

它是一个错误吗?如文档中所述,是否有任何方法可以强制枚举为每个实现提供单个项目,而不是更多?

4

1 回答 1

2

您必须将Autofac视为一种Dictionary<IComponentRegistration>. 注册可以绑定到类型或委托或具有配置行为的所有其他内容。

通过注册两次Type您将拥有两个 distinct IComponentRegistration,在您的情况下,注册将是相似的。如果您查看以下差异,您会发现您将有两个不同的注册,它们使用相同Type但具有不同的配置。

builder.RegisterType<A>().WithConstrusctorArgument("param1", "x"); 
builder.RegisterType<A>().WithConstrusctorArgument("param1", "y"); 

在这种情况下,解析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 };
    }
}

你可以像这样使用它:

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());

此注册是原始CollectionRegistrationSource的简化版本。请查看CollectionRegistrationSource.cs的源代码以获得更完整的注册版本。

于 2015-09-06T10:43:21.153 回答