BuildingBlocks Class Library uses Clean Architecture Application Layer in .NET 8 Microservices
In microservices architectures, BuildingBlocks Class Library uses Clean Architecture Application Layer to maintaining consistency and reducing duplication across services.
The BuildingBlocks class library emerges as a cornerstone in achieving this, particularly within the Clean Architecture and Domain-Driven Design (DDD) context.
I have just published course — .NET 8 Microservices: C# 12, DDD, CQRS, Vertical/Clean Architecture.
Foundation of BuildingBlocks
The BuildingBlocks folder is not just a repository of shared code; it’s the backbone that supports various microservices, encapsulating libraries for messaging, event handling, and other utilities that are integral to multiple services.
Creating the BuildingBlocks Class Library
The creation of the BuildingBlocks class library is a strategic move to centralize common functionalities like CQRS abstractions and cross-cutting concerns. This library stands as a shared resource that any microservice within the ecosystem can reference, fostering a DRY (Don’t Repeat Yourself) principle across the board.
Structuring the CQRS Folder
Within the BuildingBlocks library, a dedicated ‘CQRS’ folder houses command and query interfaces, laying the groundwork for a clear separation of concerns. This structure not only aids in managing validations more effectively but also ensures a consistent implementation of the CQRS pattern across services.
The Role of BuildingBlocks in Clean Architecture
BuildingBlocks serves as a nucleus in the Clean Architecture framework, particularly in the Application Layer. It streamlines the development process by providing a common ground for shared functionalities, which is crucial for services like the Catalog and Ordering microservices and others that need to consume these shared capabilities.
Integrating BuildingBlocks in Microservices
The integration of BuildingBlocks across microservices is a testament to its utility. For instance, the Carter library, pivotal for exposing minimal API endpoints, finds its place within BuildingBlocks, ensuring that any new microservice can kickstart its development with ease by simply referencing this library.
Project References and Dependencies
A careful orchestration of project references ensures that the Clean Architecture’s integrity is preserved. The Application layer, being at the core, references the Domain layer and the BuildingBlocks library, establishing a solid foundation. The Infrastructure layer, catering to external concerns, builds upon the Application layer, embodying the principle of dependency inversion. The API layer, being the presentation front, ties it all together by depending on both the Application and Infrastructure layers.
Leveraging MediatR Behaviors
The pre-developed logic for MediatR behaviors in BuildingBlocks epitomizes the reuse of common logic.
By registering these behaviors within the Application layer’s dependency injection setup, microservices like Ordering can leverage validation and logging behaviors seamlessly, ensuring a consistent and robust request handling mechanism.
Activate MediatR Pipeline Behaviors with BuildingBlocks
We have pre-developed common logic for behaviors in the BuildingBlocks library, which includes CQRS and Behaviors. Since Ordering.Application references BuildingBlocks, we can readily use these behaviors.
This BuildingBlocks class library also referencing into our Ordering.Application layer that we can use these common behaviours into our Ordering microservices. In order to activate these behaviors, we need to register these behaviours with MediatR’s configuration in the DependencyInjection.cs of Ordering.Application:
using BuildingBlocks.Behaviors;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
namespace Ordering.Application;
public static class DependencyInjection
{
public static IServiceCollection AddApplicationServices(this IServiceCollection services)
{
services.AddMediatR(config =>
{
config.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
config.AddOpenBehavior(typeof(ValidationBehavior<,>));
config.AddOpenBehavior(typeof(LoggingBehavior<,>));
});
return services;
}
}
AddOpenBehavior(typeof(ValidationBehavior<,>)) and AddOpenBehavior(typeof(LoggingBehavior<,>)) register our validation and logging behaviors with MediatR. This setup ensures that every request passing through MediatR is automatically validated and logged.
📚 Diving into Ordering Microservices with DDD & Clean Architecture
Our journey navigates through the core essence and periphery of the Ordering system, revealing the blueprint of a system designed for resilience, flexibility, and domain-centricity.
Conclusion
The BuildingBlocks class library is more than just a code repository; it’s a strategic asset in the microservices ecosystem, especially when aligned with Clean Architecture and DDD principles. It not only promotes code reuse and consistency but also simplifies the development process, making the architecture more resilient and adaptable to change.
I have just published course — .NET 8 Microservices: C# 12, DDD, CQRS, Vertical/Clean Architecture.
This is step-by-step development of reference microservices architecture that include microservices on .NET platforms which used ASP.NET Web API, Docker, RabbitMQ, MassTransit, Grpc, Yarp API Gateway, PostgreSQL, Redis, SQLite, SqlServer, Marten, Entity Framework Core, CQRS, MediatR, DDD, Vertical and Clean Architecture implementation with using latest features of .NET 8 and C# 12.