代码之家  ›  专栏  ›  技术社区  ›  Adam Kane

WinForms MenuStrip Isolation to improve Code Maintainability

  •  1
  • Adam Kane  · 技术社区  · 14 年前

    In Windows Forms, C#, .NET 3.5, VS2008...

    如何将menuStrip(或任何复杂的控制组)的代码以及它的子菜单项与表单的其余部分隔离开来?

    例如,当我有一个菜单的菜单,每个菜单都有一个菜单项,所有的菜单项都有点击事件时,一个吨的代码被排入两个窗体。这并没有引起任何技术上的问题,但是让这么多东西倾倒在一个地方(和表单中的其他东西)是错误的。

    在整个项目上运行代码度量时,窗体被标记为具有任何项目文件的最坏可维护性索引。通常情况下,我不太偏执地注意代码度量工具的方向,但在这种情况下,我完全同意。

    根据代码度量,窗体违反了这些最佳实践:

    1. Too much class coupling
    2. Too many lines of code
    3. Overall low maintainability

    Possible solutions to isolate the MenuStrip from the rest of the form:

    1. Stuff it into a UserControl
    2. 其他想法?
    2 回复  |  直到 14 年前
        1
  •  0
  •   STO    14 年前

    I think you should take care about isolate business logic from presentation logic, for instance does not place too much code in click handlers or implement commands for menu items.

    Get sure your code metrics does not touch generated code or don't pay attention to bad metrics in autogenerated code.

        2
  •  0
  •   maxwellb    14 年前

    Can you disable or filter your CodeMetrics from grabbing *.Designer.cs, for example?

    如果不是,我将使用工厂类,以便您可以在一行中创建这些结构。其缺点是,它降低了设计器的功能。在工厂中,您可以基于模板字符串+“.FielMeMeNu”来命名每个组件,这样您就可以在工厂构造函数中设置基“名称”。

    To reduce code spew in your Form.cs file, consider more of a MVC approach, so that when the designer generates, say a private void button1_Click button1_Click 将呼叫 InitiateMoveFileMethod( string source, string destination ) 例如。

    推荐文章