Att*_*lah 18 .net c# ninject windows-forms-designer winforms
我有一个WinForms应用程序与此主窗体:
ICountRepository countRepository;
public MainForm(ICountRepository countRepository)
{
this.countRepository = countRepository;
}
public void IncrementCount()
{
countRepository.IncrementCount();
}
Run Code Online (Sandbox Code Playgroud)
但我正在努力注入ICountRepository
主体.我怎么做 ?
Rub*_*ink 22
那么第一步是切换:
var form = new MainForm();
Application.Run(form);
Run Code Online (Sandbox Code Playgroud)
至:
var kernel = new StandardKernel( new ModuleRegisteringICountRepository());
var form = kernel.Get<MainForm>();
Application.Run(form);
Run Code Online (Sandbox Code Playgroud)
也许澄清一两个关于你想要实现的事情的编辑可能会给你一个更详细的答案.
强烈建议快速了解这方面的模式是@Mark Seemann的.NET依赖注入书(用它的说法,上面的转换使Main
你的组合根 - (单个)Get
组成 你的应用程序的对象图.