Medianiz 2.2.1
dotnet add package Medianiz --version 2.2.1
NuGet\Install-Package Medianiz -Version 2.2.1
<PackageReference Include="Medianiz" Version="2.2.1" />
<PackageVersion Include="Medianiz" Version="2.2.1" />
<PackageReference Include="Medianiz" />
paket add Medianiz --version 2.2.1
#r "nuget: Medianiz, 2.2.1"
#:package Medianiz@2.2.1
#addin nuget:?package=Medianiz&version=2.2.1
#tool nuget:?package=Medianiz&version=2.2.1
Medianiz
A lightweight implementation of the mediator pattern for .NET applications, designed to simplify in-process messaging.
Release Versions
- 2.2.1: Fixed handler discovery for
services.AddMedianiz(config => config.RegisterHandlersFromAssembly(typeof(Program)))in WebAPI/DDD solutions. Medianiz now scans the non-framework assemblies referenced by the marker assembly, so handlers from layers such as Application are registered automatically. - 2.2.0: Some libraries were updated and RegisterHandlersFromAssembly was implemented.
- 2.1.0: Some libraries were updated and Medianiz has implementation of Pipeline Behaviors.
Pipeline Behaviors
Medianiz supports Pipeline Behaviors, allowing cross-cutting logic to be executed before and/or after a request handler, following the same model used by MediatR’s IPipelineBehavior.
This feature is useful for implementing concerns such as:
- Logging
- Validation
- Transactions
- Metrics
- Exception handling
Concept
A Pipeline Behavior wraps the execution of a request handler and can:
- Execute code before the handler
- Execute code after the handler
- Stop execution by throwing exceptions
- Decorate or modify the response
Execution is performed as a chained pipeline.
IPipelineBehavior Interface
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>();
public interface IPipelineBehavior<in TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
Task<TResponse> Handle(
TRequest request,
RequestHandlerDelegate<TResponse> next,
CancellationToken cancellationToken
);
}
DOCUMENTATION
Key Features
- Simple request/response handling
- Event publishing to multiple handlers
- First-class async/await support
- CancellationToken integration
- Minimal dependencies
- Works with any DI container
Installation
dotnet add package Medianiz
Basic Usage
1. Configure Services
// In Startup.cs or Program.cs
builder.Services.AddMedianiz(typeof(MyCommandHandler));
For multi-project solutions:
// Reference assemblies by project name
builder.Services.AddMedianizFromAssemblies("MyApp.Application");
2. Define Commands and Handlers
Command:
public record CreateUserCommand(string Email) : IRequest<Guid>;
Handler:
public class CreateUserHandler : IRequestHandler<CreateUserCommand, Guid>
{
public Task<Guid> Handle(CreateUserCommand request, CancellationToken ct)
{
// Business logic here
return Task.FromResult(Guid.NewGuid());
}
}
3. Define Events and Handlers
Event:
public record UserCreatedEvent(Guid UserId) : INotification;
Handler:
public class UserCreatedHandler : INotificationHandler<UserCreatedEvent>
{
public Task Handle(UserCreatedEvent notification, CancellationToken ct)
{
// React to the event
return Task.CompletedTask;
}
}
4. Use in ASP.NET Core
Controller Example:
[ApiController]
[Route("users")]
public class UsersController : ControllerBase
{
private readonly IMedianiz _mediator;
public UsersController(IMedianiz mediator)
{
_mediator = mediator;
}
[HttpPost]
public async Task<IActionResult> CreateUser([FromBody] CreateUserCommand command)
{
var userId = await _mediator.Send(command);
return CreatedAtAction(nameof(GetUser), new { id = userId });
}
}
Advanced Scenarios
Domain Events
public class Order : Entity
{
public void Complete()
{
AddDomainEvent(new OrderCompletedEvent(Id));
}
}
// Publish events after saving
var order = await _repository.GetOrderAsync();
order.Complete();
await _mediator.PublishDomainEvents(order);
Working with Multiple Assemblies
// Create a marker interface in each project
public interface IApplicationMarker { }
// Then register all needed assemblies
builder.Services.AddMedianiz(
typeof(Application.Marker),
typeof(Domain.Marker),
typeof(Infrastructure.Marker));
Configuration Options
Register Specific Handlers Only
services.AddMedianiz(config =>
{
config.RegisterHandlersFromAssembly(typeof(Program));
config.RegisterHandler<SpecialHandler>(); // Manual registration
});
When you use a Web API marker such as typeof(Program), Medianiz also scans the non-framework assemblies referenced by that project so handlers in your Application layer are discovered automatically.
Example in a DDD solution:
// Civika.API
services.AddMedianiz(config =>
{
config.RegisterHandlersFromAssembly(typeof(Program));
});
// QueryDesempenhoMunicipioCommandHandler can live in Civika.Application
// and will still be registered automatically if Civika.API references it.
Lifecycle Management
// Change handler lifetime (default is Transient)
services.AddMedianiz(typeof(Startup))
.WithHandlerLifetime(ServiceLifetime.Scoped);
Performance Tips
- Assembly Filtering: Only scan necessary assemblies
- Marker Interfaces: Use them to limit handler discovery
- Lifetime Management: Use Scoped for DB-heavy handlers
Contributing
Pull requests are welcome. For major changes, please open an issue first.
License
GPL 3.0 - See LICENSE for details.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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 is compatible. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.CSharp (>= 4.7.0)
- Microsoft.Extensions.DependencyInjection (>= 10.0.10)
-
net10.0
- Microsoft.Extensions.DependencyInjection (>= 10.0.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.