Proxy Pattern in C# – Real-Time Example (Controlled Access to Objects)
What is Proxy Pattern?
The Proxy Pattern is a structural design pattern that provides a placeholder or surrogate object to control access to another object.
The proxy acts as an intermediary between the client and the real object.
Why Use Proxy Pattern?
- Control access to objects
- Improve performance using lazy loading
- Add security/authentication
- Enable caching and logging
Real-Time Scenario
Examples:
- Lazy loading images/files
- Authentication before accessing service
- API request caching
- Remote service access
👉 Client accesses proxy instead of real object directly.
Real-Time Example – Lazy Loading Image
Step 1: Subject Interface
public interface IImage
{
void Display();
}
Step 2: Real Object
public class RealImage : IImage
{
private readonly string _fileName;
public RealImage(string fileName)
{
_fileName = fileName;
LoadFromDisk();
}
private void LoadFromDisk()
{
Console.WriteLine($"Loading {_fileName} from disk...");
}
public void Display()
{
Console.WriteLine($"Displaying {_fileName}");
}
}
Step 3: Proxy Object
public class ProxyImage : IImage
{
private RealImage _realImage;
private readonly string _fileName;
public ProxyImage(string fileName)
{
_fileName = fileName;
}
public void Display()
{
// Lazy loading
if (_realImage == null)
{
_realImage = new RealImage(_fileName);
}
_realImage.Display();
}
}
Usage Example
IImage image = new ProxyImage("invoice.png");
// Image loaded only when needed
image.Display();
Output
Loading invoice.png from disk... Displaying invoice.png
Key Concept
Instead of:
new RealImage("invoice.png");
We do:
new ProxyImage("invoice.png");
👉 Proxy controls access to the real object.
Diagram Understanding
Client ↓ Proxy ↓ Real Object
👉 Client communicates through proxy.
Types of Proxy
| Type | Purpose |
|---|---|
| Virtual Proxy | Lazy loading |
| Protection Proxy | Security/auth |
| Remote Proxy | Remote service access |
| Caching Proxy | Cache responses |
| Logging Proxy | Track usage |
Advantages
- ✔ Controls object access
- ✔ Improves performance
- ✔ Supports lazy loading
- ✔ Adds security and caching
- ✔ Reduces unnecessary object creation
Disadvantages
- ✖ Adds extra layer
- ✖ More classes and complexity
- ✖ Slight performance overhead
When to Use
Use Proxy Pattern when:
- Object creation is expensive
- Access control is required
- Lazy loading is needed
- Caching or logging is required
- Working with remote services
Real Project Mapping (.NET + Angular)
| Feature | Usage |
|---|---|
| Authentication checks | Proxy |
| Lazy loading images/files | Proxy |
| API caching | Proxy |
| Remote microservice calls | Proxy |
| Logging wrapper | Proxy |
ASP.NET Core Real Example
Authentication Middleware
app.UseAuthentication();
👉 Middleware acts like proxy before accessing API.
Advanced Example – Protection Proxy
public class SecureServiceProxy : IService
{
public void Execute()
{
if(User.IsAuthenticated)
{
Console.WriteLine("Access Granted");
}
else
{
Console.WriteLine("Access Denied");
}
}
}
👉 Proxy validates access before calling real service.
Proxy vs Decorator
| Proxy | Decorator |
|---|---|
| Controls access | Adds behavior |
| Focus on protection/performance | Focus on feature extension |
| May hide real object | Wraps and extends object |
Pro Tip
Proxy Pattern works well with:
- Lazy loading
- Caching systems
- Security layers
- Remote APIs
- Middleware pipelines
Summary
Proxy Pattern helps you:
- Control access to objects
- Improve performance
- Add security and lazy loading
👉 Perfect for:
- Authentication
- Caching
- Lazy loading
- Remote service access
Comments
Post a Comment