Skip to content

Commit 5558fcf

Browse files
committed
Incorporated feedback from #25, including removing unneeded default constructors. Added caching to IsSerializedAsMany and GetElementType. Made ModelManager a little more subclassing-friendly.
1 parent 07a1639 commit 5558fcf

File tree

5 files changed

+52
-30
lines changed

5 files changed

+52
-30
lines changed

JSONAPI.Tests/Core/ModelManagerTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ private class InvalidModel // No Id discernable!
2020
public void FindsIdNamedId()
2121
{
2222
// Arrange
23-
var mm = new ModelManager();
23+
var mm = new ModelManager(new PluralizationService());
2424

2525
// Act
2626
PropertyInfo idprop = mm.GetIdProperty(typeof(Author));
@@ -34,7 +34,7 @@ public void FindsIdNamedId()
3434
public void DoesntFindMissingId()
3535
{
3636
// Arrange
37-
var mm = new ModelManager();
37+
var mm = new ModelManager(new PluralizationService());
3838

3939
// Act
4040
PropertyInfo idprop = mm.GetIdProperty(typeof(InvalidModel));
@@ -104,7 +104,7 @@ public void GetPropertyForJsonKeyTest()
104104
public void IsSerializedAsManyTest()
105105
{
106106
// Arrange
107-
var mm = new ModelManager();
107+
var mm = new ModelManager(new PluralizationService());
108108

109109
// Act
110110
bool isArray = mm.IsSerializedAsMany(typeof(Post[]));
@@ -125,7 +125,7 @@ public void IsSerializedAsManyTest()
125125
public void GetElementTypeTest()
126126
{
127127
// Arrange
128-
var mm = new ModelManager();
128+
var mm = new ModelManager(new PluralizationService());
129129

130130
// Act
131131
Type postTypeFromArray = mm.GetElementType(typeof(Post[]));
@@ -140,7 +140,7 @@ public void GetElementTypeTest()
140140
public void GetElementTypeInvalidArgumentTest()
141141
{
142142
// Arrange
143-
var mm = new ModelManager();
143+
var mm = new ModelManager(new PluralizationService());
144144

145145
// Act
146146
Type x = mm.GetElementType(typeof(Author));

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private enum TestEnum
107107
public void CanWritePrimitiveTest()
108108
{
109109
// Arrange
110-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
110+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new PluralizationService());
111111
// Act
112112
// Assert
113113
Assert.IsTrue(formatter.CanWriteTypeAsPrimitive(typeof(Int32)), "CanWriteTypeAsPrimitive returned wrong answer for Integer!");

JSONAPI/Core/IModelManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public interface IModelManager
4242
string GetJsonKeyForProperty(PropertyInfo propInfo); //TODO: Do we need to have a type parameter here, in case the property is inherited?
4343

4444
/// <summary>
45-
/// Returns the property corresponding to a given JSON Key. Inverse of GetJsonKeyForType.
45+
/// Returns the property corresponding to a given JSON Key. Inverse of GetJsonKeyForProperty.
4646
/// </summary>
4747
/// <param name="type">The Type to find the property on</param>
4848
/// <param name="jsonKey">The JSON key representing a property</param>

JSONAPI/Core/ModelManager.cs

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@ namespace JSONAPI.Core
1010
{
1111
public class ModelManager : IModelManager
1212
{
13-
public ModelManager() {
14-
_pluralizationService = new PluralizationService();
15-
}
16-
1713
public ModelManager(IPluralizationService pluralizationService)
1814
{
1915
_pluralizationService = pluralizationService;
2016
}
2117

22-
private IPluralizationService _pluralizationService = null;
18+
protected IPluralizationService _pluralizationService = null;
2319
public IPluralizationService PluralizationService
2420
{
2521
get
@@ -30,21 +26,31 @@ public IPluralizationService PluralizationService
3026

3127
#region Cache storage
3228

33-
private Lazy<Dictionary<Type, PropertyInfo>> _idProperties
29+
protected Lazy<Dictionary<Type, PropertyInfo>> _idProperties
3430
= new Lazy<Dictionary<Type,PropertyInfo>>(
3531
() => new Dictionary<Type, PropertyInfo>()
3632
);
3733

38-
private Lazy<Dictionary<Type, Dictionary<string, PropertyInfo>>> _propertyMaps
34+
protected Lazy<Dictionary<Type, Dictionary<string, PropertyInfo>>> _propertyMaps
3935
= new Lazy<Dictionary<Type, Dictionary<string, PropertyInfo>>>(
4036
() => new Dictionary<Type, Dictionary<string, PropertyInfo>>()
4137
);
4238

43-
private Lazy<Dictionary<Type, string>> _jsonKeysForType
39+
protected Lazy<Dictionary<Type, string>> _jsonKeysForType
4440
= new Lazy<Dictionary<Type, string>>(
4541
() => new Dictionary<Type, string>()
4642
);
4743

44+
protected Lazy<Dictionary<Type, bool>> _isSerializedAsMany
45+
= new Lazy<Dictionary<Type, bool>>(
46+
() => new Dictionary<Type, bool>()
47+
);
48+
49+
protected Lazy<Dictionary<Type, Type>> _getElementType
50+
= new Lazy<Dictionary<Type, Type>>(
51+
() => new Dictionary<Type, Type>()
52+
);
53+
4854
#endregion
4955

5056
#region Id property determination
@@ -76,7 +82,7 @@ public PropertyInfo GetIdProperty(Type type)
7682

7783
#region Property Maps
7884

79-
protected IDictionary<string, PropertyInfo> GetPropertyMap(Type type) //FIXME: Will become protected
85+
protected IDictionary<string, PropertyInfo> GetPropertyMap(Type type)
8086
{
8187
Dictionary<string, PropertyInfo> propMap = null;
8288

@@ -158,20 +164,41 @@ protected static string FormatPropertyName(string propertyName)
158164

159165
public bool IsSerializedAsMany(Type type)
160166
{
161-
bool isMany =
162-
type.IsArray ||
163-
(type.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) && type.IsGenericType);
167+
bool isMany;
168+
169+
var isManyCache = _isSerializedAsMany.Value;
170+
171+
lock (isManyCache)
172+
{
173+
if (isManyCache.TryGetValue(type, out isMany)) return isMany;
174+
175+
isMany =
176+
type.IsArray ||
177+
(type.GetInterfaces().Contains(typeof(System.Collections.IEnumerable)) && type.IsGenericType);
178+
179+
isManyCache.Add(type, isMany);
180+
}
164181

165182
return isMany;
166183
}
167184

168185
public Type GetElementType(Type manyType)
169186
{
170187
Type etype = null;
171-
if (manyType.IsGenericType)
172-
etype = manyType.GetGenericArguments()[0];
173-
else
174-
etype = manyType.GetElementType();
188+
189+
var etypeCache = _getElementType.Value;
190+
191+
lock (etypeCache)
192+
{
193+
if (etypeCache.TryGetValue(manyType, out etype)) return etype;
194+
195+
if (manyType.IsGenericType)
196+
etype = manyType.GetGenericArguments()[0];
197+
else
198+
etype = manyType.GetElementType();
199+
200+
etypeCache.Add(manyType, etype);
201+
}
175202

176203
return etype;
177204
}

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ namespace JSONAPI.Json
1919
{
2020
public class JsonApiFormatter : JsonMediaTypeFormatter
2121
{
22-
public JsonApiFormatter()
23-
: this(new ModelManager(), new ErrorSerializer())
24-
{
25-
}
26-
2722
public JsonApiFormatter(IModelManager modelManager) :
2823
this(modelManager, new ErrorSerializer())
2924
{
@@ -36,7 +31,7 @@ public JsonApiFormatter(IPluralizationService pluralizationService) :
3631

3732
// Currently for tests only.
3833
internal JsonApiFormatter(IErrorSerializer errorSerializer)
39-
: this(new ModelManager(), errorSerializer)
34+
: this(new ModelManager(new PluralizationService()), errorSerializer)
4035
{
4136

4237
}
@@ -48,7 +43,7 @@ internal JsonApiFormatter(IModelManager modelManager, IErrorSerializer errorSeri
4843
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.api+json"));
4944
}
5045

51-
[Obsolete]
46+
[Obsolete("Use ModelManager.PluralizationService instead")]
5247
public IPluralizationService PluralizationService //FIXME: Deprecated, will be removed shortly
5348
{
5449
get

0 commit comments

Comments
 (0)