1
1
using System ;
2
2
using System . Collections ;
3
3
using System . Collections . Generic ;
4
+ using System . Collections . ObjectModel ;
4
5
using System . Linq ;
5
6
using JsonApiDotNetCore . Internal ;
6
7
using JsonApiDotNetCore . Models ;
@@ -12,40 +13,38 @@ namespace JsonApiDotNetCore.Hooks
12
13
/// Contains the resources from the request and the corresponding database values.
13
14
///
14
15
/// Also contains information about updated relationships through
15
- /// implementation of IRelationshipsDictionary<typeparamref name="TEntity "/>>
16
+ /// implementation of IRelationshipsDictionary<typeparamref name="TResource "/>>
16
17
/// </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
18
19
{
19
20
/// <summary>
20
21
/// The database values of the resources affected by the request.
21
22
/// </summary>
22
- HashSet < TEntity > DatabaseValues { get ; }
23
+ HashSet < TResource > DatabaseValues { get ; }
23
24
24
25
/// <summary>
25
26
/// The resources that were affected by the request.
26
27
/// </summary>
27
- HashSet < TEntity > Entities { get ; }
28
+ EntityHashSet < TResource > Entities { get ; }
29
+
28
30
}
29
31
30
32
/// <inheritdoc />
31
- public class EntityDiffs < TEntity > : IEntityDiff < TEntity > where TEntity : class , IIdentifiable
33
+ public class EntityDiffs < TResource > : IEntityDiffs < TResource > where TResource : class , IIdentifiable
32
34
{
33
35
/// <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 ( ) ; }
40
37
/// <inheritdoc />
41
- public RelationshipsDictionary < TEntity > AffectedRelationships { get ; private set ; }
38
+ public EntityHashSet < TResource > Entities { get ; private set ; }
42
39
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 )
46
46
{
47
- Entities = requestEntities ;
48
- AffectedRelationships = new RelationshipsDictionary < TEntity > ( relationships ) ;
47
+ Entities = new EntityHashSet < TResource > ( requestEntities , relationships ) ;
49
48
_databaseValues = databaseEntities ;
50
49
_databaseValuesLoaded |= _databaseValues != null ;
51
50
}
@@ -55,39 +54,27 @@ public EntityDiffs(HashSet<TEntity> requestEntities,
55
54
/// </summary>
56
55
internal EntityDiffs ( IEnumerable requestEntities ,
57
56
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 ) ) { }
61
59
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
- }
73
60
74
61
/// <inheritdoc />
75
- public IEnumerator < EntityDiffPair < TEntity > > GetEnumerator ( )
62
+ public IEnumerator < EntityDiffPair < TResource > > GetEnumerator ( )
76
63
{
77
64
if ( ! _databaseValuesLoaded ) ThrowNoDbValuesError ( ) ;
78
65
79
66
foreach ( var entity in Entities )
80
67
{
81
- TEntity currentValueInDatabase = null ;
68
+ TResource currentValueInDatabase = null ;
82
69
currentValueInDatabase = _databaseValues . Single ( e => entity . StringId == e . StringId ) ;
83
- yield return new EntityDiffPair < TEntity > ( entity , currentValueInDatabase ) ;
70
+ yield return new EntityDiffPair < TResource > ( entity , currentValueInDatabase ) ;
84
71
}
85
72
}
86
73
87
74
/// <inheritdoc />
88
75
IEnumerator IEnumerable . GetEnumerator ( ) => GetEnumerator ( ) ;
89
76
90
- private HashSet < TEntity > ThrowNoDbValuesError ( )
77
+ private HashSet < TResource > ThrowNoDbValuesError ( )
91
78
{
92
79
throw new MemberAccessException ( "Cannot access database entities if the LoadDatabaseValues option is set to false" ) ;
93
80
}
@@ -97,9 +84,9 @@ private HashSet<TEntity> ThrowNoDbValuesError()
97
84
/// A wrapper that contains an entity that is affected by the request,
98
85
/// matched to its current database value
99
86
/// </summary>
100
- public class EntityDiffPair < TEntity > where TEntity : class , IIdentifiable
87
+ public class EntityDiffPair < TResource > where TResource : class , IIdentifiable
101
88
{
102
- public EntityDiffPair ( TEntity entity , TEntity databaseValue )
89
+ public EntityDiffPair ( TResource entity , TResource databaseValue )
103
90
{
104
91
Entity = entity ;
105
92
DatabaseValue = databaseValue ;
@@ -108,10 +95,10 @@ public EntityDiffPair(TEntity entity, TEntity databaseValue)
108
95
/// <summary>
109
96
/// The resource from the request matching the resource from the database.
110
97
/// </summary>
111
- public TEntity Entity { get ; private set ; }
98
+ public TResource Entity { get ; private set ; }
112
99
/// <summary>
113
100
/// The resource from the database matching the resource from the request.
114
101
/// </summary>
115
- public TEntity DatabaseValue { get ; private set ; }
102
+ public TResource DatabaseValue { get ; private set ; }
116
103
}
117
104
}
0 commit comments