Winforms 和 ASP.NET 中的重构

发布于 2024-12-15 17:51:40 字数 604 浏览 0 评论 0原文

当我为 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);
}

有时会有奇怪的变化。也许我会编写自己的从 EventArgs 派生的类并使用它。

这似乎是一项常见任务。也许我做错了,有更简单的方法来写很多东西吗?但如果没有,是否可以设置任何自定义重构工具来填充给定属性名称 Url 的代码?

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:

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.

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?

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

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

发布评论

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

评论(4

最笨的告白 2024-12-22 17:51:40

您的模式非常常见,并且几乎是实现此目的的最佳方法。

话虽如此,您绝对应该使用 Visual Studio Snippets 。它们允许轻松地为这样的代码创建模板。您可以定义自己的占位符,当您插入代码片段时,Visual Studio 会突出显示占位符并允许您通过 Tab 键浏览它们。

我还推荐 Snippet Designer 插件,它将使代码片段创建变得更容易、更有趣。它还有助于找出您不知道存在的片段功能。

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. 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.

I'd also recommend the Snippet Designer plugin, it will make snippet creation much easier and more fun. It also helps to find out snippet features that you didn't know existed.

月牙弯弯 2024-12-22 17:51:40

几个月前我就考虑过这个问题,并尝试过使用它:

    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));
        }
    }

但我从未在生产中运行过它,也没有花足够的时间来思考这是否是一个边际改进或不必要的复杂化。毫无疑问,它不如在每个属性中显式设置它那么灵活,因为这只会在 !equals 上引发。我想要么接受要么离开。

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. Take it or leave it I guess.

囚我心虐我身 2024-12-22 17:51:40

我可以设置任何自定义重构工具来填充给定属性名称 Url 的代码吗?

您可以创建自己的代码片段来执行此操作。

are there any custom refactoring tools that I can set up to fill in this code given the Property name Url?

You can create your own code snippet to do exactly that.

独守阴晴ぅ圆缺 2024-12-22 17:51:40

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

It depends on your architecture, but if you're trying to reduce redundancy you could create a base class that contains any shared properties and methods, and have your controls inherit from it.

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