MiF.Mediator 1.1.0

dotnet add package MiF.Mediator --version 1.1.0
                    
NuGet\Install-Package MiF.Mediator -Version 1.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MiF.Mediator" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MiF.Mediator" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MiF.Mediator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MiF.Mediator --version 1.1.0
                    
#r "nuget: MiF.Mediator, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MiF.Mediator@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MiF.Mediator&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MiF.Mediator&version=1.1.0
                    
Install as a Cake Tool

MiF.Mediator

MiF.Mediator is a lightweight and extensible .NET library that implements the Mediator design pattern. It provides a clean and decoupled way to handle queries and commands in your application, promoting separation of concerns and reducing dependencies between components.

Features

  • Mediator Pattern: Centralizes the handling of queries and commands to simplify communication between components.
  • Query and Command Handlers: Built-in abstractions for handling queries (IQueryHandler) and commands (ICommandHandler).
  • Asynchronous Support: Fully supports asynchronous operations using Task and async/await.
  • Extensibility: Easily extendable to fit your application's specific needs.
  • Dependency Injection Friendly: Designed to work seamlessly with dependency injection frameworks.

Installation

To use MiF.Mediator in your project, add the library to your solution. Ensure your project targets .NET 9 or higher.

Getting Started

1. Define a Query or Command

A query or command is a simple class that implements the IRequest<TResponse> interface.

public class GetUserQuery : IRequest<User> { public int UserId { get; set; } }

2. Create a Handler

Create a handler by implementing IQueryHandler<TQuery, TResponse> or ICommandHandler<TCommand>.

public class GetUserQueryHandler : IQueryHandler<GetUserQuery, User> { public Task<User> HandleAsync(GetUserQuery query, CancellationToken cancellationToken) { // Simulate fetching a user return Task.FromResult(new User { Id = query.UserId, Name = "John Doe" }); } }

3. Use the Mediator

The Mediator class acts as the central hub for dispatching queries and commands.

var serviceFactory = new MyServiceFactory(); 

// Your implementation of IServiceFactory 
var mediator = new Mediator(serviceFactory);

var query = new GetUserQuery { UserId = 1 }; 
var user = await mediator.SendMessageAsync(query);

Console.WriteLine($"User: {user.Name}");

4. Dependency Injection

Integrate the library with your preferred DI container by registering handlers and the Mediator class.

Example Use Cases

  • CQRS (Command Query Responsibility Segregation): Separate read and write operations in your application.
  • Decoupled Architecture: Reduce dependencies between components by centralizing communication.
  • Asynchronous Processing: Handle long-running operations or external API calls efficiently.
  • Event Sourcing: Use the mediator to handle events and commands in an event-sourced architecture.
  • Unit Testing: Easily mock the mediator and handlers for unit tests.
  • Validation: Implement validation logic in handlers to ensure data integrity before processing commands.

Contributing

Contributions are welcome! Feel free to open issues or submit pull requests to improve the library.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.1.0 242 5/15/2025
1.0.3 125 4/11/2025
1.0.2 184 4/9/2025
1.0.1 355 4/8/2025