Skip to content

Repository files navigation

AppCore .NET Mediator

NugetMyGet

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.

Packages

Latest development packages can be found on MyGet.

PackageDescription
AppCoreNet.MediatorProvides mediator framework default implementations.
AppCoreNet.Mediator.AbstractionsProvides the public API of the mediator framework.
AppCoreNet.Mediator.AuthenticationAdds support for request authentication.
AppCoreNet.Mediator.Authentication.AbstractionsProvides the public API for request authentication.

Usage

This section explains how to use the AppCore .NET Mediator framework in your project.

Installation / Getting Started

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.Mediator

or for just the abstractions:

dotnet add package AppCoreNet.Mediator.Abstractions

Registration

To 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 (Requests)

Commands (or Requests) are used to perform actions and typically have a single handler.

Defining a Command

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{}

Defining a Command Handler

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>();).

Sending a Command

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

Notifications are used to inform other parts of the application about an event that has occurred. A notification can have multiple handlers.

Defining a Notification

A notification is a class that implements the INotification interface.

Example:

usingAppCoreNet.Mediator;publicclassMyNotification:INotification{publicstringMessage{get;}publicMyNotification(stringmessage){Message=message;}}

Defining a Notification Handler

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>();).

Publishing a Notification

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);}}

Contributing

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.

About

Ambitious mediator implmentation for .NET

Topics

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages