Skip to content

Commit 09f3846

Browse files
committed
introduce IEntityFrameworkRepository and bump version
allows changes to be rolled out in a non-breaking way
1 parent 66ea1b3 commit 09f3846

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public DefaultEntityRepository(
3232
/// abstracting any EF Core APIs away from the service layer.
3333
/// </summary>
3434
public class DefaultEntityRepository<TEntity, TId>
35-
: IEntityRepository<TEntity, TId>
35+
: IEntityRepository<TEntity, TId>,
36+
IEntityFrameworkRepository<TEntity>
3637
where TEntity : class, IIdentifiable<TId>
3738
{
3839
private readonly DbContext _context;

src/JsonApiDotNetCore/Data/IEntityRepository.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,25 @@ public interface IEntityRepository<TEntity, in TId>
1212
IEntityWriteRepository<TEntity, TId>
1313
where TEntity : class, IIdentifiable<TId>
1414
{ }
15+
16+
/// <summary>
17+
/// A staging interface to avoid breaking changes that
18+
/// specifically depend on EntityFramework.
19+
/// </summary>
20+
internal interface IEntityFrameworkRepository<TEntity>
21+
{
22+
/// <summary>
23+
/// Ensures that any relationship pointers created during a POST or PATCH
24+
/// request are detached from the DbContext.
25+
/// This allows the relationships to be fully loaded from the database.
26+
///
27+
/// </summary>
28+
/// <remarks>
29+
/// The only known case when this should be called is when a POST request is
30+
/// sent with an ?include query.
31+
///
32+
/// See https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/343
33+
/// </remarks>
34+
void DetachRelationshipPointers(TEntity entity);
35+
}
1536
}

src/JsonApiDotNetCore/Data/IEntityWriteRepository.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,5 @@ public interface IEntityWriteRepository<TEntity, in TId>
1919
Task UpdateRelationshipsAsync(object parent, RelationshipAttribute relationship, IEnumerable<string> relationshipIds);
2020

2121
Task<bool> DeleteAsync(TId id);
22-
23-
/// <summary>
24-
/// Ensures that any relationship pointers created during a POST or PATCH
25-
/// request are detached from the DbContext.
26-
/// This allows the relationships to be fully loaded from the database.
27-
///
28-
/// </summary>
29-
/// <remarks>
30-
/// The only known case when this should be called is when a POST request is
31-
/// sent with an ?include query.
32-
///
33-
/// See https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/343
34-
/// </remarks>
35-
void DetachRelationshipPointers(TEntity entity);
3622
}
3723
}

src/JsonApiDotNetCore/JsonApiDotNetCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<VersionPrefix>2.5.0</VersionPrefix>
3+
<VersionPrefix>2.5.1</VersionPrefix>
44
<TargetFrameworks>$(NetStandardVersion)</TargetFrameworks>
55
<AssemblyName>JsonApiDotNetCore</AssemblyName>
66
<PackageId>JsonApiDotNetCore</PackageId>

src/JsonApiDotNetCore/Services/EntityResourceService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ public virtual async Task<TResource> CreateAsync(TResource resource)
8282
// https://github.com/json-api-dotnet/JsonApiDotNetCore/issues/343
8383
if (ShouldIncludeRelationships())
8484
{
85-
_entities.DetachRelationshipPointers(entity);
85+
if(_entities is IEntityFrameworkRepository<TEntity> efRepository)
86+
efRepository.DetachRelationshipPointers(entity);
87+
8688
return await GetWithRelationshipsAsync(entity.Id);
8789
}
8890

0 commit comments

Comments
 (0)