Skip to content

Commit d6d4a61

Browse files
committed
refactor: rename entitydiff to resourcediff
1 parent e9d553f commit d6d4a61

File tree

10 files changed

+37
-37
lines changed

10 files changed

+37
-37
lines changed

docs/usage/resources/hooks.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ public class ArticleResource : ResourceDefinition<Article>
260260
_context = context;
261261
}
262262

263-
public override IEnumerable<Article> BeforeUpdate(IEntityDiff<Article> entityDiff, ResourcePipeline pipeline)
263+
public override IEnumerable<Article> BeforeUpdate(IResourceDiff<Article> entityDiff, ResourcePipeline pipeline)
264264
{
265265
// PropertyGetter is a helper class that takes care of accessing the values on an instance of Article using reflection.
266266
var getter = new PropertyGetter<Article>();
267267

268-
// EntityDiff<T> is a class that is like a list that contains EntityDiffPair<T> elements
269-
foreach (EntityDiffPair<Article> affected in entityDiff)
268+
// ResourceDiff<T> is a class that is like a list that contains ResourceDiffPair<T> elements
269+
foreach (ResourceDiffPair<Article> affected in entityDiff)
270270
{
271271
var currentDatabaseState = affected.DatabaseValue; // the current state in the database
272272
var proposedValueFromRequest = affected.Entity; // the value from the request
@@ -288,7 +288,7 @@ public class ArticleResource : ResourceDefinition<Article>
288288
}
289289
}
290290
```
291-
In this case the `EntityDiffPair<T>.DatabaseValue` is `null`. If you try to access all database values at once (`EntityDiff.DatabaseValues`) when it they are turned off, an exception will be thrown.
291+
In this case the `ResourceDiffPair<T>.DatabaseValue` is `null`. If you try to access all database values at once (`ResourceDiff.DatabaseValues`) when it they are turned off, an exception will be thrown.
292292

293293
Note that database values are turned on by default. They can be turned of globally by configuring the startup as follows:
294294
```c#
@@ -310,7 +310,7 @@ The global setting can be used together with per-hook configuration hooks using
310310
public class ArticleResource : ResourceDefinition<Article>
311311
{
312312
[LoadDatabaseValues(true)]
313-
public override IEnumerable<Article> BeforeUpdate(IEntityDiff<Article> entityDiff, ResourcePipeline pipeline)
313+
public override IEnumerable<Article> BeforeUpdate(IResourceDiff<Article> entityDiff, ResourcePipeline pipeline)
314314
{
315315
....
316316
}
@@ -389,7 +389,7 @@ This authorization requirement can be fulfilled as follows.
389389

390390
For checking the permissions for the explicitly affected resources, `New Article` and `Alice`, we may implement the `BeforeUpdate` hook for `Article`:
391391
```c#
392-
public override IEnumerable<Article> BeforeUpdate(IEntityDiff<Article> entityDiff, ResourcePipeline pipeline)
392+
public override IEnumerable<Article> BeforeUpdate(IResourceDiff<Article> entityDiff, ResourcePipeline pipeline)
393393
{
394394
if (pipeline == ResourcePipeline.Patch)
395395
{

src/Examples/JsonApiDotNetCoreExample/Resources/PersonResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public override IEnumerable<string> BeforeUpdateRelationship(HashSet<string> ids
1717
}
1818

1919
//[LoadDatabaseValues(true)]
20-
//public override IEnumerable<Person> BeforeUpdate(IEntityDiff<Person> entityDiff, ResourcePipeline pipeline)
20+
//public override IEnumerable<Person> BeforeUpdate(IResourceDiff<Person> entityDiff, ResourcePipeline pipeline)
2121
//{
2222
// return entityDiff.Entities;
2323
//}

src/JsonApiDotNetCore/Hooks/Execution/AffectedResources.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public interface IAffectedResources<TEntity> : IAffectedResourcesBase<TEntity>,
1414
/// NOTE: you might wonder why there is a separate AffectedResourceBase and AffectedResource.
1515
/// If we merge them together, ie get rid of the base and just let the AffectedResource directly implement IEnumerable{TEntity},
1616
/// we will run in to problems with the following:
17-
/// EntityDiff{<typeparam name="TEntity"/>} inherits from AffectedResource{TEntity},
18-
/// but EntityDiff also implements IEnumerable{EntityDiffPair{TEntity}}. This means that
19-
/// EntityDiff will implement two IEnumerable{x} where (x1 = TEntity and x2 = EntityDiffPair{TEntity} )
17+
/// ResourceDiff{<typeparam name="TEntity"/>} inherits from AffectedResource{TEntity},
18+
/// but ResourceDiff also implements IEnumerable{ResourceDiffPair{TEntity}}. This means that
19+
/// ResourceDiff will implement two IEnumerable{x} where (x1 = TEntity and x2 = ResourceDiffPair{TEntity} )
2020
/// The problem with this is that when you then try to do a simple foreach loop over
21-
/// a EntityDiff instance, it will throw an error, because it does not know which of the two enumerators to pick.
22-
/// We want EntityDiff to only loop over the EntityDiffPair, so we can do that by making sure
21+
/// a ResourceDiff instance, it will throw an error, because it does not know which of the two enumerators to pick.
22+
/// We want ResourceDiff to only loop over the ResourceDiffPair, so we can do that by making sure
2323
/// it doesn't inherit the IEnumerable{TEntity} part from AffectedResources.
2424
public interface IAffectedResourcesBase<TEntity> where TEntity : class, IIdentifiable
2525
{

src/JsonApiDotNetCore/Hooks/Execution/EntityDiff.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,24 @@ namespace JsonApiDotNetCore.Hooks
88
{
99
/// <summary>
1010
/// A helper class that provides insight in what is to be updated. The
11-
/// <see cref="IEntityDiff{TEntity}.RequestEntities"/> property reflects what was parsed from the incoming request,
12-
/// where the <see cref="IEntityDiff{TEntity}.DatabaseValues"/> reflects what is the current state in the database.
11+
/// <see cref="IResourceDiff{TEntity}.RequestEntities"/> property reflects what was parsed from the incoming request,
12+
/// where the <see cref="IResourceDiff{TEntity}.DatabaseValues"/> reflects what is the current state in the database.
1313
///
1414
/// Any relationships that are updated can be retrieved via the methods implemented on
1515
/// <see cref="IAffectedRelationships{TDependent}"/>.
1616
/// </summary>
17-
public interface IEntityDiff<TEntity> : IEnumerable<EntityDiffPair<TEntity>>, IAffectedResourcesBase<TEntity> where TEntity : class, IIdentifiable
17+
public interface IResourceDiff<TEntity> : IEnumerable<ResourceDiffPair<TEntity>>, IAffectedResourcesBase<TEntity> where TEntity : class, IIdentifiable
1818
{
1919
HashSet<TEntity> DatabaseValues { get; }
2020
}
2121

22-
public class EntityDiff<TEntity> : AffectedResourcesBase<TEntity>, IEntityDiff<TEntity> where TEntity : class, IIdentifiable
22+
public class ResourceDiff<TEntity> : AffectedResourcesBase<TEntity>, IResourceDiff<TEntity> where TEntity : class, IIdentifiable
2323
{
2424
private readonly HashSet<TEntity> _databaseEntities;
2525
private readonly bool _databaseValuesLoaded;
2626
public HashSet<TEntity> DatabaseValues { get => _databaseEntities ?? ThrowNoDbValuesError(); }
2727

28-
internal EntityDiff(IEnumerable requestEntities,
28+
internal ResourceDiff(IEnumerable requestEntities,
2929
IEnumerable databaseEntities,
3030
Dictionary<RelationshipProxy, IEnumerable> relationships) : base(requestEntities, relationships)
3131
{
@@ -38,13 +38,13 @@ private HashSet<TEntity> ThrowNoDbValuesError()
3838
throw new MemberAccessException("Cannot access database entities if the LoadDatabaseValues option is set to false");
3939
}
4040

41-
public IEnumerator<EntityDiffPair<TEntity>> GetEnumerator()
41+
public IEnumerator<ResourceDiffPair<TEntity>> GetEnumerator()
4242
{
4343
foreach (var entity in Entities)
4444
{
4545
TEntity currentValueInDatabase = null;
4646
if (_databaseValuesLoaded) currentValueInDatabase = _databaseEntities.Single(e => entity.StringId == e.StringId);
47-
yield return new EntityDiffPair<TEntity>(entity, currentValueInDatabase);
47+
yield return new ResourceDiffPair<TEntity>(entity, currentValueInDatabase);
4848
}
4949
}
5050

@@ -54,9 +54,9 @@ IEnumerator IEnumerable.GetEnumerator()
5454
}
5555
}
5656

57-
public class EntityDiffPair<TEntity> where TEntity : class, IIdentifiable
57+
public class ResourceDiffPair<TEntity> where TEntity : class, IIdentifiable
5858
{
59-
internal EntityDiffPair(TEntity entity, TEntity databaseValue)
59+
internal ResourceDiffPair(TEntity entity, TEntity databaseValue)
6060
{
6161
Entity = entity;
6262
DatabaseValue = databaseValue;

src/JsonApiDotNetCore/Hooks/IResourceHookContainer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ public interface IBeforeHooks<TEntity> where TEntity : class, IIdentifiable
110110
/// layer just before updating entities of type <typeparamref name="TEntity"/>.
111111
/// <para />
112112
/// For the <see cref="ResourcePipeline.Patch"/> pipeline, the
113-
/// <paramref name="entityDiff" /> will typically contain one entity.
113+
/// <paramref name="ResourceDiff" /> will typically contain one entity.
114114
/// For <see cref="ResourcePipeline.BulkPatch"/>, this it may contain
115115
/// multiple entities.
116116
/// <para />
117117
/// The returned <see cref="IEnumerable{TEntity}"/> may be a subset
118-
/// of the <see cref="EntityDiff{TEntity}.RequestEntities"/> property in parameter <paramref name="entityDiff"/>,
118+
/// of the <see cref="ResourceDiff{TEntity}.RequestEntities"/> property in parameter <paramref name="ResourceDiff"/>,
119119
/// in which case the operation of the pipeline will not be executed
120120
/// for the omitted entities. The returned set may also contain custom
121121
/// changes of the properties on the entities.
@@ -131,9 +131,9 @@ public interface IBeforeHooks<TEntity> where TEntity : class, IIdentifiable
131131
/// hook is fired for these.
132132
/// </summary>
133133
/// <returns>The transformed entity set</returns>
134-
/// <param name="entityDiff">The entity diff.</param>
134+
/// <param name="ResourceDiff">The entity diff.</param>
135135
/// <param name="pipeline">An enum indicating from where the hook was triggered.</param>
136-
IEnumerable<TEntity> BeforeUpdate(IEntityDiff<TEntity> entityDiff, ResourcePipeline pipeline);
136+
IEnumerable<TEntity> BeforeUpdate(IResourceDiff<TEntity> ResourceDiff, ResourcePipeline pipeline);
137137
/// <summary>
138138
/// Implement this hook to run custom logic in the <see cref=" EntityResourceService{T}"/>
139139
/// layer just before deleting entities of type <typeparamref name="TEntity"/>.

src/JsonApiDotNetCore/Hooks/ResourceHookExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public virtual IEnumerable<TEntity> BeforeUpdate<TEntity>(IEnumerable<TEntity> e
4848
if (GetHook(ResourceHook.BeforeUpdate, entities, out var container, out var node))
4949
{
5050
var dbValues = LoadDbValues(typeof(TEntity), (IEnumerable<TEntity>)node.UniqueEntities, ResourceHook.BeforeUpdate, node.RelationshipsToNextLayer);
51-
var diff = new EntityDiff<TEntity>(node.UniqueEntities, dbValues, node.PrincipalsToNextLayer());
51+
var diff = new ResourceDiff<TEntity>(node.UniqueEntities, dbValues, node.PrincipalsToNextLayer());
5252
IEnumerable<TEntity> updated = container.BeforeUpdate(diff, pipeline);
5353
node.UpdateUnique(updated);
5454
node.Reassign(entities);

src/JsonApiDotNetCore/Models/ResourceDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public virtual void AfterUpdateRelationship(IAffectedRelationships<T> resourcesB
179179
/// <inheritdoc/>
180180
public virtual void BeforeRead(ResourcePipeline pipeline, bool isIncluded = false, string stringId = null) { }
181181
/// <inheritdoc/>
182-
public virtual IEnumerable<T> BeforeUpdate(IEntityDiff<T> entityDiff, ResourcePipeline pipeline) { return entityDiff.Entities; }
182+
public virtual IEnumerable<T> BeforeUpdate(IResourceDiff<T> ResourceDiff, ResourcePipeline pipeline) { return ResourceDiff.Entities; }
183183
/// <inheritdoc/>
184184
public virtual IEnumerable<T> BeforeDelete(IAffectedResources<T> affected, ResourcePipeline pipeline) { return affected; }
185185
/// <inheritdoc/>

test/UnitTests/ResourceHooks/ResourceHookExecutor/Update/BeforeUpdateTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void BeforeUpdate()
2424
hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);
2525

2626
// assert
27-
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.IsAny<IEntityDiff<TodoItem>>(), ResourcePipeline.Patch), Times.Once());
27+
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.IsAny<IResourceDiff<TodoItem>>(), ResourcePipeline.Patch), Times.Once());
2828
ownerResourceMock.Verify(rd => rd.BeforeUpdateRelationship(It.IsAny<HashSet<string>>(), It.IsAny<IAffectedRelationships<Person>>(), ResourcePipeline.Patch), Times.Once());
2929
VerifyNoOtherCalls(todoResourceMock, ownerResourceMock);
3030
}
@@ -62,7 +62,7 @@ public void BeforeUpdate_Without_Child_Hook_Implemented()
6262
hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);
6363

6464
// assert
65-
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.IsAny<IEntityDiff<TodoItem>>(), ResourcePipeline.Patch), Times.Once());
65+
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.IsAny<IResourceDiff<TodoItem>>(), ResourcePipeline.Patch), Times.Once());
6666
VerifyNoOtherCalls(todoResourceMock, ownerResourceMock);
6767
}
6868

test/UnitTests/ResourceHooks/ResourceHookExecutor/Update/BeforeUpdate_WithDbValues_Tests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void BeforeUpdate()
5959
hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);
6060

6161
// assert
62-
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IEntityDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
62+
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IResourceDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
6363
ownerResourceMock.Verify(rd => rd.BeforeUpdateRelationship(
6464
It.Is<HashSet<string>>(ids => PersonIdCheck(ids, personId)),
6565
It.Is<IAffectedRelationships<Person>>(rh => PersonCheck(lastName, rh)),
@@ -93,7 +93,7 @@ public void BeforeUpdate_Deleting_Relationship()
9393
hookExecutor.BeforeUpdate(_todoList, ResourcePipeline.Patch);
9494

9595
// assert
96-
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IEntityDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
96+
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IResourceDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
9797
ownerResourceMock.Verify(rd => rd.BeforeImplicitUpdateRelationship(
9898
It.Is<IAffectedRelationships<Person>>(rh => PersonCheck(lastName + lastName, rh)),
9999
ResourcePipeline.Patch),
@@ -140,7 +140,7 @@ public void BeforeUpdate_Without_Child_Hook_Implemented()
140140
hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);
141141

142142
// assert
143-
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IEntityDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
143+
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IResourceDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
144144
todoResourceMock.Verify(rd => rd.BeforeImplicitUpdateRelationship(
145145
It.Is<IAffectedRelationships<TodoItem>>(rh => TodoCheck(rh, description + description)),
146146
ResourcePipeline.Patch),
@@ -161,7 +161,7 @@ public void BeforeUpdate_NoImplicit()
161161
hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);
162162

163163
// assert
164-
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IEntityDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
164+
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IResourceDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
165165
ownerResourceMock.Verify(rd => rd.BeforeUpdateRelationship(
166166
It.Is<HashSet<string>>(ids => PersonIdCheck(ids, personId)),
167167
It.IsAny<IAffectedRelationships<Person>>(),
@@ -204,11 +204,11 @@ public void BeforeUpdate_NoImplicit_Without_Child_Hook_Implemented()
204204
hookExecutor.BeforeUpdate(todoList, ResourcePipeline.Patch);
205205

206206
// assert
207-
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IEntityDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
207+
todoResourceMock.Verify(rd => rd.BeforeUpdate(It.Is<IResourceDiff<TodoItem>>((diff) => TodoCheck(diff, description)), ResourcePipeline.Patch), Times.Once());
208208
VerifyNoOtherCalls(todoResourceMock, ownerResourceMock);
209209
}
210210

211-
private bool TodoCheck(IEntityDiff<TodoItem> diff, string checksum)
211+
private bool TodoCheck(IResourceDiff<TodoItem> diff, string checksum)
212212
{
213213
var dbCheck = diff.DatabaseValues.Single().Description == checksum;
214214
var reqCheck = diff.Single().Entity.Description == null;

test/UnitTests/ResourceHooks/ResourceHooksTestsSetup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ void MockHooks<TModel>(Mock<IResourceHookContainer<TModel>> resourceDefinition)
261261
.Setup(rd => rd.BeforeRead(It.IsAny<ResourcePipeline>(), It.IsAny<bool>(), It.IsAny<string>()))
262262
.Verifiable();
263263
resourceDefinition
264-
.Setup(rd => rd.BeforeUpdate(It.IsAny<IEntityDiff<TModel>>(), It.IsAny<ResourcePipeline>()))
265-
.Returns<EntityDiff<TModel>, ResourcePipeline>((entityDiff, context) => entityDiff.Entities)
264+
.Setup(rd => rd.BeforeUpdate(It.IsAny<IResourceDiff<TModel>>(), It.IsAny<ResourcePipeline>()))
265+
.Returns<ResourceDiff<TModel>, ResourcePipeline>((entityDiff, context) => entityDiff.Entities)
266266
.Verifiable();
267267
resourceDefinition
268268
.Setup(rd => rd.BeforeDelete(It.IsAny<IAffectedResources<TModel>>(), It.IsAny<ResourcePipeline>()))

0 commit comments

Comments
 (0)