1
votes

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.

1
Do you have a question?NightOwl888
Can't you simply give your UserStore a constructor which injects the IMongoContext? This would mean WithConstructorArgument() would not be necessary...timothyclifford
@NightOwl888 UpdatedMiguel A. Arilla
@timothyclifford UserStore is an Owin class, not mineMiguel A. Arilla
Why not just create a wrapper around UserStore and inject your MongoDb interface into that?casperOne

1 Answers

3
votes

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

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