Take advantage of the proxy design pattern to provide a convenient placeholder for accessing a remote, complex, or protected object Credit: Thinkstock Design patterns are used to solve the recurring problems and complexities in designing software applications. The Gang of Four design patterns are divided into three categories: creational, structural, and behavioral. Creational design patterns are used to create and manage the mechanism of creating instances of classes. Structural design patterns are used to define the structure of types and their relationships. Behavioral design patterns are used to manage object collaboration and the delegation of responsibilities among objects. The proxy design pattern belongs to the structural category. When working on applications, you might often need to access objects that are part of a separate application or domain. In these cases it is a best practice (for the sake of code maintenance and separation of concerns) to separate the logic used to communicate with the external application or domain from the logic used to access the object itself. This is where the proxy design pattern comes into play. The Gang of Four defines the proxy design pattern as follows: Provide a surrogate or placeholder for another object to control access to it. This article presents a discussion of when and why we should use the proxy design pattern and how we can implement the proxy design pattern in C#. Why use the proxy design pattern? The proxy design pattern can be used in situations where you would like to defer instantiation of a class until the time it is needed. The proxy is an object that can be used to control access to a remote resource or to a more complex object, i.e., an object whose creation or operations would consume more resources. Proxy objects can be of the following types: Remote proxy. Remote proxies encode the request and then send the encoded request to the actual object. Virtual proxy. Virtual proxies are typically used when the actual object is heavy. To conserve client resources, they defer the instantiation of the actual object until it is truly needed; i.e., instantiation is done only on demand. Protection proxy. Protection proxies are used to restrict access to a resource. If the caller of the request lacks the necessary permission to access the resource, then access is denied. Implementing the proxy design pattern in C# In this section we’ll examine how we can implement the proxy design pattern in C#. The participants in a typical implementation of the proxy design pattern are the Proxy, the Subject, and the RealSubject. While the proxy maintains a reference to access the RealSubject, the Subject is the type that defines the common interface for both the Proxy and the RealSubject. That said, let’s now implement a minimalistic implementation of the proxy pattern. Create a new Console application project in Visual Studio. Next, create the following interface called ICalculate. public interface ICalculate { double ComplexOperation(double x, double y); } This interface contains the declaration of the ComplexOperation method. Although we will keep his method simple, keep in mind that it is used here to demonstrate a complex operation that is resource intensive. The Subject class implements the ICalculate interface. This class contains the definition of the ComplexOperation method. class Subject : ICalculate { public double ComplexOperation(double x, double y) { return x + y; } } Creating the proxy in C# The proxy in our example is represented by the SimpleProxy class. SimpleProxy authenticates a user and—if the caller is authenticated—makes a call to ComplexOperation. If the caller is not authenticated, a value of 0 is returned. class SimpleProxy : ICalculate { public bool IsAuthenticated { get; private set; } private Subject subject = new Subject(); public double ComplexOperation(double x, double y) { if (IsAuthenticated) return subject.ComplexOperation(x, y); else return 0; } public bool Authenticate(string pwd) { if (pwd == "password123") { IsAuthenticated = true; return true; } else return false; } } Invoking the proxy in C# Lastly, here’s how the client invokes the proxy. Note that the credentials here have been hardcoded. I’ll leave it to you to build on this structural implementation of the proxy design pattern and change it as appropriate. static void Main(string[] args) { SimpleProxy simpleProxy = new SimpleProxy(); simpleProxy.Authenticate("password123"); Console.WriteLine(simpleProxy.ComplexOperation(5, 10)); Console.Read(); } The proxy design pattern is a good choice when you have objects in your application that are expensive to create. You can also take advantage of the proxy design pattern when you would like to have a local object that can refer to a remote object, or to create a complex object only when needed. Related content how-to How to use DispatchProxy for AOP in .NET Core Take advantage of the DispatchProxy class in C# to implement aspect-oriented programming by creating proxies that dynamically intercept method calls. By Joydip Kanjilal Nov 14, 2024 7 mins Microsoft .NET C# Development Libraries and Frameworks news Microsoft’s .NET 9 arrives, with performance, cloud, and AI boosts Cloud-native apps, AI-enabled apps, ASP.NET Core, Aspire, Blazor, MAUI, C#, and F# all get boosts with the latest major rev of the .NET platform. By Paul Krill Nov 12, 2024 4 mins C# Generative AI Microsoft .NET how-to Why use aspect-oriented programming Aspect-oriented programming allows you to isolate the cross-cutting concerns of your application, reduce code duplication, and improve the readability and maintainability of your code. By Joydip Kanjilal Oct 31, 2024 5 mins Microsoft .NET C# Development Libraries and Frameworks how-to How to use Task.WhenEach in .NET 9 Take advantage of the new Task.WhenEach method to process asynchronous tasks as they complete, enhancing the efficiency of your .NET applications. By Joydip Kanjilal Oct 17, 2024 6 mins Microsoft .NET C# Development Libraries and Frameworks Resources Videos