Small .NET library that helps with the implementation of mediator pattern for commands, events and queries.
Using a mediator instance, send commands, broadcast events and fetch queries from their respective generic handlers.
- Introduction to the mediator pattern in ASP.NET Core applications
- Using Mediator Pipelines in ASP.NET Core Applications
- Validation with Mediator Pipelines in ASP.NET Core Applications
- Transaction Management with Mediator Pipelines in ASP.NET Core
- Auditing with Mediator Pipelines in ASP.NET Core
The library is available via NuGet packages:
NuGet | Description | Version |
---|---|---|
SimpleSoft.Mediator.Abstractions | interfaces and abstract implementations (commands, events, queries, mediator, ...) | |
SimpleSoft.Mediator | core implementation | |
SimpleSoft.Mediator.Microsoft.Extensions | specialized methods and classes for the Microsoft dependency injection container and logging facades | |
SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline | mediator pipeline to enforce SimpleSoft.Database transactions | |
SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline | mediator pipeline to enforce Entity Framework Core transactions | |
SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline | pipeline that serializes commands, queries, events and results into the logging | |
SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline | pipeline that enforces validation into commands, queries and events before entering the handlers by using FluentValidation |
Install-Package SimpleSoft.Mediator.Abstractions
Install-Package SimpleSoft.Mediator
Install-Package SimpleSoft.Mediator.Microsoft.Extensions
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline
Install-Package SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline
dotnet add package SimpleSoft.Mediator.Abstractions
dotnet add package SimpleSoft.Mediator
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline
dotnet add package SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline
This library is compatible with the following frameworks:
SimpleSoft.Mediator.Abstractions
- .NET Framework 4.0+;
- .NET Standard 1.0+;
SimpleSoft.Mediator
- .NET Framework 4.0+;
- .NET Standard 1.0+;
SimpleSoft.Mediator.Microsoft.Extensions
- .NET Standard 1.1+;
SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipeline
- .NET Standard 1.1+;
SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipeline
- .NET Standard 1.3+;
SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipeline
- .NET Standard 1.1+;
SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipeline
- .NET Standard 1.1+;
Documentation is available via wiki or you can check the working examples or test code.
Here is an example of a command handler that also sends some events:
public class CreateUserCommand : Command {
public string Email { get; set; }
public string Password { get; set; }
}
public class UserCreatedEvent : Event {
public User User { get; set; }
}
public class UserByIdQuery : Query<User> {
public Guid UserId { get; set; }
}
public class User {
public Guid Id { get; set; }
public string Email { get; set; }
}
public class ExampleHandlers : ICommandHandler<CreateUserCommand>, IQueryHandler<UserByIdQuery,User> {
private readonly IMediator _mediator;
public UsersService(IMediator mediator) {
_mediator = mediator;
}
public async Task HandleAsync(CreateUserCommand cmd, CancellationToken ct) {
var userId = Guid.NewGuid();
// try add the user to some store
await _mediator.BroadcastAsync(new UserCreatedEvent {
User = new User {
Id = userId,
Email = cmd.Email
}
}, ct);
}
public async Task<User> HandleAsync(UserByIdQuery query, CancellationToken ct) {
User user = null;
// search the store by user id
return user;
}
}