Skip to content

Implement CQRS in .NET Projects

License

Notifications You must be signed in to change notification settings

walnut-co/cqrs.net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CQRS.Net

NuGet Version NuGet Downloads

A lightweight CQRS (Command Query Responsibility Segregation) implementation for .NET projects.

Features

  • Simple and clean CQRS pattern implementation
  • Request/Response handling with strongly typed interfaces
  • Built-in dependency injection integration
  • Pipeline behavior support for cross-cutting concerns
  • Minimal setup required

Installation

dotnet add package Walnut.CQRS.Net

Usage

1. Register services in your DI container

services.AddCQRS();

builder.Services.AddScoped(typeof(IRequestPipeline<,>), typeof(AuthorizationBehaviour<,>));
builder.Services.AddScoped(typeof(IRequestPipeline<,>), typeof(ValidationBehavior<,>));

// Automatically register all IRequestHandler implementations from the current assembly
var assembly = Assembly.GetExecutingAssembly();
builder.Services.Scan(scan => scan
    .FromAssemblies(assembly)
    .AddClasses(classes => classes.AssignableTo(typeof(IRequestHandler<,>)))
    .AsImplementedInterfaces()
    .WithTransientLifetime());

2. Define your commands and queries

public class SayHelloCommand : IRequest<string>
{
    public string Name { get; set; }
}

3. Inject and use the dispatcher

public class MyController : ControllerBase
{
    private readonly IRequestDispatcher _dispatcher;
    
    public MyController(IRequestDispatcher dispatcher)
    {
        _dispatcher = dispatcher;
    }
    
    [HttpPost]
    public async Task<string> SayHello([FromBody] SayHelloCommand command)
    {
        return await _dispatcher.Send(command);
    }
}

License

This project is licensed under the Apache License.

About

Implement CQRS in .NET Projects

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages