我有一个带有此主窗体的 WinForms 应用程序:
ICountRepository countRepository;
public MainForm(ICountRepository countRepository)
{
this.countRepository = countRepository;
}
public void IncrementCount()
{
countRepository.IncrementCount();
}
但我正在努力将 ICountRepository
注入(inject)主窗体。我该怎么做?
最佳答案
好吧,第一步是从:
var form = new MainForm();
Application.Run(form);
到:
var kernel = new StandardKernel( new ModuleRegisteringICountRepository());
var form = kernel.Get<MainForm>();
Application.Run(form);
也许对您希望实现的目标进行一两次澄清编辑可能会让您得到更详细的答案。
@Mark Seemann 的 Dependency Injection in .NET 强烈建议您尽快了解相关模式。 book(用它的说法,上面的转换使 Main
你的 Composition Root - 你的应用程序的(单个)Get
Composes the object graph。
关于c# - 如何在 Windows 窗体应用程序中使用 Ninject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4129092/