Skip to content

Commit 44fa79b

Browse files
author
Chris Santero
committed
extract key-name determination facility
Figured this made more sense as an extension method.
1 parent 0a26101 commit 44fa79b

4 files changed

Lines changed: 54 additions & 19 deletions

File tree

JSONAPI.EntityFramework.Tests/EntityFrameworkMaterializerTests.cs renamed to JSONAPI.EntityFramework.Tests/DbContextExtensionsTests.cs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
using FluentAssertions;
88
using System.Collections.Generic;
99
using System.Data.Entity;
10-
using JSONAPI.Core;
11-
using Moq;
1210

1311
namespace JSONAPI.EntityFramework.Tests
1412
{
1513
[TestClass]
16-
public class EntityFrameworkMaterializerTests
14+
public class DbContextExtensionsTests
1715
{
1816
private class TestDbContext : DbContext
1917
{
@@ -60,12 +58,8 @@ private void CleanupTest()
6058
[TestMethod]
6159
public void GetKeyNamesStandardIdTest()
6260
{
63-
// Arrange
64-
var mockMetadataManager = new Mock<IMetadataManager>(MockBehavior.Strict);
65-
var materializer = new EntityFrameworkMaterializer(_context, mockMetadataManager.Object);
66-
6761
// Act
68-
IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Post)).ToArray();
62+
IEnumerable<string> keyNames = _context.GetKeyNames(typeof(Post)).ToArray();
6963

7064
// Assert
7165
keyNames.Count().Should().Be(1);
@@ -75,12 +69,8 @@ public void GetKeyNamesStandardIdTest()
7569
[TestMethod]
7670
public void GetKeyNamesNonStandardIdTest()
7771
{
78-
// Arrange
79-
var mockMetadataManager = new Mock<IMetadataManager>(MockBehavior.Strict);
80-
var materializer = new EntityFrameworkMaterializer(_context, mockMetadataManager.Object);
81-
8272
// Act
83-
IEnumerable<string> keyNames = materializer.GetKeyNames(typeof(Backlink)).ToArray();
73+
IEnumerable<string> keyNames = _context.GetKeyNames(typeof(Backlink)).ToArray();
8474

8575
// Assert
8676
keyNames.Count().Should().Be(1);
@@ -90,12 +80,8 @@ public void GetKeyNamesNonStandardIdTest()
9080
[TestMethod]
9181
public void GetKeyNamesNotAnEntityTest()
9282
{
93-
// Arrange
94-
var mockMetadataManager = new Mock<IMetadataManager>(MockBehavior.Strict);
95-
var materializer = new EntityFrameworkMaterializer(_context, mockMetadataManager.Object);
96-
9783
// Act
98-
Action action = () => materializer.GetKeyNames(typeof (NotAnEntity));
84+
Action action = () => _context.GetKeyNames(typeof (NotAnEntity));
9985
action.ShouldThrow<ArgumentException>().Which.Message.Should().Be("The Type NotAnEntity was not found in the DbContext with Type TestDbContext");
10086
}
10187
}

JSONAPI.EntityFramework.Tests/JSONAPI.EntityFramework.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
<Compile Include="Acceptance\AcceptanceTestsBase.cs" />
117117
<Compile Include="Acceptance\SortingTests.cs" />
118118
<Compile Include="ActionFilters\AsynchronousEnumerationTransformerTests.cs" />
119-
<Compile Include="EntityFrameworkMaterializerTests.cs" />
119+
<Compile Include="DbContextExtensionsTests.cs" />
120120
<Compile Include="Helpers\TestDbAsyncEnumerable.cs" />
121121
<Compile Include="Helpers\WaitsUntilCancellationDbAsyncEnumerator.cs" />
122122
<Compile Include="Helpers\TestDbAsyncEnumerator.cs" />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.Entity;
4+
using System.Data.Entity.Core.Objects;
5+
using System.Data.Entity.Infrastructure;
6+
using System.Linq;
7+
using System.Reflection;
8+
9+
namespace JSONAPI.EntityFramework
10+
{
11+
internal static class DbContextExtensions
12+
{
13+
internal static IEnumerable<string> GetKeyNames(this DbContext dbContext, Type type)
14+
{
15+
var openMethod = typeof(DbContextExtensions).GetMethod("GetKeyNamesFromGeneric", BindingFlags.NonPublic | BindingFlags.Static);
16+
var method = openMethod.MakeGenericMethod(type);
17+
try
18+
{
19+
return (IEnumerable<string>)method.Invoke(null, new object[] { dbContext });
20+
}
21+
catch (TargetInvocationException ex)
22+
{
23+
throw ex.InnerException;
24+
}
25+
}
26+
27+
// ReSharper disable once UnusedMember.Local
28+
private static IEnumerable<string> GetKeyNamesFromGeneric<T>(this DbContext dbContext) where T : class
29+
{
30+
var objectContext = ((IObjectContextAdapter)dbContext).ObjectContext;
31+
ObjectSet<T> objectSet;
32+
try
33+
{
34+
objectSet = objectContext.CreateObjectSet<T>();
35+
36+
}
37+
catch (InvalidOperationException e)
38+
{
39+
throw new ArgumentException(
40+
String.Format("The Type {0} was not found in the DbContext with Type {1}", typeof(T).Name, dbContext.GetType().Name),
41+
e
42+
);
43+
}
44+
return objectSet.EntitySet.ElementType.KeyMembers.Select(k => k.Name).ToArray();
45+
}
46+
47+
}
48+
}

JSONAPI.EntityFramework/JSONAPI.EntityFramework.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
</ItemGroup>
7272
<ItemGroup>
7373
<Compile Include="ActionFilters\AsynchronousEnumerationTransformer.cs" />
74+
<Compile Include="DbContextExtensions.cs" />
7475
<Compile Include="EntityFrameworkMaterializer.cs" />
7576
<Compile Include="EntityFrameworkMaterializer_Util.cs" />
7677
<Compile Include="Http\ApiController.cs" />

0 commit comments

Comments
 (0)