-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- [FIX] key exists exception when multiple handlers registered for an operation (resolve the last registered handler type) - update deps
- Loading branch information
Showing
7 changed files
with
141 additions
and
97 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace ESCd.Extensions.OperationInvoker; | ||
|
||
internal sealed class HandlerDescriptorResolver( IEnumerable<OperationHandlerDescriptor> descriptors ) | ||
{ | ||
private readonly Dictionary<Type, OperationHandlerDescriptor> descriptorsByOperationType = ToDictionary( descriptors ); | ||
|
||
public OperationHandlerDescriptor? Resolve( Type operationType ) | ||
{ | ||
if( descriptorsByOperationType.TryGetValue( operationType, out var descriptor ) ) | ||
{ | ||
return descriptor; | ||
} | ||
|
||
return default; | ||
} | ||
|
||
private static Dictionary<Type, OperationHandlerDescriptor> ToDictionary( IEnumerable<OperationHandlerDescriptor> descriptors ) | ||
{ | ||
var descriptorsByOperationType = new Dictionary<Type, OperationHandlerDescriptor>(); | ||
foreach( var descriptor in descriptors ) | ||
{ | ||
descriptorsByOperationType[ descriptor.OperationType ] = descriptor; | ||
} | ||
|
||
return descriptorsByOperationType; | ||
} | ||
} |
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,32 @@ | ||
using ESCd.Extensions.OperationInvoker.Abstractions; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace ESCd.Extensions.OperationInvoker.Tests; | ||
|
||
public sealed class HandlerDescriptorResolverTests | ||
{ | ||
[Fact( DisplayName = "Resolve: returns last registered handler" )] | ||
public void Resolve_Returns_LastRegisteredHandler( ) | ||
{ | ||
using var services = new ServiceCollection() | ||
.AddOperationHandler<TestHandler>() | ||
.AddOperationHandler<OtherTestHandler>() | ||
.BuildServiceProvider(); | ||
|
||
var resolver = services.GetRequiredService<HandlerDescriptorResolver>(); | ||
var descriptor = resolver.Resolve( typeof( TestOperation ) ); | ||
|
||
Assert.Equal( typeof( OtherTestHandler ), descriptor?.HandlerType ); | ||
} | ||
|
||
private sealed record TestOperation : IOperation; | ||
private sealed class TestHandler : IOperationHandler<TestOperation> | ||
{ | ||
public Task Invoke( TestOperation operation, CancellationToken cancellation ) => throw new NotImplementedException(); | ||
} | ||
|
||
private sealed class OtherTestHandler : IOperationHandler<TestOperation> | ||
{ | ||
public Task Invoke( TestOperation operation, CancellationToken cancellation ) => throw new NotImplementedException(); | ||
} | ||
} |
Oops, something went wrong.