修订版 | 586504c9d8295316ca0a9f9da102c0ff136a48f7 (tree) |
---|---|
时间 | 2022-05-26 19:08:06 |
作者 | yoshy <yoshy.org.bitbucket@gz.j...> |
Commiter | yoshy |
[MOD] ハンドラコンテキストファクトリのマップ化により複数のハンドラキューを使い分け可能な機能を実装
@@ -2,8 +2,9 @@ | ||
2 | 2 | |
3 | 3 | namespace CleanAuLait48.Adaptor.Boundary.Controller |
4 | 4 | { |
5 | - public interface IAsyncHandlerContextFactory | |
5 | + public interface IAsyncHandlerContextFactory : IAttachHandlerQueueMap | |
6 | 6 | { |
7 | - IAsyncHandlerContext Create(IScopedComponentProvider scopedProvider); | |
7 | + IAsyncHandlerContext Create( | |
8 | + string handlerTypesName, IScopedComponentProvider scopedProvider); | |
8 | 9 | } |
9 | 10 | } |
\ No newline at end of file |
@@ -0,0 +1,13 @@ | ||
1 | +using System; | |
2 | +using System.Collections.Generic; | |
3 | +using System.Linq; | |
4 | +using System.Text; | |
5 | +using System.Threading.Tasks; | |
6 | + | |
7 | +namespace CleanAuLait48.Adaptor.Boundary.Controller | |
8 | +{ | |
9 | + public interface IAttachHandlerQueueMap | |
10 | + { | |
11 | + IDictionary<string, IEnumerable<Type>> HandlerQueueMap { get; set; } | |
12 | + } | |
13 | +} |
@@ -2,8 +2,9 @@ | ||
2 | 2 | |
3 | 3 | namespace CleanAuLait48.Adaptor.Boundary.Controller |
4 | 4 | { |
5 | - public interface IHandlerContextFactory | |
5 | + public interface IHandlerContextFactory : IAttachHandlerQueueMap | |
6 | 6 | { |
7 | - IHandlerContext Create(IScopedComponentProvider scopedProvider); | |
7 | + IHandlerContext Create( | |
8 | + string handlerTypesName, IScopedComponentProvider scopedProvider); | |
8 | 9 | } |
9 | 10 | } |
\ No newline at end of file |
@@ -0,0 +1,32 @@ | ||
1 | +using CleanAuLait48.Adaptor.Boundary.Controller; | |
2 | +using CleanAuLait48.Core.DI; | |
3 | +using System; | |
4 | +using System.Collections.Generic; | |
5 | + | |
6 | +namespace CleanAuLait48.Adaptor.Controller | |
7 | +{ | |
8 | + public abstract class AbstractHandlerContextFactoryRegistrar<IFACTORY, FACTORY> | |
9 | + where IFACTORY : class, IAttachHandlerQueueMap | |
10 | + where FACTORY : IFACTORY, new() | |
11 | + { | |
12 | + private readonly IDictionary<string, IEnumerable<Type>> map = new Dictionary<string, IEnumerable<Type>>(); | |
13 | + | |
14 | + public void Add(string factoryName, IEnumerable<Type> handlerTypes) | |
15 | + { | |
16 | + map.Add(factoryName, handlerTypes); | |
17 | + } | |
18 | + | |
19 | + public void Register(IComponentRegistry registry) | |
20 | + { | |
21 | + registry.RegisterSingleton(() => | |
22 | + { | |
23 | + IFACTORY factory = new FACTORY | |
24 | + { | |
25 | + HandlerQueueMap = map | |
26 | + }; | |
27 | + | |
28 | + return factory; | |
29 | + }); | |
30 | + } | |
31 | + } | |
32 | +} |
@@ -2,31 +2,48 @@ | ||
2 | 2 | using CleanAuLait48.Adaptor.Boundary.Controller.Handler; |
3 | 3 | using CleanAuLait48.Adaptor.Controller.Handler; |
4 | 4 | using CleanAuLait48.Core.DI; |
5 | +using System; | |
5 | 6 | using System.Collections.Generic; |
6 | 7 | |
7 | 8 | namespace CleanAuLait48.Adaptor.Controller |
8 | 9 | { |
9 | 10 | public class AsyncHandlerContextFactory : IAsyncHandlerContextFactory |
10 | 11 | { |
11 | - private readonly IEnumerable<IAsyncRequestHandler> handlers; | |
12 | + public IDictionary<string, IEnumerable<Type>> HandlerQueueMap { get; set; } | |
12 | 13 | |
13 | - public AsyncHandlerContextFactory(IEnumerable<IAsyncRequestHandler> handlers) | |
14 | + public IAsyncHandlerContext Create(string handlerQueueName, IScopedComponentProvider scopedProvider) | |
14 | 15 | { |
15 | - this.handlers = handlers; | |
16 | + if (!HandlerQueueMap.TryGetValue(handlerQueueName, out IEnumerable<Type> handlerTypes)) | |
17 | + { | |
18 | + throw new DIException($"Handler Queue [{handlerQueueName}] not registered."); | |
19 | + } | |
20 | + | |
21 | + return CreateInternal(scopedProvider, handlerTypes); | |
22 | + } | |
23 | + | |
24 | + protected static IAsyncHandlerContext CreateInternal( | |
25 | + IScopedComponentProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
26 | + { | |
27 | + Queue<IAsyncRequestHandler> queue = CreateHandlerQueue(scopedProvider, handlerTypes); | |
28 | + | |
29 | + IAsyncHandlerContext context = new AsyncHandlerContext(queue, scopedProvider); | |
30 | + | |
31 | + return context; | |
16 | 32 | } |
17 | 33 | |
18 | - public IAsyncHandlerContext Create(IScopedComponentProvider scopedProvider) | |
34 | + protected static Queue<IAsyncRequestHandler> CreateHandlerQueue( | |
35 | + IScopedComponentProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
19 | 36 | { |
20 | 37 | Queue<IAsyncRequestHandler> queue = new Queue<IAsyncRequestHandler>(); |
21 | 38 | |
22 | - foreach (IAsyncRequestHandler handler in handlers) | |
39 | + foreach (Type handlerType in handlerTypes) | |
23 | 40 | { |
41 | + IAsyncRequestHandler handler = scopedProvider.Resolve<IAsyncRequestHandler>(handlerType); | |
42 | + | |
24 | 43 | queue.Enqueue(handler); |
25 | 44 | } |
26 | 45 | |
27 | - IAsyncHandlerContext context = new AsyncHandlerContext(queue, scopedProvider); | |
28 | - | |
29 | - return context; | |
46 | + return queue; | |
30 | 47 | } |
31 | 48 | } |
32 | 49 | } |
\ No newline at end of file |
@@ -0,0 +1,12 @@ | ||
1 | +using CleanAuLait48.Adaptor.Boundary.Controller; | |
2 | +using CleanAuLait48.Core.DI; | |
3 | +using System; | |
4 | +using System.Collections.Generic; | |
5 | + | |
6 | +namespace CleanAuLait48.Adaptor.Controller | |
7 | +{ | |
8 | + public class AsyncHandlerContextFactoryRegistrar : | |
9 | + AbstractHandlerContextFactoryRegistrar<IAsyncHandlerContextFactory, AsyncHandlerContextFactory> | |
10 | + { | |
11 | + } | |
12 | +} |
@@ -2,37 +2,48 @@ | ||
2 | 2 | using CleanAuLait48.Adaptor.Boundary.Controller.Handler; |
3 | 3 | using CleanAuLait48.Adaptor.Controller.Handler; |
4 | 4 | using CleanAuLait48.Core.DI; |
5 | +using System; | |
5 | 6 | using System.Collections.Generic; |
6 | 7 | |
7 | 8 | namespace CleanAuLait48.Adaptor.Controller |
8 | 9 | { |
9 | 10 | public class HandlerContextFactory : IHandlerContextFactory |
10 | 11 | { |
11 | - private readonly IEnumerable<IRequestHandler> handlers; | |
12 | + public IDictionary<string, IEnumerable<Type>> HandlerQueueMap { get; set; } | |
12 | 13 | |
13 | - public HandlerContextFactory(IEnumerable<IRequestHandler> handlers) | |
14 | + public IHandlerContext Create(string handlerQueueName, IScopedComponentProvider scopedProvider) | |
14 | 15 | { |
15 | - this.handlers = handlers; | |
16 | + if (!HandlerQueueMap.TryGetValue(handlerQueueName, out IEnumerable<Type> handlerTypes)) | |
17 | + { | |
18 | + throw new DIException($"Handler Queue [{handlerQueueName}] not registered."); | |
19 | + } | |
20 | + | |
21 | + return CreateInternal(scopedProvider, handlerTypes); | |
16 | 22 | } |
17 | 23 | |
18 | - public IHandlerContext Create(IScopedComponentProvider scopedProvider) | |
24 | + protected static IHandlerContext CreateInternal( | |
25 | + IScopedComponentProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
19 | 26 | { |
20 | - return CreateInternal(scopedProvider, this.handlers); | |
27 | + Queue<IRequestHandler> queue = CreateHandlerQueue(scopedProvider, handlerTypes); | |
28 | + | |
29 | + IHandlerContext context = new HandlerContext(queue, scopedProvider); | |
30 | + | |
31 | + return context; | |
21 | 32 | } |
22 | 33 | |
23 | - protected static IHandlerContext CreateInternal( | |
24 | - IScopedComponentProvider scopedProvider, IEnumerable<IRequestHandler> handlers) | |
34 | + protected static Queue<IRequestHandler> CreateHandlerQueue( | |
35 | + IScopedComponentProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
25 | 36 | { |
26 | 37 | Queue<IRequestHandler> queue = new Queue<IRequestHandler>(); |
27 | 38 | |
28 | - foreach (IRequestHandler handler in handlers) | |
39 | + foreach (Type handlerType in handlerTypes) | |
29 | 40 | { |
41 | + IRequestHandler handler = scopedProvider.Resolve<IRequestHandler>(handlerType); | |
42 | + | |
30 | 43 | queue.Enqueue(handler); |
31 | 44 | } |
32 | 45 | |
33 | - IHandlerContext context = new HandlerContext(queue, scopedProvider); | |
34 | - | |
35 | - return context; | |
46 | + return queue; | |
36 | 47 | } |
37 | 48 | } |
38 | 49 | } |
\ No newline at end of file |
@@ -0,0 +1,12 @@ | ||
1 | +using CleanAuLait48.Adaptor.Boundary.Controller; | |
2 | +using CleanAuLait48.Core.DI; | |
3 | +using System; | |
4 | +using System.Collections.Generic; | |
5 | + | |
6 | +namespace CleanAuLait48.Adaptor.Controller | |
7 | +{ | |
8 | + public class HandlerContextFactoryRegistrar : | |
9 | + AbstractHandlerContextFactoryRegistrar<IHandlerContextFactory, HandlerContextFactory> | |
10 | + { | |
11 | + } | |
12 | +} |
@@ -13,13 +13,6 @@ namespace CleanAuLait48.Adaptor.Controller.Router | ||
13 | 13 | { |
14 | 14 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
15 | 15 | |
16 | - private readonly IAsyncRequestScopeGenerator scopeGenerator; | |
17 | - | |
18 | - public AsyncUseCaseRouter(IAsyncRequestScopeGenerator scopeGenerator) | |
19 | - { | |
20 | - this.scopeGenerator = scopeGenerator; | |
21 | - } | |
22 | - | |
23 | 16 | public async Task<UseCaseResponse> HandleAsync(UseCaseRequest req, IAsyncHandlerContext context) |
24 | 17 | { |
25 | 18 | try |
@@ -28,7 +21,6 @@ namespace CleanAuLait48.Adaptor.Controller.Router | ||
28 | 21 | |
29 | 22 | IAsyncUseCaseRouterAwareInteractor asyncInteractor = GetComponent(context.ScopedProvider, req); |
30 | 23 | |
31 | - // routing map is null, or request type key not registered on routing map. | |
32 | 24 | if (asyncInteractor == null) |
33 | 25 | { |
34 | 26 | logger.Error("get async interactor instance failed."); |
@@ -45,7 +37,6 @@ namespace CleanAuLait48.Adaptor.Controller.Router | ||
45 | 37 | } |
46 | 38 | catch (DIException e) |
47 | 39 | { |
48 | - // component concrete instance not registered on di container. | |
49 | 40 | string msg = "コンポーネントがDIコンテナに登録されていません"; |
50 | 41 | throw new ApplicationException(msg, e); |
51 | 42 | } |
@@ -12,13 +12,6 @@ namespace CleanAuLait48.Adaptor.Controller.Router | ||
12 | 12 | { |
13 | 13 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
14 | 14 | |
15 | - private readonly IRequestScopeGenerator scopeGenerator; | |
16 | - | |
17 | - public UseCaseRouter(IRequestScopeGenerator scopeGenerator) | |
18 | - { | |
19 | - this.scopeGenerator = scopeGenerator; | |
20 | - } | |
21 | - | |
22 | 15 | public UseCaseResponse Handle(UseCaseRequest req, IHandlerContext context) |
23 | 16 | { |
24 | 17 | try |
@@ -54,6 +54,10 @@ | ||
54 | 54 | <Reference Include="System.Xml" /> |
55 | 55 | </ItemGroup> |
56 | 56 | <ItemGroup> |
57 | + <Compile Include="Adaptor\Boundary\Controller\IAttachHandlerQueueMap.cs" /> | |
58 | + <Compile Include="Adaptor\Controller\AbstractHandlerContextFactoryRegistrar.cs" /> | |
59 | + <Compile Include="Adaptor\Controller\AsyncHandlerContextFactoryRegistrar.cs" /> | |
60 | + <Compile Include="Adaptor\Controller\HandlerContextFactoryRegistrar.cs" /> | |
57 | 61 | <Compile Include="CleanAuLait48ComponentRegisterer.cs" /> |
58 | 62 | <Compile Include="Adaptor\Boundary\Controller\Handler\IAsyncRequestHandler.cs" /> |
59 | 63 | <Compile Include="Adaptor\Boundary\Controller\Handler\IRequestHandler.cs" /> |
@@ -34,9 +34,6 @@ namespace CleanAuLait48 | ||
34 | 34 | componentRegistry.RegisterSingleton<IRequestScopeGenerator, RequestScopeGenerator>(); |
35 | 35 | componentRegistry.RegisterSingleton<IAsyncRequestScopeGenerator, AsyncRequestScopeGenerator>(); |
36 | 36 | |
37 | - componentRegistry.RegisterSingleton<IHandlerContextFactory, HandlerContextFactory>(); | |
38 | - componentRegistry.RegisterSingleton<IAsyncHandlerContextFactory, AsyncHandlerContextFactory>(); | |
39 | - | |
40 | 37 | logger.Trace("RegisterTypes end"); |
41 | 38 | } |
42 | 39 |