使用 Ninject 解决依赖于先前绑定的依赖关系

Resolve dependency dependant on previous bind using Ninject

我正在使用 Ninject 作为 IoC,我正在按照教程将 ASP.NET Owin 安全默认方法转换为遵循依赖注入模式。

我的数据库上下文,MongoDB 在这种情况下,是这样绑定的:

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

目前我的安全模块(非依赖注入)是这样的:

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

我想像我在这个 Unity 解决方案中看到的那样解决 UserStore:

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

嗯,上面的代码使用了 Entity Framework,在我的例子中,它与我的 MongoContext 类似。

我认为它类似于

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

所以我需要知道如何将解析的 MongoContext 传递给 UserStore 绑定。

编辑:IUserStore 和 UserStore 是系统 类,不是我的。

经过一番尝试,我想我找到了一个优雅的解决方案:

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