Skip to content

neoxack/CQRS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

.NET NuGet NuGet

CQRS

This library provides a .NET implementation of the CQRS pattern, allowing you to separate command and query responsibilities in your applications. CQRS promotes a clear separation between the command side that performs operations and the query side that retrieves data.

Installing CQRS

You should install Neoxack.CQRS with NuGet:

Install-Package Neoxack.CQRS

Or via the .NET Core command line interface:

dotnet add package Neoxack.CQRS

Either commands, from Package Manager Console or .NET Core CLI, will download and install Neoxack.CQRS and all required dependencies.

Example

public class PingCommand : ICommand<string>
{
    public string Value { get; }

    public PingCommand(string value)
    {
        Value = value;
    }
}
public class PingCommandHandler : ICommandHandler<PingCommand, string>
{
    public string Handle(PingCommand command)
    {
        return command.Value;
    }
}
public class PingController
{
    private readonly IQueryDispatcher _queryDispatcher;
    private readonly ICommandDispatcher _commandDispatcher;

    public PingController(IQueryDispatcher queryDispatcher, ICommandDispatcher commandDispatcher)
    {
        _queryDispatcher = queryDispatcher;
        _commandDispatcher = commandDispatcher;
    }

    [HttpPost("ping")]
    public string Ping()
    {
        return _commandDispatcher.Execute(new PingCommand("Ping"));
    }
}

Registering with IServiceCollection

CQRS supports Microsoft.Extensions.DependencyInjection.Abstractions directly. To register CQRS services and handlers:

services.AddCQRS(typeof(Startup).Assembly);

This registers:

  • ICommandDispather as scoped
  • IQueryDispatcher as scoped
  • ICommandHandler<,> concrete implementations as scoped
  • IQueryHandler<,> concrete implementations as scoped

Benchmarks

BenchmarkDotNet v0.13.10, macOS Sonoma 14.1.2 (23B92) [Darwin 23.1.0]
Apple M2 Pro, 1 CPU, 12 logical and 12 physical cores
.NET SDK 8.0.100
  [Host]     : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD
  DefaultJob : .NET 6.0.20 (6.0.2023.32017), Arm64 RyuJIT AdvSIMD
Method Mean Error StdDev Ratio RatioSD Gen0 Allocated Alloc Ratio
Neoxack.CQRS 73.89 ns 0.167 ns 0.140 ns 1.00 0.00 0.0114 24 B 1.00
MediatR 189.76 ns 2.430 ns 2.154 ns 2.57 0.02 0.1760 368 B 15.33

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

About

Simple and fast CQRS implementation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages