I have two classes PaymentGatewayFoo
, PaymentGatewayBoo
that both implements a common interface of IPaymentGateway
:
interface IPaymentGateway { }
class PaymentGatewayFoo : IPaymentGateway { }
class PaymentGatewayBoo : IPaymentGateway { }
The client side request has an identifier who's response to determine which implementation to use:
public class OrderService
{
private readonly IPaymentGateway _service;
public void DoOrder(bool isFoo)
{
if (isFoo)
//this._service should be resolved with PaymentGatewayFoo
else
//this._service should be resolved with PaymentGatewayBoo
this._service.Pay();
}
}
How do I resolve the proper implementation based on the client's request on run-time?
This question is not duplicate, its similar but its about two separate controllers (Even the answers suggested that the code didn't even needed the conditional dependency injection), in my case the conditional dependency is needed on run-time based on a client property value.
There are several options here, but the two that to me are the most obvious are using a factory or the adapter pattern.
public class OrderService
{
private readonly IPaymentGatewayFactory _factory;
public void DoOrder(bool isFoo)
{
IPaymentGateway service = _factory.Create(isFoo);
this._service.Pay();
}
}
Where the factory can be:
public class PaymentGatewayFactory : IPaymentGatewayFactory
{
public PaymentGatewayFactory(PaymentGatewayFoo foo, PaymentGatewayBoo boo) {...}
public IPaymentGateway Create(bool isFoo) =>
isFoo ? this.foo : this.boo;
}
IPaymentGateway
.public class OrderService
{
private readonly IPayment _payment;
public void DoOrder(bool isFoo)
{
_payment.Pay(isFoo);
}
}
Where the adapter can be:
public class PaymentAdapter : IPayment
{
public PaymentAdapter(PaymentGatewayFoo foo, PaymentGatewayBoo boo) {...}
public void Pay(bool isFoo)
{
var service = isFoo ? this.foo : this.boo;
service.Pay();
}
}
As you noticed, in my factory and adapter, the implementations are injected directly. Not even by their abstractions, but by their concrete types. This might seem strange, but doing so is completely fine as long as the adapter and factory are part of the application's entry point (a.k.a. the Composition Root).
But other, more dynamic options can be used, such as:
Func<PaymentType, IPaymentGateway>
delegate to resolve the types.Dictionary<PaymentType, IPaymentGateway>
.Identifier
property to the interface, where it only exists for technical reasons. No consumer (other than the adapter or the factory) is interested in this identifier. It, therefore, doesn't belong to the interface. A better solution is to solve this problem in the Composition Root, possibly by marking the implementations with an attribute, for instance.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With