Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/SzymonHalucha/Minerals.AutoCQRS
Simple NuGet package that provides interfaces for implementing the CQRS pattern along with automatic dependency injection and no MediatR package overhead
https://github.com/SzymonHalucha/Minerals.AutoCQRS
cqrs cqrs-pattern csharp csharp-sourcegenerator dependency-injection dotnet roslyn
Last synced: 2 days ago
JSON representation
Simple NuGet package that provides interfaces for implementing the CQRS pattern along with automatic dependency injection and no MediatR package overhead
- Host: GitHub
- URL: https://github.com/SzymonHalucha/Minerals.AutoCQRS
- Owner: SzymonHalucha
- License: mit
- Created: 2024-05-18T06:27:16.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2024-06-01T07:01:39.000Z (5 months ago)
- Last Synced: 2024-07-20T11:32:09.030Z (4 months ago)
- Topics: cqrs, cqrs-pattern, csharp, csharp-sourcegenerator, dependency-injection, dotnet, roslyn
- Language: C#
- Homepage:
- Size: 101 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - Minerals.AutoCQRS
README
# Minerals.AutoCQRS
![GitHub License](https://img.shields.io/github/license/SzymonHalucha/Minerals.AutoCQRS?style=for-the-badge)
![NuGet Version](https://img.shields.io/nuget/v/Minerals.AutoCQRS?style=for-the-badge)
![NuGet Downloads](https://img.shields.io/nuget/dt/Minerals.AutoCQRS?style=for-the-badge)[Package on nuget.org](https://www.nuget.org/packages/Minerals.AutoCQRS/)
This NuGet package offers a lightweight and efficient way to implement the CQRS pattern in ASP.NET Core applications. It focuses on providing basic interfaces for commands, queries and pipelines, along with automatic dependency injection, without introducing the overhead of a mediator library such as MediatR.
## Features
- **Reduced boilerplate:** Package has dedicated interfaces for commands and queries, you don't need to write additional intermediate interfaces for code readability.
- **Pipelines:** Package provides easy to use pipelines for concrete commands and queries.
- **Automatic Dependency Injection:** Package provides automatic registration of all classes that implements *ICommandHandler*, *IQueryHandler*, *ICommandPipeline* or *IQueryPipeline* interfaces.
- **Performance:** Package does not use System.Reflection at all, dependency injection is resolved at compile time.
- **Compatible with .NET Standard 2.0 and C# 7.3+:** Works on a wide range of platforms and development environments.## Installation
Add the Minerals.AutoCQRS nuget package to your C# project using the following methods:
### 1. Project file definition
```xml
```
### 2. dotnet command
```bat
dotnet add package Minerals.AutoCQRS
```## Usage
The examples below shows how to use the package and its components.
### ICommandHandler and IQueryHandler
Example implementation of command and query along with their handlers.
```csharp
namespace Minerals.Examples
{
public class ExampleCommand : Minerals.AutoCQRS.Interfaces.ICommand
{
// ...
}public class ExampleQuery : Minerals.AutoCQRS.Interfaces.IQuery
{
// ...
}public class ExampleCommandHandler : Minerals.AutoCQRS.Interfaces.ICommandHandler
{
public Task Handle(ExampleCommand command, CancellationToken cancellation = default)
{
// ...
}
}public class ExampleQueryHandler : Minerals.AutoCQRS.Interfaces.IQueryHandler
{
public Task Handle(ExampleQuery query, CancellationToken cancellation = default)
{
// ...
}
}
}
```### Dependency Registration
By default, dependencies are registered as Singleton.
```csharp
// Adding Handlers
builder.Services.AddAutoCQRSHandlers();// Adding Pipelines
builder.Services.AddAutoCQRSPipelines();// or
// You can set your own policy for dependency injection
builder.Services.AddAutoCQRSHandlers((collection, serviceInterfaceType, serviceConcreteType) => collection.AddScoped(serviceInterfaceType, serviceConcreteType));builder.Services.AddAutoCQRSPipelines((collection, serviceInterfaceType, serviceConcreteType) => collection.AddScoped(serviceInterfaceType, serviceConcreteType));
```
### Pipelines
Below is an example of a pipeline configuration for a command with three handlers. A Class which implements the ``ICommandPipeline`` or ``IQueryPipeline`` interface must have the ``partial`` modifier.
```csharp
public class TestCommand : ICommand
{
// ...
}public class TestCommandHandler1 : ICommandHandler
{
// ...
}public class TestCommandHandler2 : ICommandHandler
{
// ...
}public class TestCommandHandler3 : ICommandHandler
{
// ...
}// That's all you need to create a pipeline for the selected command or query
// Pipelines interfaces have overloads up to 8 handlers
public partial class TestCommandPipeline : ICommandPipeline;
```### Dispatching
If one command or query has multiple handlers the last handler will be executed and returned.
```csharp
// Dispatching selected command (ICommandDispatcher)
string result = await _commandDispatcher.Dispatch(new TestCommand());// Dispatching selected query (IQueryDispatcher)
string result = await _queryDispatcher.Dispatch(new TestQuery());// Dispatching selected command pipeline (ICommandPipelineDispatcher)
IReadOnlyList results = await _commandPipelineDispatcher.Dispatch(new TestCommand());// Dispatching selected query pipeline (IQueryPipelineDispatcher)
IReadOnlyList results = await _queryPipelineDispatcher.Dispatch(new TestQuery());
```### Custom Dispatchers
You can write your own implementation of a command, query or pipeline dispatcher using the appropriate interface, ``ICommandDispatcher``, ``IQueryDispatcher``, ``ICommandPipelineDispatcher`` or ``IQueryPipelineDispatcher``.
```csharp
public class CustomCommandDispatcher : ICommandDispatcher
{
public Task Dispatch(TCommand command, CancellationToken cancellation = default)
where TCommand : ICommand, new()
where TResult : notnull
{
// ...
}
}// or
public class CommandPipelineDispatcher : ICommandPipelineDispatcher
{
public async Task> Dispatch(TCommand command, CancellationToken cancellation = default)
where TCommand : ICommand, new()
where TResult : notnull
{
// ...
}
}
```## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [branches on this repository](https://github.com/SzymonHalucha/Minerals.AutoCQRS/branches).
## Authors
- **Szymon Hałucha** - Maintainer
See also the list of [contributors](https://github.com/SzymonHalucha/Minerals.AutoCQRS/contributors) who participated in this project.
## License
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.