适用于所有类型的通用Autofac容器注册

时间:2016-03-16 08:30:25

标签: .net wcf autofac ioc-container

有没有办法注册泛型类型而无需在Autofac上按类型注册?

现在:

builder.RegisterType<Repository<Authorization>>().As<IRepository<Authorization>>().InstancePerLifetimeScope();
builder.RegisterType<Repository<Branch>>().As<IRepository<Branch>>().InstancePerLifetimeScope();
builder.RegisterType<Repository<Customer>>().As<IRepository<Customer>>().InstancePerLifetimeScope();
builder.RegisterType<Repository<Foo>>().As<IRepository<Foo>>().InstancePerLifetimeScope();
....

用以下内容注册所有内容:

builder.RegisterType<Repository<T>>().As<IRepository<T>>().InstancePerLifetimeScope();

1 个答案:

答案 0 :(得分:0)

This is我在寻找什么。

builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();

builder.RegisterAssemblyTypes(typeof(BranchService).Assembly)
.Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces().AsSelf().InstancePerLifetimeScope();