Q:

Dependency Injection - How to resolve a dependency based on the value and not type?

I have one interface and two classes (implemented from that interface) in my application as below:
public interface ISMSServiceProvider
{
    NotificationSentResponse Send(SMSMessage sms);
}

public class NexmoProvider: ISMSServiceProvider
{
    NotificationSentResponse Send(SMSMessage sms);
}

public class TwilioProvider: ISMSServiceProvider
{
    NotificationSentResponse Send(SMSMessage sms);
}

At some places in my application I need to use NexmoProvider, and at some places TwilioProvider. That's been decided based on the value of a variable of following enum:
public enum NotificationProvider
{   
    Twilio = 1,
    Nexmo = 2
}

I am using service stack in my application, and having following code to resolve the dependency:
container.RegisterAutoWiredAs<TwilioProvider, ISMSServiceProvider>();

But as you can see there is nothing in ISMSServiceProvider that would allow me to register two different types at the same time. 
Could anyone please suggest what changes I may need to do in below structure to enable me using either of Nexmo or Twilio provider classes at runtime?

A:

You can only have a single registration per Interface or Type so this registration:
container.RegisterAutoWiredAs<TwilioProvider, ISMSServiceProvider>();

Will return an instance of TwilioProvider when you resolve from an Interface, e.g:
var smsProvider = container.Resolve<ISMSServiceProvider>();

If you want to be able to register both types you can register against the concrete Type, e.g:
container.RegisterAutoWired<TwilioProvider>();
container.RegisterAutoWired<NexmoProvider>();

Resolved with:
var twilio = container.Resolve<TwilioProvider>();
var nexmo = container.Resolve<NexmoProvider>();

If you want to resolve using an Enum you can register a factory, e.g:
public class SmsFactory
{
    public ISMSServiceProvider Resolve(NotificationProvider provider) =>
        provider == NotificationProvider.Twilio 
            ? HostConfig.Resolve<TwilioProvider>()
            : HostConfig.Resolve<NexmoProvider>();
}

container.Register(c => new SmsFactory());

Which you can inject in your Service with:
public SmsFactory SmsFactory { get; set; }

Then use as normal, i.e:
var smsProvider = SmsFactory.Resolve(NotificationProvider.Twilio);

