Which ninject binding should I use?

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

What should my ninject binding look like? The only thing I could get, even for compilation, is as follows, but this does not match the correct context.

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

which is tied to something but not the correct context used in the numbered line

0
source share
2 answers

Try using ConstructorArgument

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

But...

In fact, you should also add dependency to yours UserStore<ApplicationUser>, binding ApplicationDbContext. Then the structure will build the whole chart for you:

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

+2
source

From cvbarros answer we came to the following:

kernel.Bind<ApplicationDbContext>().ToSelf().InRequestScope();
kernel.Bind<IUserStore<ApplicationUser>>()
    .To<UserStore<ApplicationUser>>()
    .WithConstructorArgument("context", context => kernel.Get<ApplicationDbContext>());

ApplicationDbContext / UserManager. UserManager ApplicationDbContext .

. /App_Start/NinjectWebCommon.cs

0

Source: https://habr.com/ru/post/1529278/


All Articles