我有这个主窗体的WinForms应用程序:
I have an WinForms application with this Main Form :
ICountRepository countRepository;
public MainForm(ICountRepository countRepository)
{
this.countRepository = countRepository;
}
public void IncrementCount()
{
countRepository.IncrementCount();
}
但我在努力注入 ICountRepository
到MainForm的。我该怎么办呢?
but i am struggling to inject ICountRepository
into the mainform. How do I do that ?
那么第一个步骤是,从切换:
Well the first steps are to switch from:
var form = new MainForm();
Application.Run(form);
到
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.
强烈推荐来加快速度与周围的图案,这是@马克塞曼的依赖注入.NET 书(在它的说法,上述品牌的转变主
您的成分根 - (单个)获取
的组成您的应用程序的对象图。
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.