Combination Methods

In my application, I have a lot of code for copying and pasting, which is exactly the same and performs exactly the same function (button click events, etc.). This redundant code lives in the code of many of my pages. Therefore, I decided to reduce duplication of code and transfer these methods to the class file and only call them from the code pages.

Here is an example of a button click event in my code for calling 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

Is it possible to combine this try / catch block into another method and transfer 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 my code behind the files to be as clean and simple as possible, so I can make all the changes in one place.

, . , .

+3
4

try , , try/catch. , . , , , try .

+3

.

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)
  {
     ...
  }
}

( downvoters:???) , , .

, , :

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

... ( ..)

, , ( GridView MSG) . , . DRY (Do not Repeat Yourself) - .

0

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

}

That way, you can change your attempt / catch later if you think this affects performance, etc ... Hope this helps.

0
source

Source: https://habr.com/ru/post/1755831/


All Articles