c# – 如何使用autofac注册开放泛型类型,封闭泛型类型和装饰?

我使用Autofac作为我的IoC容器.
我有:

> IRepository<&gt ;,我的存储库界面;
> DbContextRepository<>,使用EntityFramework的DbContext的存储库的通用实现;
>部件中的一些封闭类型存储库,例如PersonRepository:DbContextRepository< Person&gt ;;
>还有一个RepositoryDe​​corator<>,用一些标准的额外行为来装饰我的存储库;

我正在使用autofac将它们全部注册为:

builder.RegisterGeneric(typeof(DbContextRepository<>))
            .Named("repo", typeof(IRepository<>));

builder.RegisterGenericDecorator(
                typeof(RepositoryDecorator<>),
                typeof(IRepository<>),
                fromKey: "repo");            

var repositorios = Assembly.GetAssembly(typeof(PersonRepository));
builder.RegisterAssemblyTypes(repositorios).Where(t => t.Name.EndsWith("Repository"))
          .AsClosedTypesOf(typeof(IRepository<>))
          .Named("repo2", typeof(IRepository<>))
          .PropertiesAutowired();

builder.RegisterGenericDecorator(
               typeof(RepositoryDecorator<>),
               typeof(IRepository<>),
               fromKey: "repo2");

我想要做的是:

>注册DbContextRepository<>作为IRepository<>的通用实现;
>然后注册已关闭的类型存储库,以便它们可以在需要时重载先前的注册;
>然后装饰它们,当我要求容器解析IRepository时,它给我一个RepositoryDe​​corator,它具有正确的IRepository实现,是DbContextRepository或已经注册的封闭类型.

当我尝试解析没有封闭类型实现的IRepository< Product>时,它正确地返回装饰的DbContextRepository.

但是,当我尝试解析一个具有封闭类型实现的IRepository< Person>时,它还为我提供了一个装饰DbContextRepository,而不是一个装饰PersonRepository.

解决方法:

问题是Named(“repo2”,typeof(IRepository<>))没有按照你的想法做.您需要为正在扫描的类型明确指定类型.

static Type GetIRepositoryType(Type type)
{
    return type.GetInterfaces()
        .Where(i => i.IsGenericType
            && i.GetGenericTypeDefinition() == typeof(IRepository<>))
        .Single();
}

builder.RegisterAssemblyTypes(this.GetType().Assembly)
    .Where(t => t.IsClosedTypeOf(typeof(DbContextRepository<>)))
    .As(t => new Autofac.Core.KeyedService("repo2", GetIRepositoryType(t)))
    .PropertiesAutowired();
上一篇:IoC容器Autofac(3) - 理解Autofac原理,我实现的部分Autofac功能(附源码)


下一篇:ASP.NET Core2使用Autofac实现IOC依赖注入竟然能如此的优雅简便(转载)