title: Improving this Generic Request Response Pattern
body: <p>Lately I seem to be working with lots of request/response stuff and I thought of creating something generic. </p>
  
  <p>I have the following but I am not happy about creating multiple ifs statements and I would like to avoid them.</p>
  
  <p>The idea is this:</p>
  
  <p>Regardless of the request /response process them.
  How can I remove these if statement within my generic request Handler?</p>
  
  <p>Code</p>
  
  <pre><code>class Program
  {
      private static void Main()
      {
          IRequestResponseFactory factory = new RequestResponseFactory();
  
          var customerRequest = new CustomerRequest { Name = "Joe", Surname = "Bloggs" };
          var customerResponse = factory.ProcessRequest&lt;CustomerRequest, CustomerResponse&gt;(customerRequest);
  
          var billRequest = new BillRequest() { Amount = 100m };
          var billResponse = factory.ProcessRequest&lt;BillRequest, BillResponse&gt;(billRequest);
  
          Console.WriteLine(billResponse.Success);
          Console.WriteLine(customerResponse.Success);
          Console.ReadKey();
  
      }
  }
  
  
  public class CustomerRequest : IRequestData&lt;CustomerResponse&gt;
  {
      public string Name { get; set; }
      public string Surname { get; set; }
  }
  
  public class CustomerResponse
  {
      public bool Success { get; set; }
  }
  public class BillRequest : IRequestData&lt;BillResponse&gt;
  {
      public decimal Amount { get; set; }
  }
  public class BillResponse
  {
      public bool Success { get; set; }
  }
  public interface IRequestData&lt;TResponse&gt;
  {
  }
  
  public interface IRequestHandler&lt;TRequest, TResponse&gt; where TRequest : IRequestData&lt;TResponse&gt;
  {
      TResponse ProcessRequest(TRequest request);
  }
  
  public interface IRequestResponseFactory
  {
      TResponse ProcessRequest&lt;TRequest, TResponse&gt;(TRequest request) where TRequest : IRequestData&lt;TResponse&gt;;
  }
  class RequestResponseFactory : IRequestResponseFactory
  {
      public TResponse ProcessRequest&lt;TRequest, TResponse&gt;(TRequest request) where TRequest : IRequestData&lt;TResponse&gt;
      {
          var handler = new GenericRequestHandler&lt;TRequest, TResponse&gt;();
          TResponse response = handler.ProcessRequest(request);
          return response;
      }
  }
  public class GenericRequestHandler&lt;TRequest, TResponse&gt; : IRequestHandler&lt;TRequest, TResponse&gt; where TRequest : IRequestData&lt;TResponse&gt;
  {
      public TResponse ProcessRequest(TRequest request)
      {
          var response = default(TResponse);
  
          //How do I avoid this if statements????
  
          if (request is IRequestData&lt;CustomerResponse&gt;)
          {
              var tempResponse = new CustomerResponse { Success = true };
              response = (TResponse)Convert.ChangeType(tempResponse, typeof(TResponse));
              return response;
          }
          if (request is IRequestData&lt;BillResponse&gt;)
          {
              var tempResponse = new BillResponse { Success = false };
              response = (TResponse)Convert.ChangeType(tempResponse, typeof(TResponse));
              return response;
          }
          return response;
      }
  }
  </code></pre>