Title: Allow configuring handler service lifetime (Scoped, Transient, Singleton)
Labels: enhancement, mediator
Body:
Problem
In ServiceCollectionExtensions.cs, all handlers (ICommandHandler, IQueryHandler, INotificationHandler, IStreamQueryHandler) are registered with WithScopedLifetime() hardcoded. There is no way to configure this.
Some architectures prefer:
- Transient handlers when handlers are stateless and scope leaks are a concern
- Singleton handlers for high-throughput scenarios with stateless handlers
Proposed Solution
Add a HandlerLifetime property to MediatorOptions:
public class MediatorOptions
{
public ServiceLifetime HandlerLifetime { get; set; } = ServiceLifetime.Scoped;
// ...existing properties...
}
Use this in RegisterHandlers instead of hardcoded .WithScopedLifetime().
Title: Allow configuring handler service lifetime (Scoped, Transient, Singleton)
Labels:
enhancement,mediatorBody:
Problem
In
ServiceCollectionExtensions.cs, all handlers (ICommandHandler,IQueryHandler,INotificationHandler,IStreamQueryHandler) are registered withWithScopedLifetime()hardcoded. There is no way to configure this.Some architectures prefer:
Proposed Solution
Add a
HandlerLifetimeproperty toMediatorOptions:Use this in
RegisterHandlersinstead of hardcoded.WithScopedLifetime().