forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelManager.cs
More file actions
340 lines (281 loc) · 12.5 KB
/
Copy pathModelManager.cs
File metadata and controls
340 lines (281 loc) · 12.5 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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using System.Collections.ObjectModel;
using JSONAPI.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JSONAPI.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace JSONAPI.Core
{
public class ModelManager : IModelManager
{
public ModelManager(IPluralizationService pluralizationService)
{
_pluralizationService = pluralizationService;
RegistrationsByName = new Dictionary<string, TypeRegistration>();
RegistrationsByType = new Dictionary<Type, TypeRegistration>();
}
protected IPluralizationService _pluralizationService = null;
public IPluralizationService PluralizationService
{
get
{
return _pluralizationService;
}
}
#region Cache storage
/// <summary>
/// Represents a type's registration with a model manager
/// </summary>
protected sealed class TypeRegistration
{
internal TypeRegistration() { }
/// <summary>
/// The type that has been registered
/// </summary>
public Type Type { get; internal set; }
/// <summary>
/// The serialized format of the type's name
/// </summary>
public string ResourceTypeName { get; internal set; }
/// <summary>
/// The property to be used as this type's ID.
/// </summary>
public PropertyInfo IdProperty { get; internal set; }
/// <summary>
/// A resource's properties, keyed by name.
/// </summary>
public IReadOnlyDictionary<string, ModelProperty> Properties { get; internal set; }
}
protected readonly IDictionary<string, TypeRegistration> RegistrationsByName;
protected readonly IDictionary<Type, TypeRegistration> RegistrationsByType;
protected Lazy<Dictionary<Type, bool>> _isSerializedAsMany
= new Lazy<Dictionary<Type, bool>>(
() => new Dictionary<Type, bool>()
);
protected Lazy<Dictionary<Type, Type>> _getElementType
= new Lazy<Dictionary<Type, Type>>(
() => new Dictionary<Type, Type>()
);
#endregion
public PropertyInfo GetIdProperty(Type type)
{
return GetRegistrationByType(type).IdProperty;
}
public ModelProperty[] GetProperties(Type type)
{
var typeRegistration = GetRegistrationByType(type);
return typeRegistration.Properties.Values.ToArray();
}
public ModelProperty GetPropertyForJsonKey(Type type, string jsonKey)
{
var typeRegistration = GetRegistrationByType(type);
ModelProperty property;
return (typeRegistration.Properties.TryGetValue(jsonKey, out property)) ? property : null;
}
public string GetResourceTypeNameForType(Type type)
{
return GetRegistrationByType(type).ResourceTypeName;
}
public Type GetTypeByResourceTypeName(string resourceTypeName)
{
lock (RegistrationsByName)
{
TypeRegistration typeRegistration;
if (RegistrationsByName.TryGetValue(resourceTypeName, out typeRegistration))
return typeRegistration.Type;
throw new InvalidOperationException(String.Format("The resource type name `{0}` was not registered.",
resourceTypeName));
}
}
public bool TypeIsRegistered(Type type)
{
var registration = FindRegistrationForType(type);
return registration != null;
}
private TypeRegistration FindRegistrationForType(Type type)
{
lock (RegistrationsByType)
{
if (IsSerializedAsMany(type))
type = GetElementType(type);
var currentType = type;
while (currentType != null && currentType != typeof(Object))
{
TypeRegistration registration;
if (RegistrationsByType.TryGetValue(currentType, out registration))
return registration;
// This particular type wasn't registered, but maybe the base type was.
currentType = currentType.BaseType;
}
}
return null;
}
private TypeRegistration GetRegistrationByType(Type type)
{
var registration = FindRegistrationForType(type);
if (registration != null) return registration;
throw new InvalidOperationException(String.Format("The type `{0}` was not registered.", type.FullName));
}
/// <summary>
/// Registers a type with this ModelManager.
/// </summary>
/// <param name="type">The type to register.</param>
public ModelManager RegisterResourceType(Type type)
{
var resourceTypeName = CalculateResourceTypeNameForType(type);
return RegisterResourceType(type, resourceTypeName);
}
/// <summary>
/// Registeres a type with this ModelManager, using a default resource type name.
/// </summary>
/// <param name="type">The type to register.</param>
/// <param name="resourceTypeName">The resource type name to use</param>
public ModelManager RegisterResourceType(Type type, string resourceTypeName)
{
lock (RegistrationsByType)
{
lock (RegistrationsByName)
{
if (RegistrationsByType.ContainsKey(type))
throw new InvalidOperationException(String.Format("The type `{0}` has already been registered.",
type.FullName));
if (RegistrationsByName.ContainsKey(resourceTypeName))
throw new InvalidOperationException(
String.Format("The resource type name `{0}` has already been registered.", resourceTypeName));
var registration = new TypeRegistration
{
Type = type,
ResourceTypeName = resourceTypeName
};
var propertyMap = new Dictionary<string, ModelProperty>();
var idProperty = CalculateIdProperty(type);
if (idProperty == null)
throw new InvalidOperationException(String.Format(
"Unable to determine Id property for type `{0}`.", resourceTypeName));
registration.IdProperty = idProperty;
var props = type.GetProperties();
foreach (var prop in props)
{
var jsonKey = prop == registration.IdProperty
? "id"
: CalculateJsonKeyForProperty(prop);
if (propertyMap.ContainsKey(jsonKey))
throw new InvalidOperationException(
String.Format("The type `{0}` already contains a property keyed at `{1}`.",
resourceTypeName, jsonKey));
var property = CreateModelProperty(prop, jsonKey);
propertyMap[jsonKey] = property;
}
registration.Properties = new ReadOnlyDictionary<string, ModelProperty>(propertyMap);
RegistrationsByType.Add(type, registration);
RegistrationsByName.Add(resourceTypeName, registration);
}
}
return this;
}
/// <summary>
/// Creates a cacheable model property representation from a PropertyInfo
/// </summary>
/// <param name="prop">The property</param>
/// <param name="jsonKey">The key that this model property will be serialized as</param>
/// <returns>A model property represenation</returns>
protected virtual ModelProperty CreateModelProperty(PropertyInfo prop, string jsonKey)
{
var type = prop.PropertyType;
var ignoreByDefault =
prop.CustomAttributes.Any(c => c.AttributeType == typeof(JsonIgnoreAttribute));
if (prop.PropertyType.CanWriteAsJsonApiAttribute())
return new FieldModelProperty(prop, jsonKey, ignoreByDefault);
var isToMany =
type.IsArray ||
(type.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) && type.IsGenericType);
Type relatedType;
if (isToMany)
{
relatedType = type.IsGenericType ? type.GetGenericArguments()[0] : type.GetElementType();
}
else
{
relatedType = type;
}
return new RelationshipModelProperty(prop, jsonKey, ignoreByDefault, relatedType, isToMany);
}
/// <summary>
/// Determines the resource type name for a given type.
/// </summary>
/// <param name="type">The type to calculate the resouce type name for</param>
/// <returns>The type's resource type name</returns>
protected virtual string CalculateResourceTypeNameForType(Type type)
{
var attrs = type.CustomAttributes.Where(x => x.AttributeType == typeof(Newtonsoft.Json.JsonObjectAttribute)).ToList();
string title = type.Name;
if (attrs.Any())
{
var titles = attrs.First().NamedArguments.Where(arg => arg.MemberName == "Title")
.Select(arg => arg.TypedValue.Value.ToString()).ToList();
if (titles.Any()) title = titles.First();
}
return FormatPropertyName(PluralizationService.Pluralize(title)).Dasherize();
}
/// <summary>
/// Determines the key that a property will be serialized as.
/// </summary>
/// <param name="propInfo">The property</param>
/// <returns>The key to serialize the given property as</returns>
protected internal virtual string CalculateJsonKeyForProperty(PropertyInfo propInfo)
{
var jsonPropertyAttribute = (JsonPropertyAttribute)propInfo.GetCustomAttributes(typeof (JsonPropertyAttribute)).FirstOrDefault();
return jsonPropertyAttribute != null ? jsonPropertyAttribute.PropertyName : FormatPropertyName(propInfo.Name);
}
private static string FormatPropertyName(string propertyName)
{
string result = propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
return result;
}
public bool IsSerializedAsMany(Type type)
{
bool isMany;
var isManyCache = _isSerializedAsMany.Value;
lock (isManyCache)
{
if (isManyCache.TryGetValue(type, out isMany)) return isMany;
isMany =
type.IsArray ||
(type.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) && type.IsGenericType);
isManyCache.Add(type, isMany);
}
return isMany;
}
public Type GetElementType(Type manyType)
{
Type etype = null;
var etypeCache = _getElementType.Value;
lock (etypeCache)
{
if (etypeCache.TryGetValue(manyType, out etype)) return etype;
if (manyType.IsGenericType)
etype = manyType.GetGenericArguments()[0];
else
etype = manyType.GetElementType();
etypeCache.Add(manyType, etype);
}
return etype;
}
/// <summary>
/// Calculates the ID property for a given resource type.
/// </summary>
/// <param name="type">The type to use to calculate the ID for</param>
/// <returns>The ID property to use for this type</returns>
protected virtual PropertyInfo CalculateIdProperty(Type type)
{
return
type
.GetProperties()
.FirstOrDefault(p => p.CustomAttributes.Any(attr => attr.AttributeType == typeof(UseAsIdAttribute)))
?? type.GetProperty("Id");
}
}
}