-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: RegisterMediator helper to easily register mediator
- Loading branch information
Showing
7 changed files
with
88 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,42 @@ | ||
# Mediator | ||
|
||
This project contains a simple implementation of the common mediator pattern. The implementation focused on simplifying version 11.1.0 of the MediatR package. The pipelining was removed and a pure mediator implementation remains. | ||
This project contains a simple implementation of the common mediator pattern. The implementation focused on simplifying version 11.1.0 of the MediatR package. The pipelining was removed (SimpleInjector has a powerful built-in decorator feature) and a pure mediator implementation based on SimleInjector remains. | ||
|
||
`ICommand`, `IQuery`: Use those instead of directly using the `IRequest` interfaces to clearly distinguish between commands and queries. | ||
`OutOfBandAttribute`: You can put this on your `Handler`-class. When used together with the [Hangfire-Decorator](../Hangfire/README.md), the execution of the handler will be queued as a background job automatically. | ||
|
||
# Registration | ||
|
||
To register the mediator as well as your customer command/notification-handlers, use the following extenion where you can provide all the assemblies you want to scan for handlers. | ||
|
||
```cs | ||
container.RegisterMediator(services, [ typeof(Program).Assembly ]); | ||
``` | ||
|
||
## Mediator transaction handling | ||
|
||
There are decorators to run all requests and notifications within a transaction. | ||
To enable this feature use the following SimpleInjector-Configuration: | ||
When using the `RegisterMediator` method from above, all requests and notifications will run within a TransactionScope. | ||
To disable this feature, configure as following: | ||
|
||
```cs | ||
Container.RegisterDecorator(typeof(IRequestHandler<,>), typeof(TransactionalRequestHandlerDecorator<,>)); | ||
Container.RegisterDecorator(typeof(INotificationHandler<>), typeof(TransactionNotificationHandlerDecorator<>)); | ||
Container.RegisterSingleton<ITransactionScopeHandler, TransactionScopeHandler>(); | ||
container.RegisterMediator(services, [ typeof(Program).Assembly ], configure => | ||
{ | ||
EnableTransactionalDecorators = false; | ||
}); | ||
``` | ||
|
||
## Custom IMediator implementation | ||
|
||
When using the `RegisterMediator` method from above, our default SimpleInjectorMediator will be registered. | ||
If you want to use your custom mediator, configure as following: | ||
|
||
```cs | ||
|
||
container.Options.AllowOverridingRegistrations = true; | ||
container.RegisterSingleton(typeof(IMediator), typeof(MyCustomMediator)); | ||
|
||
class MyCustomMediator : IMediator | ||
{ | ||
... | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Fusonic GmbH. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
|
||
using System.Reflection; | ||
using Fusonic.Extensions.Common.Transactions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator; | ||
|
||
public static class ContainerExtensions | ||
{ | ||
/// <summary> | ||
/// Registers all <see cref="IRequestHandler{TRequest, TResponse}"/> and <see cref="INotificationHandler{TNotification}"/> in the given <paramref name="assemblies"/> collection. | ||
/// Also registers the <see cref="SimpleInjectorMediator"/> as the mediator implementation for <see cref="IMediator"/>. | ||
/// </summary> | ||
public static void RegisterMediator(this Container container, IServiceCollection services, Assembly[] assemblies, Action<MediatorOptions>? configureOptions = null) | ||
{ | ||
var options = new MediatorOptions(); | ||
configureOptions?.Invoke(options); | ||
|
||
if (options.EnableTransactionalDecorators) | ||
{ | ||
container.RegisterDecorator(typeof(IRequestHandler<,>), typeof(TransactionalRequestHandlerDecorator<,>), Lifestyle.Scoped); | ||
container.RegisterDecorator(typeof(INotificationHandler<>), typeof(TransactionalNotificationHandlerDecorator<>), Lifestyle.Scoped); | ||
container.RegisterSingleton<ITransactionScopeHandler, TransactionScopeHandler>(); | ||
} | ||
|
||
container.RegisterSingleton(typeof(IMediator), typeof(SimpleInjectorMediator)); | ||
container.Register(typeof(IRequestHandler<,>), assemblies, Lifestyle.Scoped); | ||
container.Register(typeof(IAsyncEnumerableRequestHandler<,>), assemblies, Lifestyle.Scoped); | ||
container.Collection.Register(typeof(INotificationHandler<>), assemblies, Lifestyle.Scoped); | ||
|
||
services.AddSingleton(_ => container.GetInstance<IMediator>()); | ||
} | ||
|
||
public class MediatorOptions | ||
{ | ||
/// <summary> | ||
/// Controls whether <see cref="TransactionalRequestHandlerDecorator{TCommand, TResult}"/> and <see cref="TransactionalNotificationHandlerDecorator{TNotification}"/> are enabled. | ||
/// Default is <c>true</c>. | ||
/// </summary> | ||
public bool EnableTransactionalDecorators { get; set; } = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters