我如何重构WinForms应用程序?

时间:2021-09-25 06:14:38

I'm going to bugfix a WinForms application (.NET 2.0) in the near future. Looking through the source code I find large code files (more than 2000 lines) most of them are generated dialogs with lots of code-behind.

我将在不久的将来修复WinForms应用程序(.NET 2.0)。通过源代码查看,我发现大型代码文件(超过2000行),其中大多数是生成的对话框,有很多代码隐藏。

Has anyone tips for me to share? Any war stories or best-practices for bug fixing or refactoring WinForms applications?

有没有提示让我分享?修复或重构WinForms应用程序的任何战争故事或最佳实践?

4 个解决方案

#1


  • First try to understand the code and the logic behind it.
  • 首先尝试理解代码及其背后的逻辑。

  • Once you understand some of the code try to model the necessary classes for the business objects
  • 一旦理解了一些代码,就尝试为业务对象建模必要的类

  • Use some kind of layer approach in your new design (3 layer is the classic): Interface layer, Business Layer, Data Access Layer (or some other service layer).
  • 在新设计中使用某种层方法(3层是经典的):接口层,业务层,数据访问层(或其他一些服务层)。

  • Lots of patience and document everything!
  • 很多耐心和记录一切!

I think this is the basic stuff. A lot of people here must have lots of suggestions. Good luck!

我认为这是基本的东西。这里的很多人必须有很多建议。祝好运!

#2


The short version:

简短版本:

Buy Michael Feathers' book, Working Effectively with Legacy Code.

购买Michael Feathers的书,有效地使用遗留代码。

The longer version:

版本较长:

The biggest challenge when maintaining (changing) such an application is doing it without breaking something. This means you need to understand the behaviour of the application, and the easiest way to do this is to get tests around it. As you've noted, this can be daunting at first, but it is not impossible. It requires discipline and patience.

维护(更改)此类应用程序时遇到的最大挑战是不破坏某些内容。这意味着您需要了解应用程序的行为,最简单的方法是围绕它进行测试。正如你所指出的,一开始这可能是令人生畏的,但这并非不可能。这需要纪律和耐心。

You don't have to start with unit tests. It's usually easier to get an automation framework, such as NUnitForms or White, to drive the application as a black box first. Build up a suite of tests around the area you need to change to give you enough confidence to change it without breaking something. Then go in and start refactoring towards unit testability.

您不必从单元测试开始。获得自动化框架(例如NUnitForms或White)通常更容易将应用程序作为黑盒子首先驱动。围绕您需要更改的区域构建一套测试,以便您有足够的信心在不破坏某些内容的情况下进行更改。然后进入并开始重构单元可测试性。

If it's anything like the application I'm working on, it mainly involves:

如果它与我正在处理的应用程序类似,它主要涉及:

  • deleting unused code (it's a distraction).
  • 删除未使用的代码(这是一个分心)。

  • removing public static method calls and replacing them with interfaces (Dependency Injection is a key technique here).
  • 删除公共静态方法调用并用接口替换它们(依赖注入是一种关键技术)。

  • extracting duplicated code into classes.
  • 将重复的代码提取到类中。

  • hiding file handling, network IO, and user interface code behind adapters (these can be very thin wrappers at first, just enough so that you can replace them with stubs when testing).
  • 隐藏文件处理,网络IO和适配器后面的用户界面代码(这些代码最初可能是非常薄的包装器,只需足以在测试时用存根替换它们)。

  • looking for opportunities to extract behaviour into small classes.
  • 寻找机会将行为提取到小班。

Sometimes it will be easier to rewrite sections of the code rather than refactor them, but that requires an understanding of the behaviour, either through testing or by referring to a spec (if one exists).

有时重写代码的部分而不是重构代码会更容易,但这需要通过测试或引用规范(如果存在的话)来理解行为。

Apart from any practical advice, if you're part of a team working on this make sure you are working together. It will make the work easier, more enjoyable and ensure that changes you make today aren't undone tomorrow by someone who didn't understand what you were working on.

除了任何实用的建议,如果你是一个团队的一员,确保你在一起工作。它将使工作变得更轻松,更愉快,并确保您明天所做的更改不会被那些不了解您正在处理的人明天所取消。

I could write all day on this topic, but Mr Feathers is much better at it, and I'm hungry. Good luck!

我可以整天写这个话题,但是Feathers先生在这方面要好得多,而且我很饿。祝好运!

#3


I'd start by writing some unit tests. You're going to need those to keep your sanity if the code is fairly dense.

我首先编写一些单元测试。如果代码相当密集,你将需要那些来保持你的理智。

They'll give you some confidence to be fairly aggressive in your refactoring.

他们会让你有信心在你的重构中相当积极。

#4


Couple of things from top head :

顶尖的几件事:

  • Put all generated code to a partial class or region (I use regions)
  • 将所有生成的代码放入部分类或区域(我使用区域)

  • Move all code from the actions to a separate region or class as functions
  • 将所有代码从操作移动到单独的区域或类作为函数

  • Write a unit test for each function
  • 为每个功能编写单元测试

  • Do common refactoring patterns after this point, combine same/similar functionality into one function
  • 在此之后执行常见的重构模式,将相同/相似的功能组合到一个函数中

  • Ensure that different buttons don't use different functions to do the same thing (especially in VB.NET this is quite common and design, so you can spot in your application)
  • 确保不同的按钮不使用不同的功能来做同样的事情(特别是在VB.NET中这是很常见的设计,所以你可以在应用程序中找到)

  • Use sender object properly where possible instead of hardcoding the control name in control event handlers
  • 尽可能正确使用sender对象,而不是在控件事件处理程序中对控件名进行硬编码

  • If you see any of this code can be coverted to a nice class, do it. Get rid of all non-GUI code from the WinForms. Sometimes it's required to refactor the original class to make it possible work with GUI, write your unit tests and refactor them. (generally even without a unit test you should be fine)
  • 如果您看到任何此代码可以转换为一个很好的类,那就去做吧。摆脱WinForms中的所有非GUI代码。有时需要重构原始类以使其可以与GUI一起工作,编写单元测试并重构它们。 (通常即使没有单元测试你也应该没问题)

#1


  • First try to understand the code and the logic behind it.
  • 首先尝试理解代码及其背后的逻辑。

  • Once you understand some of the code try to model the necessary classes for the business objects
  • 一旦理解了一些代码,就尝试为业务对象建模必要的类

  • Use some kind of layer approach in your new design (3 layer is the classic): Interface layer, Business Layer, Data Access Layer (or some other service layer).
  • 在新设计中使用某种层方法(3层是经典的):接口层,业务层,数据访问层(或其他一些服务层)。

  • Lots of patience and document everything!
  • 很多耐心和记录一切!

I think this is the basic stuff. A lot of people here must have lots of suggestions. Good luck!

我认为这是基本的东西。这里的很多人必须有很多建议。祝好运!

#2


The short version:

简短版本:

Buy Michael Feathers' book, Working Effectively with Legacy Code.

购买Michael Feathers的书,有效地使用遗留代码。

The longer version:

版本较长:

The biggest challenge when maintaining (changing) such an application is doing it without breaking something. This means you need to understand the behaviour of the application, and the easiest way to do this is to get tests around it. As you've noted, this can be daunting at first, but it is not impossible. It requires discipline and patience.

维护(更改)此类应用程序时遇到的最大挑战是不破坏某些内容。这意味着您需要了解应用程序的行为,最简单的方法是围绕它进行测试。正如你所指出的,一开始这可能是令人生畏的,但这并非不可能。这需要纪律和耐心。

You don't have to start with unit tests. It's usually easier to get an automation framework, such as NUnitForms or White, to drive the application as a black box first. Build up a suite of tests around the area you need to change to give you enough confidence to change it without breaking something. Then go in and start refactoring towards unit testability.

您不必从单元测试开始。获得自动化框架(例如NUnitForms或White)通常更容易将应用程序作为黑盒子首先驱动。围绕您需要更改的区域构建一套测试,以便您有足够的信心在不破坏某些内容的情况下进行更改。然后进入并开始重构单元可测试性。

If it's anything like the application I'm working on, it mainly involves:

如果它与我正在处理的应用程序类似,它主要涉及:

  • deleting unused code (it's a distraction).
  • 删除未使用的代码(这是一个分心)。

  • removing public static method calls and replacing them with interfaces (Dependency Injection is a key technique here).
  • 删除公共静态方法调用并用接口替换它们(依赖注入是一种关键技术)。

  • extracting duplicated code into classes.
  • 将重复的代码提取到类中。

  • hiding file handling, network IO, and user interface code behind adapters (these can be very thin wrappers at first, just enough so that you can replace them with stubs when testing).
  • 隐藏文件处理,网络IO和适配器后面的用户界面代码(这些代码最初可能是非常薄的包装器,只需足以在测试时用存根替换它们)。

  • looking for opportunities to extract behaviour into small classes.
  • 寻找机会将行为提取到小班。

Sometimes it will be easier to rewrite sections of the code rather than refactor them, but that requires an understanding of the behaviour, either through testing or by referring to a spec (if one exists).

有时重写代码的部分而不是重构代码会更容易,但这需要通过测试或引用规范(如果存在的话)来理解行为。

Apart from any practical advice, if you're part of a team working on this make sure you are working together. It will make the work easier, more enjoyable and ensure that changes you make today aren't undone tomorrow by someone who didn't understand what you were working on.

除了任何实用的建议,如果你是一个团队的一员,确保你在一起工作。它将使工作变得更轻松,更愉快,并确保您明天所做的更改不会被那些不了解您正在处理的人明天所取消。

I could write all day on this topic, but Mr Feathers is much better at it, and I'm hungry. Good luck!

我可以整天写这个话题,但是Feathers先生在这方面要好得多,而且我很饿。祝好运!

#3


I'd start by writing some unit tests. You're going to need those to keep your sanity if the code is fairly dense.

我首先编写一些单元测试。如果代码相当密集,你将需要那些来保持你的理智。

They'll give you some confidence to be fairly aggressive in your refactoring.

他们会让你有信心在你的重构中相当积极。

#4


Couple of things from top head :

顶尖的几件事:

  • Put all generated code to a partial class or region (I use regions)
  • 将所有生成的代码放入部分类或区域(我使用区域)

  • Move all code from the actions to a separate region or class as functions
  • 将所有代码从操作移动到单独的区域或类作为函数

  • Write a unit test for each function
  • 为每个功能编写单元测试

  • Do common refactoring patterns after this point, combine same/similar functionality into one function
  • 在此之后执行常见的重构模式,将相同/相似的功能组合到一个函数中

  • Ensure that different buttons don't use different functions to do the same thing (especially in VB.NET this is quite common and design, so you can spot in your application)
  • 确保不同的按钮不使用不同的功能来做同样的事情(特别是在VB.NET中这是很常见的设计,所以你可以在应用程序中找到)

  • Use sender object properly where possible instead of hardcoding the control name in control event handlers
  • 尽可能正确使用sender对象,而不是在控件事件处理程序中对控件名进行硬编码

  • If you see any of this code can be coverted to a nice class, do it. Get rid of all non-GUI code from the WinForms. Sometimes it's required to refactor the original class to make it possible work with GUI, write your unit tests and refactor them. (generally even without a unit test you should be fine)
  • 如果您看到任何此代码可以转换为一个很好的类,那就去做吧。摆脱WinForms中的所有非GUI代码。有时需要重构原始类以使其可以与GUI一起工作,编写单元测试并重构它们。 (通常即使没有单元测试你也应该没问题)