Decorator Pattern In ASP.NET Core

Milan Jovanović - Sep 10 '23 - - Dev Community

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
            .Set<Member>()
            .First(member => member.Id == id);
    }
}
Enter fullscreen mode Exit fullscreen mode

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(
            key,
            entry => {
                entry.SetAbsouluteExpiration(
                    TimeSpan.FromMinutes(5));

                return _repository.GetById(id);
            });
    }
}
Enter fullscreen mode Exit fullscreen mode

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),
         cache);
});
Enter fullscreen mode Exit fullscreen mode

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>();
Enter fullscreen mode Exit fullscreen mode

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.


P.S. Whenever you're ready, there are 2 ways I can help you:

  1. Pragmatic Clean Architecture: This comprehensive course will teach you the system I use to ship production-ready applications using Clean Architecture. Learn how to apply the best practices of modern software architecture. Join 950+ students here.

  2. Patreon Community: Think like a senior software engineer with access to the source code I use in my YouTube videos and exclusive discounts for my courses. Join 820+ engineers here.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .