Home » C# » Unity » Registering with Microsoft Unity an interface to a concrete class with multiple constructors

Registering with Microsoft Unity an interface to a concrete class with multiple constructors

If you are using inversion of control, you may come into the scenario that you want to register your interface to a concrete class that has multiple constructors. Registering with Microsoft Unity an interface to a concrete class with multiple constructors is something that require to specify to Unity which of the constructor to use.

container
    .RegisterType<IMainDbContext, MainDbContext>();

The code above can work if MainDbContext has only one constructor. Even if this one require several parameters, it does not matter. Unity will try to solve every parameters. However, in the case that MainDbContext has several constructor, Unity does not know which one to evaluate. Well, that is not totally true. In fact, Microsoft Unity try the one with the most parameters. This can be problematic if you have more than one parameter with the same amount of parameters.

To select which constructors you have to use the InjectionConstructor class. This class allows to specify parameters type. You can use the ResolvedParameter that is generic. You can specify for every parameters the type you want.

container
     .RegisterType<IMainDbContext, MainDbContext>(
        new InjectionConstructor(
            new ResolvedParameter<IRunningContext>()
        )
     );

As you can see, this indicate to Microsoft Unity to resolve the IRunningContext from its container to inject the object into the single parameter constructor of MainDbContext. In that case, we take the single constructor of MainDbContext that has its parameter of IRunningContext.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.