dihon.SimpleMediator
1.0.0
dotnet add package dihon.SimpleMediator --version 1.0.0
NuGet\Install-Package dihon.SimpleMediator -Version 1.0.0
<PackageReference Include="dihon.SimpleMediator" Version="1.0.0" />
<PackageVersion Include="dihon.SimpleMediator" Version="1.0.0" />
<PackageReference Include="dihon.SimpleMediator" />
paket add dihon.SimpleMediator --version 1.0.0
#r "nuget: dihon.SimpleMediator, 1.0.0"
#:package dihon.SimpleMediator@1.0.0
#addin nuget:?package=dihon.SimpleMediator&version=1.0.0
#tool nuget:?package=dihon.SimpleMediator&version=1.0.0
SimpleMediator
A lightweight, easy-to-use mediator pattern implementation for .NET applications. Perfect for implementing CQRS patterns without the complexity.
Features
- ✅ Simple and lightweight
- ✅ No external dependencies (except Microsoft.Extensions.DependencyInjection.Abstractions)
- ✅ Support for request/response patterns
- ✅ Support for commands without return values
- ✅ Automatic handler discovery and registration
- ✅ Full async/await support
- ✅ .NET 8.0+ compatible
Installation
dotnet add package SimpleMediator
Or via Package Manager:
Install-Package SimpleMediator
Quick Start
1. Define a Query/Command
// Query with response
public class GetUserByIdQuery : IRequest<UserDto>
{
public int Id { get; set; }
}
// Command without response
public class DeleteUserCommand : IRequest
{
public int Id { get; set; }
}
2. Create Handlers
public class GetUserByIdQueryHandler : IRequestHandler<GetUserByIdQuery, UserDto>
{
private readonly IUserRepository _repository;
public GetUserByIdQueryHandler(IUserRepository repository)
{
_repository = repository;
}
public async Task<UserDto> Handle(GetUserByIdQuery request, CancellationToken cancellationToken)
{
var user = await _repository.GetByIdAsync(request.Id);
return new UserDto { Id = user.Id, Name = user.Name };
}
}
public class DeleteUserCommandHandler : IRequestHandler<DeleteUserCommand>
{
private readonly IUserRepository _repository;
public DeleteUserCommandHandler(IUserRepository repository)
{
_repository = repository;
}
public async Task<Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
{
await _repository.DeleteAsync(request.Id);
return Unit.Value;
}
}
3. Register Services
// In Program.cs or Startup.cs
builder.Services.AddSimpleMediator(typeof(GetUserByIdQuery).Assembly);
4. Use in Controllers
[ApiController]
[Route("api/[controller]")]
public class UsersController : ControllerBase
{
private readonly IMediator _mediator;
public UsersController(IMediator mediator)
{
_mediator = mediator;
}
[HttpGet("{id}")]
public async Task<ActionResult<UserDto>> Get(int id)
{
var result = await _mediator.Send(new GetUserByIdQuery { Id = id });
return Ok(result);
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _mediator.Send(new DeleteUserCommand { Id = id });
return NoContent();
}
}
Advanced Usage
Scanning Multiple Assemblies
services.AddSimpleMediator(
typeof(ApplicationLayer).Assembly,
typeof(AnotherAssembly).Assembly
);
Command Pattern (No Return Value)
For commands that don't return a value, implement IRequest (without type parameter):
public class SendEmailCommand : IRequest
{
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
public class SendEmailCommandHandler : IRequestHandler<SendEmailCommand>
{
public async Task<Unit> Handle(SendEmailCommand request, CancellationToken cancellationToken)
{
// Send email logic
await _emailService.SendAsync(request.To, request.Subject, request.Body);
return Unit.Value;
}
}
Best Practices
- Keep handlers focused - Each handler should do one thing
- Use meaningful names - GetUserByIdQuery vs GetUserQuery
- Validate in handlers - Add validation logic in your handlers
- Use cancellation tokens - Pass them through to async operations
- Register scoped - Handlers are registered as scoped by default
Performance
SimpleMediator uses:
- Reflection caching for handler discovery
- Concurrent dictionaries for type caching
- Minimal overhead compared to direct method calls
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Perfect for projects that need basic CQRS without the complexity!
| Product | Versions 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. |
-
net9.0
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 |
|---|