Mastering the Factory Method Design Pattern in ASP.NET Core

In the world of ASP.NET Core development, design patterns play a crucial role in creating maintainable, flexible, and scalable applications. One such essential pattern is the Factory Method design pattern.

Introduction:

In the world of ASP.NET Core development, design patterns play a crucial role in creating maintainable, flexible, and scalable applications. One such essential pattern is the Factory Method design pattern. In this blog post, I will explore the Factory Method pattern and its implementation in ASP.NET Core with a real-world example.

Factory Method Design Pattern:

The Factory Method pattern is a creational design pattern that provides an interface for creating objects in a super class, but allows subclasses to alter the type of objects that will be created. This pattern promotes loose coupling between client code and the objects it creates, enabling easier extension and modification of the codebase.

Core Components of the Factory Method Pattern in ASP.NET Core:

  1. Creator: The abstract class or interface that declares the factory method for creating objects.
  2. Concrete Creator: Subclasses that implement the factory method to create specific objects.
  3. Product: The abstract class or interface that defines the interface of objects the factory method creates.
  4. Concrete Product: The classes that implement the Product interface and represent the objects created by the factory method.

Example: Creating Different Payment Gateways with Factory Method

Let’s demonstrate the Factory Method pattern in ASP.NET Core with an example of creating different payment gateways.

1. Create the Product interface – IPaymentGateway.cs

public interface IPaymentGateway
{
    void ProcessPayment(decimal amount);
}

2. Implement Concrete Products – PayPalGateway.cs and StripeGateway.cs

public class PayPalGateway : IPaymentGateway
{
    public void ProcessPayment(decimal amount)
    {
        // Integration code for processing payment through PayPal API
        Console.WriteLine($"Processing payment of {amount} USD using PayPal Gateway...");
    }
}

public class StripeGateway : IPaymentGateway
{
    public void ProcessPayment(decimal amount)
    {
        // Integration code for processing payment through Stripe API
        Console.WriteLine($"Processing payment of {amount} USD using Stripe Gateway...");
    }
}

3. Create the Creator abstract class – PaymentGatewayFactory.cs

public abstract class PaymentGatewayFactory
{
    public abstract IPaymentGateway CreateGateway();
}

4. Implement Concrete Creators – PayPalGatewayFactory.cs and StripeGatewayFactory.cs

public class PayPalGatewayFactory : PaymentGatewayFactory
{
    public override IPaymentGateway CreateGateway()
    {
        return new PayPalGateway();
    }
}

public class StripeGatewayFactory : PaymentGatewayFactory
{
    public override IPaymentGateway CreateGateway()
    {
        return new StripeGateway();
    }
}

5. Client Code – Startup.cs (ConfigureServices method)

public void ConfigureServices(IServiceCollection services)
{
    // Register the desired payment gateway factory based on configuration or user selection
    services.AddScoped<PaymentGatewayFactory, PayPalGatewayFactory>();
    //services.AddScoped<PaymentGatewayFactory, StripeGatewayFactory>();
}

6. Client Code – PaymentController.cs

public class PaymentController : ControllerBase
{
    private readonly PaymentGatewayFactory _paymentGatewayFactory;

    public PaymentController(PaymentGatewayFactory paymentGatewayFactory)
    {
        _paymentGatewayFactory = paymentGatewayFactory;
    }

    [HttpPost("process-payment")]
    public IActionResult ProcessPayment(decimal amount)
    {
        IPaymentGateway gateway = _paymentGatewayFactory.CreateGateway();
        gateway.ProcessPayment(amount);

        return Ok("Payment processed successfully.");
    }
}

Conclusion:

The Factory Method Design Pattern in ASP.NET Core provides a powerful mechanism for creating objects with loose coupling. By encapsulating the object creation process within a factory method, we can easily switch between different implementations of payment gateways without modifying the client code. This flexibility enhances the maintainability and scalability of our ASP.NET Core applications.

Through the example of creating different payment gateways, I have explored how to implement the Factory Method pattern in ASP.NET Core, allowing developers to build more organized and extensible codebases. By leveraging design patterns like Factory Method, ASP.NET Core developers can craft robust and adaptable solutions that meet the diverse requirements of modern web applications.

 

Please find original article here,  Mastering the Factory Method Design Pattern in ASP.NET Core.



If you find anything inappropriate please report it here.
Top