【问题标题】:Generic Autofac Container Register for All Types适用于所有类型的通用 Autofac 容器寄存器
【发布时间】:2016-07-02 00:23:39
【问题描述】:

有没有办法注册泛型类型而不在 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();

【问题讨论】:

标签: .net wcf autofac ioc-container


【解决方案1】:

This is 我在找什么。

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

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

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 1970-01-01
    • 2019-01-05
    • 2020-09-14
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多