It looks like you want to provide different implementations of IObjectContainer before CustomerRepository and FooRepository . If so, the attributes can be a thin metal ruler . Instead, I will show you how to implement many implementations using Autofac.
(Calls such as .ContainerScoped() have been omitted for brevity.)
First, register a version of IObjectContainer for each connection string, naming the registration:
builder .Register(c => new ObjectContainer(ConnectionStrings.CustomerDB)) .As<IObjectContainer>() .Named("CustomerObjectContainer"); builder .Register(c => new ObjectContainer(ConnectionStrings.FooDB)) .As<IObjectContainer>() .Named("FooObjectContainer");
Then, enable specific instances in the repository registration:
builder.Register(c => new CustomerRepository( c.Resolve<IObjectContainer>("CustomerObjectContainer")); builder.Register(c => new FooRepository( c.Resolve<IObjectContainer>("FooObjectContainer"));
This leaves the repositories without configuration information:
class CustomerRepository { public CustomerRepository(IObjectContainer db) { ... } } class FooRepository { public FooRepository(IObjectContainer db) { ... } }
source share