1
1
2
+ using System ;
2
3
using System . Collections ;
3
4
using System . Collections . Generic ;
5
+ using System . Linq ;
6
+ using JsonApiDotNetCore . Internal ;
4
7
using JsonApiDotNetCore . Models ;
5
8
6
9
namespace JsonApiDotNetCore . Hooks
7
10
{
11
+
8
12
/// <summary>
9
13
/// A helper class that provides insight in what is to be updated. The
10
14
/// <see cref="IEntityDiff{TEntity}.RequestEntities"/> property reflects what was parsed from the incoming request,
@@ -21,14 +25,21 @@ public interface IEntityDiff<TEntity> : IAffectedRelationships<TEntity> where TE
21
25
22
26
public class EntityDiff < TEntity > : AffectedRelationships < TEntity > , IEntityDiff < TEntity > where TEntity : class , IIdentifiable
23
27
{
28
+ private readonly HashSet < TEntity > _databaseEntities ;
29
+ public HashSet < TEntity > DatabaseEntities { get => _databaseEntities ?? ThrowNoDbValuesError ( ) ; }
30
+
24
31
public HashSet < TEntity > RequestEntities { get ; private set ; }
25
- public HashSet < TEntity > DatabaseEntities { get ; private set ; }
26
32
public EntityDiff ( IEnumerable requestEntities ,
27
33
IEnumerable databaseEntities ,
28
- Dictionary < RelationshipProxy , IEnumerable > relationships ) : base ( relationships )
34
+ Dictionary < RelationshipProxy , IEnumerable > relationships ) : base ( relationships )
29
35
{
30
36
RequestEntities = ( HashSet < TEntity > ) requestEntities ;
31
- DatabaseEntities = ( HashSet < TEntity > ) databaseEntities ;
37
+ _databaseEntities = ( HashSet < TEntity > ) databaseEntities ;
38
+ }
39
+
40
+ private HashSet < TEntity > ThrowNoDbValuesError ( )
41
+ {
42
+ throw new MemberAccessException ( "Cannot access database entities if the LoadDatabaseValues option is set to false" ) ;
32
43
}
33
44
}
34
45
}
0 commit comments