Home Decorator Pattern in .NET Core 3.1
Post
Cancel

Decorator Pattern in .NET Core 3.1

The decorator pattern is a structural design pattern used for dynamically adding behavior to a class without changing the class. You can use multiple decorators to extend the functionality of a class whereas each decorator focuses on a single-tasking, promoting separations of concerns. Decorator classes allow functionality to be added dynamically without changing the class thus respecting the open-closed principle.

You can see this behavior on the UML diagram where the decorator implements the interface to extend the functionality.

Decorator Pattern UML

Decorator Pattern UML (Source)

When to use the Decorator Pattern

The decorator pattern should be used to extend classes without changing them. Mostly this pattern is used for cross-cutting concerns like logging or caching. Another use case is to modify data that is sent to or from a component.

Decorator Pattern Implementation in ASP .NET Core 3.1

You can find the code of the demo on GitHub.

I created a DataService class with a GetData method which returns a list of ints. Inside the loop, I added a Thread.Sleep to slow down the data collection a bit to make it more real-world like.

This method is called in the GetData action and then printed to the website. The first feature I want to add with a decorator is logging. To achieve that, I created the DataServiceLoggingDecorator class and implement the IDataService interface. In the GetData method, I add a stopwatch to measure how long collecting data takes and then log the time it took.

Additionally, I want to add caching also using a decorator. To do that, I created the DataServiceCachingDecorator class and also implemented the IDataService interface. To cache the data, I use IMemoryCache and check the cache if it contains my data. If not, I load it and then add it to the cache. If the cache already has the data, I simply return it. The cache item is valid for 2 hours.

All that is left to do is to register the service and decorator in the ConfigureServices method of the startup class with the following code:

With everything in place, I can call the GetData method from the service which gets logged and the data placed in the cache. When I call the method again, the data will be loaded from the cache.

Start the application and click on Get Data. After a couple of seconds, you will see the data displayed.

Displaying the loaded data

Displaying the loaded data

When you load the site again, the data will be displayed immediately due to the cache.

Conclusion

The decorator pattern can be used to extend classes without changing the code. This helps you to achieve the open-closed principle and separation of concerns. Mostly the decorator pattern is used for cross-cutting concerns like caching and logging.

You can find the code of the demo on GitHub.

This post is licensed under CC BY 4.0 by the author.

Blazor Server vs. Blazor WebAssembly

IT Books you should read

Comments powered by Disqus.