Using Autofac Keyed Services
Using strings as Key multiple dependencies can be registered.
For example creating a Enum where each enum value corresponds to an implementation of the service:
Public Enum PaymentState { Online, Cash}
The implementations are as below.
public class CardPaymentState : IPaymentState {}
public class CashPaymentState : IPaymentState {}
The enum values can then be registered as keys for the implementations below.
var builder = new ContainerBuilder()
builder.RegisterType<CardPaymentState>().Keyed<IPaymentState>PaymentState.Online);
builder.RegisterType<CashPaymentState>().Keyed<IPaymentState>(PaymentState.Cash);
Now they can be resolved like below.
var cardPayment = container.ResolveKeyed<IPaymentState>(PaymentState.Online)
var cashPayment = container.ResolveKeyed<IPaymentState>(PaymentState.Cash);
*Using .Net Core Built-In DI *
Firstly a shared delegate has to be declared.
Public delegate IPaymentService PaymentServiceResolver(string key);
Then in startup.cs, the multiple concrete registrations and a manual mapping of those types have to be set.
services.AddTransient<CreditCardService>()
services.AddTransient<CashService>();
services.AddTransient<PaymentServiceResolver>(serviceProvider => key =
{
switch (key)
{
case "online":
return serviceProvider.GetService<CreditCardService>();
case "cash":
return serviceProvider.GetService<CashService>();
default:
throw new KeyNotFoundException();
}
});
Now it can be used from any class registered with DI.
public class Payment
{
private readonly IPaymentService paymentService;
public Consumer(PaymentServiceResolver paymentServiceResover)
{
paymentService= paymentServiceResover("Online");
}
public void PayByCard()
{
paymentService.ProcessPayment();
}
}
Top comments (0)