Skip to content

Commit ffc3413

Browse files
committed
Refactored to make ModelManager not a singleton, rather a instance variable of JsonApiFormatter. Adds an IModelManager interface to allow the user to create their own ModelManager.
1 parent d79f6ba commit ffc3413

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
@@ -7,23 +7,9 @@
77

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

2814
#region Cache storage
2915

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
PropertyInfo[] props = value.GetType().GetProperties();
@@ -819,7 +826,7 @@ protected object GetById(Type type, string id)
819826
{
820827
// Only good for creating dummy relationship objects...
821828
object retval = Activator.CreateInstance(type);
822-
PropertyInfo idprop = ModelManager.Instance.GetIdProperty(type);
829+
PropertyInfo idprop = _modelManager.GetIdProperty(type);
823830
idprop.SetValue(retval, System.Convert.ChangeType(id, idprop.PropertyType));
824831
return retval;
825832
}
@@ -848,7 +855,7 @@ protected string GetValueForIdProperty(PropertyInfo idprop, object obj)
848855
protected string GetIdFor(object obj)
849856
{
850857
Type type = obj.GetType();
851-
PropertyInfo idprop = ModelManager.Instance.GetIdProperty(type);
858+
PropertyInfo idprop = _modelManager.GetIdProperty(type);
852859
return GetValueForIdProperty(idprop, obj);
853860
}
854861

0 commit comments

Comments
 (0)