将Unity Container集成到现有WinForms应用中:注入和子窗体问题 [英] Integrating Unity Container into existing WinForms app: injection and child forms questions

查看:71
本文介绍了将Unity Container集成到现有WinForms应用中:注入和子窗体问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的90k + Winforms应用程序,我正在尝试对其进行重构,向其添加单元测试,向其添加依赖项注入,并最终将其移交给MVC架构.

I have an existing 90k+ line Winforms application that I am trying to refactor, add unit testing to, add dependency injection to, and eventually get it over to an MVC architecture.

因此,我试图弄清楚如何将Unity容器与Winforms一起使用,并至少注入一些表示某些数据访问层类的依赖项(主要是远程REST服务),以使其实现.

And so I'm trying to figure out how to use Unity Container with Winforms and get it going with at least injecting some dependencies that represent some Data Access Layer classes (mostly remote REST services).

一些关于我在哪里的代码:

Some bits of code regarding where I'm at:

在我的Program.cs中:

Over in my Program.cs:

private static UnityContainer container;

public static void Main()
{
    container = new UnityContainer();
    container.RegisterType<IIncidentDataService, IncidentQuery>();
    container.RegisterType<IRuleService, RulesQuery>();

    Application.Run(container.Resolve<MainForm>());
}

在我的MainForm.cs中:

Over in my MainForm.cs:

public partial class MainForm: Form
{
    private IIncidentDataServcie incidentDataService;
    private IRuleService ruleService;

    // constructor
    public MainForm(IIncidentDataService passedIncidentDataService, IRuleService passedRuleService)
    {
       this.InitializeComponent();
       incidentDataService = passedIncidentDataService;
       ruleService = passedRuleService;

     }

  <lots more code>
}

我知道我目前还没有这样做.当然,我不想将越来越多的参数传递给MainForm上的构造函数,因为我还有其他一些传递的服务.

I realize I'm not doing this quite right yet. Certainly, I don't want to have to pass more and more parameters to the constructor on MainForm, as I have several other services yet to pass.

现在,我要传递其他几项服务的原因之一是MainForm上具有用于召唤子表单的控件……而我将需要将服务对象/依赖项传递给这些控件.

Now, one of the reason I have several other services to pass is that MainForm has controls on it that are used to summon child forms... and I'll need to pass service objects / dependencies over to those.

那么,我应该如何将一个具有多个依赖关系的容器传递给此Windows表单?

So, how SHOULD I be passing a container of multiple dependencies over to this Windows form?

我觉得我应该在所有这些服务类中创建一个对象...然后单独传递它.

I get the feeling I should be making one single object out of all of these service-classes... and passing that alone instead.

推荐答案

Application.Run(container.Resolve< MainForm>());

我知道我现在还没有这样做.

I realize I'm not doing this quite right yet.

相反,这是完美的.

您的关键字是 factory -如果 MainForm 需要创建 SomeControl 的实例(本身可能具有依赖项),请注入 ISomeControlFactory (具有 SomeControl Create(); 方法)进入 MainForm .

Your keyword is factory - if MainForm needs to create an instance of SomeControl (that may have dependencies itself), inject an ISomeControlFactory (that has a SomeControl Create(); method) into MainForm.

复制此答案:

对于独立于您要创建的实例的依赖项,请将其注入工厂并存储到需要时为止.

For dependencies that are independent of the instance you're creating, inject them into the factory and store them until needed.

对于与创建上下文无关但需要为每个创建的实例重新创建的依赖项,请将工厂注入工厂并存储它们.

For dependencies that are independent of the context of creation but need to be recreated for each created instance, inject factories into the factory and store them.

对于依赖于创建上下文的依赖项,请将其传递给工厂的Create方法.

For dependencies that are dependent on the context of creation, pass them into the Create method of the factory.

这篇关于将Unity Container集成到现有WinForms应用中:注入和子窗体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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