Skip to content

Commit e01060b

Browse files
committed
Merged upstream
2 parents 47339fd + ffc3413 commit e01060b

File tree

5 files changed

+35
-23
lines changed

5 files changed

+35
-23
lines changed

JSONAPI.Tests/Core/ModelManagerTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ private class InvalidModel
1818
public void FindsIdNamedId()
1919
{
2020
// Arrange
21+
var mm = new ModelManager();
22+
2123
// Act
22-
PropertyInfo idprop = ModelManager.Instance.GetIdProperty(typeof(Author));
24+
PropertyInfo idprop = mm.GetIdProperty(typeof(Author));
2325

2426
// Assert
2527
Assert.AreSame(typeof(Author).GetProperty("Id"), idprop);
@@ -30,8 +32,10 @@ public void FindsIdNamedId()
3032
public void DoesntFindMissingId()
3133
{
3234
// Arrange
35+
var mm = new ModelManager();
36+
3337
// Act
34-
PropertyInfo idprop = ModelManager.Instance.GetIdProperty(typeof(InvalidModel));
38+
PropertyInfo idprop = mm.GetIdProperty(typeof(InvalidModel));
3539

3640
// Assert
3741
Assert.Fail("An InvalidOperationException should be thrown and we shouldn't get here!");

JSONAPI/Core/IModelManager.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace JSONAPI.Core
9+
{
10+
public interface IModelManager
11+
{
12+
PropertyInfo GetIdProperty(Type type);
13+
}
14+
}

JSONAPI/Core/ModelManager.cs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,9 @@
88

99
namespace JSONAPI.Core
1010
{
11-
internal class ModelManager
11+
public class ModelManager : IModelManager
1212
{
13-
#region Singleton pattern
14-
15-
private static readonly ModelManager instance = new ModelManager();
16-
17-
private ModelManager() { }
18-
19-
public static ModelManager Instance
20-
{
21-
get
22-
{
23-
return instance;
24-
}
25-
}
26-
27-
#endregion
13+
public ModelManager() { }
2814

2915
#region Cache storage
3016

JSONAPI/JSONAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<Compile Include="Attributes\IncludeInPayload.cs" />
6969
<Compile Include="Attributes\LinkTemplate.cs" />
7070
<Compile Include="Attributes\SerializeAs.cs" />
71+
<Compile Include="Core\IModelManager.cs" />
7172
<Compile Include="Core\IPluralizationService.cs" />
7273
<Compile Include="Core\IMaterializer.cs" />
7374
<Compile Include="Core\MetadataManager.cs" />

JSONAPI/Json/JsonApiFormatter.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,23 @@ public class JsonApiFormatter : JsonMediaTypeFormatter
2222
public JsonApiFormatter()
2323
: this(new ErrorSerializer())
2424
{
25+
if (_modelManager == null) _modelManager = new ModelManager();
26+
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.api+json"));
2527
}
2628

2729
internal JsonApiFormatter(IErrorSerializer errorSerializer)
2830
{
2931
_errorSerializer = errorSerializer;
30-
SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/vnd.api+json"));
3132
}
3233

33-
public IPluralizationService PluralizationService { get; set; }
34+
public JsonApiFormatter(IModelManager modelManager) : this()
35+
{
36+
_modelManager = modelManager;
37+
}
38+
39+
public IPluralizationService PluralizationService { get; set; } //FIXME: Deprecated, will be removed shortly
3440
private readonly IErrorSerializer _errorSerializer;
41+
private readonly IModelManager _modelManager;
3542

3643
private Lazy<Dictionary<Stream, RelationAggregator>> _relationAggregators
3744
= new Lazy<Dictionary<Stream, RelationAggregator>>(
@@ -152,7 +159,7 @@ protected void Serialize(object value, Stream writeStream, JsonWriter writer, Js
152159

153160
// Do the Id now...
154161
writer.WritePropertyName("id");
155-
var idProp = ModelManager.Instance.GetIdProperty(value.GetType());
162+
var idProp = _modelManager.GetIdProperty(value.GetType());
156163
writer.WriteValue(GetValueForIdProperty(idProp, value));
157164

158165
// Leverage the cached map to avoid another costly call to GetProperties()
@@ -799,7 +806,7 @@ protected object GetById(Type type, string id)
799806
{
800807
// Only good for creating dummy relationship objects...
801808
object retval = Activator.CreateInstance(type);
802-
PropertyInfo idprop = ModelManager.Instance.GetIdProperty(type);
809+
PropertyInfo idprop = _modelManager.GetIdProperty(type);
803810
idprop.SetValue(retval, System.Convert.ChangeType(id, idprop.PropertyType));
804811
return retval;
805812
}
@@ -828,7 +835,7 @@ protected string GetValueForIdProperty(PropertyInfo idprop, object obj)
828835
protected string GetIdFor(object obj)
829836
{
830837
Type type = obj.GetType();
831-
PropertyInfo idprop = ModelManager.Instance.GetIdProperty(type);
838+
PropertyInfo idprop = _modelManager.GetIdProperty(type);
832839
return GetValueForIdProperty(idprop, obj);
833840
}
834841

0 commit comments

Comments
 (0)