如何获得在Windows窗体构造函数中的依赖注入 [英] how to get dependencies injected in constructors in Windows Forms

查看:477
本文介绍了如何获得在Windows窗体构造函数中的依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆