Now that we have a decorator, it is time to create a second decorator to demonstrate the power of chaining decorators.Context: we need another content wrapper but don’t want to modify existing classes. To achieve this, we concluded that creating a second decorator would be perfect, so we created the following DecoratorB class: public class […]
Project – Adding behaviors – Structural Patterns
Let’s implement the previous example to help visualize the Decorator pattern, which adds some arbitrary behaviors. Each Operation() method returns a string that is then outputted to the response stream. It is not fancy but visually shows how the pattern works.First, let’s look at the IComponent interface: public interface IComponent{ string Operation();} The IComponent interface […]
Design – Structural Patterns
A decorator class must implement and use the interface the decorated class implements. Let’s see this step by step, starting with a non-decorated class design: Figure 11.1: A class diagram representing the ComponentA class implementing the IComponent interface In the preceding diagram, we have the following components: This translates into the following sequence diagram: Figure […]
Implementing the Decorator design pattern – Structural Patterns
Before you begin: Join our book community on Discord Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the “architecting-aspnet-core-apps-3e” channel under EARLY ACCESS SUBSCRIPTION). https://packt.link/EarlyAccess This chapter explores four design patterns from the well-known Gang of Four (GoF). We use Structural patterns to build […]
Writing logs – Logging patterns-2
In the Act phase, we call the Execute method of our service. This method logs a line to the ILogger implementation that is injected upon instantiation. Then, we assert that the line was written in the lines list (that’s what AssertableLogger does; it writes to a List<string>). In an ASP.NET Core application, all that logging […]
Writing logs – Logging patterns-1
First, the logging system is provider-based, meaning we must register one or more ILoggerProvider instances if we want our log entries to go somewhere. By default, when calling WebApplication.CreateBuilder(args), it registers the Console, Debug, EventSource, and EventLog (Windows only) providers, but we can modify this list. You can add and remove providers if you need […]
About logging – Logging patterns
Before you begin: Join our book community on Discord Give your feedback straight to the author himself and chat to other early readers on our Discord server (find the “architecting-aspnet-core-apps-3e” channel under EARLY ACCESS SUBSCRIPTION). https://packt.link/EarlyAccess This chapter covers a .NET-specific feature and closes the Designing for ASP.NET Core section. The logging feature that comes […]