11using System ;
22using System . Collections ;
33using System . Collections . Generic ;
4+ using System . Collections . ObjectModel ;
45using System . Linq ;
56using JsonApiDotNetCore . Internal ;
67using JsonApiDotNetCore . Models ;
@@ -12,40 +13,38 @@ namespace JsonApiDotNetCore.Hooks
1213 /// Contains the resources from the request and the corresponding database values.
1314 ///
1415 /// Also contains information about updated relationships through
15- /// implementation of IRelationshipsDictionary<typeparamref name="TEntity "/>>
16+ /// implementation of IRelationshipsDictionary<typeparamref name="TResource "/>>
1617 /// </summary>
17- public interface IEntityDiff < TEntity > : IRelationshipsDictionary < TEntity > , IEnumerable < EntityDiffPair < TEntity > > where TEntity : class , IIdentifiable
18+ public interface IEntityDiffs < TResource > : IEnumerable < EntityDiffPair < TResource > > where TResource : class , IIdentifiable
1819 {
1920 /// <summary>
2021 /// The database values of the resources affected by the request.
2122 /// </summary>
22- HashSet < TEntity > DatabaseValues { get ; }
23+ HashSet < TResource > DatabaseValues { get ; }
2324
2425 /// <summary>
2526 /// The resources that were affected by the request.
2627 /// </summary>
27- HashSet < TEntity > Entities { get ; }
28+ EntityHashSet < TResource > Entities { get ; }
29+
2830 }
2931
3032 /// <inheritdoc />
31- public class EntityDiffs < TEntity > : IEntityDiff < TEntity > where TEntity : class , IIdentifiable
33+ public class EntityDiffs < TResource > : IEntityDiffs < TResource > where TResource : class , IIdentifiable
3234 {
3335 /// <inheritdoc />
34- public HashSet < TEntity > DatabaseValues { get => _databaseValues ?? ThrowNoDbValuesError ( ) ; }
35- private readonly HashSet < TEntity > _databaseValues ;
36- private readonly bool _databaseValuesLoaded ;
37-
38- /// <inheritdoc />
39- public HashSet < TEntity > Entities { get ; private set ; }
36+ public HashSet < TResource > DatabaseValues { get => _databaseValues ?? ThrowNoDbValuesError ( ) ; }
4037 /// <inheritdoc />
41- public RelationshipsDictionary < TEntity > AffectedRelationships { get ; private set ; }
38+ public EntityHashSet < TResource > Entities { get ; private set ; }
4239
43- public EntityDiffs ( HashSet < TEntity > requestEntities ,
44- HashSet < TEntity > databaseEntities ,
45- Dictionary < RelationshipAttribute , HashSet < TEntity > > relationships )
40+ private readonly HashSet < TResource > _databaseValues ;
41+ private readonly bool _databaseValuesLoaded ;
42+
43+ public EntityDiffs ( HashSet < TResource > requestEntities ,
44+ HashSet < TResource > databaseEntities ,
45+ Dictionary < RelationshipAttribute , HashSet < TResource > > relationships )
4646 {
47- Entities = requestEntities ;
48- AffectedRelationships = new RelationshipsDictionary < TEntity > ( relationships ) ;
47+ Entities = new EntityHashSet < TResource > ( requestEntities , relationships ) ;
4948 _databaseValues = databaseEntities ;
5049 _databaseValuesLoaded |= _databaseValues != null ;
5150 }
@@ -55,39 +54,27 @@ public EntityDiffs(HashSet<TEntity> requestEntities,
5554 /// </summary>
5655 internal EntityDiffs ( IEnumerable requestEntities ,
5756 IEnumerable databaseEntities ,
58- Dictionary < RelationshipAttribute , IEnumerable > relationships )
59- : this ( ( HashSet < TEntity > ) requestEntities , ( HashSet < TEntity > ) databaseEntities , TypeHelper . ConvertRelationshipDictionary < TEntity > ( relationships ) ) { }
60-
57+ Dictionary < RelationshipAttribute , IEnumerable > relationships )
58+ : this ( ( HashSet < TResource > ) requestEntities , ( HashSet < TResource > ) databaseEntities , TypeHelper . ConvertRelationshipDictionary < TResource > ( relationships ) ) { }
6159
62- /// <inheritdoc />
63- public Dictionary < RelationshipAttribute , HashSet < TEntity > > GetByRelationship < TPrincipalResource > ( ) where TPrincipalResource : class , IIdentifiable
64- {
65- return GetByRelationship ( typeof ( TPrincipalResource ) ) ;
66- }
67-
68- /// <inheritdoc />
69- public Dictionary < RelationshipAttribute , HashSet < TEntity > > GetByRelationship ( Type principalType )
70- {
71- return AffectedRelationships . GetByRelationship ( principalType ) ;
72- }
7360
7461 /// <inheritdoc />
75- public IEnumerator < EntityDiffPair < TEntity > > GetEnumerator ( )
62+ public IEnumerator < EntityDiffPair < TResource > > GetEnumerator ( )
7663 {
7764 if ( ! _databaseValuesLoaded ) ThrowNoDbValuesError ( ) ;
7865
7966 foreach ( var entity in Entities )
8067 {
81- TEntity currentValueInDatabase = null ;
68+ TResource currentValueInDatabase = null ;
8269 currentValueInDatabase = _databaseValues . Single ( e => entity . StringId == e . StringId ) ;
83- yield return new EntityDiffPair < TEntity > ( entity , currentValueInDatabase ) ;
70+ yield return new EntityDiffPair < TResource > ( entity , currentValueInDatabase ) ;
8471 }
8572 }
8673
8774 /// <inheritdoc />
8875 IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
8976
90- private HashSet < TEntity > ThrowNoDbValuesError ( )
77+ private HashSet < TResource > ThrowNoDbValuesError ( )
9178 {
9279 throw new MemberAccessException ( "Cannot access database entities if the LoadDatabaseValues option is set to false" ) ;
9380 }
@@ -97,9 +84,9 @@ private HashSet<TEntity> ThrowNoDbValuesError()
9784 /// A wrapper that contains an entity that is affected by the request,
9885 /// matched to its current database value
9986 /// </summary>
100- public class EntityDiffPair < TEntity > where TEntity : class , IIdentifiable
87+ public class EntityDiffPair < TResource > where TResource : class , IIdentifiable
10188 {
102- public EntityDiffPair ( TEntity entity , TEntity databaseValue )
89+ public EntityDiffPair ( TResource entity , TResource databaseValue )
10390 {
10491 Entity = entity ;
10592 DatabaseValue = databaseValue ;
@@ -108,10 +95,10 @@ public EntityDiffPair(TEntity entity, TEntity databaseValue)
10895 /// <summary>
10996 /// The resource from the request matching the resource from the database.
11097 /// </summary>
111- public TEntity Entity { get ; private set ; }
98+ public TResource Entity { get ; private set ; }
11299 /// <summary>
113100 /// The resource from the database matching the resource from the request.
114101 /// </summary>
115- public TEntity DatabaseValue { get ; private set ; }
102+ public TResource DatabaseValue { get ; private set ; }
116103 }
117104}
0 commit comments