Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leanwit/dotnet-cqrs
A CQRS Project using a DDD structure without MediatR library
https://github.com/leanwit/dotnet-cqrs
cqrs csharp dotnet-cqrs
Last synced: 2 months ago
JSON representation
A CQRS Project using a DDD structure without MediatR library
- Host: GitHub
- URL: https://github.com/leanwit/dotnet-cqrs
- Owner: Leanwit
- Created: 2020-05-03T23:57:12.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-31T12:43:02.000Z (about 2 years ago)
- Last Synced: 2023-08-04T09:06:44.273Z (over 1 year ago)
- Topics: cqrs, csharp, dotnet-cqrs
- Language: C#
- Homepage:
- Size: 62.5 KB
- Stars: 12
- Watchers: 6
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# dotnet-cqrs
This project shows a clean way to use CQRS without using the MediatR library.
In C# is common to use a library named [MediatR](https://github.com/jbogard/MediatR) to implement CQRS. This is an amazing library but forces you to implement the interface INotification, INotificationHandler and IRequestHandler in your domain/application layer coupling this with an infrastructure library.
This is a different approach to avoid add this coupling.## Commands
### A command example
```csharp
public class CreateItemCommand : Command
{
public CreateItemCommand(Guid id, string name)
{
Id = id;
Name = name;
}
}
```### A handler example
```csharp
public class CreateItemCommandHandler : CommandHandler
{
private readonly ItemRepository _repository;public CreateItemCommandHandler(ItemRepository repository)
{
_repository = repository;
}public async Task Handle(CreateItemCommand command)
{
await _repository.Add(new Item(command.Id, command.Name));
}
}
```### Interfaces to add in Domain Layer
[Command Interfaces](https://github.com/Leanwit/dotnet-cqrs/tree/master/Src/CQRS.Shared/Domain/Bus/Command)
## Queries
### A query example:
```csharp
public class FindItemQuery : Query
{
public Guid Id { get; private set; }public FindItemQuery(Guid id)
{
Id = id;
}
}
```### A handler example
```csharp
public class FindItemQueryHandler : QueryHandler
{
private readonly ItemRepository _repository;public FindItemQueryHandler(ItemRepository repository)
{
_repository = repository;
}public async Task Handle(FindItemQuery query)
{
Item item = await _repository.GetById(query.Id);
return new ItemResponse(item.Id, item.Name, item.IsCompleted);
}
}
```### Interfaces to add in Domain Layer
[Query Interfaces](https://github.com/Leanwit/dotnet-cqrs/tree/master/Src/CQRS.Shared/Domain/Bus/Query)
## InMemoryBus implementation
[InMemoryCommandBus](https://github.com/Leanwit/dotnet-cqrs/blob/master/Src/CQRS.Shared/Infrastructure/Bus/Command/InMemoryCommandBus.cs)
[InMemoryQueryBus](https://github.com/Leanwit/dotnet-cqrs/blob/master/Src/CQRS.Shared/Infrastructure/Bus/Query/InMemoryQueryBus.cs)
## Dependency Injection
### Command
```csharp
services.AddScoped, CreateItemCommandHandler>();
```### Query
```csharp
services.AddScoped, FindItemQueryHandler>();
```### Automatic Load
```csharp
services.AddCommandServices(typeof(Command).Assembly);
services.AddQueryServices(typeof(Query).Assembly);
```