-
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.
refactor: Refactored mediator interfaces, got rid of object overloads…
… and default implementations
- Loading branch information
1 parent
cc73761
commit b41796a
Showing
17 changed files
with
256 additions
and
180 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
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
17 changes: 17 additions & 0 deletions
17
src/Mediator/src/SimpleInjector/AsyncEnumerableRequestHandlerWrapper.cs
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,17 @@ | ||
using System.Runtime.CompilerServices; | ||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator.SimpleInjector; | ||
|
||
internal sealed class AsyncEnumerableRequestHandlerWrapper<TRequest, TResponse> : IAsyncEnumerableRequestHandlerWrapper | ||
where TRequest : IAsyncEnumerableRequest<TResponse> | ||
{ | ||
public async IAsyncEnumerable<object> Handle(object request, Container container, [EnumeratorCancellation] CancellationToken cancellationToken) | ||
{ | ||
var handlers = container.GetInstance<IAsyncEnumerableRequestHandler<TRequest, TResponse>>(); | ||
await foreach (var item in handlers.Handle((TRequest)request, cancellationToken)) | ||
{ | ||
yield return item!; | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Mediator/src/SimpleInjector/IAsyncEnumerableRequestHandlerWrapper.cs
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,8 @@ | ||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator.SimpleInjector; | ||
|
||
internal interface IAsyncEnumerableRequestHandlerWrapper | ||
{ | ||
IAsyncEnumerable<object> Handle(object request, Container container, CancellationToken cancellationToken); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Mediator/src/SimpleInjector/INotificationHandlerWrapper.cs
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,8 @@ | ||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator.SimpleInjector; | ||
|
||
internal interface INotificationHandlerWrapper | ||
{ | ||
Task Handle(object notification, Container container, CancellationToken cancellationToken); | ||
} |
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,8 @@ | ||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator.SimpleInjector; | ||
|
||
internal interface IRequestHandlerWrapper | ||
{ | ||
Task<object> Handle(object request, Container container, CancellationToken cancellationToken); | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Mediator/src/SimpleInjector/NotificationHandlerWrapper.cs
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,16 @@ | ||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator.SimpleInjector; | ||
|
||
internal sealed class NotificationHandlerWrapper<T> : INotificationHandlerWrapper | ||
where T : INotification | ||
{ | ||
public async Task Handle(object notification, Container container, CancellationToken cancellationToken) | ||
{ | ||
var handlers = container.GetAllInstances<INotificationHandler<T>>(); | ||
foreach (var handler in handlers) | ||
{ | ||
await handler.Handle((T)notification, cancellationToken); | ||
} | ||
} | ||
} |
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,15 @@ | ||
// Copyright (c) Fusonic GmbH. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
|
||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator.SimpleInjector; | ||
|
||
internal sealed class RequestHandlerWrapper<TRequest, TResponse> : IRequestHandlerWrapper | ||
where TRequest : IRequest<TResponse> | ||
{ | ||
public async Task<object> Handle(object request, Container container, CancellationToken cancellationToken) | ||
{ | ||
return (await container.GetInstance<IRequestHandler<TRequest, TResponse>>().Handle((TRequest)request, cancellationToken))!; | ||
} | ||
} |
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,69 @@ | ||
// Copyright (c) Fusonic GmbH. All rights reserved. | ||
// Licensed under the MIT License. See LICENSE file in the project root for license information. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Runtime.CompilerServices; | ||
using SimpleInjector; | ||
|
||
namespace Fusonic.Extensions.Mediator.SimpleInjector; | ||
|
||
public class SimpleInjectorMediator(Container container) : IMediator | ||
{ | ||
private static readonly ConcurrentDictionary<Type, IRequestHandlerWrapper> RequestWrappers = new(); | ||
private static readonly ConcurrentDictionary<Type, INotificationHandlerWrapper> NotificationWrappers = new(); | ||
private static readonly ConcurrentDictionary<Type, IAsyncEnumerableRequestHandlerWrapper> AsyncEnumWrappers = new(); | ||
|
||
public async Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
|
||
var wrapper = RequestWrappers.GetOrAdd(request.GetType(), | ||
t => | ||
{ | ||
var wrapperType = typeof(RequestHandlerWrapper<,>).MakeGenericType(t, typeof(TResponse)); | ||
var wrapperInstance = Activator.CreateInstance(wrapperType) as IRequestHandlerWrapper | ||
?? throw new ActivationException($"Could not create wrapper for type {t.FullName}."); | ||
|
||
return wrapperInstance; | ||
}); | ||
|
||
return (TResponse)await wrapper.Handle(request, container, cancellationToken); | ||
} | ||
|
||
public async Task Publish(INotification notification, CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentNullException.ThrowIfNull(notification); | ||
|
||
var wrapper = NotificationWrappers.GetOrAdd(notification.GetType(), | ||
t => | ||
{ | ||
var wrapperType = typeof(NotificationHandlerWrapper<>).MakeGenericType(t); | ||
var wrapperInstance = Activator.CreateInstance(wrapperType) as INotificationHandlerWrapper | ||
?? throw new ActivationException($"Could not create wrapper for type {t.FullName}."); | ||
|
||
return wrapperInstance; | ||
}); | ||
|
||
await wrapper.Handle(notification, container, cancellationToken); | ||
} | ||
|
||
public async IAsyncEnumerable<TResponse> CreateAsyncEnumerable<TResponse>(IAsyncEnumerableRequest<TResponse> request, [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
ArgumentNullException.ThrowIfNull(request); | ||
|
||
var wrapper = AsyncEnumWrappers.GetOrAdd(request.GetType(), | ||
t => | ||
{ | ||
var wrapperType = typeof(AsyncEnumerableRequestHandlerWrapper<,>).MakeGenericType(t, typeof(TResponse)); | ||
var wrapperInstance = Activator.CreateInstance(wrapperType) as IAsyncEnumerableRequestHandlerWrapper | ||
?? throw new ActivationException($"Could not create wrapper for type {t.FullName}."); | ||
|
||
return wrapperInstance; | ||
}); | ||
|
||
await foreach (var item in wrapper.Handle(request, container, cancellationToken)) | ||
{ | ||
yield return (TResponse)item; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.