简体   繁体   English

在Winforms和ASP.NET中重构

[英]Refactoring in Winforms and ASP.NET

When I'm making user controls for both Winforms and ASP.NET, I find myself writing the same section of code over and over again for each property on the form, which is this: 当我为Winforms和ASP.NET创建用户控件时,我发现自己一遍又一遍地为表单上的每个属性编写相同的代码段,这是:

private string _url;

public string Url
{
  get { return _url; }
  set
  {
    if (_url == value)
      return;

    _url = value;
    OnUrlChange(EventArgs.Empty);
  }
}

public event EventHandler UrlChanged;

protected virtual void OnUrlChange(EventArgs e)
{
  //Maybe add some extra logic here

  if(UrlChanged != null)
    UrlChanged(this, e);
}

Sometimes there's the odd change. 有时会有奇怪的变化。 Maybe I'll writing my own class that derives from EventArgs and use that. 也许我会编写自己的类,它来自EventArgs并使用它。

This seems like it must be a common task. 这似乎是一项常见的任务。 Maybe I'm doing it wrong and there's a much easier way to write the lot? 也许我做错了,有一个更简单的方法来写这个地段? But if not, are there any custom refactoring tools that I can set up to fill in this code given the Property name Url ? 但如果没有,是否有任何自定义重构工具,我可以设置为在给定属性名称Url填写此代码?

Your pattern is pretty common, and is pretty much the best way to do this. 你的模式很常见,几乎是最好的方法。

That being said, you should definitely use Visual Studio Snippets . 话虽这么说,你绝对应该使用Visual Studio Snippets They allow for easily creating templates for code like this. 它们允许轻松地为这样的代码创建模板。 You can define your own placeholders, and when you insert the snippet, Visual Studio highlights the placeholders and allows you to tab through them. 您可以定义自己的占位符,当您插入代码段时,Visual Studio会突出显示占位符并允许您对其进行制表。

I'd also recommend the Snippet Designer plugin, it will make snippet creation much easier and more fun. 我还推荐了Snippet Designer插件,它将使代码片段创建更容易,更有趣。 It also helps to find out snippet features that you didn't know existed. 它还有助于找出您不知道存在的代码段功能。

I had thought about this some couple of months ago and toyed with using this: 几个月前我曾经想过这个并玩弄了这个:

    public event PropertyChangedEventHandler PropertyChanged;

    int _MyProperty;
    public int MyProperty
    {
        get { return _MyProperty; }
        set { ChangeIfUnequal(ref _MyProperty, value, "MyProperty"); }
    }

    int _AnotherProperty;
    public int AnotherProperty
    {
        get { return _AnotherProperty; }
        set { ChangeIfUnequal(ref _AnotherProperty, value); }
    }


    void ChangeIfUnequal<T>(ref T Val, T NewValue, string Identifier="")
    {
        if (!Val.Equals(NewValue))
        {
            Val = NewValue;
            var temp = PropertyChanged;
            if(temp != null)
                temp(this, new PropertyChangedEventArgs(Identifier));
        }
    }

But I never ran it in production and haven't spent enough time to think if this is a marginal improvement or unnecessary complication. 但是我从来没有在生产中运行它并且没有花足够的时间来思考这是否是一个微小的改进或不必要的并发症。 Undoubtedly it's not as flexible as explicitly setting it in each property as this would only raise on !equals. 毫无疑问,它不像在每个属性中明确设置它那样灵活,因为这只会提高!equals。 Take it or leave it I guess. 拿它或者离开我猜。

are there any custom refactoring tools that I can set up to fill in this code given the Property name Url ? 是否有任何自定义重构工具,我可以设置为在给定属性名称Url填写此代码?

You can create your own code snippet to do exactly that. 您可以创建自己的代码段来完成该操作。

这取决于您的体系结构,但如果您尝试减少冗余,则可以创建一个包含任何共享属性和方法的基类,并让您的控件继承它。

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

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