Skip to main content
The BuildingBlocks library provides reusable components and patterns that are shared across all microservices in the AspNetRun project. This library implements common concerns like CQRS patterns, validation, logging, exception handling, and pagination.

Architecture

The BuildingBlocks library is organized into several key areas:
  • CQRS: Command and Query interfaces based on MediatR
  • Behaviors: Cross-cutting concerns using MediatR pipeline behaviors
  • Exceptions: Custom exception types and global exception handler
  • Pagination: Standardized pagination request and response types

Key Dependencies

The BuildingBlocks library relies on the following NuGet packages:
<PackageReference Include="FluentValidation" Version="11.9.0" />
<PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.9.0" />
<PackageReference Include="Mapster" Version="7.4.0" />
<PackageReference Include="MediatR" Version="12.2.0" />
<PackageReference Include="Microsoft.FeatureManagement.AspNetCore" Version="3.2.0" />

Project Structure

BuildingBlocks/
├── CQRS/
│   ├── ICommand.cs
│   ├── ICommandHandler.cs
│   ├── IQuery.cs
│   └── IQueryHandler.cs
├── Behaviors/
│   ├── ValidationBehavior.cs
│   └── LoggingBehavior.cs
├── Exceptions/
│   ├── BadRequestException.cs
│   ├── NotFoundException.cs
│   ├── InternalServerException.cs
│   └── Handler/
│       └── CustomExceptionHandler.cs
└── Pagination/
    ├── PaginationRequest.cs
    └── PaginatedResult.cs

Usage Across Microservices

Each microservice in the AspNetRun project references the BuildingBlocks library to:
  1. Define Commands and Queries: Using CQRS interfaces to separate read and write operations
  2. Validate Requests: Automatically validate commands using FluentValidation
  3. Log Operations: Track request execution time and log warnings for slow operations
  4. Handle Exceptions: Convert exceptions to standardized HTTP problem details responses
  5. Paginate Results: Return consistent paginated responses across all APIs

Benefits

  • Consistency: All microservices follow the same patterns and conventions
  • Reusability: Common code is written once and shared across services
  • Maintainability: Changes to cross-cutting concerns are made in one place
  • Type Safety: Strong typing for commands, queries, and handlers
  • Separation of Concerns: Clean separation between business logic and infrastructure

Next Steps

CQRS Pattern

Learn about the Command and Query interfaces

Pipeline Behaviors

Understand validation and logging behaviors

Exception Handling

Explore custom exceptions and global handler

Pagination

Implement pagination in your queries

Build docs developers (and LLMs) love