Table of Contents

Class ProxyDemo

Namespace
PatternKit.Examples.ProxyDemo
Assembly
PatternKit.Examples.dll

Demonstrates various proxy patterns including virtual, protection, caching, logging, and a complete mocking framework.

public static class ProxyDemo
Inheritance
ProxyDemo
Inherited Members

Methods

DemonstrateCachingProxy()

Demonstrates the caching proxy pattern for result memoization.

public static void DemonstrateCachingProxy()

Remarks

The caching proxy stores results of expensive calculations and returns cached values for repeated inputs, avoiding redundant computation.

Ideal for:

  • Expensive computations (e.g., Fibonacci, cryptography)
  • Database queries with stable data
  • API calls with rate limits
  • Image/video processing

DemonstrateCachingProxy(TextWriter)

public static void DemonstrateCachingProxy(TextWriter writer)

Parameters

writer TextWriter

DemonstrateCustomInterception()

Demonstrates custom interception for implementing retry logic with exponential backoff.

public static void DemonstrateCustomInterception()

Remarks

This example shows how the proxy pattern can add resilience to unreliable services by automatically retrying failed operations.

Retry logic is essential for:

  • Network calls that may fail transiently
  • Distributed systems with eventual consistency
  • Cloud services with throttling
  • Microservices communication

DemonstrateCustomInterception(TextWriter)

public static void DemonstrateCustomInterception(TextWriter writer)

Parameters

writer TextWriter

DemonstrateLoggingProxy()

Demonstrates the logging proxy pattern for invocation tracking and debugging.

public static void DemonstrateLoggingProxy()

Remarks

The logging proxy transparently logs all method invocations and their results, useful for debugging, auditing, and monitoring.

Common applications:

  • Audit trails for compliance
  • Performance monitoring
  • Debugging production issues
  • Usage analytics

DemonstrateLoggingProxy(TextWriter)

public static void DemonstrateLoggingProxy(TextWriter writer)

Parameters

writer TextWriter

DemonstrateMockFramework()

Demonstrates a complete mocking framework built with the proxy pattern.

public static void DemonstrateMockFramework()

Remarks

Shows how to create test doubles, configure behavior, and verify interactions using the proxy pattern - the foundation of all .NET mocking frameworks.

DemonstrateMockFramework(TextWriter)

public static void DemonstrateMockFramework(TextWriter writer)

Parameters

writer TextWriter

DemonstrateProtectionProxy()

Demonstrates the protection proxy pattern for access control.

public static void DemonstrateProtectionProxy()

Remarks

The protection proxy validates user permissions before allowing access to documents. This implements role-based access control (RBAC) at the proxy level.

Use cases:

  • Role-based access control
  • Authentication and authorization
  • API rate limiting
  • Feature flags and permissions

DemonstrateProtectionProxy(TextWriter)

public static void DemonstrateProtectionProxy(TextWriter writer)

Parameters

writer TextWriter

DemonstrateRemoteProxy()

Demonstrates the remote proxy pattern with caching for network optimization.

public static void DemonstrateRemoteProxy()

Remarks

Combines multiple proxy concerns (logging + caching) to create an efficient remote proxy that minimizes network calls and provides visibility.

Remote proxy use cases:

  • REST API clients
  • Distributed object systems (RPC, gRPC)
  • Database connection pools
  • Message queue consumers

DemonstrateRemoteProxy(TextWriter)

public static void DemonstrateRemoteProxy(TextWriter writer)

Parameters

writer TextWriter

DemonstrateVirtualProxy()

Demonstrates the virtual proxy pattern for lazy initialization of expensive resources.

public static void DemonstrateVirtualProxy()

Remarks

The virtual proxy delays creating the expensive database connection until the first query is executed. Subsequent queries reuse the initialized connection.

This is particularly useful for:

  • Database connections
  • File handles
  • Network connections
  • Heavy computational resources

DemonstrateVirtualProxy(TextWriter)

public static void DemonstrateVirtualProxy(TextWriter writer)

Parameters

writer TextWriter

RunAllDemos()

Runs all proxy pattern demonstrations.

public static void RunAllDemos()

RunAllDemos(TextWriter)

public static void RunAllDemos(TextWriter writer)

Parameters

writer TextWriter