Created
January 10, 2021 00:45
-
-
Save ThiagoBarradas/4c7b5c036843d9e82fa615863c12ad16 to your computer and use it in GitHub Desktop.
SOLID [O] - Deciding implementations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sample enum for payment.Type | |
public enum PaymentType | |
{ | |
CreditCard, | |
DebitCard, | |
BankInvoice, | |
Pix | |
} | |
// simple factory | |
public static class PaymentServiceFactory | |
{ | |
public IPaymentService GetProcessor(PaymentType type) | |
{ | |
switch (type) | |
{ | |
case PaymentType.CreditCard: | |
return new CreditCardPaymentService(); | |
case PaymentType.DebitCard: | |
return new DebitCardPaymentService(); | |
case PaymentType.BankInvoice: | |
return new BankInvoicePaymentService(); | |
case PaymentType.Pix: | |
return new PixPaymentService(); | |
default: | |
throw new NotImplementedException("Payment type not implemented"); | |
} | |
} | |
} | |
// example calling by action into controller | |
[HttpPost("payments")] | |
public IActionResult ProcessPayment([FromBody] CreatePaymentRequest request) | |
{ | |
// do something | |
var payment = request.Map<Payment>(); | |
var paymentProcessor = PaymentServiceFactory.GetProcessor(payment.Type); | |
var result = paymentProcessor.ProcessPayment(payment); | |
// return something | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment