Search code examples
asp.net-mvcasp.net-mvc-4dependency-injectionasp.net-identitysimple-injector

Registering Identity Framework UserStore with Simpleinjector for AccountController


I have a situation where I want to Use custom UserStore from Identity framework . So my controller looks something like this .

public AccountController()
: this(new UserManager<ApplicationUser>(new MyUserStore()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
      UserManager = userManager;
}

Now Simpleinjector doesnt allow multiple constructors . It can allows to register those classes with exactly one constructor . How am I supposed to resolve this when the situation is like that of the above . with Simpleinjector registration .


Solution

  • Simple Injector requires one public constructor. It cannot determine which one to use at runtime if you have more than one.

    The solution is a design issue - why do you need more than one constructor? This can be answered by asking yourself...

    Do i want SimpleInjector to create instances of IUserStore or should this be done manually?

    I would assume it to be the former, given that it makes the AccountController difficult to test if you are not allowing dependencies to be injected.

    The following configuration (usually stored in App_Start/SimpleInjectorInitializer.cs) will allow IUserStore instances to be of your custom type: MyUserStore, and will be injected into IUserManager if the constructor expects this...

    container.Register<IUserStore, MyUserStore>();    
    container.Register<IUserManager<ApplicationUser>, UserManager<ApplicationUser>>();