本文介绍了Ninject的UserManager和UserStore的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是使用ninject注入的UserManager和UserStore到控制器最优雅的方式?例如,上下文可以注射这样的:

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;
    }



能否ninject注入的UserManager和UserStore用一行代码到控制器? !如果没有,什么是最简单的方法?
我不想用这样的:

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()));

感谢您提前。

推荐答案

没问题,你只需要确保有一个为所有相关的绑定( ApplicationDbContext 的UserManager< T> UserStore< T> )。结合开放式泛型是这样做的:

Sure thing, you only need to make sure there's bindings for all dependencies (ApplicationDbContext, UserManager<T> and UserStore<T>). Binding open generics is done like this:

kernel.Bind(typeof(UserStore<>)).ToSelf().InRequestScope(); // scope as necessary.

如果它将有一个接口,你绑定它是这样的:

if it would have an interface, you'd bind it like this:

kernel.Bind(typeof(IUserStore<>)).To(typeof(UserStore<>));



所以,用这些绑定你要善于去:

So, with these bindings you should be good to go:

kernel.Bind<ApplicationDbContext>().ToSelf().InRequestScope();
kernel.Bind(typeof(UserManager<>)).ToSelf(); // add scoping as necessary
kernel.Bind(typeof(UserStore<>)).ToSelf(); // add scoping as necessary

这篇关于Ninject的UserManager和UserStore的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 23:00