组合方法
在我的应用程序中,我有很多完全相同的复制和粘贴代码,并且执行完全相同的功能(按钮单击事件等)。这些冗余代码存在于我的许多页面的代码隐藏中。因此,我决定减少代码重复,并将这些方法移至类文件中,并且仅从代码隐藏页面调用它们。
以下是我的代码中调用类文件中的方法后的按钮单击事件的示例:
#region DELETE selected users - button
protected void btnDeleteSelected_Click(object sender, EventArgs e)
{
try
{
UserGvUtil.DeleteSelectedUsersAndProfiles(GridView1, Msg);
}
catch (Exception ex)
{
UserGvUtil.ExceptionErrorMessage(Msg, ex);
}
finally
{
UserGvUtil.RefreshGridView(GridView1);
}
}
#endregion
我可以将此 try/catch 块合并到另一个方法中并将其移动到同一个类文件中吗?因此,我在点击事件中唯一拥有的就是一行代码。
这样做有意义吗?不知道为什么,但我希望文件背后的代码尽可能干净和简单,这样我就可以在一个地方进行所有编辑。
抱歉,如果我没有道理。我刚刚学习了课程和方法,很多想法涌入我的脑海。
In my app I have a lot of copy and paste code that is exactly the same and performs exactly the same function (button click events and the like). These redundant code live in the code-behind of many of my pages. So I decided to reduce the code duplication and to move these methods into a class file and only make a call to them from the code-behind pages.
Here is an example of a button click event in my code behind calling the methods from a class file:
#region DELETE selected users - button
protected void btnDeleteSelected_Click(object sender, EventArgs e)
{
try
{
UserGvUtil.DeleteSelectedUsersAndProfiles(GridView1, Msg);
}
catch (Exception ex)
{
UserGvUtil.ExceptionErrorMessage(Msg, ex);
}
finally
{
UserGvUtil.RefreshGridView(GridView1);
}
}
#endregion
Can I combine this try/catch block into yet another method and move it to the same class file? So, the only thing I have in the click event is a single line of code.
Does it make sense to do this? Not sure why, but I would like to have my code behind files as clean and simple as possible so I can make all the edits in a single place.
Sorry if I make no sense. I'm just learning about classes and methods and it floods my head with lots of ideas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以将
try
块内的内容移动到匿名委托中,然后将其传递给具有 try/catch 的共享方法。不过,您确实不需要将刷新放入最后。事实上,我认为您只想在 try 块成功时运行它。You can move the stuff inside the
try
block into an anonymous delegate that you pass to a shared method that has a try/catch. You really don't need to put the refresh into the finally, though. In fact, I would think you would only want to run it if thetry
block succeeds.您可以手动连接事件处理程序。
编辑(对于投反对票的人:???)
该代码将为您提供一个单行代码,当自定义事件全部相同时,您不必担心编写自定义事件。
另外,如果实用程序方法具有相同的签名,您可以拥有一个通用方法:
在代码中:
You can wire the event handlers manually.
Edit (for downvoters: ???)
The code will give you a one liner and you won't have to worry about writing custom events when they are all the same.
Also, if the utility methods have the same signature you could have a generic method:
And in code:
我冒着投反对票的风险,但这里是我的 2 美分:
不要使用 try/catch,而是使用返回状态代码的方法。
例如(只是一个想法,您可以使用更复杂的类,而不是使用枚举):
然后在后面的代码中:
这样,如果您认为它会影响性能等,您可以稍后更改您的 try/catch...
希望有帮助。
I'm risking a downvote, but here are my 2 cents:
Instead of using a try/catch, use a method that returns a status code.
For instance (just an idea, instead of using enum you can use a more complex class):
Then in code behind:
That way you can change your try/catch later if you think it affects performance etc...
Hope it helps.
只需将多个单击处理程序后面重复的所有代码移至单独类中的方法中,并传递所需的任何内容(在本例中为 GridView 和 MSG 对象)作为参数。如果它可以节省大量重复,那么这样做是有意义的。 DRY(不要重复自己)是一条有效的原则。
Just move all of that code that's duplicated behind multiple click handlers into a method in a separate class and pass whatever's needed (in this case the GridView and whatever that MSG object is) as parameters. If its saving significant duplication then it would make sense to do so. DRY (Don't Repeat Yourself) is a valid principle.