Skip to content

Commit 2697a18

Browse files
committed
ApiActionDescriptor优化
1 parent 08cbe5a commit 2697a18

File tree

10 files changed

+217
-113
lines changed

10 files changed

+217
-113
lines changed

WebApiClient/ApiParameterDescriptor.cs

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,7 @@ public class ApiParameterDescriptor : ICloneable
3838
/// <summary>
3939
/// 获取参数类型是否为HttpContent类型
4040
/// </summary>
41-
public bool IsHttpContent { get; internal set; }
42-
43-
/// <summary>
44-
/// 获取参数类型是否为简单类型
45-
/// </summary>
46-
public bool IsSimpleType { get; internal set; }
47-
48-
/// <summary>
49-
/// 获取参数类型是否为可列举类型
50-
/// </summary>
51-
public bool IsEnumerable { get; internal set; }
52-
53-
/// <summary>
54-
/// 获取参数类型是否为IDictionaryOf(string,object)
55-
/// </summary>
56-
public bool IsDictionaryOfObject { get; internal set; }
57-
58-
/// <summary>
59-
/// 获取参数类型是否为IDictionaryOf(string,string)
60-
/// </summary>
61-
public bool IsDictionaryOfString { get; internal set; }
41+
public bool IsHttpContent { get; internal set; }
6242

6343
/// <summary>
6444
/// 获取参数类型是否为IApiParameterable类型
@@ -91,10 +71,6 @@ public object Clone()
9171
Index = this.Index,
9272
IsApiParameterable = this.IsApiParameterable,
9373
IsHttpContent = this.IsHttpContent,
94-
IsSimpleType = this.IsSimpleType,
95-
IsEnumerable = this.IsEnumerable,
96-
IsDictionaryOfObject = this.IsDictionaryOfObject,
97-
IsDictionaryOfString = this.IsDictionaryOfString,
9874
Name = this.Name,
9975
ParameterType = this.ParameterType,
10076
Value = this.Value

WebApiClient/Interfaces/IJsonFormatter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ namespace WebApiClient
1212
public interface IJsonFormatter
1313
{
1414
/// <summary>
15-
/// 将参数值序列化为json文本
15+
/// 将对象序列化为json文本
1616
/// </summary>
1717
/// <param name="obj">对象</param>
1818
/// <param name="options">选项</param>
1919
/// <returns></returns>
2020
string Serialize(object obj, FormatOptions options);
2121

2222
/// <summary>
23-
/// 将接口回复的内容反序列化对象
23+
/// 将json文本反序列化对象
2424
/// </summary>
25-
/// <param name="content">json文本内容</param>
25+
/// <param name="json">json文本内容</param>
2626
/// <param name="objType">对象类型</param>
2727
/// <returns></returns>
28-
object Deserialize(string content, Type objType);
28+
object Deserialize(string json, Type objType);
2929
}
3030
}

WebApiClient/Interfaces/IKeyValueFormatter.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77
namespace WebApiClient
88
{
99
/// <summary>
10-
/// 定义参数转换为键值对的行为
10+
/// 定义将对象转换为键值对的行为
1111
/// </summary>
1212
public interface IKeyValueFormatter
1313
{
1414
/// <summary>
15-
/// 序列化模型对象为键值对
15+
/// 序列化对象为键值对
1616
/// </summary>
17-
/// <param name="model">对象</param>
17+
/// <param name="name">对象名称</param>
18+
/// <param name="obj">对象实例</param>
1819
/// <param name="options">选项</param>
1920
/// <returns></returns>
20-
IEnumerable<KeyValuePair<string, string>> Serialize(object model, FormatOptions options);
21+
IEnumerable<KeyValuePair<string, string>> Serialize(string name, object obj, FormatOptions options);
2122

2223
/// <summary>
23-
/// 将参数值序列化为键值对
24+
/// 序列化参数为键值对
2425
/// </summary>
2526
/// <param name="parameter">参数</param>
2627
/// <param name="options">选项</param>

WebApiClient/Interfaces/IXmlFormatter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ namespace WebApiClient
1212
public interface IXmlFormatter
1313
{
1414
/// <summary>
15-
/// 将参数值序列化为xml文本
15+
/// 将对象序列化为xml文本
1616
/// </summary>
1717
/// <param name="obj">对象</param>
1818
/// <param name="encoding">编码</param>
1919
/// <returns></returns>
2020
string Serialize(object obj, Encoding encoding);
2121

2222
/// <summary>
23-
/// 将接口回复的内容反序列化对象
23+
/// 将xml文本反序列化对象
2424
/// </summary>
25-
/// <param name="content">xml文本内容</param>
25+
/// <param name="xml">xml文本内容</param>
2626
/// <param name="objType">对象类型</param>
2727
/// <returns></returns>
28-
object Deserialize(string content, Type objType);
28+
object Deserialize(string xml, Type objType);
2929
}
3030
}

WebApiClient/Internal/ApiDescriptorCache.cs

Lines changed: 88 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ private static ApiActionDescriptor GetActionDescriptor(MethodInfo method)
4848
{
4949
var actionAttributes = method
5050
.FindDeclaringAttributes<IApiActionAttribute>(true)
51-
.Distinct(new AttributeComparer<IApiActionAttribute>())
51+
.Distinct(new MultiplableComparer<IApiActionAttribute>())
5252
.OrderBy(item => item.OrderIndex)
5353
.ToArray();
5454

5555
var filterAttributes = method
5656
.FindDeclaringAttributes<IApiActionFilterAttribute>(true)
57-
.Distinct(new AttributeComparer<IApiActionFilterAttribute>())
57+
.Distinct(new MultiplableComparer<IApiActionFilterAttribute>())
5858
.OrderBy(item => item.OrderIndex)
5959
.ToArray();
6060

@@ -81,34 +81,32 @@ private static ApiParameterDescriptor GetParameterDescriptor(ParameterInfo param
8181

8282
var descriptor = new ApiParameterDescriptor
8383
{
84+
Attributes = null,
8485
Value = null,
8586
Name = parameterName,
8687
Index = parameter.Position,
8788
ParameterType = parameterType,
8889
IsApiParameterable = parameterType.IsInheritFrom<IApiParameterable>() || parameterType.IsInheritFrom<IEnumerable<IApiParameterable>>(),
8990
IsHttpContent = parameterType.IsInheritFrom<HttpContent>(),
90-
IsSimpleType = parameterType.IsSimple(),
91-
IsEnumerable = parameterType.IsInheritFrom<IEnumerable>(),
92-
IsDictionaryOfObject = parameterType.IsInheritFrom<IDictionary<string, object>>(),
93-
IsDictionaryOfString = parameterType.IsInheritFrom<IDictionary<string, string>>(),
94-
Attributes = parameter.GetAttributes<IApiParameterAttribute>(true).ToArray()
9591
};
9692

97-
if (descriptor.Attributes.Length == 0)
93+
var defined = parameter.GetAttributes<IApiParameterAttribute>(true);
94+
var attributes = new ParameterAttributeCollection(defined);
95+
96+
if (descriptor.IsApiParameterable == true)
9897
{
99-
if (descriptor.IsApiParameterable == true)
100-
{
101-
descriptor.Attributes = new[] { new ParameterableAttribute() };
102-
}
103-
else if (descriptor.IsHttpContent == true)
104-
{
105-
descriptor.Attributes = new[] { new HttpContentAttribute() };
106-
}
107-
else
108-
{
109-
descriptor.Attributes = new[] { new PathQueryAttribute() };
110-
}
98+
attributes.Add(new ParameterableAttribute());
11199
}
100+
else if (descriptor.IsHttpContent == true)
101+
{
102+
attributes.AddIfNotExists(new HttpContentAttribute());
103+
}
104+
else if (attributes.Count == 0)
105+
{
106+
attributes.Add(new PathQueryAttribute());
107+
}
108+
109+
descriptor.Attributes = attributes.ToArray();
112110
return descriptor;
113111
}
114112

@@ -138,17 +136,83 @@ private static ApiReturnDescriptor GetReturnDescriptor(MethodInfo method)
138136
}
139137

140138
/// <summary>
141-
/// 特性比较器
139+
/// 表示参数特性集合
140+
/// </summary>
141+
private class ParameterAttributeCollection
142+
{
143+
/// <summary>
144+
/// 特性列表
145+
/// </summary>
146+
private readonly List<IApiParameterAttribute> attribueList = new List<IApiParameterAttribute>();
147+
148+
/// <summary>
149+
/// 获取元素数量
150+
/// </summary>
151+
public int Count
152+
{
153+
get
154+
{
155+
return this.attribueList.Count;
156+
}
157+
}
158+
159+
/// <summary>
160+
/// 参数特性集合
161+
/// </summary>
162+
/// <param name="defined">声明的特性</param>
163+
public ParameterAttributeCollection(IEnumerable<IApiParameterAttribute> defined)
164+
{
165+
this.attribueList.AddRange(defined);
166+
}
167+
168+
/// <summary>
169+
/// 添加新特性
170+
/// </summary>
171+
/// <param name="attribute"></param>
172+
public void Add(IApiParameterAttribute attribute)
173+
{
174+
this.attribueList.Add(attribute);
175+
}
176+
177+
/// <summary>
178+
/// 添加新特性
179+
/// </summary>
180+
/// <param name="attribute"></param>
181+
/// <returns></returns>
182+
public bool AddIfNotExists(IApiParameterAttribute attribute)
183+
{
184+
var type = attribute.GetType();
185+
if (this.attribueList.Any(item => item.GetType() == type) == true)
186+
{
187+
return false;
188+
}
189+
190+
this.attribueList.Add(attribute);
191+
return true;
192+
}
193+
194+
/// <summary>
195+
/// 转换为数组
196+
/// </summary>
197+
/// <returns></returns>
198+
public IApiParameterAttribute[] ToArray()
199+
{
200+
return this.attribueList.ToArray();
201+
}
202+
}
203+
204+
/// <summary>
205+
/// 是否允许重复的特性比较器
142206
/// </summary>
143-
private class AttributeComparer<T> : IEqualityComparer<T> where T : IAttributeMultiplable
207+
private class MultiplableComparer<TAttributeMultiplable> : IEqualityComparer<TAttributeMultiplable> where TAttributeMultiplable : IAttributeMultiplable
144208
{
145209
/// <summary>
146210
/// 是否相等
147211
/// </summary>
148212
/// <param name="x"></param>
149213
/// <param name="y"></param>
150214
/// <returns></returns>
151-
public bool Equals(T x, T y)
215+
public bool Equals(TAttributeMultiplable x, TAttributeMultiplable y)
152216
{
153217
// 如果其中一个不允许重复,返回true将y过滤
154218
return x.AllowMultiple == false || y.AllowMultiple == false;
@@ -159,7 +223,7 @@ public bool Equals(T x, T y)
159223
/// </summary>
160224
/// <param name="obj"></param>
161225
/// <returns></returns>
162-
public int GetHashCode(T obj)
226+
public int GetHashCode(TAttributeMultiplable obj)
163227
{
164228
return obj.GetType().GetHashCode();
165229
}

WebApiClient/Internal/DefaultJsonFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace WebApiClient
1919
class DefaultJsonFormatter : IJsonFormatter
2020
{
2121
/// <summary>
22-
/// 将参数值序列化为json文本
22+
/// 将对象列化为json文本
2323
/// </summary>
2424
/// <param name="obj">对象</param>
2525
/// <param name="options">选项</param>
@@ -45,7 +45,7 @@ public string Serialize(object obj, FormatOptions options)
4545
}
4646

4747
/// <summary>
48-
/// 反序列化对象
48+
/// 反序列化json为对象
4949
/// </summary>
5050
/// <param name="json">json</param>
5151
/// <param name="objType">对象类型</param>

0 commit comments

Comments
 (0)