Reputation: 41
Is it possible to group components' registrations by name using autofac?
For instance, the bellow code has two registrations for IDbContextProvider.
And I want WfcService to resolve only a named instance ("proxyless") of IDbContextProvider.
I'd like to use something like DependsOnNamed("proxyless") method to explicitly set a dependency on a set of named instances that have the same name.
I want the whole system to use the default implementation of IDbContextProvider while successors of IWfcService should use their own named instances.
var builder = new ContainerBuilder();
builder.RegisterType<DbContextProvider>().As<IDbContextProvider>().As<IDbContextProvider>();
builder.RegisterType<ProxyLessDbContextProvider>().Named<IDbContextProvider>("proxyless");
builder.RegisterType<Repository>().As<IRepository>();//depends on IDbContextProvider
builder.RegisterType<BusinessService>().As<IBusinessService>();//depends on IRepository
builder.RegisterType<WfcService>().As<IWfcService>().DependsOnNamed("proxyless");//depends on IBusinessService
IContainer container = builder.Build();
var wcf = container.Resolve<IWfcService>(); // should depend on "proxyless" IDbContextProvider
Upvotes: 1
Views: 752
Reputation: 41
The most appropriate way to achieve described behavior is to use the separate scope with overridden registration. If you want to override a registration for WCF services you should
var _wcfScope = _container.BeginLifetimeScope(b=>
{
b.RegisterType<NewDbContextProvider>().As<DbContextProvider>();
//put wcf services registration here because of the issue http://code.google.com/p/autofac/issues/detail?id=365
});
AutofacHostFactory.Container = _wcfScope;
Upvotes: 1