What is the most elegant way to inject UserManager and UserStore into a controller using ninject? For example, the context can be injected like this:
kernel.Bind<EmployeeContext>().ToSelf().InRequestScope();
public class EmployeeController : Controller
{
private EmployeeContext _context;
public EmployeeController(EmployeeContext context)
{
_context = context;
}
Can ninject inject UserManager and UserStore with a one line of code into a controller?! If not, what is the easiest way? I don't want to use this:
var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
Thank you in advance.
Sure thing, you only need to make sure there's bindings for all dependencies (
ApplicationDbContext
,UserManager<T>
andUserStore<T>
). Binding open generics is done like this:if it would have an interface, you'd bind it like this:
So, with these bindings you should be good to go:
Spent 8 hours trying to figure this one out, and I think I have it. The one difference that might need to be modified in other implementations is the SharedContext. My code has a SharedContext that inherits from DBContext.
I Also made changes to the AccountController.
Hope this saves someone else some time.