如何获得在Windows窗体构造函数中的依赖注入 [英] how to get dependencies injected in constructors in Windows Forms
问题描述
在asp.net-MVC我有温莎控制器工厂,注入所有依赖到控制器,但你是怎么得到这个在Windows窗体?
有关例如,如果有这样的Form1中,我应该如何去得到Form1的一个实例,我应该使用的决心(这就是所谓的ServiceLocator和反模式被一些脂肪酶)?
公共类Form1的
{
私人IBarService _barService;
公共Form1中(IBarService barService)
{
_barService = barService;
}
}
是的在Program.cs中你应该温莎决心 Form1中
。为了能够查看窗体设计器中的缘故,你添加一个空的构造,并与过时
属性装饰。
in asp.net-mvc I have the Windsor Controller Factory that injects all the dependencies into the controllers, but how do you get this in Windows Forms ?
for example if have this Form1, how am I going to get an instance of Form1, should I use resolve (which is called ServiceLocator and anti-pattern by some ppl)?
public class Form1
{
private IBarService _barService;
public Form1(IBarService barService)
{
_barService = barService;
}
}
Yes, in program.cs you should make windsor resolve Form1
. For the sake of being able to view the form in the designer you add an empty constructor and decorate it with the Obsolete
attribute.
这篇关于如何获得在Windows窗体构造函数中的依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!