C# Design Patterns: Proxy APPLYING THE PROXY PATTERN Steve Smith FORCE MULTIPLIER FOR DEV TEAMS @ardalis | ardalis.com | weeklydevtips.com
t h s What problems does proxy solve? Objectives How is the proxy pattern structured? Apply the pattern in real code Recognize related patterns
Problem: Need to control access to a type for performance, security, or other reasons.
Real World Examples
Common Software Examples
Common Software Examples API Service Client App User Interface Twittergrambook Twittergrambook API ServiceProxy Endpoints
Proxy Usage in Software
Proxy Structure
Proxy Structure
Proxy Variants Virtual Proxy Remote Proxy Smart Proxy Protective Proxy
s h s Virtual Proxy Stands in for an expensive-to-create object Typically responsible for getting real object UI Placeholders Lazy-Loaded Entity Properties
t h s Demo Virtual Proxy in C#
s h s Remote Proxy Client works with proxy as if remote resource were local Hides network details from client Centralizes knowledge of network details
t h s Demo Remote Proxy in C#
s h s Smart Proxy Performs additional logic around resource access Resource counting Cache management Locking shared resources
t h s Demo Smart Proxy in C#
s h s Protective Proxy Manages access to a resource based on authorization rules Eliminates repetitive security checks from client code and the resource itself Acts as a gatekeeper around a resource
t h s Demo Protective Proxy in C#
When used properly, proxy implementations help you to follow Separation of Concerns and the Single Responsibility Principle.
Related Principles Some principles suggest the use of a Proxy as the solution in certain cases. 1 Separation of Concerns Loose Coupling Single Responsibility Avoid mixing separate Prefer loose coupling to Classes should have only concerns or ideas in the third party dependencies one responsibility; one same class or method reason to change
Related Patterns Decorator Prototype Adapter Flyweight
t A proxy controls access to another class h s There are at least 4 kinds of proxy variants: Key Virtual Takeaways Remote Smart Protective Proxy classes can be generated automatically, especially remote proxies An appropriate use of proxy often helps your code follow good coding principles Latest sample code: https://github.com/ardalis/DesignPatternsInCSharp
C# Design Patterns: Proxy APPLYING THE PROXY PATTERN Steve Smith FORCE MULTIPLIER FOR DEV TEAMS @ardalis | ardalis.com | weeklydevtips.com
Recommend
More recommend