我应该使用什么ninject绑定? [英] What ninject binding should I use?

查看:81
本文介绍了我应该使用什么ninject绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public AccountController(IUserStore<ApplicationUser> userStore)
    {
        //uncommenting the following line, uses the correct context, but
        //unit testing fails to work, as it is overwritten, so I need to use IoC 
        //to  inject

        //userStore = new UserStore<ApplicationUser>(new ApplicationDbContext());

        UserManager = new UserManager<ApplicationUser>(userStore);

我的ninject绑定应该是什么样?我什至可以编译的唯一内容如下所示,但这并没有获得正确的上下文.

What should my ninject binding look like? The only thing that I could get to even compile looks like the following, but that is not getting the correct context.

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

绑定到某些东西,但注释行中使用的上下文不正确

which is binding to something, but not the correct context used in the commented out line

推荐答案

尝试使用ConstructorArgument

kernel.Bind<IUserStore<ApplicationUser>()
    .To<UserStore<ApplicationUser>>()
    .WithConstructorArgument(new ConstructorArgument("context", new ApplicationDbContext())

但是...

实际上,您还应该通过绑定ApplicationDbContext在您的UserStore<ApplicationUser>中注入依赖项.然后,框架将为您构建整个图形:

In reality, you should also inject the dependency in your UserStore<ApplicationUser>, by binding ApplicationDbContext. The framework will then construct the whole graph for you:

kernel.Bind<ApplicationDbContext>().ToSelf()

这篇关于我应该使用什么ninject绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆