简体   繁体   English

向现有的旧版 WinForm 添加演示者以添加单元测试?

[英]Adding A Presenter To An Existing Legacy WinForm To Add UnitTests?

At my work, we have a large legacy codebase written using winforms.在我的工作中,我们有一个使用 winforms 编写的大型遗留代码库。

The legacy code was not written with any form of separation of concern and highly sensitive and fragile business logic is intertwined with button click events, grid row events, etc.遗留代码没有以任何形式的关注点分离编写,高度敏感和脆弱的业务逻辑与按钮单击事件、网格行事件等交织在一起。

I would like to start bringing some of this logic under unit tests to give myself a better understanding of the logic and (hopefully) allow for a future migration away from winforms to be done with sligtly greater confidence and speed (is that a pipe dream?)我想开始在单元测试中引入其中的一些逻辑,以便让自己更好地理解逻辑,并(希望)允许未来以更大的信心和速度完成从 winforms 的迁移(这是 pipe 的梦想吗? )

For begining to add unit tests, I am aware of Michael Feather's great book 'Working With Legacy Code' and the concept of introducing seams in the code that allow for testing.为了开始添加单元测试,我知道 Michael Feather 的好书“使用遗留代码”以及在代码中引入允许测试的接缝的概念。 As I understand it, this is done by introducing services that can be injected into the forms to perform the business logic outside of the ui layer.据我了解,这是通过引入可以注入 forms 的服务来执行 ui 层之外的业务逻辑来完成的。 This seems like a great approach for the buisness logic, but it doesn't seem to me to work well for testing more mundane ui elements, such as setting the visible state of user controls based on some user action.对于业务逻辑来说,这似乎是一种很好的方法,但在我看来,它不适用于测试更普通的 ui 元素,例如基于某些用户操作设置用户控件的可见 state。

For the above example, I was wondering if it makes sense to introduce a Presenter class for a legacy winform, pass the user interaction up to the newly created presenter, and have the presenter perform the correct logic and set the view state via a view-interface.对于上面的示例,我想知道为旧版 winform 引入演示者 class 是否有意义,将用户交互传递给新创建的演示者,并让演示者执行正确的逻辑并通过视图设置视图 state -界面。 This would then allow me to write a test on the presenter's method with a mocked view interface.然后,这将允许我使用模拟视图界面对演示者的方法编写测试。

So something like the following所以像下面这样

public class LegacyPresenter
{
  ILegacyView view;
  
  public void SetViewState(string value)
  {
    switch (value)
    {
      case "case 1":
        view.Button1Visible = true;
      case "case 2"
        view.Button1Visible = false;
    }
  }
}

public class LegacyView
{
  public LegacyPresenter Presenter;
  ...
  private void SomeEvent_Clicked()
  {
    this.Presenter.SetViewState("case 2");
  }
}

Currently in our code, there is a defect in the above 'SetViewState' method that exists inside the WinForm.目前在我们的代码中,WinForm 中存在的上述“SetViewState”方法存在缺陷。 Does it make sense to introduce a Presenter class for this legacy form, move the 'SetViewState' from the view to the new presenter, and then write unit tests against the presenter?为这个遗留表单引入 Presenter class,将“SetViewState”从视图移动到新的 Presenter,然后针对 Presenter 编写单元测试是否有意义?

The form is currently 4k lines of code so I would only refactor this one method at this time to the presenter.该表单目前是 4k 行代码,所以我现在只将这一种方法重构给演示者。 Does this make any sense or would I just be wasting my time on legacy code?这是否有意义,或者我只是在遗留代码上浪费时间?

To answer your question, yes it makes sense to create a partial implementation.要回答您的问题,是的,创建部分实现是有意义的。 This is then a starting point to move on from.然后这是继续前进的起点。 When you need to make changes to the View, or if you have some time, you can move other pieces of logic to that Presenter.当您需要对 View 进行更改时,或者如果您有时间,您可以将其他逻辑片段移至该 Presenter。 Over time, the View will become more and more light-weight and you can unit test most parts of your Presenter.随着时间的推移,视图将变得越来越轻量级,您可以对 Presenter 的大部分部分进行单元测试。

As you progress, try to keep your Presenter clean (adhering to basic SOLID principles) so that you don't end up with a Presenter with 4k lines of code.随着你的进步,尽量保持你的 Presenter 干净(遵守基本的 SOLID 原则),这样你就不会得到一个只有 4k 行代码的 Presenter。 How you do that depends on your code of course.你如何做到这一点当然取决于你的代码。

But, going on a limb here, I would say that a unit-tested class of 4k lines of code is still better than a non-tested class of 4k lines of code.但是,在这里,我想说的是,经过单元测试的 4k 行代码的 class 仍然比未经测试的 4k 行代码的 class 好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM