This repository includes projects containing abstractions and implementations of the mediator framework.
All artifacts are licensed under the MIT license. You are free to use them in open-source or commercial projects as long as you keep the copyright notice intact when redistributing or otherwise reusing our artifacts.
Latest development packages can be found on MyGet.
| Package | Description |
|---|---|
AppCoreNet.Mediator | Provides mediator framework default implementations. |
AppCoreNet.Mediator.Abstractions | Provides the public API of the mediator framework. |
AppCoreNet.Mediator.Authentication | Adds support for request authentication. |
AppCoreNet.Mediator.Authentication.Abstractions | Provides the public API for request authentication. |
This section explains how to use the AppCore .NET Mediator framework in your project.
To get started with AppCore .NET Mediator, you'll need to install the necessary NuGet packages.
The primary package to get started is AppCoreNet.Mediator. If you are building abstractions or extensions that
only depend on the mediator's contracts, you might only need AppCoreNet.Mediator.Abstractions.
Using .NET CLI:
dotnet add package AppCoreNet.Mediatoror for just the abstractions:
dotnet add package AppCoreNet.Mediator.AbstractionsTo use the mediator, you first need to register it in your application's service container. You can do this using
the AddMediator() extension method on IServiceCollection.
Example:
usingMicrosoft.Extensions.DependencyInjection;usingAppCoreNet.Extensions.DependencyInjection;// Required for AddMediator()publicclassStartup{publicvoidConfigureServices(IServiceCollectionservices){// ... other service registrationsservices.AddMediator().AddRequestHandlersFrom(f =>f.Assembly(typeof(Startup).Assembly)).AddNotificationHandlersFrom(f =>f.Assembly(typeof(Startup).Assembly));});// ...}}Commands (or Requests) are used to perform actions and typically have a single handler.
A command is a class that implements the IRequest<TResponse> interface, where TResponse is the type of the
response the command will return. If a command does not return a value, you can use VoidResponse.
Example:
usingAppCoreNet.Mediator;// Command that returns a string responsepublicclassMyCommand:IRequest<string>{publicstringInputData{get;}publicMyCommand(stringinputData){InputData=inputData;}}// Command that does not return a valuepublicclassMyParameterlessCommand:IRequest{}A command handler is a class that implements the IRequestHandler<TRequest, TResponse> interface.
Example:
usingAppCoreNet.Mediator;usingSystem.Threading;usingSystem.Threading.Tasks;publicclassMyCommandHandler:IRequestHandler<MyCommand,string>{publicTask<string>HandleAsync(MyCommandrequest,CancellationTokencancellationToken){// Process the commandstringresult=$"Processed: {request.InputData}";returnTask.FromResult(result);}}publicclassMyParameterlessCommandHandler:IRequestHandler<MyParameterlessCommand,VoidResponse>{publicTask<VoidResponse>HandleAsync(MyParameterlessCommandrequest,CancellationTokencancellationToken){// Process the commandSystem.Console.WriteLine("MyParameterlessCommand handled");returnVoidResponse.Task;}}Note: Command handlers are automatically discovered and registered if they are in an assembly registered via
builder.AddHandlersFrom(...) during mediator setup. Otherwise, they need to be registered manually in the DI
container (e.g., builder.AddRequestHandler<MyCommandHandler>();).
To send a command, inject IMediator and use the ProcessAsync method.
Example:
usingAppCoreNet.Mediator;usingSystem.Threading.Tasks;publicclassMyService{privatereadonlyIMediator_mediator;publicMyService(IMediatormediator){_mediator=mediator;}publicasyncTaskDoSomethingAsync(stringdata){varcommand=newMyCommand(data);stringresponse=await_mediator.ProcessAsync(command);// Use the responseSystem.Console.WriteLine(response);// Send a command with no responseawait_mediator.ProcessAsync(newMyParameterlessCommand());}}Notifications are used to inform other parts of the application about an event that has occurred. A notification can have multiple handlers.
A notification is a class that implements the INotification interface.
Example:
usingAppCoreNet.Mediator;publicclassMyNotification:INotification{publicstringMessage{get;}publicMyNotification(stringmessage){Message=message;}}A notification handler is a class that implements the INotificationHandler<TNotification> interface.
Example:
usingAppCoreNet.Mediator;usingSystem.Threading;usingSystem.Threading.Tasks;publicclassMyNotificationHandler1:INotificationHandler<MyNotification>{publicTaskHandleAsync(MyNotificationnotification,CancellationTokencancellationToken){// Handle the notificationSystem.Console.WriteLine($"Handler 1 received: {notification.Message}");returnTask.CompletedTask;}}publicclassMyNotificationHandler2:INotificationHandler<MyNotification>{publicTaskHandleAsync(MyNotificationnotification,CancellationTokencancellationToken){// Handle the notificationSystem.Console.WriteLine($"Handler 2 received: {notification.Message}");returnTask.CompletedTask;}}Note: Notification handlers are automatically discovered and registered if they are in an assembly registered via
builder.AddNotificationHandlersFrom(...) during mediator setup. Otherwise, they need to be registered manually in
the DI container (e.g., builder.AddNotificationHandler<MyNotificationHandler1>();).
To publish a notification, inject IMediator and use the PublishAsync method.
Example:
usingAppCoreNet.Mediator;usingSystem.Threading.Tasks;publicclassMyOtherService{privatereadonlyIMediator_mediator;publicMyOtherService(IMediatormediator){_mediator=mediator;}publicasyncTaskNotifySomethingAsync(stringeventMessage){varnotification=newMyNotification(eventMessage);await_mediator.PublishAsync(notification);}}Contributions, whether you file an issue, fix some bug or implement a new feature, are highly appreciated. The whole user community will benefit from them.
Please refer to the Contribution guide.