Search code examples
c#objectpropertiesclass-design

Should I use response object or property for returning response


I have a requirement where I need to send SMS to our customers. For sending SMS we have multiple providers. To achieve this, I have designed an interface and created different classes for providers by implementing this interface

public interface IProvider
{
     Task<bool> SendAsync(ProviderRequest providerRequest);
}

public class Provider1: IProvider
{
     public async Task<bool> SendAsync(ProviderRequest providerRequest)
     {
          //call provider1
          return somethingBool;
     }
}
public class Provider2: IProvider
{
     public async Task<bool> SendAsync(ProviderRequest providerRequest)
     {
          //call provider2
          return somethingBool;
     }
}

We want to extend the requirement to get back the response from the provider. Now, to implement this I can think of 2 ways.

  1. Create a ProviderResponse object and map the response to it and return.
public interface IProvider
{
     //Updated interface method just to be concise, this will be a new version of the method 
     Task<ProviderResponse> SendAsync(ProviderRequest providerRequest);
}
// response class
public class ProviderResponse
{
    public bool IsSuccess { get; set; }
    public string ProviderResponse { get; set; }
}
  1. Add a getter property and fill it from provider concrete classes. The calling code will have an instance of the provider selected and can access this property. The return type of the method does not change.
public interface IProvider
{
     Task<bool> SendAsync(ProviderRequest providerRequest);
     string ProviderResponse { get; }
}

Which design I should go with? Where will be the best places to use these design solutions?


Solution

  • Seem the First approach look more valid.

    1. Thread Safe
      • We can create a singleton object and save multiple object creation
    2. Extend in Response Object:
      • If we are expecting more parameters in the future from Response, we can create a property in the object. In the second approach, we have to pollute the Interface to add more parameters.
    3. Extend in Interface:
      • Suppose IProvider require one more function to expose which have a different response and different parameter. Then in the Second approach IProvider becomes a design issue, ProviderResponse output is responsible for which function.

    So overall feel, the First approach looks more valid in terms of Thread Safe, Design, Performance, and extendable.