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);
}
Programming C# 12 Book, by Ian Griffiths, published by O'Reilly Media, is now available to buy.

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.

The Introduction to Rx.NET 2nd Edition (2024) Book, by Ian Griffiths & Lee Campbell, is now available to download for FREE.

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 and LinkedIn Learning instructor. She worked at endjin from 2016 to 2021, focused on delivering cloud-first solutions to a variety of problems. These included highly performant serverless architectures, web applications, reporting and insight pipelines, and data analytics engines. After a three-year career break spent travelling around the world, she rejoined endjin in 2024.

Carmel has written many blog posts covering a huge range of topics, including deconstructing Rx operators, agile estimation and planning and mental well-being and managing remote working.

Carmel has released two courses on LinkedIn Learning - one on the Az-204 exam (developing solutions for Microsoft Azure) and one on Azure Data Lake. She has also spoken at NDC, APISpecs, and SQLBits, covering a range of topics from reactive big-data processing to secure Azure architectures.

She is passionate about diversity and inclusivity in tech. She spent two years as a STEM ambassador in her local community and 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.