CQRS.Base
1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CQRS.Base --version 1.0.2
NuGet\Install-Package CQRS.Base -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CQRS.Base" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CQRS.Base" Version="1.0.2" />
<PackageReference Include="CQRS.Base" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CQRS.Base --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CQRS.Base, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CQRS.Base@1.0.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CQRS.Base&version=1.0.2
#tool nuget:?package=CQRS.Base&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CQRS Pattern Library
This library provides a simple and extensible foundation for implementing the Command Query Responsibility Segregation (CQRS) pattern in .NET applications.
Features
- Command and Query Handler Interfaces: Define commands and queries with strong typing.
- Result Types: Standardized result objects for success, failure, and validation errors.
- Automatic DI Registration: Extension method to scan assemblies and register all command and query handlers as scoped services.
Getting Started
1. Define Commands, Queries, and Handlers
Command Example:
public class CreateUserCommand : ICommand<Guid>
{
public string Name { get; set; }
}
Command Handler Example:
public class CreateUserCommandHandler : ICommandHandler<CreateUserCommand, Guid>
{
public Task<Result<Guid>> Handle(CreateUserCommand command, CancellationToken cancellationToken)
{
// Business logic here
return Task.FromResult(Result<Guid>.Success(Guid.NewGuid()));
}
}
Query Example:
public class GetUserQuery : IQuery<UserDto>
{
public Guid UserId { get; set; }
}
Query Handler Example:
public class GetUserQueryHandler : IQueryHandler<GetUserQuery, UserDto>
{
public Task<Result<UserDto>> Handle(GetUserQuery query, CancellationToken cancellationToken)
{
// Business logic here
return Task.FromResult(Result<UserDto>.Success(new UserDto()));
}
}
2. Register Handlers in DI
In your Program.cs
or Startup.cs
:
using CQRS.Base;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
// Register all CQRS handlers from the current assembly
builder.Services.AddCQRSHandlersFromAssembly(Assembly.GetExecutingAssembly());
var app = builder.Build();
// ...
3. Use Handlers via Minimal API
app.MapPost("/users", async (CreateUserCommand command, ICommandHandler<CreateUserCommand, Guid> handler, CancellationToken cancellationToken) =>
{
var result = await handler.Handle(command, cancellationToken);
return result.IsSuccess
? Results.Ok(result.Value)
: Results.BadRequest(result.ErrorMessage);
});
Summary
- Define your commands, queries, and handlers.
- Register all handlers with one line using the provided extension method.
- Use handlers via DI in your application.
This approach keeps your codebase clean, testable, and scalable using the CQRS pattern.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Microsoft.Extensions.DependencyInjection (>= 9.0.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.