Skip to main content

RA.Utilities.Feature

· 2 min read
Redon Alla
.NET, React, Angular Developer

Version 10.0.0-rc.2

NuGet version

This release modernizes the RA.Utilities.Feature package, providing a foundational toolkit for implementing the Vertical Slice Architecture pattern using CQRS. It offers base handlers, validation behaviors, and seamless integration with the Result<T> type to streamline feature development.

✨ New Features & Improvements

  • Base Handlers for CQRS:

    • Provides abstract base classes (BaseHandler<TRequest, TResponse>) that encapsulate common logic like logging and exception handling.
    • Handlers automatically catch exceptions and wrap them in a Result.Failure, ensuring robust error handling without boilerplate try-catch blocks.
  • Automatic Validation Pipeline Behavior:

    • Includes a ValidationBehavior<TRequest, TResponse> for MediatR pipelines.
    • Automatically intercepts incoming requests, finds the corresponding FluentValidation validator, and executes it.
    • If validation fails, the pipeline is short-circuited, and a Result.Failure containing a ValidationException is returned immediately, preventing invalid data from reaching your business logic.
  • Seamless Result<T> Integration:

    • Designed from the ground up to work with the Result<T> type from RA.Utilities.Core, promoting explicit and predictable error handling for business logic failures.
  • Updated Documentation:

    • The README.md has been updated to provide a clear, step-by-step guide for creating a complete feature slice, including the command, validator, handler, and DI registration.

🚀 Getting Started

Register MediatR, the validation behavior, and your validators in Program.cs:

var builder = WebApplication.CreateBuilder(args);

// 1. Add MediatR and register handlers
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

// 2. Add the validation pipeline behavior from this package
builder.Services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));

// 3. Scan and register all FluentValidation validators
builder.Services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());