11using System ;
22using System . Collections ;
33using System . Collections . Generic ;
4+ using System . Collections . ObjectModel ;
45using System . Linq ;
56using JsonApiDotNetCore . Models ;
67
@@ -11,44 +12,104 @@ public interface IAffectedRelationships { }
1112 /// <summary>
1213 /// A helper class that provides insights in which relationships have been updated for which entities.
1314 /// </summary>
14- public interface IAffectedRelationships < TDependent > : IAffectedRelationships where TDependent : class , IIdentifiable
15+ public interface IAffectedRelationships < TDependentResource > : IAffectedRelationships where TDependentResource : class , IIdentifiable
1516 {
1617 /// <summary>
1718 /// Gets a dictionary of all entities grouped by affected relationship.
1819 /// </summary>
19- Dictionary < RelationshipAttribute , HashSet < TDependent > > AllByRelationships ( ) ;
20+ Dictionary < RelationshipAttribute , HashSet < TDependentResource > > AllByRelationships ( ) ;
2021
2122 /// <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 "/>
2324 /// </summary>
24- Dictionary < RelationshipAttribute , HashSet < TDependent > > GetByRelationship < TPrincipal > ( ) where TPrincipal : class , IIdentifiable ;
25+ Dictionary < RelationshipAttribute , HashSet < TDependentResource > > GetByRelationship < TPrincipalResource > ( ) where TPrincipalResource : class , IIdentifiable ;
2526 /// <summary>
2627 /// Gets a dictionary of all entities that have an affected relationship to type <paramref name="principalType"/>
2728 /// </summary>
28- Dictionary < RelationshipAttribute , HashSet < TDependent > > GetByRelationship ( Type principalType ) ;
29+ Dictionary < RelationshipAttribute , HashSet < TDependentResource > > GetByRelationship ( Type principalType ) ;
2930 }
3031
31- public class AffectedRelationships < TDependent > : IAffectedRelationships < TDependent > where TDependent : class , IIdentifiable
32+ /// <inheritdoc />
33+ public class AffectedRelationships < TDependentResource > : IAffectedRelationships < TDependentResource > where TDependentResource : class , IIdentifiable
3234 {
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 ;
3445
35- public Dictionary < RelationshipAttribute , HashSet < TDependent > > AllByRelationships ( )
46+ /// <inheritdoc />
47+ public AffectedRelationships ( Dictionary < RelationshipAttribute , HashSet < TDependentResource > > relationships )
3648 {
37- return _groups ;
49+ _groups = relationships ;
3850 }
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 ( )
4058 {
41- _groups = relationships . ToDictionary ( kvp => kvp . Key , kvp => new HashSet < TDependent > ( ( IEnumerable < TDependent > ) kvp . Value ) ) ;
59+ return _groups ;
4260 }
4361
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
4564 {
46- return GetByRelationship ( typeof ( TPrincipal ) ) ;
65+ return GetByRelationship ( typeof ( TPrincipalResource ) ) ;
4766 }
4867
49- public Dictionary < RelationshipAttribute , HashSet < TDependent > > GetByRelationship ( Type principalType )
68+ /// <inheritdoc />
69+ public Dictionary < RelationshipAttribute , HashSet < TDependentResource > > GetByRelationship ( Type principalType )
5070 {
5171 return _groups ? . Where ( p => p . Key . PrincipalType == principalType ) . ToDictionary ( p => p . Key , p => p . Value ) ;
5272 }
5373 }
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+ //}
54115}
0 commit comments