/
BatchApiControllerBuilder.cs
173 lines (149 loc) · 5.98 KB
/
BatchApiControllerBuilder.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Linq;
using System.Reflection;
using System.Web.Http.Filters;
using Abp.Application.Services;
using Abp.Dependency;
using Abp.Extensions;
namespace Abp.WebApi.Controllers.Dynamic.Builders
{
/// <summary>
/// This interface is used to define a dynamic api controllers.
/// </summary>
/// <typeparam name="T">Type of the proxied object</typeparam>
internal class BatchApiControllerBuilder<T> : IBatchApiControllerBuilder<T>
{
private readonly string _servicePrefix;
private readonly Assembly _assembly;
private IFilter[] _filters;
private Func<Type, string> _serviceNameSelector;
private Func<Type, bool> _typePredicate;
private bool _conventionalVerbs;
private Action<IApiControllerActionBuilder<T>> _forMethodsAction;
private bool? _isApiExplorerEnabled;
private readonly IIocResolver _iocResolver;
private readonly IDynamicApiControllerBuilder _dynamicApiControllerBuilder;
private bool? _isProxyScriptingEnabled;
public BatchApiControllerBuilder(
IIocResolver iocResolver,
IDynamicApiControllerBuilder dynamicApiControllerBuilder,
Assembly assembly,
string servicePrefix)
{
_iocResolver = iocResolver;
_dynamicApiControllerBuilder = dynamicApiControllerBuilder;
_assembly = assembly;
_servicePrefix = servicePrefix;
}
public IBatchApiControllerBuilder<T> Where(Func<Type, bool> predicate)
{
_typePredicate = predicate;
return this;
}
public IBatchApiControllerBuilder<T> WithFilters(params IFilter[] filters)
{
_filters = filters;
return this;
}
public IBatchApiControllerBuilder<T> WithApiExplorer(bool isEnabled)
{
_isApiExplorerEnabled = isEnabled;
return this;
}
public IBatchApiControllerBuilder<T> WithProxyScripts(bool isEnabled)
{
_isProxyScriptingEnabled = isEnabled;
return this;
}
public IBatchApiControllerBuilder<T> WithServiceName(Func<Type, string> serviceNameSelector)
{
_serviceNameSelector = serviceNameSelector;
return this;
}
public IBatchApiControllerBuilder<T> ForMethods(Action<IApiControllerActionBuilder> action)
{
_forMethodsAction = action;
return this;
}
public IBatchApiControllerBuilder<T> WithConventionalVerbs()
{
_conventionalVerbs = true;
return this;
}
public void Build()
{
var types =
from
type in _assembly.GetTypes()
where
(type.IsPublic || type.IsNestedPublic) &&
type.IsInterface &&
typeof(T).IsAssignableFrom(type) &&
_iocResolver.IsRegistered(type) &&
!RemoteServiceAttribute.IsExplicitlyDisabledFor(type)
select
type;
if (_typePredicate != null)
{
types = types.Where(t => _typePredicate(t));
}
foreach (var type in types)
{
var serviceName = _serviceNameSelector != null
? _serviceNameSelector(type)
: GetConventionalServiceName(type);
if (!string.IsNullOrWhiteSpace(_servicePrefix))
{
serviceName = _servicePrefix + "/" + serviceName;
}
var builder = typeof(IDynamicApiControllerBuilder)
.GetMethod("For", BindingFlags.Public | BindingFlags.Instance)
.MakeGenericMethod(type)
.Invoke(_dynamicApiControllerBuilder, new object[] { serviceName });
if (_filters != null)
{
builder.GetType()
.GetMethod("WithFilters", BindingFlags.Public | BindingFlags.Instance)
.Invoke(builder, new object[] { _filters });
}
if (_isApiExplorerEnabled != null)
{
builder.GetType()
.GetMethod("WithApiExplorer", BindingFlags.Public | BindingFlags.Instance)
.Invoke(builder, new object[] { _isApiExplorerEnabled });
}
if (_isProxyScriptingEnabled != null)
{
builder.GetType()
.GetMethod("WithProxyScripts", BindingFlags.Public | BindingFlags.Instance)
.Invoke(builder, new object[] { _isProxyScriptingEnabled.Value });
}
if (_conventionalVerbs)
{
builder.GetType()
.GetMethod("WithConventionalVerbs", BindingFlags.Public | BindingFlags.Instance)
.Invoke(builder, new object[0]);
}
if (_forMethodsAction != null)
{
builder.GetType()
.GetMethod("ForMethods", BindingFlags.Public | BindingFlags.Instance)
.Invoke(builder, new object[] { _forMethodsAction });
}
builder.GetType()
.GetMethod("Build", BindingFlags.Public | BindingFlags.Instance)
.Invoke(builder, new object[0]);
}
}
public static string GetConventionalServiceName(Type type)
{
var typeName = type.Name;
typeName = typeName.RemovePostFix(ApplicationService.CommonPostfixes);
if (typeName.Length > 1 && typeName.StartsWith("I") && char.IsUpper(typeName, 1))
{
typeName = typeName.Substring(1);
}
return typeName.ToCamelCase();
}
}
}