Proxy Design Pattern in .NET

The Proxy pattern provides a placeholder or surrogate for another object. It controls access to the real object and can add extra behavior like caching, logging, lazy initialization, or security.

 

Purpose

To control access to another object. It can be used to defer resource-heavy operations, restrict access, or wrap remote calls and security logic.

 

Key Characteristics

  • Implements the same interface as the real object
  • Delegates requests to the actual object
  • Can add extra logic (e.g., access control, logging)

 

Pros

  • Adds control, performance optimizations, or protection
  • Supports lazy loading or remote access
  • Keeps real object usage transparent to the client

 

Cons

  • Introduces additional layers
  • Can increase complexity
  • Misuse may lead to tight coupling

 

Use Cases

  • Security proxies for sensitive financial operations
  • Virtual proxies to delay expensive report generation
  • Caching proxies for frequent balance or interest rate checks
  • Remote proxies for microservice/API gateways

 

Real-Time Finance Example: Secure Access to Account Details

You want to protect access to sensitive account data. Instead of letting users directly query the service, you use a proxy that checks if the user has permission.

 

1. Define the Interface

public interface IAccountService

{

    void ShowAccountDetails(string user);

}

 

2. Implement the Real Service

public class RealAccountService : IAccountService

{

    public void ShowAccountDetails(string user)

    {

        Console.WriteLine($"Showing account details for: {user}");

    }

}

 

3. Implement the Proxy

public class AccountProxy : IAccountService

{

    private readonly RealAccountService _realService = new();

 

    private readonly List<string> _authorizedUsers = new() { "Priya", "Admin" };

 

    public void ShowAccountDetails(string user)

    {

        if (_authorizedUsers.Contains(user))

        {

            Console.WriteLine("Access granted.");

            _realService.ShowAccountDetails(user);

        }

        else

        {

            Console.WriteLine("Access denied.");

        }

    }

}

 

4. Client Code Usage

class Program

{

    static void Main()

    {

        IAccountService accountService = new AccountProxy();

 

        accountService.ShowAccountDetails("Priya");     // Allowed

        Console.WriteLine();

        accountService.ShowAccountDetails("UnknownUser"); // Denied

    }

}

 

Output

Access granted.

Showing account details for: Priya

 

Access denied.

 

Summary

  • Proxy Pattern is ideal when access to the real object must be controlled, delayed, or enhanced
  • In finance, it's great for access control, audit logging, lazy-loaded services, or external API gateways
  • Keeps client interaction simple, while protecting and managing the real object

Comments

Popular posts from this blog

Multiline to singleline IN C# - CODING

EF Core interview questions for beginners

EF Core interview questions for experienced