簡體   English   中英

Ninject:如何解決內核本身/問題?

[英]Ninject: How to resolve Kernel itself/this?

這是模塊:

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

絕對不要在模塊外部直接使用內核。 內核本身具有自動模塊加載功能,如果需要,它會在您的基本目錄中掃描ninject模塊。 如果組件需要解析特定實例,則應使用提供的擴展名,例如工廠擴展名,該擴展名允許根據接口注入Func,Lazy或動態工廠。 如果任何擴展都無濟於事,請注入IResolutionRoot接口,但不要注入IKernel!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM