Skip to content
Carmel Eve By Carmel Eve Software Engineer I
Design patterns in C# - The Proxy Pattern

Continuing my series on design patterns, this week we're focusing on the proxy pattern!

The proxy pattern is used to provide access to an object. It is often used to enable this access over some distance - this could be providing remote access, or adding an extra level of protection around the object. The crucial thing is that the proxy pattern offers a way to indirectly provide (and control) access. It is similar in implementation to the decorator pattern, but its purpose is different. Both patterns wrap an inner object, however the decorator pattern expands on the functionality of the inner object, where the proxy pattern instead governs the access to the object.

The proxy pattern can be used to restrict access to an object, to provide a simpler or lightweight interface, or to allow the client to communicate with a remote object via a local representation.

Now, if we look at an example...

Proxy Example

So, say we have an INest:

public interface INest
{
    void Access(string name);
}

which is implemented by a RealNest:

public class RealNest : INest
{
    public void Access(string name)
    {
        Console.WriteLine($"{name} has access to the nest");
    }
}

Now, we don't want just anyone to be able to access our Nest. We need to be able to restrict access to only those that aren't going to cause harm. To do this, we can implement a SecureNestProxy:

public class SecureNestProxy : INest
{
    private readonly INest nest;

    public SecureNestProxy(INest nest)
    {
        this.nest = nest;
    }

    public void Access(string name)
    {
        if (name == "TRex" || name == "Gigantosaurus")
        {
            throw new UnauthorizedAccessException($"{name} is not allowed to access the nest.");
        }
        else
        {
            this.nest.Access(name);
        }
    }
}

This Proxy will reject access by dinosaurs we know to be carnivorous. If we then run the following:

var secureNestProxy = new SecureNestProxy(new RealNest());

secureNestProxy.Access("Stegosaurus");
secureNestProxy.Access("TRex");

Then the output will be the following:

Stegosaurus has access to the nest

Unhandled Exception: System.UnauthorizedAccessException: TRex is not allowed to access the nest.

And we can see that if we wrap our underlying objects in our proxy, we can restrict access as we need.

Other examples of uses for this pattern include caching (where the requested data doesn't need to be repeatedly requested from the service, and a copy is instead stored in memory inside the proxy), for lazy loading of large objects, or to locally represent objects which exist outside of your system.

Thanks for reading! Here's a link to the GitHub repository which contains all the code for this blog series. And watch of for my next blog on design patterns in C#!

Carmel Eve

Software Engineer I

Carmel Eve

Carmel is a software engineer, LinkedIn Learning instructor and STEM ambassador.

Over the past four years she has been focused on delivering cloud-first solutions to a variety of problems. These have ranged from highly-performant serverless architectures, to web applications, to reporting and insight pipelines and data analytics engines.

In her time at endjin, she has written many blog posts covering a huge range of topics, including deconstructing Rx operators and mental well-being and managing remote working.

Carmel's first LinkedIn Learning course on how to prepare for the Az-204 exam - developing solutions for Microsoft Azure - is due for release next month. And over the last couple of years she has also spoken at NDC, APISpecs and SQLBits. These talks covered a range of topics, from reactive big-data processing to secure Azure architectures.

She is also passionate about diversity and inclusivity in tech. She is a STEM ambassador in her local community and is taking part in a local mentorship scheme. Through this work she hopes to be a part of positive change in the industry.

Carmel won "Apprentice Engineer of the Year" at the Computing Rising Star Awards 2019.