SRenerCq 26.1.17
dotnet add package SRenerCq --version 26.1.17
NuGet\Install-Package SRenerCq -Version 26.1.17
<PackageReference Include="SRenerCq" Version="26.1.17" />
<PackageVersion Include="SRenerCq" Version="26.1.17" />
<PackageReference Include="SRenerCq" />
paket add SRenerCq --version 26.1.17
#r "nuget: SRenerCq, 26.1.17"
#:package SRenerCq@26.1.17
#addin nuget:?package=SRenerCq&version=26.1.17
#tool nuget:?package=SRenerCq&version=26.1.17
SRenerCQ
SRenerCQ is a simple CQRS implementation focused on using Dependency Injection to decouple commands and queries.
How to Use
Enabling SRenerCQ
Before using, it's important to enable SRenerCQ at the application by registering it in the service collection:
using SRenerCQ;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSRenerCQ();
...
Using SRenerCQ
After implementing commands/queries as described below, invoking them is done using the ISender interface by Dependency Injection. To use them:
public class MyInjectedClass(ISender sender) : IMyInjectedClass
{
public async Task ProcessCommand(){
MyCommand command = new("Foo");
await sender.Command(command);
}
public async Task ProcessQuery(){
MyQuery query = new("Foo");
string result = await sender.Query(query);
Console.WriteLine(result);
}
}
By convention, queries return data and commands perform state changes. This library allows commands to return a result (e.g., an identifier) — if you prefer strict CQRS, treat commands as void/Task and return read-model results via queries.
Commands
Commands could or could not return a value and are reserved to instructions that has the main goal of changing a piece of data. To use then:
Implement an ICommand and the corresponding ICommandHandler
public record MyCommand(string Name) : ICommand;
public class MyCommandHandler : ICommandHandler<MyCommand>
{
public async Task Handle(MyCommand command, CancellationToken cancellationToken)
{
Console.WriteLine($"Hello {command.Name}");
}
}
or, when it needs to return a value:
public record MyCommand(string Name) : ICommand<string>;
public class MyCommandHandler : ICommandHandler<MyCommand, string>
{
public async Task<string> Handle(MyCommand command, CancellationToken cancellationToken)
{
return $"Hello {command.Name}";
}
}
Queries
Queries are responsible to search and return a certain kind of data from the system. To accomplish that:
public record MyQuery(string Name) : IQuery<string>;
public class MyQueryHandler : IQueryHandler<MyQuery, string>
{
public async Task<string> Handle(MyQuery query, CancellationToken cancellationToken)
{
return $"Hello {query.Name}";
}
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.