Skip to content

Repository files navigation

Mediator

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.

Articles

Installation

The library is available via NuGet packages:

NuGetDescriptionVersion
SimpleSoft.Mediator.Abstractionsinterfaces and abstract implementations (commands, events, queries, mediator, ...)NuGet
SimpleSoft.Mediatorcore implementationNuGet
SimpleSoft.Mediator.Microsoft.Extensionsspecialized methods and classes for the Microsoft dependency injection container and logging facadesNuGet
SimpleSoft.Mediator.Microsoft.Extensions.DatabaseTransactionPipelinemediator pipeline to enforce SimpleSoft.Database transactionsNuGet
SimpleSoft.Mediator.Microsoft.Extensions.EFCoreTransactionPipelinemediator pipeline to enforce Entity Framework Core transactionsNuGet
SimpleSoft.Mediator.Microsoft.Extensions.LoggingPipelinepipeline that serializes commands, queries, events and results into the loggingNuGet
SimpleSoft.Mediator.Microsoft.Extensions.ValidationPipelinepipeline that enforces validation into commands, queries and events before entering the handlers by using FluentValidationNuGet

Package Manager

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

.NET CLI

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

Compatibility

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+;

Usage

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:

publicclassCreateUserCommand:Command{publicstringEmail{get;set;}publicstringPassword{get;set;}}publicclassUserCreatedEvent:Event{publicUserUser{get;set;}}publicclassUserByIdQuery:Query<User>{publicGuidUserId{get;set;}}publicclassUser{publicGuidId{get;set;}publicstringEmail{get;set;}}publicclassExampleHandlers:ICommandHandler<CreateUserCommand>,IQueryHandler<UserByIdQuery,User>{privatereadonlyIMediator_mediator;publicUsersService(IMediatormediator){_mediator=mediator;}publicasyncTaskHandleAsync(CreateUserCommandcmd,CancellationTokenct){varuserId=Guid.NewGuid();// try add the user to some storeawait_mediator.BroadcastAsync(newUserCreatedEvent{User=newUser{Id=userId,Email=cmd.Email}},ct);}publicasyncTask<User>HandleAsync(UserByIdQueryquery,CancellationTokenct){Useruser=null;// search the store by user idreturnuser;}}

About

Small .NET library that helps with the implementation of mediator pattern for commands, events and queries

Topics

Resources

Stars

41 stars

Watchers

6 watching

Forks

Releases

Packages

Used by

Contributors

Languages