如何重构 WinForms 应用程序?

发布于 2024-07-17 02:16:39 字数 167 浏览 7 评论 0原文

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

有没有人给我一些建议可以分享? 有任何错误修复或重构 WinForms 应用程序的战争故事或最佳实践吗?

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

药祭#氼 2024-07-24 02:16:40

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

它们会给你一些信心,让你能够在重构中相当积极地进行。

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.

聆听风音 2024-07-24 02:16:40

顶部的几件事:

  • 将所有生成的代码放入部分类或区域(我使用区域
  • 将所有代码作为函数从操作移动到单独的区域或类
  • 为每个函数编写单元测试
  • 在这一点之后进行常见的重构模式,将相同/相似的功能合并到一个函数中
  • 确保不同的按钮不会使用不同的函数来执行相同的操作(特别是在 VB.NET 中,这是相当常见的设计,因此您可以在应用程序)
  • 尽可能正确使用发送者对象,而不是在控件事件处理程序中对控件名称进行硬编码。
  • 如果您发现任何此类代码可以转换为一个不错的类,请执行此操作。 从 WinForms 中删除所有非 GUI 代码。 有时需要重构原始类以使其可以与 GUI 一起使用,编写单元测试并重构它们。 (通常即使没有单元测试,你也应该没问题)

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)
  • Use sender object properly where possible instead of hardcoding the control name in control event handlers
  • 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)
撞了怀 2024-07-24 02:16:39

简短版本:

购买 Michael Feathers 的书《有效使用遗留代码》。

较长的版本:

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

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

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

  • 删除未使用的代码(这是一种干扰)。
  • 删除公共静态方法调用并将其替换为接口(依赖注入是这里的关键技术)。
  • 将重复的代码提取到类中。
  • 将文件处理、网络 IO 和用户界面代码隐藏在适配器后面(这些最初可能是非常薄的包装器,足以让您在测试时用存根替换它们)。
  • 寻找机会将行为提取到小班中。

有时重写代码的各个部分比重构它们更容易,但这需要通过测试或引用规范(如果存在)来理解行为。

除了任何实用建议之外,如果您是从事此工作的团队的一员,请确保你们一起工作。 这将使工作变得更轻松、更愉快,并确保您今天所做的更改明天不会被不了解您所做工作的人撤销。

我可以就这个话题写一整天,但羽毛先生更擅长,而且我很饿。 祝你好运!

The short version:

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

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.

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).
  • 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!

小女人ら 2024-07-24 02:16:39
  • 首先尝试理解代码及其背后的逻辑。
  • 一旦您理解了一些代码,请尝试为业务对象建模必要的类
  • 在您的新设计中使用某种层方法(3层是经典):接口层,业务层,数据访问层(或其他一些服务层) )。
  • 要有足够的耐心并记录一切!

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

  • 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).
  • 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文