Miguel A. Arilla
Miguel A. Arilla

Reputation: 5364

Resolve dependency dependant on previous bind using Ninject

I'm using Ninject as IoC and I'm following a tutorial to convert the ASP.NET Owin security default methods to follow the dependency injection pattern.

My database context, MongoDB in this case, is binded like this:

kernel.Bind<IMongoContext>().To<MongoContext>().InSingletonScope();

Currently, my security module (non dependency injection) is like this:

var users = MongoContext.Create().GetCollection<ApplicationUser>();
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(users));

I'd like to resolve UserStore like I've seen in this Unity resolution:

container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(
        new InjectionConstructor(typeof(ApplicationDbContext)));

Well, the above code is using Entity Framework, in my case, it would be similar with my MongoContext.

I think It would be something similar to

kernel.Bind<IUserStore>.To<UserStore>().WithConstructorArgument(/*some extra option to pass a resolve of my MongoContext*/)

So I need to know how to pass the resolved MongoContext to the UserStore bind.

EDIT: IUserStore and UserStore are System classes, not mine.

Upvotes: 1

Views: 938

Answers (1)

Miguel A. Arilla
Miguel A. Arilla

Reputation: 5364

After some attempts, I think I've found an elegant solution:

kernel.Bind<IUserStore<ApplicationUser>>().To<IUserStore<ApplicationUser>>()
    .WithConstructorArgument(kernel.Get<IMongoContext>().GetCollection<ApplicationUser>());

Upvotes: 3

Related Questions