Home > Net >  How to unify similar methods in c #
How to unify similar methods in c #

Time:06-25

I have various methods that does the same things. Attached below the code :

    private async Task<CheckResponse> ManageCheckResponseError(string code)
    {
        await LogErrorResponseAsync(new { StatusCode = code });
        return CheckResponse.GetError(code.ToString());
    }

    private async Task<BalanceResponse> ManageBalanceResponseError(string code)
    {
        await LogErrorResponseAsync(new { StatusCode = code });
        return BalanceResponse.GetError(code.ToString());
    }

    private async Task<DebitResponse> ManageDebitResponseError(string code)
    {
        await LogErrorResponseAsync(new { StatusCode = code });
        return DebitResponse.GetError(code.ToString());
    }

    private async Task<CreditResponse> ManageCreditResponseError(string code)
    {
        await LogErrorResponseAsync(new { StatusCode = code });
        return CreditResponse.GetError(code.ToString());
    }

    private async Task<CancelResponse> ManageCancelResponseError(string code)
    {
        await LogErrorResponseAsync(new { StatusCode = code });
        return CancelResponse.GetError(code.ToString());
    }

An example of these classes is as follows. All are made in the same way apart from the properties that obviously are different :

   public class SampleResponse : ICommonResponseOperations<SampleResponse>
   {
      // Props

      // Error Management
      public ErrorModel ErrorModel { get; set; }

      public static LoginResponse GetError(string code)
      {
        return new LoginResponse
        {
            Entry = "",
            EntryEmbedded = "",
            ErrorModel = ErrorModel.GetError(code)
        };
      }
  }

Is there a way through generics to standardize everything by making the code more elegant? Thank you

CodePudding user response:

In the next version of C# (11) you will be able to define static members of interfaces.

You can try that out in the latest version of Visual Studio 2022 by adding the following to your project file (assuming .NET 6.0):

<EnablePreviewFeatures>True</EnablePreviewFeatures>
<LangVersion>preview</LangVersion>

After doing that you can modify your ICommonResponseOperations interface with a static member like so:

public interface ICommonResponseOperations<out TLoginResponse>
{
    static abstract TLoginResponse GetError(string code);
    // ... Other members
}

(You might need to remove the out depending on how the TLoginResponse type is used in the interface.)

Then you could define a common generic method like so:

private async Task<TLoginResponse> ManageResponseError<TLoginResponse>(string code) where TLoginResponse : ICommonResponseOperations<TLoginResponse>
{
    await LogErrorResponseAsync(new { StatusCode = code });
    return TLoginResponse.GetError(code);
}

Then instead of:

private async Task<BalanceResponse> ManageBalanceResponseError(string code)
{
    await LogErrorResponseAsync(new { StatusCode = code });
    return BalanceResponse.GetError(code.ToString());
}

You could just call ManageResponseError<BalanceResponse>(code) without having to specify a separate method to call.

Of course, C#11 isn't out yet and this feature is not guaranteed to make it, but it's looking pretty likely.

  • Related