JustFixMe just

Just.Cqrs (1.1.2)

Published 2025-11-11 23:58:03 +04:00 by just in just/Just.Cqrs

Installation

dotnet nuget add source --name just --username your_username --password your_token 
dotnet add package --source just --version 1.1.2 Just.Cqrs

About this package

Lightweight, easy-to-use C# library designed to simplify the implementation of the Command Query Responsibility Segregation (CQRS) pattern.

Just.Cqrs

Inspired by MediatR

Just.Cqrs is a lightweight, easy-to-use C# library designed to simplify the implementation of the Command Query Responsibility Segregation (CQRS) pattern in .NET applications. With a focus on simplicity and flexibility, Just.Cqrs provides a clean and intuitive way to separate command and query logic.

Features

  • Separate dispatching of Commands/Queries
  • Middleware-like Behaviors

Compatibility

Just.Cqrs is built for .Net Standard 2.1 and .NET 8.0 and 9.0.

Getting Started

Install from NuGet.org

# install the package using NuGet
dotnet add package Just.Cqrs

Register in DI with IServiceCollection

services.AddCqrs(opt => opt
    .AddQueryHandler<SomeQueryHandler>()
    .AddCommandHandler<SomeCommandHandler>()
    .AddBehavior<SomeQueryBehavior>()
    .AddOpenBehavior(typeof(LoggingBehavior<,>))
);

Example Usage

Define a Query and Handler

record GetUserByIdQuery(int UserId) : IKnownQuery<User>;

class GetUserByIdQueryHandler : IQueryHandler<GetUserByIdQuery, User>
{
    public ValueTask<User> Handle(GetUserByIdQuery query, CancellationToken cancellationToken)
    {
        // Fetch user logic here
    }
}

// Use Dispatcher to execute the query
class GetUserByIdUseCase(IQueryDispatcher dispatcher)
{
    public async Task<IResult> Execute(int userId, CancellationToken cancellationToken)
    {
        var user = await dispatcher.Dispatch(new GetUserByIdQuery(userId), cancellationToken);
    }
}

* the same principles apply to commands

Define a Behavior

class LoggingBehavior<TRequest, TResult>(ILogger logger) : IDispatchBehavior<TRequest, TResult>
    where TRequest: notnull
{
    public async ValueTask<TResult> Handle(
        TRequest request,
        DispatchFurtherDelegate<TResult> next,
        CancellationToken cancellationToken)
    {
        logger.LogInformation("Handling request: {RequestType}", typeof(TRequest).Name);
        var result = await next();
        logger.LogInformation("Request handled: {RequestType}", typeof(TRequest).Name);
        return result;
    }
}

class SomeQueryBehavior : IDispatchBehavior<SomeQuery, SomeQueryResult>
{
    public async ValueTask<SomeQueryResult> Handle(
        SomeQuery request,
        DispatchFurtherDelegate<SomeQueryResult> next,
        CancellationToken cancellationToken)
    {
        // do something
        return await next();
    }
}

License

Just.Cqrs is licensed under the MIT License.

Dependencies

ID Version Target Framework
Just.Cqrs.Abstractions 1.1.2 .NETStandard2.1
Microsoft.Extensions.DependencyInjection.Abstractions 10.0.0 .NETStandard2.1
Just.Cqrs.Abstractions 1.1.2 net10.0
Microsoft.Extensions.DependencyInjection.Abstractions 10.0.0 net10.0
Just.Cqrs.Abstractions 1.1.2 net8.0
Microsoft.Extensions.DependencyInjection.Abstractions 8.0.2 net8.0
Just.Cqrs.Abstractions 1.1.2 net9.0
Microsoft.Extensions.DependencyInjection.Abstractions 9.0.11 net9.0
Details
NuGet
2025-11-11 23:58:03 +04:00
1
JustFixMe
33 KiB
Assets (2)
Versions (3) View all
1.1.2 2025-11-11
1.1.0 2025-02-04
1.0.0 2025-02-02