Creating HTTP proxies for services I have a HTTP client factory that creates a separate HTTP proxy instance for each of my services - which are in the cloud. The factory creates each instance on the basis of an interface which the service implements. All of the proxies inherit from a base class called DalServiceBaseClient which you will see in my code. I don't quite like the way the factory resolves the type to be returned based on the interface.

Here's a simplified version of my code:

Here's how I call my factory to create one of the possible proxies:

var gatewayFactory = new GatewayFactory();
var masterClient = gatewayFactory.CreateMasterClient("someBaseAddress");


Here that same method in GatewayFactory:

public IMasterService CreateMasterClient(string baseAddress)
{
    return (MasterServiceClient)CreateHttpClient&lt;IMasterService&gt;($"{baseAddress}api/master/");
}


And here's the private create method in GatewayFactory where all of the magic happens:

private DalServiceBaseClient CreateHttpClient&lt;T&gt;(string baseAddress)
{
    var httpClient = new HttpClient
    {
        BaseAddress = new Uri(baseAddress)
    };

    if (typeof(T) == typeof(IMasterService))
    {
        return new MasterServiceClient(httpClient);
    }
    if (typeof(T) == typeof(ISlaveService))
    {
        return new SlaveServiceClient(httpClient);
    }
    if (typeof(T) == typeof(IRoleService))
    {
        return new RoleServiceClient(httpClient);
    }

    // A lot more ifs..

    throw new ArgumentException("No type exists for the provided interface");
}


Since there are a lot of different proxies my method ends up having a lot of ifs and I haven't been able to find a more elegant way of doing this.
Create a strategy to find the mapped implementation of the interface provided:

IDictionary&lt;Type, Type&gt; mappings; //Populated with all interface to implementation types

private DalServiceBaseClient CreateHttpClient&lt;T&gt;(string baseAddress) {
    var httpClient = new HttpClient {
        BaseAddress = new Uri(baseAddress)
    };
    var implementationType = mappings[typeof(T)];
    if (implementationType == null)
        throw new ArgumentException("No type exists for the provided interface");

    return (DalServiceBaseClient)Activator.CreateInstance(implementationType, httpClient);
}


Note how reflection is used to create an instance of the implementation.

The mappings would be pre-populated and can look like...

mappings = new Dictionary&lt;Type, Type&gt;();
mappings[typeof(IMasterService)] = typeof(MasterServiceClient);
mappings[typeof(ISlaveService)] = typeof(SlaveServiceClient);

//...other types;


Another option if not too keen on using reflection, is to use a factory delegate which would allow a more strongly typed mapping.

IDictionary&lt;Type, Func&lt;HttpClient, DalServiceBaseClient&gt;&gt; mappings; //Populated with all interface to implementation types

private DalServiceBaseClient CreateHttpClient&lt;T&gt;(string baseAddress) {
    var httpClient = new HttpClient {
        BaseAddress = new Uri(baseAddress)
    };
    var implementationFactory = mappings[typeof(T)];
    if (implementationFactory == null)
        throw new ArgumentException("No type exists for the provided interface");

    return implementationFactory(httpClient);
}


Which would be populated like

mappings = new Dictionary&lt;Type, Func&lt;HttpClient, DalServiceBaseClient&gt;&gt;();
mappings[typeof(IMasterService)] = (httpClient) =&gt; new MasterServiceClient(httpClient);
mappings[typeof(ISlaveService)] = (httpClient) =&gt; new SlaveServiceClient(httpClient);

//...other types;