Winforms 和 ASP.NET 中的重构
当我为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的模式非常常见,并且几乎是实现此目的的最佳方法。
话虽如此,您绝对应该使用 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.
几个月前我就考虑过这个问题,并尝试过使用它:
但我从未在生产中运行过它,也没有花足够的时间来思考这是否是一个边际改进或不必要的复杂化。毫无疑问,它不如在每个属性中显式设置它那么灵活,因为这只会在 !equals 上引发。我想要么接受要么离开。
I had thought about this some couple of months ago and toyed with using this:
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.
您可以创建自己的代码片段来执行此操作。
You can create your own code snippet to do exactly that.
这取决于您的体系结构,但如果您想减少冗余,您可以创建一个包含任何共享属性和方法的基类,并让您的控件继承它。
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.