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 . Models ;
6
7
@@ -11,44 +12,104 @@ public interface IAffectedRelationships { }
11
12
/// <summary>
12
13
/// A helper class that provides insights in which relationships have been updated for which entities.
13
14
/// </summary>
14
- public interface IAffectedRelationships < TDependent > : IAffectedRelationships where TDependent : class , IIdentifiable
15
+ public interface IAffectedRelationships < TDependentResource > : IAffectedRelationships where TDependentResource : class , IIdentifiable
15
16
{
16
17
/// <summary>
17
18
/// Gets a dictionary of all entities grouped by affected relationship.
18
19
/// </summary>
19
- Dictionary < RelationshipAttribute , HashSet < TDependent > > AllByRelationships ( ) ;
20
+ Dictionary < RelationshipAttribute , HashSet < TDependentResource > > AllByRelationships ( ) ;
20
21
21
22
/// <summary>
22
- /// Gets a dictionary of all entities that have an affected relationship to type <typeparamref name="TPrincipal "/>
23
+ /// Gets a dictionary of all entities that have an affected relationship to type <typeparamref name="TPrincipalResource "/>
23
24
/// </summary>
24
- Dictionary < RelationshipAttribute , HashSet < TDependent > > GetByRelationship < TPrincipal > ( ) where TPrincipal : class , IIdentifiable ;
25
+ Dictionary < RelationshipAttribute , HashSet < TDependentResource > > GetByRelationship < TPrincipalResource > ( ) where TPrincipalResource : class , IIdentifiable ;
25
26
/// <summary>
26
27
/// Gets a dictionary of all entities that have an affected relationship to type <paramref name="principalType"/>
27
28
/// </summary>
28
- Dictionary < RelationshipAttribute , HashSet < TDependent > > GetByRelationship ( Type principalType ) ;
29
+ Dictionary < RelationshipAttribute , HashSet < TDependentResource > > GetByRelationship ( Type principalType ) ;
29
30
}
30
31
31
- public class AffectedRelationships < TDependent > : IAffectedRelationships < TDependent > where TDependent : class , IIdentifiable
32
+ /// <inheritdoc />
33
+ public class AffectedRelationships < TDependentResource > : IAffectedRelationships < TDependentResource > where TDependentResource : class , IIdentifiable
32
34
{
33
- private readonly Dictionary < RelationshipAttribute , HashSet < TDependent > > _groups ;
35
+ internal static Dictionary < RelationshipAttribute , HashSet < TDependentResource > > ConvertRelationshipDictionary ( Dictionary < RelationshipAttribute , IEnumerable > relationships )
36
+ {
37
+ return relationships . ToDictionary ( pair => pair . Key , pair => ( HashSet < TDependentResource > ) pair . Value ) ;
38
+ }
39
+
40
+ /// <summary>
41
+ /// a dictionary with affected relationships as keys and values being the corresponding resources
42
+ /// that were affected
43
+ /// </summary>
44
+ private readonly Dictionary < RelationshipAttribute , HashSet < TDependentResource > > _groups ;
34
45
35
- public Dictionary < RelationshipAttribute , HashSet < TDependent > > AllByRelationships ( )
46
+ /// <inheritdoc />
47
+ public AffectedRelationships ( Dictionary < RelationshipAttribute , HashSet < TDependentResource > > relationships )
36
48
{
37
- return _groups ;
49
+ _groups = relationships ;
38
50
}
39
- public AffectedRelationships ( Dictionary < RelationshipAttribute , IEnumerable > relationships )
51
+
52
+ /// <summary>
53
+ /// Used internally by the ResourceHookExecutor to make live a bit easier with generics
54
+ /// </summary>
55
+ internal AffectedRelationships ( Dictionary < RelationshipAttribute , IEnumerable > relationships ) : this ( ConvertRelationshipDictionary ( relationships ) ) { }
56
+
57
+ public Dictionary < RelationshipAttribute , HashSet < TDependentResource > > AllByRelationships ( )
40
58
{
41
- _groups = relationships . ToDictionary ( kvp => kvp . Key , kvp => new HashSet < TDependent > ( ( IEnumerable < TDependent > ) kvp . Value ) ) ;
59
+ return _groups ;
42
60
}
43
61
44
- public Dictionary < RelationshipAttribute , HashSet < TDependent > > GetByRelationship < TPrincipal > ( ) where TPrincipal : class , IIdentifiable
62
+ /// <inheritdoc />
63
+ public Dictionary < RelationshipAttribute , HashSet < TDependentResource > > GetByRelationship < TPrincipalResource > ( ) where TPrincipalResource : class , IIdentifiable
45
64
{
46
- return GetByRelationship ( typeof ( TPrincipal ) ) ;
65
+ return GetByRelationship ( typeof ( TPrincipalResource ) ) ;
47
66
}
48
67
49
- public Dictionary < RelationshipAttribute , HashSet < TDependent > > GetByRelationship ( Type principalType )
68
+ /// <inheritdoc />
69
+ public Dictionary < RelationshipAttribute , HashSet < TDependentResource > > GetByRelationship ( Type principalType )
50
70
{
51
71
return _groups ? . Where ( p => p . Key . PrincipalType == principalType ) . ToDictionary ( p => p . Key , p => p . Value ) ;
52
72
}
53
73
}
74
+
75
+ ///// <inheritdoc />
76
+ //public class AffectedRelationships<TDependentResource> : ReadOnlyDictionary<RelationshipAttribute, HashSet<TDependentResource>>, IAffectedRelationships<TDependentResource> where TDependentResource : class, IIdentifiable
77
+ //{
78
+ // private readonly Dictionary<RelationshipAttribute, HashSet<TDependentResource>> _groups;
79
+
80
+ // private static IDictionary<RelationshipAttribute, HashSet<TDependentResource>> test(Dictionary<RelationshipAttribute, IEnumerable> relationship)
81
+ // {
82
+ // return relationship.ToDictionary(kvp => kvp.Key, kvp => new HashSet<TDependentResource>((IEnumerable<TDependentResource>)kvp.Value));
83
+ // }
84
+
85
+ // public AffectedRelationships(Dictionary<RelationshipAttribute, IEnumerable> relationship) : base(test(relationship))
86
+ // {
87
+ // }
88
+
89
+
90
+ // /// <inheritdoc />
91
+ // public AffectedRelationships(Dictionary<RelationshipAttribute, IEnumerable> relationships)
92
+ // {
93
+ // _groups = relationships.ToDictionary(kvp => kvp.Key, kvp => new HashSet<TDependentResource>((IEnumerable<TDependentResource>)kvp.Value));
94
+ // }
95
+
96
+ // public Dictionary<RelationshipAttribute, HashSet<TDependentResource>> AllByRelationships()
97
+ // {
98
+ // return _groups;
99
+ // }
100
+
101
+
102
+
103
+ // /// <inheritdoc />
104
+ // public Dictionary<RelationshipAttribute, HashSet<TDependentResource>> GetByRelationship<TPrincipalResource>() where TPrincipalResource : class, IIdentifiable
105
+ // {
106
+ // return GetByRelationship(typeof(TPrincipalResource));
107
+ // }
108
+
109
+ // /// <inheritdoc />
110
+ // public Dictionary<RelationshipAttribute, HashSet<TDependentResource>> GetByRelationship(Type principalType)
111
+ // {
112
+ // return _groups?.Where(p => p.Key.PrincipalType == principalType).ToDictionary(p => p.Key, p => p.Value);
113
+ // }
114
+ //}
54
115
}
0 commit comments