This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGServiceExec.cs
More file actions
124 lines (113 loc) · 4.22 KB
/
GServiceExec.cs
File metadata and controls
124 lines (113 loc) · 4.22 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
123
using System;
using System.Collections.Generic;
using System.Reflection;
using SimpleStack.Enums;
namespace SimpleStack
{
public class GServiceExec
{
public const string Execute = "Execute";
public const string ExecuteAsync = "ExecuteAsync";
public const string RestGet = "Get";
public const string RestPost = "Post";
public const string RestPut = "Put";
public const string RestDelete = "Delete";
public const string RestPatch = "Patch";
private static readonly Dictionary<Type, MethodInfo> ServiceExecCache = new Dictionary<Type, MethodInfo>();
[Obsolete("Obsolete API ?")]
public static MethodInfo GetExecMethodInfo(Type serviceType, Type requestType)
{
MethodInfo mi = null;
lock (ServiceExecCache)
{
if (!ServiceExecCache.TryGetValue(requestType /*serviceType */, out mi))
{
//TODO: vdaron : fix next line if not obsolete
//var genericType = typeof(ServiceExec<>).MakeGenericType(requestType);
//mi = genericType.GetMethod("Execute", BindingFlags.Public | BindingFlags.Static);
ServiceExecCache.Add(requestType /* serviceType */, mi);
}
}
return mi;
}
public static MethodInfo GetRunTimeExecMethod(Type serviceType, Type requestType, EndpointAttributes attrs)
{
if ((attrs & EndpointAttributes.OneWay) == EndpointAttributes.OneWay)
{
var mi = serviceType.GetMethod(ExecuteAsync, new[] { requestType });
if (mi != null) return mi;
}
else if ((attrs & EndpointAttributes.HttpGet) == EndpointAttributes.HttpGet)
{
var mi = serviceType.GetMethod(RestGet, new[] { requestType });
if (mi != null) return mi;
}
else if ((attrs & EndpointAttributes.HttpPost) == EndpointAttributes.HttpPost)
{
var mi = serviceType.GetMethod(RestPost, new[] { requestType });
if (mi != null) return mi;
}
else if ((attrs & EndpointAttributes.HttpPut) == EndpointAttributes.HttpPut)
{
var mi = serviceType.GetMethod(RestPut, new[] { requestType });
if (mi != null) return mi;
}
else if ((attrs & EndpointAttributes.HttpDelete) == EndpointAttributes.HttpDelete)
{
var mi = serviceType.GetMethod(RestDelete, new[] { requestType });
if (mi != null) return mi;
}
else if ((attrs & EndpointAttributes.HttpPatch) == EndpointAttributes.HttpPatch)
{
var mi = serviceType.GetMethod(RestPatch, new[] { requestType });
if (mi != null) return mi;
}
return serviceType.GetMethod(Execute, new[] { requestType });
}
}
//Remark: Obsolete code
// public class ServiceExec<TReq>
// {
// public const string ExecuteMethodName = "Execute";
//
// public static object Execute(IService<TReq> service, TReq request, EndpointAttributes attrs)
// {
// if ((attrs & EndpointAttributes.OneWay) == EndpointAttributes.OneWay)
// {
// var asyncService = service as IAsyncService<TReq>;
// if (asyncService != null) return asyncService.ExecuteAsync(request);
// }
// else if ((attrs & EndpointAttributes.HttpGet) == EndpointAttributes.HttpGet)
// {
// var restService = service as IRestGetService<TReq>;
// if (restService != null) return restService.Get(request);
// }
// else if ((attrs & EndpointAttributes.HttpPost) == EndpointAttributes.HttpPost)
// {
// var restService = service as IRestPostService<TReq>;
// if (restService != null) return restService.Post(request);
// }
// else if ((attrs & EndpointAttributes.HttpPut) == EndpointAttributes.HttpPut)
// {
// var restService = service as IRestPutService<TReq>;
// if (restService != null) return restService.Put(request);
// }
// else if ((attrs & EndpointAttributes.HttpDelete) == EndpointAttributes.HttpDelete)
// {
// var restService = service as IRestDeleteService<TReq>;
// if (restService != null) return restService.Delete(request);
// }
// else if ((attrs & EndpointAttributes.HttpOptions) == EndpointAttributes.HttpOptions)
// {
// var restService = service as IRestOptionsService<TReq>;
// if (restService != null) return restService.Options(request);
// }
// else if ((attrs & EndpointAttributes.HttpPatch) == EndpointAttributes.HttpPatch)
// {
// var restService = service as IRestPatchService<TReq>;
// if (restService != null) return restService.Patch(request);
// }
// return service.Execute(request);
// }
// }
}