1

这是模块:

public class InjectModule : NinjectModule
{
    public override void Load()
    {
        Bind<DbContext>().ToSelf().InSingletonScope();
        Bind<ISomeRepository>().To<SomeRepository>()
            .InThreadScope();
        Bind<MainWindow>().ToSelf().InThreadScope();
        Bind<IKernel>() //how to bind???
    }
}

我的应用程序:

protected override void OnStartup(StartupEventArgs e)
{         
    IKernel kernel = new StandardKernel(new InjectModule());
    MainWindow window = kernel.Get<MainWindow>();
    window.Show();
    base.OnStartup(e);
}

我需要内核作为DependencyResolver主窗口中的属性。如何使它工作?

public partial class MainWindow
{
    [Inject]
    public IKernel DependencyResolver { get; set; }
}
4

1 回答 1

1

你不应该直接在模块之外使用内核。内核本身具有自动模块加载功能,如果您希望它这样做,它会扫描您的基本目录中的 ninject 模块。如果组件需要解析特定实例,您应该使用提供的扩展,例如工厂扩展,它允许根据接口注入 Func、Lazy 或动态工厂。如果任何扩展都不起作用,则注入 IResolutionRoot 接口,但绝不注入 IKernel!

于 2013-08-06T21:18:19.910 回答