Skip to content

Commit 5eaeb68

Browse files
author
Chris Santero
committed
inject IMetadataManager onto EntityFrameworkMaterializer
1 parent c6946de commit 5eaeb68

12 files changed

Lines changed: 45 additions & 21 deletions

File tree

JSONAPI.EntityFramework.Tests.TestWebApp/Controllers/CommentsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public CommentsController(TestDbContext dbContext)
1515

1616
protected override IMaterializer MaterializerFactory()
1717
{
18-
return new EntityFrameworkMaterializer(DbContext);
18+
return new EntityFrameworkMaterializer(DbContext, MetadataManager.Instance);
1919
}
2020
}
2121
}

JSONAPI.EntityFramework.Tests.TestWebApp/Controllers/PostsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public PostsController(TestDbContext dbContext)
1515

1616
protected override IMaterializer MaterializerFactory()
1717
{
18-
return new EntityFrameworkMaterializer(DbContext);
18+
return new EntityFrameworkMaterializer(DbContext, MetadataManager.Instance);
1919
}
2020
}
2121
}

JSONAPI.EntityFramework.Tests.TestWebApp/Controllers/TagsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public TagsController(TestDbContext dbContext)
1515

1616
protected override IMaterializer MaterializerFactory()
1717
{
18-
return new EntityFrameworkMaterializer(DbContext);
18+
return new EntityFrameworkMaterializer(DbContext, MetadataManager.Instance);
1919
}
2020
}
2121
}

JSONAPI.EntityFramework.Tests.TestWebApp/Controllers/UserGroupsController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public UserGroupsController(TestDbContext dbContext)
1515

1616
protected override IMaterializer MaterializerFactory()
1717
{
18-
return new EntityFrameworkMaterializer(DbContext);
18+
return new EntityFrameworkMaterializer(DbContext, MetadataManager.Instance);
1919
}
2020
}
2121
}

JSONAPI.EntityFramework.Tests.TestWebApp/Controllers/UsersController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public UsersController(TestDbContext dbContext)
1515

1616
protected override IMaterializer MaterializerFactory()
1717
{
18-
return new EntityFrameworkMaterializer(DbContext);
18+
return new EntityFrameworkMaterializer(DbContext, MetadataManager.Instance);
1919
}
2020
}
2121
}

JSONAPI.EntityFramework.Tests/EntityConverterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public async Task UnderpostingTest()
126126
JsonApiFormatter formatter = new JSONAPI.Json.JsonApiFormatter(new JSONAPI.Core.PluralizationService());
127127
MemoryStream stream = new MemoryStream();
128128

129-
EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context);
129+
EntityFrameworkMaterializer materializer = new EntityFrameworkMaterializer(context, MetadataManager.Instance);
130130

131131
string underpost = @"{""posts"":{""id"":""" + p.Id.ToString() + @""",""title"":""Not at all linkbait!""}}";
132132
stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(underpost));

JSONAPI.EntityFramework.Tests/EntityFrameworkMaterializerTests.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using FluentAssertions;
66
using System.Collections.Generic;
77
using System.Data.Entity;
8+
using JSONAPI.Core;
9+
using Moq;
810

911
namespace JSONAPI.EntityFramework.Tests
1012
{
@@ -54,7 +56,8 @@ public void SetupEntities()
5456
public void GetKeyNamesStandardIdTest()
5557
{
5658
// Arrange
57-
var materializer = new EntityFrameworkMaterializer(context);
59+
var mockMetadataManager = new Mock<IMetadataManager>(MockBehavior.Strict);
60+
var materializer = new EntityFrameworkMaterializer(context, mockMetadataManager.Object);
5861

5962
// Act
6063
IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Post));
@@ -68,7 +71,8 @@ public void GetKeyNamesStandardIdTest()
6871
public void GetKeyNamesNonStandardIdTest()
6972
{
7073
// Arrange
71-
var materializer = new EntityFrameworkMaterializer(context);
74+
var mockMetadataManager = new Mock<IMetadataManager>(MockBehavior.Strict);
75+
var materializer = new EntityFrameworkMaterializer(context, mockMetadataManager.Object);
7276

7377
// Act
7478
IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Backlink));
@@ -82,7 +86,8 @@ public void GetKeyNamesNonStandardIdTest()
8286
public void GetKeyNamesNotAnEntityTest()
8387
{
8488
// Arrange
85-
var materializer = new EntityFrameworkMaterializer(context);
89+
var mockMetadataManager = new Mock<IMetadataManager>(MockBehavior.Strict);
90+
var materializer = new EntityFrameworkMaterializer(context, mockMetadataManager.Object);
8691

8792
// Act
8893
Action action = () =>

JSONAPI.EntityFramework/EntityFrameworkMaterializer.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ namespace JSONAPI.EntityFramework
1717
/// </summary>
1818
public partial class EntityFrameworkMaterializer : IMaterializer
1919
{
20+
private readonly IMetadataManager _metadataManager;
21+
2022
/// <summary>
2123
/// The DbContext instance used to perform materializer operations
2224
/// </summary>
@@ -26,8 +28,9 @@ public partial class EntityFrameworkMaterializer : IMaterializer
2628
/// Creates a new EntityFrameworkMaterializer.
2729
/// </summary>
2830
/// <param name="context">The DbContext instance used to perform materializer operations</param>
29-
public EntityFrameworkMaterializer(DbContext context)
31+
public EntityFrameworkMaterializer(DbContext context, IMetadataManager metadataManager)
3032
{
33+
_metadataManager = metadataManager;
3134
DbContext = context;
3235
}
3336

@@ -308,7 +311,7 @@ private async Task Merge (Type type, object ephemeral, object material)
308311
foreach (PropertyInfo prop in props)
309312
{
310313
// Comply with the spec, if a key was not set, it should not be updated!
311-
if (!MetadataManager.Instance.PropertyWasPresent(ephemeral, prop)) continue;
314+
if (!_metadataManager.PropertyWasPresent(ephemeral, prop)) continue;
312315

313316
if (IsMany(prop.PropertyType))
314317
{

JSONAPI.EntityFramework/Http/ApiController.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using System.Text;
66
using System.Threading.Tasks;
7+
using JSONAPI.Core;
78

89
namespace JSONAPI.EntityFramework.Http
910
{
@@ -18,7 +19,8 @@ protected override JSONAPI.Core.IMaterializer MaterializerFactory()
1819
if (_materializer == null)
1920
{
2021
DbContext context = (DbContext)Activator.CreateInstance(typeof(TC));
21-
_materializer = new JSONAPI.EntityFramework.EntityFrameworkMaterializer(context);
22+
var metadataManager = MetadataManager.Instance;
23+
_materializer = new JSONAPI.EntityFramework.EntityFrameworkMaterializer(context, metadataManager);
2224
}
2325
return _materializer;
2426
}

JSONAPI/Core/IMetadataManager.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Reflection;
2+
3+
namespace JSONAPI.Core
4+
{
5+
/// <summary>
6+
/// Manages request-specific metadata
7+
/// </summary>
8+
public interface IMetadataManager
9+
{
10+
/// <summary>
11+
/// Find whether or not a given property was
12+
/// posted in the original JSON--i.e. to determine whether an update operation should be
13+
/// performed, and/or if a default value should be used.
14+
/// </summary>
15+
/// <param name="deserialized">The object deserialized by JsonApiFormatter</param>
16+
/// <param name="prop">The property to check</param>
17+
/// <returns>Whether or not the property was found in the original JSON and set by the deserializer</returns>
18+
bool PropertyWasPresent(object deserialized, PropertyInfo prop);
19+
}
20+
}

0 commit comments

Comments
 (0)