combining methods

扶醉桌前 提交于 2019-12-23 21:01:06

问题


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.


回答1:


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.




回答2:


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



回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/3302713/combining-methods

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!