MNW #006: Decorator Pattern In ASP.NET Core

Oct 08 2022

3 min read

A big thank you to the first-ever sponsor of this newsletter.

This week's issue is sponsored by QuadSpinner.

Let's imagine we have an existing Repository implementation, and we want to introduce caching to reduce the load on the database.

How can we achieve this without changing anything about the Repository implementation?

Decorator pattern is a structural design pattern that allows you to introduce new behavior to an existing class, without modifying the original class in any way.

I'll show you how you can implement this with the ASP.NET Core DI container.

How To Implement The Decorator Pattern

We'll start with an existing MemberRepository implementation that implements the IMemberRepository interface.

It has only one method, which loads the Member from the database.

Here's what the implementation looks like:

public interface IMemberRepository
    Member GetById(int id);

public class MemberRepository : IMemberRepository
    private readonly DatabaseContext _dbContext;

    public MemberRepository(DatabaseContext dbContext)
        _dbContext = dbContext;

    public Member GetById(int id)
        return _dbContext
            .First(member => member.Id == id);

We want to introduce caching to the MemberRepository implementation without modifying the existing class.

To achieve this, we can use the Decorator pattern and create a wrapper around our MemberRepository implementation.

We can create a CachingMemberRepository that will have a dependency on IMemberRepository.

public class CachingMemberRepository : IMemberRepository
    private readonly IMemberRepository _repository;
    private readonly IMemoryCache _cache;

    public CachingMemberRepository(
        IMemberRepository repository,
        IMemoryCache cache)
        _repository = repository;
        _cache = cache;

    public Member GetById(int id)
        string key = $"members-{id}";

        return _cache.GetOrCreate(
            entry => {

                return _repository.GetById(id);

Now I'm going to show you the power of ASP.NET Core DI.

We will configure the IMemberRepository to resolve an instance of CachingMemberRepository, while it will receive the MemberRepository instance as its dependency.

Configuring The Decorator In ASP .NET Core DI

For the DI container to be able to resolve IMemberRepository as CachingMemberRepository, we need to manually configure the service.

We can use the overload that exposes a service provider, that we will use to resolve the services required to construct a MemberRepository.

Here's what the configuration would look like:

services.AddScoped<IMemberRepository>(provider => {
    var context = provider.GetService<DatabaseContext>();
    var cache = provider.GetService<IMemoryCache>();

    return new CachingRepository(
         new MemberRepository(context),

Now you can inject the IMemberRepository, and the DI will be able to resolve an instance of CachingMemberRepository.

Configuring The Decorator With Scrutor

If the previous approach seems cumbersome to you and like a lot of manual work - that's because it is.

However, there is a simpler way to achieve the same behavior.

We can use the Scrutor library to register the decorator:

services.AddScoped<IMemberRepository, MemberRepository>();

services.Decorate<IMemberRepository, CachingMemberRepository>();

Scrutor exposes the Decorate method. The call to Decorate will register the CachingMemberRepository while ensuring that it receives the expected MemberRepository instance as its dependency.

I think this approach is much simpler, and it's what I use in my projects.

Whenever you're ready, there are 3 ways I can help you 🔥

  1. To access all of the source code that I use in my YouTube videos support me on Patreon
  2. For more practical & in-depth coding examples subscribe to my YouTube channel
  3. Promote yourself to 11k+ subscribers by sponsoring this newsletter (booked out 2 months)

Special Offers 📢

About the Newsletter

11k+ subscribers get one tip to improve their software engineering and .NET skills every Saturday morning.

Connect with Me

Join 11,774 .NET engineers getting 1 actionable .NET tip every Saturday

Actionable tips

Easy to implement

5 minute read

Free, forever