How to use Ninject in a Windows Forms application?

前端 未结 1 1665
终归单人心
终归单人心 2020-12-05 18:40

I have an WinForms application with this Main Form :

    ICountRepository countRepository;
    public MainForm(ICountRepository countRepository)
    {
               


        
相关标签:
1条回答
  • 2020-12-05 19:22

    Well the first steps are to switch from:

    var form = new MainForm();
    Application.Run(form);
    

    to:

    var kernel = new StandardKernel( new ModuleRegisteringICountRepository());
    var form = kernel.Get<MainForm>();
    Application.Run(form);
    

    Perhaps a clarifying edit or two about what sort of thing you're looking to achieve might get you a more detailed answer.


    Highly recommended to get up to speed with the patterns around this is @Mark Seemann's Dependency Injection in .NET book (in it's parlance, the transformation above makes Main your Composition Root - the (single) Get Composes the object graph of your app.

    0 讨论(0)
提交回复
热议问题