组合方法

发布于 2024-09-10 12:21:35 字数 727 浏览 2 评论 0原文

在我的应用程序中,我有很多完全相同的复制和粘贴代码,并且执行完全相同的功能(按钮单击事件等)。这些冗余代码存在于我的许多页面的代码隐藏中。因此,我决定减少代码重复,并将这些方法移至类文件中,并且仅从代码隐藏页面调用它们。

以下是我的代码中调用类文件中的方法后的按钮单击事件的示例:

#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 技术交流群。

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

发布评论

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

评论(4

厌味 2024-09-17 12:21:35

您可以将 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 the try block succeeds.

指尖上的星空 2024-09-17 12:21:35

您可以手动连接事件处理程序。

btnDeleteSelected1.Click += Events.BtnDeleteSelected_Click;
btnDeleteSelected2.Click += Events.BtnDeleteSelected_Click;
...
btnDeleteSelected3.Click += Events.BtnDeleteSelected_Click;

public static class Events
{
  public static BtnDeleteSelected_Click(object sender, EventArgs e)
  {
     ...
  }
}

编辑(对于投反对票的人:???)
该代码将为您提供一个单行代码,当自定义事件全部相同时,您不必担心编写自定义事件。

另外,如果实用程序方法具有相同的签名,您可以拥有一个通用方法:

public void ExecuteGvMethod(Action<GridView, string> gvMethod, GridView gv, string msg)
{
    try
    {
        gvMethod(gv, msg);
    }
    catch (Exception ex)
    {
        UserGvUtil.ExceptionErrorMessage(msg, ex);
    }
    finally
    {
        UserGvUtil.RefreshGridView(GridView1);
    }
}

在代码中:

public static class Events
{
  public static BtnDeleteSelected_Click(object sender, EventArgs e)
  {
    ExecuteGvMethod(UserGvUtil.DeleteSelectedUsersAndProfiles, (GridView)sender, "hi of whatever");
  }
}

You can wire the event handlers manually.

btnDeleteSelected1.Click += Events.BtnDeleteSelected_Click;
btnDeleteSelected2.Click += Events.BtnDeleteSelected_Click;
...
btnDeleteSelected3.Click += Events.BtnDeleteSelected_Click;

public static class Events
{
  public static BtnDeleteSelected_Click(object sender, EventArgs e)
  {
     ...
  }
}

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:

public void ExecuteGvMethod(Action<GridView, string> gvMethod, GridView gv, string msg)
{
    try
    {
        gvMethod(gv, msg);
    }
    catch (Exception ex)
    {
        UserGvUtil.ExceptionErrorMessage(msg, ex);
    }
    finally
    {
        UserGvUtil.RefreshGridView(GridView1);
    }
}

And in code:

public static class Events
{
  public static BtnDeleteSelected_Click(object sender, EventArgs e)
  {
    ExecuteGvMethod(UserGvUtil.DeleteSelectedUsersAndProfiles, (GridView)sender, "hi of whatever");
  }
}
心是晴朗的。 2024-09-17 12:21:35

我冒着投反对票的风险,但这里是我的 2 美分:

不要使用 try/catch,而是使用返回状态代码的方法。
例如(只是一个想法,您可以使用更复杂的类,而不是使用枚举):

public enum StatusCode
{
    Success = 1,
    Error =2
}

public class UserGvUtil
{
    public StatusCode getStatusAfterDelete(GridView GridView1, string Msg) 
    {
        try
        {
            DeleteSelectedUsersAndProfiles(GridView1, Msg);
            Return StatusCode.Success;
        }
        catch (Exception ex)
        {
            UserGvUtil.ExceptionErrorMessage(Msg, ex);
            Return StatusCode.Error;
        }
    }

//your other methods here
}

然后在后面的代码中:

protected void btnDeleteSelected_Click(object sender, EventArgs e)
{
    StatusCode sc = UserGvUtil.getStatusAfterDelete(GridView1, Msg);

    //then do something with the status code if you have to:
    if (sc==StatusCode.Error) throw new Exception("Error deleting users and profiles");
    else UserGvUtil.RefreshGridView(GridView1);

}

这样,如果您认为它会影响性能等,您可以稍后更改您的 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):

public enum StatusCode
{
    Success = 1,
    Error =2
}

public class UserGvUtil
{
    public StatusCode getStatusAfterDelete(GridView GridView1, string Msg) 
    {
        try
        {
            DeleteSelectedUsersAndProfiles(GridView1, Msg);
            Return StatusCode.Success;
        }
        catch (Exception ex)
        {
            UserGvUtil.ExceptionErrorMessage(Msg, ex);
            Return StatusCode.Error;
        }
    }

//your other methods here
}

Then in code behind:

protected void btnDeleteSelected_Click(object sender, EventArgs e)
{
    StatusCode sc = UserGvUtil.getStatusAfterDelete(GridView1, Msg);

    //then do something with the status code if you have to:
    if (sc==StatusCode.Error) throw new Exception("Error deleting users and profiles");
    else UserGvUtil.RefreshGridView(GridView1);

}

That way you can change your try/catch later if you think it affects performance etc...
Hope it helps.

软甜啾 2024-09-17 12:21:35

我有很多复制和粘贴代码
这是完全相同的......(按钮
点击事件等)

只需将多个单击处理程序后面重复的所有代码移至单独类中的方法中,并传递所需的任何内容(在本例中为 GridView 和 MSG 对象)作为参数。如果它可以节省大量重复,那么这样做是有意义的。 DRY(不要重复自己)是一条有效的原则。

I have a lot of copy and paste code
that is exactly the same ... (button
click events and the like)

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.

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