# Just.Cqrs Inspired by [MediatR](https://github.com/jbogard/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 ```bash # install the package using NuGet dotnet add package Just.Cqrs ``` ### Register in DI with ```IServiceCollection``` ```csharp services.AddCqrs(opt => opt .AddQueryHandler() .AddCommandHandler() .AddBehavior() .AddOpenBehavior(typeof(LoggingBehavior<,>)) ); ``` ## Example Usage ### Define a Query and Handler ```csharp record GetUserByIdQuery(int UserId) : IKnownQuery; class GetUserByIdQueryHandler : IQueryHandler { public ValueTask Handle(GetUserByIdQuery query, CancellationToken cancellationToken) { // Fetch user logic here } } // Use Dispatcher to execute the query class GetUserByIdUseCase(IQueryDispatcher dispatcher) { public async Task Execute(int userId, CancellationToken cancellationToken) { var user = await dispatcher.Dispatch(new GetUserByIdQuery(userId), cancellationToken); } } ``` \* *the same principles apply to commands* ### Define a Behavior ```csharp class LoggingBehavior(ILogger logger) : IDispatchBehavior where TRequest: notnull { public async ValueTask Handle( TRequest request, DispatchFurtherDelegate 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 { public async ValueTask Handle( SomeQuery request, DispatchFurtherDelegate next, CancellationToken cancellationToken) { // do something return await next(); } } ``` ## License **Just.Cqrs** is licensed under the [MIT License](LICENSE).