Given a typical setup of a MVC controller constructor passing UserManager
(which takes UserStore
) to it's parent class, how would this be converted to be injected via IoC?
Starting with this:
public AccountController()
: this(new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
I would think something like this:
public AccountController(IUserStore store)
: this(new UserManager<ApplicationUser>(store)))
{
}
Though this does, of course, lose the IdentityDbContext
.
How should the IoC be setup and how should the constructor be defined to allow injection of the UserManager, UserStore and IdentityDbContext?
You would need to create some classes to allow for easier injection.
Let us start with the UserStore. Create the desired interface and have it inherit from
IUserStore<ApplicationUser>
Create an implementation as follows.
The UserManager can then be done as desired in the OP.
SO now all that is left is to make sure that which ever IoC container you decide to use registers the necessary classes.
If you want to go a step further and abstract the UserManager then just create an interface that exposes the functionality you want
and have you manager inherit from that.
This now means that the Controller can now depend on an abstraction and not on implementation concerns
And again you register the interface with the implementation in the IoC container.
UPDATE:
If you are feeling adventurous and want to abstract identity framework itself take a look at the answer given here