How to specify what type I need to register to specific type by Autofac

I want to define different DB for different repositories (I use more than one datasources for different datatypes). Like builder.RegisterType().As().Where(t=>t.Name‌​.Contains("someName")); and builder.RegisterType().As().Where(t=>t.Nam‌​e.Not.Contains("someName"));

It might be better to remove the ambiguity from your design. Your IDbDataContext is ambiguous, since FirstDbDataContext and SecondDbDataContext are not really compatible implementations of the same contract, since they aren't interchangeable; a repository requires a certain database and it will fail when an IDbDataContext for the wrong database is passed in.

Try giving each context its own abstraction, such as IFirstDbDataContext and ISecondDbDataContext. By letting a repository depend explicitly on either of those interfaces it becomes clear to anyone who looks at the constructor what this repository depends on.

But it not only becomes much easier for anyone maintaining the code, it becomes significantly easier to wire everything together using a DI container, simply because you removed the ambiguity.

This is the registration you'll end up with:



  1. Luka

    • 2015/12/10

    You can't “new up” an abstract class or an interface. You have to have an implementation, right? Specifying a Constructor¶. You can manually choose a particular 

  2. Wes

    • 2019/5/4

    var builder = new ContainerBuilder(); builder.RegisterType<MyComponent> (); builder.RegisterType<ConsoleLogger> ().As<ILogger> (); var container = builder.Build(); using(var scope = container.BeginLifetimeScope()) { var component = scope.Resolve<MyComponent> (); } When you resolve your component, Autofac will see that you have an ILogger registered, but you don’t have an IConfigReader registered.

  3. Louis

    • 2019/10/17

    public class CustomerNotifier { public CustomerNotifier(ISender notificationStrategy) { } } When you register things in Autofac, you might have 

  4. Matteo

    • 2019/10/28

    And the Autofac registration: var builder = new ContainerBuilder(); builder .RegisterGeneric(typeof(Repository<>)).AsSelf(); builder .RegisterGeneric(typeof(UnitOfWork<>)) .As(typeof(IUnitOfWork<>)) .InstancePerDependency(); var container = builder.Build(); // sample usage var u = container.Resolve<IUnitOfWork<MyEntity>>();

  5. Izaiah

    • 2015/2/27

    You cannot have partially opened classes (e.g. with UnitOfWork<Repository<>,> you have specified T but not O ) inside a typeof , try it with:

  6. Roger

    • 2019/12/4

    You’ll also need to register the component with the filter so the container knows to look for it. var builder = new ContainerBuilder (); // Register the service to consume with metadata. // Since we're using attributed metadata, we also // need to register the AttributedMetadataModule // so the metadata attributes get read. builder .

  7. Kabir

    • 2020/5/17

    NET platform, has an out of the box generic service registration feature which we builder) { Type baseEntityType = typeof(BaseEntity); Assembly assembly 

  8. Smith

    • 2015/1/4

    Line 6 actually creates our Autofac container and line 8 registers a new AutofacDependencyResolver (using our container) as the dependency resolver for ASP.NET MVC. That means that not only have we set up a dependency resolver for services that we need in our code, but also for the services that ASP.NET MVC will need as well.

  9. Alan

    • 2019/1/10

    Setting the Scene. Let's start with a simple service which has a dependency. public class MyService { public MyService(IMyServiceDependency 

  10. Christopher

    • 2018/11/15

    Option 1: Add predicate overloads to registration methods. This is probably the simplest thing that would work. Using RegisterType<T> as an example, we'd just provide an additional optional parameter to the method to allow this to work: builder . RegisterType < First > ( ctx => !ctx.

  11. Francis

    • 2018/5/17

    As(typeof(IGenRepo<>));. The compile error I get on this line is: Using the generic type 'Domain.Concrete.EFGenRepo` requires 2 type arguments 

  12. Vance

    • 2020/6/25

    A simple example might be registration of a specific logger type to be used by a class. If many loggers are registered with their own key, the consumer can simply specify the key filter as an attribute to the constructor parameter.

  13. Noah

    • 2017/3/15

    var container = Register();. 11. 12. //Get all subcommands and filter the one with required type. 13. var type = typeof(OneSubCommand);.

  14. Dariel

    • 2015/3/18

    Just set the parameter to ctx.ResolveKeyed<IEnumerable<ISender>>("order") with the appropriate key in the processor registration; and register each sender with the appropriate key. If you have the ability to change the registrations and you’re not locked into doing assembly scanning for all your registrations, this is the recommended option.

  15. Marini

    • 2018/2/6

    namespace DIContainers { public static class ContainerConfig { public static IContainer Configure() { var builder = new ContainerBuilder(); //Registering types 

  16. Malakhi

    • 2021/1/1

    Have a question about this project? Registration Concepts¶. Autofac supports automatically resolving particular types implicitly to support 

  17. Sullivan

    • 2021/4/19

    Forms needs an instance of a particular type, the dependency that uses the Autofac dependency injection container to resolve any types 

Comments are closed.

Recent Posts