如何在下游生成新的Windows窗体时使用DI?

我有Unity DI容器最初使用我的Windows窗体应用程序。 在Program.cs我有以下内容:

 static void Main() { var container = BuildUnityContainer(); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(container.Resolve()); } private static IUnityContainer BuildUnityContainer() { var container = new UnityContainer(); container.RegisterType(); container.RegisterType(); return container; } 

在我的MainForm构造函数中,我有以下工作

 private readonly ITest test; public MainForm(ITest test) { this.test = test; InitializeComponent(); } 

容器已解析,代码工作正常。 问题/问题是,我如何从MainForm实例化一个新表单,比如具有以下构造函数的Form2

 private readonly ISomeOtherTest someOtherTest; public Form2(ISomeOtherTest someOtherTest) { this.someOtherTest = someOtherTest; InitializeComponent(); } 

如果我在我的MainForm尝试以下内容:

 Form2 form2 = new Form2(); form2.Show(); 

它会破坏,抱怨我没有将值提供给构造函数。 但是我已经解决了我的容器,我认为所有下游容器都将得到解决。 显然我在这里遗漏了一些东西,因为它不起作用。

这是否意味着我必须将所有依赖项加载到MainForm即使该表单不使用它,以便我可以将它们传递给我制作的任何新表单实例? 如果我有50个依赖项要解决并且顶级表单的构造函数将它们全部解析,那将会很奇怪。 请帮助澄清我的理解,因为我几乎只在Web API和MVC中使用Unity和DI容器,它已经为控制器内置了DI解析器,所以我必须在这里缺少一些部分和理解。

您应该像这样创建表单

 Form2 form = container.Resolve(); 

您没有使用容器,因此Form没有不带参数的构造函数。 如果使用容器解析它,它将检查构造函数,找到依赖项并自动将它们注入构造函数中。

那么..也许你的问题是你无法访问MainForm中的容器? 如果这是问题,有两种方法..

将IUnityContainer注入MainForm构造函数

但是……按照“组合根”模式生活的人会告诉你,你应该只使用应用程序根目录中的容器(在这种情况下,可能是Main())另一个选项是……

从组合根(Main)创建一个Form2工厂类,它被注入MainForm,MainForm使用工厂创建Form2

您应该阅读更多关于组合根思维的理论……

组成根


更新

我以前从来没有这样做,但我认为第二种方法看起来像这样……

 public class Form2Factory : IForm2Factory { private readonly ISomeOtherTest someOtherTest; public Form2Factory(ISomeOtherTest someOtherTest) { this.someOtherTest = someOtherTest; } public Form2 Create() { return new Form2(someOtherTest); } } public class MainForm { private readonly IForm2Factory form2Factory; public MainForm(IForm2Factory form2Factory) { this.form2Factory = form2Factory; } private void DoingSomething() { Form2 form = form2Factory.Create(); form.Show(); } }