リビジョン | 9a78b11a152465e143c6f339759b754e8b15e604 (tree) |
---|---|
日時 | 2022-05-26 19:07:40 |
作者 | yoshy <yoshy.org.bitbucket@gz.j...> |
コミッター | yoshy |
[MOD] ハンドラコンテキストファクトリのマップ化により複数のハンドラキューを使い分け可能な機能を実装
@@ -9,7 +9,7 @@ namespace CleanAuLait.Adaptor.Boundary.Controller | ||
9 | 9 | { |
10 | 10 | Queue<IAsyncRequestHandler> HandlerQueue { get; } |
11 | 11 | |
12 | - IScopedProvider Scope { get; } | |
12 | + IScopedProvider ScopedProvider { get; } | |
13 | 13 | |
14 | 14 | Task<UseCaseResponse> HandleNextAsync(UseCaseRequest req, IAsyncHandlerContext context); |
15 | 15 | } |
@@ -2,8 +2,9 @@ | ||
2 | 2 | |
3 | 3 | namespace CleanAuLait.Adaptor.Boundary.Controller |
4 | 4 | { |
5 | - public interface IAsyncHandlerContextFactory | |
5 | + public interface IAsyncHandlerContextFactory : IAttachHandlerQueueMap | |
6 | 6 | { |
7 | - IAsyncHandlerContext Create(IScopedProvider scope); | |
7 | + IAsyncHandlerContext Create( | |
8 | + string handlerTypesName,IScopedProvider 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 CleanAuLait.Adaptor.Boundary.Controller | |
8 | +{ | |
9 | + public interface IAttachHandlerQueueMap | |
10 | + { | |
11 | + IDictionary<string, IEnumerable<Type>> HandlerQueueMap { get; set; } | |
12 | + } | |
13 | +} |
@@ -9,7 +9,7 @@ namespace CleanAuLait.Adaptor.Boundary.Controller | ||
9 | 9 | { |
10 | 10 | Queue<IRequestHandler> HandlerQueue { get; } |
11 | 11 | |
12 | - IScopedProvider Scope { get; } | |
12 | + IScopedProvider ScopedProvider { get; } | |
13 | 13 | |
14 | 14 | UseCaseResponse HandleNext(UseCaseRequest req, IHandlerContext context); |
15 | 15 | } |
@@ -2,8 +2,9 @@ | ||
2 | 2 | |
3 | 3 | namespace CleanAuLait.Adaptor.Boundary.Controller |
4 | 4 | { |
5 | - public interface IHandlerContextFactory | |
5 | + public interface IHandlerContextFactory : IAttachHandlerQueueMap | |
6 | 6 | { |
7 | - IHandlerContext Create(IScopedProvider scope); | |
7 | + IHandlerContext Create( | |
8 | + string handlerTypesName, IScopedProvider scopedProvider); | |
8 | 9 | } |
9 | 10 | } |
\ No newline at end of file |
@@ -0,0 +1,30 @@ | ||
1 | +using CleanAuLait.Adaptor.Boundary.Controller; | |
2 | +using Prism.Ioc; | |
3 | + | |
4 | +namespace CleanAuLait.Adaptor.Controller | |
5 | +{ | |
6 | + public abstract class AbstractHandlerContextFactoryRegistrar<IFACTORY, FACTORY> | |
7 | + where IFACTORY : class, IAttachHandlerQueueMap | |
8 | + where FACTORY : IFACTORY, new() | |
9 | + { | |
10 | + private readonly IDictionary<string, IEnumerable<Type>> map = new Dictionary<string, IEnumerable<Type>>(); | |
11 | + | |
12 | + public void Add(string factoryName, IEnumerable<Type> handlerTypes) | |
13 | + { | |
14 | + map.Add(factoryName, handlerTypes); | |
15 | + } | |
16 | + | |
17 | + public void Register(IContainerRegistry registry) | |
18 | + { | |
19 | + registry.RegisterSingleton<IFACTORY>(() => | |
20 | + { | |
21 | + IFACTORY factory = new FACTORY | |
22 | + { | |
23 | + HandlerQueueMap = map | |
24 | + }; | |
25 | + | |
26 | + return factory; | |
27 | + }); | |
28 | + } | |
29 | + } | |
30 | +} |
@@ -1,31 +1,48 @@ | ||
1 | 1 | using CleanAuLait.Adaptor.Boundary.Controller; |
2 | 2 | using CleanAuLait.Adaptor.Boundary.Controller.Handler; |
3 | 3 | using CleanAuLait.Adaptor.Controller.Handler; |
4 | +using CleanAuLait.Core.DI; | |
4 | 5 | using Prism.Ioc; |
5 | 6 | |
6 | 7 | namespace CleanAuLait.Adaptor.Controller |
7 | 8 | { |
8 | 9 | public class AsyncHandlerContextFactory : IAsyncHandlerContextFactory |
9 | 10 | { |
10 | - private readonly IEnumerable<IAsyncRequestHandler> handlers; | |
11 | + public IDictionary<string, IEnumerable<Type>> HandlerQueueMap { get; set; } | |
11 | 12 | |
12 | - public AsyncHandlerContextFactory(IEnumerable<IAsyncRequestHandler> handlers) | |
13 | + public IAsyncHandlerContext Create(string handlerQueueName, IScopedProvider scopedProvider) | |
13 | 14 | { |
14 | - this.handlers = handlers; | |
15 | + if (!HandlerQueueMap.TryGetValue(handlerQueueName, out IEnumerable<Type> handlerTypes)) | |
16 | + { | |
17 | + throw new DIException($"Handler Queue [{handlerQueueName}] not registered."); | |
18 | + } | |
19 | + | |
20 | + return CreateInternal(scopedProvider, handlerTypes); | |
21 | + } | |
22 | + | |
23 | + protected static IAsyncHandlerContext CreateInternal( | |
24 | + IScopedProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
25 | + { | |
26 | + Queue<IAsyncRequestHandler> queue = CreateHandlerQueue(scopedProvider, handlerTypes); | |
27 | + | |
28 | + IAsyncHandlerContext context = new AsyncHandlerContext(queue, scopedProvider); | |
29 | + | |
30 | + return context; | |
15 | 31 | } |
16 | 32 | |
17 | - public IAsyncHandlerContext Create(IScopedProvider scope) | |
33 | + protected static Queue<IAsyncRequestHandler> CreateHandlerQueue( | |
34 | + IScopedProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
18 | 35 | { |
19 | 36 | Queue<IAsyncRequestHandler> queue = new(); |
20 | 37 | |
21 | - foreach (IAsyncRequestHandler handler in handlers) | |
38 | + foreach (Type handlerType in handlerTypes) | |
22 | 39 | { |
40 | + IAsyncRequestHandler handler = (IAsyncRequestHandler)scopedProvider.Resolve(handlerType); | |
41 | + | |
23 | 42 | queue.Enqueue(handler); |
24 | 43 | } |
25 | 44 | |
26 | - IAsyncHandlerContext context = new AsyncHandlerContext(queue, scope); | |
27 | - | |
28 | - return context; | |
45 | + return queue; | |
29 | 46 | } |
30 | 47 | } |
31 | 48 | } |
\ No newline at end of file |
@@ -0,0 +1,12 @@ | ||
1 | +using CleanAuLait.Adaptor.Boundary.Controller; | |
2 | +using CleanAuLait.Core.DI; | |
3 | +using System; | |
4 | +using System.Collections.Generic; | |
5 | + | |
6 | +namespace CleanAuLait.Adaptor.Controller | |
7 | +{ | |
8 | + public class AsyncHandlerContextFactoryRegistrar : | |
9 | + AbstractHandlerContextFactoryRegistrar<IAsyncHandlerContextFactory, AsyncHandlerContextFactory> | |
10 | + { | |
11 | + } | |
12 | +} |
@@ -9,12 +9,12 @@ namespace CleanAuLait.Adaptor.Controller.Handler | ||
9 | 9 | public class AsyncHandlerContext : IAsyncHandlerContext |
10 | 10 | { |
11 | 11 | public Queue<IAsyncRequestHandler> HandlerQueue { get; } |
12 | - public IScopedProvider Scope { get; } | |
12 | + public IScopedProvider ScopedProvider { get; } | |
13 | 13 | |
14 | - public AsyncHandlerContext(Queue<IAsyncRequestHandler> queue, IScopedProvider scope) | |
14 | + public AsyncHandlerContext(Queue<IAsyncRequestHandler> queue, IScopedProvider scopedProvider) | |
15 | 15 | { |
16 | 16 | this.HandlerQueue = queue; |
17 | - this.Scope = scope; | |
17 | + this.ScopedProvider = scopedProvider; | |
18 | 18 | } |
19 | 19 | |
20 | 20 | public async Task<UseCaseResponse> HandleNextAsync(UseCaseRequest req, IAsyncHandlerContext context) |
@@ -9,12 +9,12 @@ namespace CleanAuLait.Adaptor.Controller.Handler | ||
9 | 9 | public class HandlerContext : IHandlerContext |
10 | 10 | { |
11 | 11 | public Queue<IRequestHandler> HandlerQueue { get; } |
12 | - public IScopedProvider Scope { get; } | |
12 | + public IScopedProvider ScopedProvider { get; } | |
13 | 13 | |
14 | - public HandlerContext(Queue<IRequestHandler> queue, IScopedProvider scope) | |
14 | + public HandlerContext(Queue<IRequestHandler> queue, IScopedProvider scopedProvider) | |
15 | 15 | { |
16 | 16 | this.HandlerQueue = queue; |
17 | - this.Scope = scope; | |
17 | + this.ScopedProvider = scopedProvider; | |
18 | 18 | } |
19 | 19 | |
20 | 20 | public UseCaseResponse HandleNext(UseCaseRequest req, IHandlerContext context) |
@@ -1,31 +1,48 @@ | ||
1 | 1 | using CleanAuLait.Adaptor.Boundary.Controller; |
2 | 2 | using CleanAuLait.Adaptor.Boundary.Controller.Handler; |
3 | 3 | using CleanAuLait.Adaptor.Controller.Handler; |
4 | +using CleanAuLait.Core.DI; | |
4 | 5 | using Prism.Ioc; |
5 | 6 | |
6 | 7 | namespace CleanAuLait.Adaptor.Controller |
7 | 8 | { |
8 | 9 | public class HandlerContextFactory : IHandlerContextFactory |
9 | 10 | { |
10 | - private readonly IEnumerable<IRequestHandler> handlers; | |
11 | + public IDictionary<string, IEnumerable<Type>> HandlerQueueMap { get; set; } | |
11 | 12 | |
12 | - public HandlerContextFactory(IEnumerable<IRequestHandler> handlers) | |
13 | + public IHandlerContext Create(string handlerQueueName, IScopedProvider scopedProvider) | |
13 | 14 | { |
14 | - this.handlers = handlers; | |
15 | + if (!HandlerQueueMap.TryGetValue(handlerQueueName, out IEnumerable<Type> handlerTypes)) | |
16 | + { | |
17 | + throw new DIException($"Handler Queue [{handlerQueueName}] not registered."); | |
18 | + } | |
19 | + | |
20 | + return CreateInternal(scopedProvider, handlerTypes); | |
21 | + } | |
22 | + | |
23 | + protected static IHandlerContext CreateInternal( | |
24 | + IScopedProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
25 | + { | |
26 | + Queue<IRequestHandler> queue = CreateHandlerQueue(scopedProvider, handlerTypes); | |
27 | + | |
28 | + IHandlerContext context = new HandlerContext(queue, scopedProvider); | |
29 | + | |
30 | + return context; | |
15 | 31 | } |
16 | 32 | |
17 | - public IHandlerContext Create(IScopedProvider scope) | |
33 | + protected static Queue<IRequestHandler> CreateHandlerQueue( | |
34 | + IScopedProvider scopedProvider, IEnumerable<Type> handlerTypes) | |
18 | 35 | { |
19 | 36 | Queue<IRequestHandler> queue = new(); |
20 | 37 | |
21 | - foreach (IRequestHandler handler in handlers) | |
38 | + foreach (Type handlerType in handlerTypes) | |
22 | 39 | { |
40 | + IRequestHandler handler = (IRequestHandler)scopedProvider.Resolve(handlerType); | |
41 | + | |
23 | 42 | queue.Enqueue(handler); |
24 | 43 | } |
25 | 44 | |
26 | - IHandlerContext context = new HandlerContext(queue, scope); | |
27 | - | |
28 | - return context; | |
45 | + return queue; | |
29 | 46 | } |
30 | 47 | } |
31 | 48 | } |
\ No newline at end of file |
@@ -0,0 +1,12 @@ | ||
1 | +using CleanAuLait.Adaptor.Boundary.Controller; | |
2 | +using CleanAuLait.Core.DI; | |
3 | +using System; | |
4 | +using System.Collections.Generic; | |
5 | + | |
6 | +namespace CleanAuLait.Adaptor.Controller | |
7 | +{ | |
8 | + public class HandlerContextFactoryRegistrar : | |
9 | + AbstractHandlerContextFactoryRegistrar<IHandlerContextFactory, HandlerContextFactory> | |
10 | + { | |
11 | + } | |
12 | +} |
@@ -9,18 +9,11 @@ namespace CleanAuLait.Adaptor.Controller.Router | ||
9 | 9 | { |
10 | 10 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
11 | 11 | |
12 | - protected readonly IRequestScopeGenerator scopeGenerator; | |
13 | - | |
14 | - public AbstractUseCaseRouter(IRequestScopeGenerator scopeGenerator) | |
15 | - { | |
16 | - this.scopeGenerator = scopeGenerator; | |
17 | - } | |
18 | - | |
19 | - protected T GetComponent(IScopedProvider scope, IUseCaseRouterAware req) | |
12 | + protected T GetComponent(IScopedProvider scopedProvider, IUseCaseRouterAware req) | |
20 | 13 | { |
21 | 14 | Type interactorType = req.GetInteractorType(); |
22 | 15 | |
23 | - if (!interactorType.IsAssignableTo(typeof(T))) | |
16 | + if (!typeof(T).IsAssignableFrom(interactorType)) | |
24 | 17 | { |
25 | 18 | logger.Trace($"{interactorType.Name} not assignable to {typeof(T).Name}.)"); |
26 | 19 | return default; |
@@ -28,7 +21,7 @@ namespace CleanAuLait.Adaptor.Controller.Router | ||
28 | 21 | |
29 | 22 | try |
30 | 23 | { |
31 | - return (T)scope.Resolve(interactorType); | |
24 | + return (T)scopedProvider.Resolve(interactorType); | |
32 | 25 | } |
33 | 26 | catch (ContainerResolutionException e) |
34 | 27 | { |
@@ -12,20 +12,14 @@ namespace CleanAuLait.Adaptor.Controller.Router | ||
12 | 12 | { |
13 | 13 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
14 | 14 | |
15 | - public AsyncUseCaseRouter(IRequestScopeGenerator scopeGenerator) : base(scopeGenerator) | |
16 | - { | |
17 | - } | |
18 | - | |
19 | 15 | public async Task<UseCaseResponse> HandleAsync(UseCaseRequest req, IAsyncHandlerContext context) |
20 | 16 | { |
21 | 17 | try |
22 | 18 | { |
23 | - using IScopedProvider scope = scopeGenerator.CreateScope(); | |
24 | 19 | logger.Trace($"-> ASYNC REQUEST = {req}"); |
25 | 20 | |
26 | - IAsyncUseCaseRouterAwareInteractor asyncInteractor = GetComponent(scope, req); | |
21 | + IAsyncUseCaseRouterAwareInteractor asyncInteractor = GetComponent(context.ScopedProvider, req); | |
27 | 22 | |
28 | - // routing map is null, or request type key not registered on routing map. | |
29 | 23 | if (asyncInteractor == null) |
30 | 24 | { |
31 | 25 | logger.Error("get async interactor instance failed."); |
@@ -42,7 +36,6 @@ namespace CleanAuLait.Adaptor.Controller.Router | ||
42 | 36 | } |
43 | 37 | catch (DIException e) |
44 | 38 | { |
45 | - // component concrete instance not registered on di container. | |
46 | 39 | string msg = "コンポーネントがDIコンテナに登録されていません"; |
47 | 40 | throw new ApplicationException(msg, e); |
48 | 41 | } |
@@ -12,20 +12,14 @@ namespace CleanAuLait.Adaptor.Controller.Router | ||
12 | 12 | { |
13 | 13 | private static readonly ILogger logger = LogManager.GetCurrentClassLogger(); |
14 | 14 | |
15 | - public UseCaseRouter(IRequestScopeGenerator scopeGenerator) : base(scopeGenerator) | |
16 | - { | |
17 | - } | |
18 | - | |
19 | 15 | public UseCaseResponse Handle(UseCaseRequest req, IHandlerContext context) |
20 | 16 | { |
21 | 17 | try |
22 | 18 | { |
23 | - using IScopedProvider scope = scopeGenerator.CreateScope(); | |
24 | 19 | logger.Trace($"-> REQUEST = {req}"); |
25 | 20 | |
26 | - IUseCaseRouterAwareInteractor interactor = GetComponent(scope, req); | |
21 | + IUseCaseRouterAwareInteractor interactor = GetComponent(context.ScopedProvider, req); | |
27 | 22 | |
28 | - // routing map is null, or request type key not registered on routing map. | |
29 | 23 | if (interactor == null) |
30 | 24 | { |
31 | 25 | logger.Error("get interactor instance failed."); |
@@ -42,7 +36,6 @@ namespace CleanAuLait.Adaptor.Controller.Router | ||
42 | 36 | } |
43 | 37 | catch (DIException e) |
44 | 38 | { |
45 | - // component concrete instance not registered on di container. | |
46 | 39 | string msg = $"コンポーネントがDIコンテナに登録されていません:{e}"; |
47 | 40 | throw new ApplicationException(msg, e); |
48 | 41 | } |
@@ -24,9 +24,6 @@ namespace CleanAuLait | ||
24 | 24 | |
25 | 25 | containerRegistry.RegisterSingleton<IRequestScopeGenerator, RequestScopeGenerator>(); |
26 | 26 | |
27 | - containerRegistry.RegisterSingleton<IHandlerContextFactory, HandlerContextFactory>(); | |
28 | - containerRegistry.RegisterSingleton<IAsyncHandlerContextFactory, AsyncHandlerContextFactory>(); | |
29 | - | |
30 | 27 | /// |
31 | 28 | /// Resources |
32 | 29 | /// |