-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandlerShell.cs
More file actions
122 lines (95 loc) · 4.8 KB
/
Copy pathHandlerShell.cs
File metadata and controls
122 lines (95 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Micro.Net.Abstractions;
using Micro.Net.Abstractions.Sagas;
using Micro.Net.Core.Abstractions.Pipeline;
using Micro.Net.Core.Pipeline;
using Micro.Net.Receive;
using Micro.Net.Sagas;
using Microsoft.Extensions.DependencyInjection;
namespace Micro.Net.Handling
{
internal static class SagaCache
{
public static readonly IDictionary<Type, MethodInfo> _sagaHandleCache = new ConcurrentDictionary<Type, MethodInfo>();
public static readonly IDictionary<Type, Type> _sagaDataMessageMapCache = new ConcurrentDictionary<Type, Type>();
public static readonly IDictionary<Type, MethodInfo> _slSagaHandleCache = new ConcurrentDictionary<Type, MethodInfo>();
public static readonly IDictionary<(Type, Type), Func<object, SagaFinderContext, object>> _slSagaDataFindCache = new ConcurrentDictionary<(Type, Type), Func<object, SagaFinderContext, object>>();
public static readonly IDictionary<Type, Func<object>> _slSagaStartCache = new ConcurrentDictionary<Type, Func<object>>();
}
internal static class HandlerCache
{
public static readonly IDictionary<(Type, Type), Func<object, HandlerContext, Task<object>>> _handleCache =
new ConcurrentDictionary<(Type, Type), Func<object, HandlerContext, Task<object>>>();
}
public class HandlerShell<TMessage> : IPipelineTail<IReceiveContext<TMessage,ValueTuple>,ValueTuple> where TMessage : IContract
{
private readonly IServiceProvider _provider;
public HandlerShell(IServiceProvider provider)
{
_provider = provider;
}
public async Task<ValueTuple> Handle(IReceiveContext<TMessage, ValueTuple> request)
{
IHandle<TMessage> svc = _provider.GetService<IHandle<TMessage>>();
if (request.Request.Payload is ISagaContract)
{
SagaCache._sagaHandleCache[request.Request.Payload.GetType()] ??= this.GetType().GetMethod(nameof(SagaShell.HandleSaga), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod(typeof(TMessage));
await (Task)SagaCache._sagaHandleCache[request.Request.Payload.GetType()].Invoke(null, new object[] { request, _provider, CancellationToken.None });
}
else
{
HandlerContext context = new HandlerContext(_provider.GetService<IPipeChannel>(), _provider.GetService<MicroSystemConfiguration>());
await svc.Handle(request.Request.Payload, context);
request.Response.Payload = new ValueTuple();
//TODO: Set up terminate handler and resolve handler
}
return ValueTuple.Create();
}
}
public class HandlerShell<TRequest, TResponse> : IPipelineTail<IReceiveContext<TRequest, TResponse>, ValueTuple>
where TRequest : IContract<TResponse>
{
private readonly IServiceProvider _provider;
public HandlerShell(IServiceProvider provider)
{
_provider = provider;
}
public async Task<ValueTuple> Handle(IReceiveContext<TRequest, TResponse> request)
{
HandlerContext context = new HandlerContext(_provider.GetService<IPipeChannel>(), _provider.GetService<MicroSystemConfiguration>());
Func<object, HandlerContext, Task<object>> _handle = null;
if (HandlerCache._handleCache.TryGetValue((typeof(TRequest), typeof(TResponse)), out Func<object, HandlerContext, Task<object>> handle))
{
_handle = handle;
}
if (_handle == null)
{
if (typeof(TResponse) != typeof(ValueTuple))
{
IHandle<TRequest, TResponse> svc = _provider.GetService<IHandle<TRequest, TResponse>>();
HandlerCache._handleCache[(typeof(TRequest), typeof(TResponse))] = _handle = async (obj, ctx) =>
{
return await svc.Handle(request.Request.Payload, context);
};
}
else
{
dynamic svc = _provider.GetService(typeof(IHandle<>).MakeGenericType(typeof(TRequest)));
HandlerCache._handleCache[(typeof(TRequest), typeof(TResponse))] = _handle = async (obj, ctx) =>
{
await svc.Handle(request.Request.Payload, context);
return default(TResponse);
};
}
}
request.Response.Payload = (TResponse) await _handle.Invoke(request, context);
return ValueTuple.Create();
}
}
}