5

I'm very new to dependency injection and I've just set up Unity.Mvc5 and has some success with it. But now I face the problem of multiple contructors in my controller class. I already have constructors that handle my UserManager and following tutorials I understand I need another constructor to instantiate my interface. When I do this however, I get the following error:

The type OrganisationController has multiple constructors of length 1. Unable to disambiguate.

A snippet from my controller:

    private IPush _pushMessage;

    // Here is the problem!
    public OrganisationController(IPush pushMessage)
    {
        _pushMessage = pushMessage;
    }

    public OrganisationController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
    }

    public OrganisationController(UserManager<ApplicationUser> userManager)
    {
        UserManager = userManager;
        var provider = new DpapiDataProtectionProvider("MyApp");
        UserManager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(provider.Create("PasswordReset"));
    }

    public UserManager<ApplicationUser> UserManager { get; private set; }

And my UnityConfig.cs as follows:

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        container.RegisterType<IPush, PushMessage>();
        container.RegisterType<IController, OrganisationController>();
        container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>();
        container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());

        DependencyResolver.SetResolver(new UnityDependencyResolver(container));
    }
}

I am not sure how to tell Unity that I have another constructor being used to implement an interface.

1
  • Try marking the ctor you want to use for DI with [InjectionConstructor] attribute. This will allow unity identify which ctor to resolve. Commented Mar 17, 2016 at 15:20

2 Answers 2

5

When using DI, constructors are specifically for DI. There is no reason to create alternate constructors (especially on controllers, since the only caller is the ControllerFactory which delegates the call to the DI container).

In fact, using multiple constructors with dependency injection is anti-pattern and should be avoided.

Related: Rebuttal: Constructor over-injection anti-pattern

Sign up to request clarification or add additional context in comments.

1 Comment

Accepting this answer, not because it solves my exact problem but because it removes the root cause of my problem. I've removed the other constructors and instantiated UserManager elsewhere. Thank you for the info.
2

While I agree with other answer by @NightOwl888. In some cases you may want to have multiple constructors.

Have you tried InjectionConstructor attribute?

[InjectionConstructor]
public OrganisationController(IPush pushMessage)
{
    _pushMessage = pushMessage;
}

2 Comments

Does this need a neuget package?
yes, search for Unity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.