An open API service indexing awesome lists of open source software.

https://github.com/Prognetics/Prognetics.CQRS

We would like to share with the community our companies production verified mediator pattern based CQRS library.
https://github.com/Prognetics/Prognetics.CQRS

cqrs csharp dotnet

Last synced: over 1 year ago
JSON representation

We would like to share with the community our companies production verified mediator pattern based CQRS library.

Awesome Lists containing this project

README

          

# Welcome to Prognetics CQRS!

We would like to share with the community our companies production verified mediator pattern based CQRS library.

## **How it works?**

Throught the Mediator object you are able to issue different kinds of handlers to be called.

First are commands:
```c#
void Send(TCommand command) where TCommand : ICommand;

Task SendAsync(TCommand command) where TCommand : ICommand;
```
They allow you to run a logic that is supposed to modify the state of the application. They do not return a value.

Second are queries:
```c#
TResult Fetch(TQuery query) where TQuery : IQuery;

Task FetchAsync(TQuery query) where TQuery : IQuery;
```
They are used to return values, and should not perform any state modification operations.

Third are events:
```c#
Task Publish(TEvent @event) where TEvent : IEvent;
```
While each query or command can only have a single synchronous and asynchronous handler, this is not the case for events. When an event is published, the mediator will run all the handlers that are registered to handle it.

## **Setup**
We prepared two integrations for your comfort. Depending on which DI continer you would like to use, please pick one of them or download base 'Prognetics.CQRS' package and implement your own integration!

The library `Prognetics.CQRS.Autofac` provides an easy-to-use extension for integration with Autofac. To register handler implementations, it requires a collection of assemblies to be scanned:

```c#
builder.RegisterProgenticsCQRSModule(Assembly.GetExecutingAssembly());
```

The framework can also be integreted with Microsoft Dependency Injection. Install `Prognetics.CQRS.MicrosoftDI` and add to ths service collection:

```c#
services.AddProgneticsCQRS(Assembly.GetExecutingAssembly());
```

**However, unlike Autofac, it does not support the use of generic handlers, so these will be skipped if they are defined.**

The library can be used with other/without dependency injection. Install `Prognetics.CQRS` package and define the way of resolving handlers in your application by implementing the `Prognetics.CQRS.IHandlerResolver` interface and pass the implementation to `Prognetics.CQRS.Mediator`. The mediator will then be ready to work.

## **Defining Queries, Commands and Events**

### **Query**
The query must implement the `IQuery` interface, where TResult is the type of the expected result:

```c#
public record MyQuery(string Data) : IQuery
```

You can implement the query handler in two ways:

- **Synchronously** by implementing the `IQueryHandler` interface

```c#
public class MyQueryHandler : IQueryHandler
{
public string Handle(MyQuery query)
{
// Process the query
}
}
```

- **Asynchronously** by implementing the `IAsyncQueryHandler` interface

```c#
public class MyQueryHandler : IAsyncQueryHandler
{
public async Task Handle(MyQuery query)
{
// Process the query
// await sth
}
}
```

where `TQuery` is the type of your query and `TResult` is the type of the result specified in the `IQuery` interface.

### **Command**
The command must implement the `ICommand` interface:

```c#
public record MyCommand(string Data) : ICommand
```

Command handler can be:
- **Synchronous** by implementing the `ICommandHandler` interface

```c#
public class SumCommandHandler : ICommandHandler
{
public void Handle(MyCommand command)
{
// Process the command
}
}
```

- **Asynchronously** by implementing the `IAsyncCommandHandler` interface

```c#
public class SumCommandHandler : IAsyncCommandHandler
{
public async Task Handle(MyCommand command)
{
// Process the command
// await sth
}
}
```
where `TCommand` is the type of your command

**NOTE: You can define both an asynchronous and synchronous handler for the same query or command**

### **Event**
The event must implement the `IEvent` interface.

```c#
public record MyEvent(string Data) : IEvent
```

Event handler can must be defined by implementing the `IEventHandler` interface:
```c#
public class MyEventHandler : IEventHandler
{
public async Task Handle(MyEvent @event)
{
// Process the event
// await sth
}
}
```
where `TEvent` is the type of your event.

## Running Tests

The tests project is based on xunit with visual studio runner. To run right-click the project file and select "Run Tests".