Resolve dependency with autofac based on constructor parameter attribute

Posted by Andrew Davey on Stack Overflow See other posts from Stack Overflow or by Andrew Davey
Published on 2010-03-19T13:28:15Z Indexed on 2010/03/19 13:31 UTC
Read the original article Hit count: 830

Filed under:
|

I'm using AutoFac. I want to inject a different implementation of a dependency based on an attribute I apply to the constructor parameter. For example:

class CustomerRepository
{
    public CustomerRepository([CustomerDB] IObjectContainer db) { ... }
}

class FooRepository
{
    public FooRepository([FooDB] IObjectContainer db) { ... }
}

builder.Register(c => /* return different instance based on attribute on the parameter */)
       .As<IObjectContainer>();

The attributes will be providing data, such as a connection string, which I can use to instance the correct object.

How can I do this?

© Stack Overflow or respective owner

Related posts about autofac

Related posts about ioc