Skip to content

Commit 721f98a

Browse files
committed
Much more refactoring. Moved PluralizationService, still have to move IsMany and GetSingleType.
1 parent e01060b commit 721f98a

File tree

8 files changed

+98
-57
lines changed

8 files changed

+98
-57
lines changed

JSONAPI.EntityFramework.Tests/EntityConverterTests.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,7 @@ public void SetupEntities()
106106
public void SerializeTest()
107107
{
108108
// Arrange
109-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
110-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
109+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
111110
MemoryStream stream = new MemoryStream();
112111

113112
// Act
@@ -124,8 +123,7 @@ public void SerializeTest()
124123
public void DeserializePostIntegrationTest()
125124
{
126125
// Arrange
127-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
128-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
126+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
129127
MemoryStream stream = new MemoryStream();
130128

131129
EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context);
@@ -159,8 +157,7 @@ public void DeserializePostIntegrationTest()
159157
public void UnderpostingTest()
160158
{
161159
// Arrange
162-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
163-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
160+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
164161
MemoryStream stream = new MemoryStream();
165162

166163
EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context);

JSONAPI.Tests/Core/MetadataManagerTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ public void PropertyWasPresentTest()
1515
{
1616
// Arrange
1717

18-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
19-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
18+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
2019
MemoryStream stream = new MemoryStream();
2120

2221
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""posts"":{""id"":42,""links"":{""author"":""18""}}}"));

JSONAPI.Tests/Core/ModelManagerTests.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public void DoesntFindMissingId()
4545
public void GetPropertyMapTest()
4646
{
4747
// Arrange
48+
var mm = new ModelManager();
49+
4850
// Act
49-
var propMap = ModelManager.Instance.GetPropertyMap(typeof(Post));
51+
var propMap = mm.GetPropertyMap(typeof(Post));
5052

5153
// Assert
5254
Assert.AreSame(typeof(Post).GetProperty("Id"), propMap["id"]);
@@ -58,11 +60,12 @@ public void GetJsonKeyForTypeTest()
5860
{
5961
// Arrange
6062
var pluralizationService = new PluralizationService();
63+
var mm = new ModelManager(pluralizationService);
6164

6265
// Act
63-
var postKey = ModelManager.Instance.GetJsonKeyForType(typeof(Post), pluralizationService);
64-
var authorKey = ModelManager.Instance.GetJsonKeyForType(typeof(Author), pluralizationService);
65-
var commentKey = ModelManager.Instance.GetJsonKeyForType(typeof(Comment), pluralizationService);
66+
var postKey = mm.GetJsonKeyForType(typeof(Post));
67+
var authorKey = mm.GetJsonKeyForType(typeof(Author));
68+
var commentKey = mm.GetJsonKeyForType(typeof(Comment));
6669

6770
// Assert
6871
Assert.AreEqual("posts", postKey);

JSONAPI.Tests/Json/JsonApiMediaFormaterTests.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ public void SerializerIntegrationTest()
133133
//ModelConverter mc = new ModelConverter();
134134
//ContractResolver.PluralizationService = new PluralizationService();
135135

136-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
137-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
136+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
138137
MemoryStream stream = new MemoryStream();
139138

140139
// Act
@@ -158,8 +157,7 @@ public void SerializeArrayIntegrationTest()
158157
//ModelConverter mc = new ModelConverter();
159158
//ContractResolver.PluralizationService = new PluralizationService();
160159

161-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
162-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
160+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
163161
MemoryStream stream = new MemoryStream();
164162

165163
// Act
@@ -180,7 +178,6 @@ public void Should_serialize_error()
180178
{
181179
// Arrange
182180
var formatter = new JSONAPI.Json.JsonApiFormatter(new MockErrorSerializer());
183-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
184181
var stream = new MemoryStream();
185182

186183
// Act
@@ -199,8 +196,7 @@ public void Should_serialize_error()
199196
public void SerializeErrorIntegrationTest()
200197
{
201198
// Arrange
202-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
203-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
199+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
204200
MemoryStream stream = new MemoryStream();
205201

206202
// Act
@@ -224,8 +220,7 @@ public void SerializeErrorIntegrationTest()
224220
public void DeserializeCollectionIntegrationTest()
225221
{
226222
// Arrange
227-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
228-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
223+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
229224
MemoryStream stream = new MemoryStream();
230225

231226
formatter.WriteToStreamAsync(typeof(Post), new List<Post> {p, p2}, stream, (System.Net.Http.HttpContent)null, (System.Net.TransportContext)null);
@@ -246,8 +241,7 @@ public void DeserializeCollectionIntegrationTest()
246241
[TestMethod(), Timeout(1000)]
247242
public void DeserializeExtraPropertyTest()
248243
{
249-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
250-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
244+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
251245
MemoryStream stream = new MemoryStream();
252246

253247
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""authors"":{""id"":13,""name"":""Jason Hater"",""bogus"":""PANIC!"",""links"":{""posts"":[]}}}"));
@@ -264,8 +258,7 @@ public void DeserializeExtraPropertyTest()
264258
[TestMethod(), Timeout(1000)]
265259
public void DeserializeExtraRelationshipTest()
266260
{
267-
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter();
268-
formatter.PluralizationService = new JSONAPI.Core.PluralizationService();
261+
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
269262
MemoryStream stream = new MemoryStream();
270263

271264
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(@"{""authors"":{""id"":13,""name"":""Jason Hater"",""links"":{""posts"":[],""bogus"":[""PANIC!""]}}}"));

JSONAPI.Tests/Json/LinkTemplateTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public void SetupModels()
5050
public void GetResourceWithLinkTemplateRelationship()
5151
{
5252
var formatter = new JsonApiFormatter
53-
{
54-
PluralizationService = new JSONAPI.Core.PluralizationService()
55-
};
53+
(
54+
new JSONAPI.Core.PluralizationService()
55+
);
5656
var stream = new MemoryStream();
5757

5858
formatter.WriteToStreamAsync(typeof(Post), ThePost, stream, null, null);

JSONAPI/Core/IModelManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ namespace JSONAPI.Core
99
{
1010
public interface IModelManager
1111
{
12+
IPluralizationService PluralizationService { get; }
13+
1214
PropertyInfo GetIdProperty(Type type);
15+
string GetJsonKeyForType(Type type);
16+
string GetJsonKeyForProperty(PropertyInfo propInfo); //TODO: Do we need to have a type parameter here, in case propInfo is inherited?
17+
PropertyInfo GetPropertyForJsonKey(Type type, string jsonKey);
18+
19+
[Obsolete]
20+
IDictionary<string, PropertyInfo> GetPropertyMap(Type type);
1321
}
1422
}

JSONAPI/Core/ModelManager.cs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,23 @@ namespace JSONAPI.Core
1010
{
1111
public class ModelManager : IModelManager
1212
{
13-
public ModelManager() { }
13+
public ModelManager() {
14+
_pluralizationService = new PluralizationService();
15+
}
16+
17+
public ModelManager(IPluralizationService pluralizationService)
18+
{
19+
_pluralizationService = pluralizationService;
20+
}
21+
22+
private IPluralizationService _pluralizationService = null;
23+
public IPluralizationService PluralizationService
24+
{
25+
get
26+
{
27+
return _pluralizationService;
28+
}
29+
}
1430

1531
#region Cache storage
1632

@@ -60,7 +76,7 @@ public PropertyInfo GetIdProperty(Type type)
6076

6177
#region Property Maps
6278

63-
public Dictionary<string, PropertyInfo> GetPropertyMap(Type type)
79+
public IDictionary<string, PropertyInfo> GetPropertyMap(Type type) //FIXME: Will become protected
6480
{
6581
Dictionary<string, PropertyInfo> propMap = null;
6682

@@ -74,7 +90,7 @@ public Dictionary<string, PropertyInfo> GetPropertyMap(Type type)
7490
PropertyInfo[] props = type.GetProperties();
7591
foreach (PropertyInfo prop in props)
7692
{
77-
propMap[JsonApiFormatter.FormatPropertyName(prop.Name)] = prop;
93+
propMap[GetJsonKeyForProperty(prop)] = prop;
7894
}
7995

8096
propMapCache.Add(type, propMap);
@@ -83,13 +99,20 @@ public Dictionary<string, PropertyInfo> GetPropertyMap(Type type)
8399
return propMap;
84100
}
85101

102+
public PropertyInfo GetPropertyForJsonKey(Type type, string jsonKey)
103+
{
104+
PropertyInfo propInfo;
105+
if (GetPropertyMap(type).TryGetValue(jsonKey, out propInfo)) return propInfo;
106+
else return null; // Or, throw an exception here??
107+
}
108+
86109
#endregion
87110

88111
//TODO: This has been "moved" here so we can cache the results and improve performance...but
89112
// it raises the question of whether the various methods called within here should belong
90113
// to JsonApiFormatter at all...should they move here also? Should the IPluralizationService
91114
// instance belong to ModelManager instead?
92-
internal string GetJsonKeyForType(Type type, IPluralizationService pluralizationService)
115+
public string GetJsonKeyForType(Type type)
93116
{
94117
string key = null;
95118

@@ -112,12 +135,24 @@ internal string GetJsonKeyForType(Type type, IPluralizationService pluralization
112135
if (titles.Any()) title = titles.First();
113136
}
114137

115-
key = JsonApiFormatter.FormatPropertyName(pluralizationService.Pluralize(title));
138+
key = FormatPropertyName(PluralizationService.Pluralize(title));
116139

117140
keyCache.Add(type, key);
118141
}
119142

120143
return key;
121144
}
145+
146+
public string GetJsonKeyForProperty(PropertyInfo propInfo)
147+
{
148+
return FormatPropertyName(propInfo.Name);
149+
//TODO: Respect [JsonProperty(PropertyName = "FooBar")], and probably cache the result.
150+
}
151+
152+
protected static string FormatPropertyName(string propertyName)
153+
{
154+
string result = propertyName.Substring(0, 1).ToLower() + propertyName.Substring(1);
155+
return result;
156+
}
122157
}
123158
}

0 commit comments

Comments
 (0)