@@ -19,20 +19,22 @@ namespace JsonApiDotNetCore.Hooks.Internal.Execution
19
19
/// <inheritdoc />
20
20
internal sealed class HookExecutorHelper : IHookExecutorHelper
21
21
{
22
+ private static readonly HooksCollectionConverter CollectionConverter = new HooksCollectionConverter ( ) ;
23
+ private static readonly HooksObjectFactory ObjectFactory = new HooksObjectFactory ( ) ;
22
24
private static readonly IncludeChainConverter IncludeChainConverter = new IncludeChainConverter ( ) ;
23
25
24
26
private readonly IdentifiableComparer _comparer = IdentifiableComparer . Instance ;
25
27
private readonly IJsonApiOptions _options ;
26
- private readonly IGenericServiceFactory _genericProcessorFactory ;
28
+ private readonly IGenericServiceFactory _genericServiceFactory ;
27
29
private readonly IResourceContextProvider _resourceContextProvider ;
28
30
private readonly Dictionary < RightType , IResourceHookContainer > _hookContainers ;
29
31
private readonly Dictionary < RightType , IHooksDiscovery > _hookDiscoveries ;
30
32
private readonly List < ResourceHook > _targetedHooksForRelatedResources ;
31
33
32
- public HookExecutorHelper ( IGenericServiceFactory genericProcessorFactory , IResourceContextProvider resourceContextProvider , IJsonApiOptions options )
34
+ public HookExecutorHelper ( IGenericServiceFactory genericServiceFactory , IResourceContextProvider resourceContextProvider , IJsonApiOptions options )
33
35
{
34
36
_options = options ;
35
- _genericProcessorFactory = genericProcessorFactory ;
37
+ _genericServiceFactory = genericServiceFactory ;
36
38
_resourceContextProvider = resourceContextProvider ;
37
39
_hookContainers = new Dictionary < RightType , IResourceHookContainer > ( ) ;
38
40
_hookDiscoveries = new Dictionary < RightType , IHooksDiscovery > ( ) ;
@@ -48,7 +50,7 @@ public IResourceHookContainer GetResourceHookContainer(RightType targetResource,
48
50
// so we need not even bother.
49
51
if ( ! _hookContainers . TryGetValue ( targetResource , out IResourceHookContainer container ) )
50
52
{
51
- container = _genericProcessorFactory . Get < IResourceHookContainer > ( typeof ( ResourceHooksDefinition < > ) , targetResource ) ;
53
+ container = _genericServiceFactory . Get < IResourceHookContainer > ( typeof ( ResourceHooksDefinition < > ) , targetResource ) ;
52
54
_hookContainers [ targetResource ] = container ;
53
55
}
54
56
@@ -91,23 +93,17 @@ public IResourceHookContainer<TResource> GetResourceHookContainer<TResource>(Res
91
93
92
94
public IEnumerable LoadDbValues ( LeftType resourceTypeForRepository , IEnumerable resources , params RelationshipAttribute [ ] relationshipsToNextLayer )
93
95
{
94
- LeftType idType = TypeHelper . GetIdType ( resourceTypeForRepository ) ;
96
+ LeftType idType = ObjectFactory . GetIdType ( resourceTypeForRepository ) ;
95
97
96
98
MethodInfo parameterizedGetWhere =
97
99
GetType ( ) . GetMethod ( nameof ( GetWhereWithInclude ) , BindingFlags . NonPublic | BindingFlags . Instance ) ! . MakeGenericMethod ( resourceTypeForRepository ,
98
100
idType ) ;
99
101
100
- IEnumerable < IIdentifiable > cast = ( ( IEnumerable < object > ) resources ) . Cast < IIdentifiable > ( ) ;
101
- IList ids = TypeHelper . CopyToList ( cast . Select ( resource => resource . GetTypedId ( ) ) , idType ) ;
102
- var values = ( IEnumerable ) parameterizedGetWhere . Invoke ( this , ArrayFactory . Create < object > ( ids , relationshipsToNextLayer ) ) ;
102
+ IEnumerable < object > resourceIds = ( ( IEnumerable < object > ) resources ) . Cast < IIdentifiable > ( ) . Select ( resource => resource . GetTypedId ( ) ) ;
103
+ IList idsAsList = CollectionConverter . CopyToList ( resourceIds , idType ) ;
104
+ var values = ( IEnumerable ) parameterizedGetWhere . Invoke ( this , ArrayFactory . Create < object > ( idsAsList , relationshipsToNextLayer ) ) ;
103
105
104
- if ( values == null )
105
- {
106
- return null ;
107
- }
108
-
109
- return ( IEnumerable ) Activator . CreateInstance ( typeof ( HashSet < > ) . MakeGenericType ( resourceTypeForRepository ) ,
110
- TypeHelper . CopyToList ( values , resourceTypeForRepository ) ) ;
106
+ return values == null ? null : CollectionConverter . CopyToHashSet ( values , resourceTypeForRepository ) ;
111
107
}
112
108
113
109
public bool ShouldLoadDbValues ( LeftType resourceType , ResourceHook hook )
@@ -146,7 +142,7 @@ private IHooksDiscovery GetHookDiscovery(LeftType resourceType)
146
142
{
147
143
if ( ! _hookDiscoveries . TryGetValue ( resourceType , out IHooksDiscovery discovery ) )
148
144
{
149
- discovery = _genericProcessorFactory . Get < IHooksDiscovery > ( typeof ( IHooksDiscovery < > ) , resourceType ) ;
145
+ discovery = _genericServiceFactory . Get < IHooksDiscovery > ( typeof ( IHooksDiscovery < > ) , resourceType ) ;
150
146
_hookDiscoveries [ resourceType ] = discovery ;
151
147
}
152
148
@@ -199,7 +195,7 @@ private static FilterExpression CreateFilterByIds<TId>(IReadOnlyCollection<TId>
199
195
private IResourceReadRepository < TResource , TId > GetRepository < TResource , TId > ( )
200
196
where TResource : class , IIdentifiable < TId >
201
197
{
202
- return _genericProcessorFactory . Get < IResourceReadRepository < TResource , TId > > ( typeof ( IResourceReadRepository < , > ) , typeof ( TResource ) , typeof ( TId ) ) ;
198
+ return _genericServiceFactory . Get < IResourceReadRepository < TResource , TId > > ( typeof ( IResourceReadRepository < , > ) , typeof ( TResource ) , typeof ( TId ) ) ;
203
199
}
204
200
205
201
public IDictionary < RelationshipAttribute , IEnumerable > LoadImplicitlyAffected ( IDictionary < RelationshipAttribute , IEnumerable > leftResourcesByRelation ,
@@ -209,10 +205,10 @@ public IDictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(ID
209
205
210
206
var implicitlyAffected = new Dictionary < RelationshipAttribute , IEnumerable > ( ) ;
211
207
212
- foreach ( KeyValuePair < RelationshipAttribute , IEnumerable > kvp in leftResourcesByRelation )
208
+ foreach ( KeyValuePair < RelationshipAttribute , IEnumerable > pair in leftResourcesByRelation )
213
209
{
214
- RelationshipAttribute relationship = kvp . Key ;
215
- IEnumerable lefts = kvp . Value ;
210
+ RelationshipAttribute relationship = pair . Key ;
211
+ IEnumerable lefts = pair . Value ;
216
212
217
213
if ( relationship is HasManyThroughAttribute )
218
214
{
@@ -225,45 +221,31 @@ public IDictionary<RelationshipAttribute, IEnumerable> LoadImplicitlyAffected(ID
225
221
AddToImplicitlyAffected ( includedLefts , relationship , existingRightResourceList , implicitlyAffected ) ;
226
222
}
227
223
228
- return implicitlyAffected . ToDictionary ( kvp => kvp . Key , kvp => TypeHelper . CreateHashSetFor ( kvp . Key . RightType , kvp . Value ) ) ;
224
+ return implicitlyAffected . ToDictionary ( pair => pair . Key , pair => CollectionConverter . CopyToHashSet ( pair . Value , pair . Key . RightType ) ) ;
229
225
}
230
226
231
227
private void AddToImplicitlyAffected ( IEnumerable includedLefts , RelationshipAttribute relationship , List < IIdentifiable > existingRightResourceList ,
232
228
Dictionary < RelationshipAttribute , IEnumerable > implicitlyAffected )
233
229
{
234
230
foreach ( IIdentifiable ip in includedLefts )
235
231
{
236
- IList dbRightResourceList = TypeHelper . CreateListFor ( relationship . RightType ) ;
237
232
object relationshipValue = relationship . GetValue ( ip ) ;
238
-
239
- if ( ! ( relationshipValue is IEnumerable ) )
240
- {
241
- if ( relationshipValue != null )
242
- {
243
- dbRightResourceList . Add ( relationshipValue ) ;
244
- }
245
- }
246
- else
247
- {
248
- AddToList ( dbRightResourceList , ( IEnumerable ) relationshipValue ) ;
249
- }
250
-
251
- List < IIdentifiable > dbRightResourceListCast = dbRightResourceList . Cast < IIdentifiable > ( ) . ToList ( ) ;
233
+ ICollection < IIdentifiable > dbRightResources = CollectionConverter . ExtractResources ( relationshipValue ) ;
252
234
253
235
if ( existingRightResourceList != null )
254
236
{
255
- dbRightResourceListCast = dbRightResourceListCast . Except ( existingRightResourceList , _comparer ) . ToList ( ) ;
237
+ dbRightResources = dbRightResources . Except ( existingRightResourceList , _comparer ) . ToList ( ) ;
256
238
}
257
239
258
- if ( dbRightResourceListCast . Any ( ) )
240
+ if ( dbRightResources . Any ( ) )
259
241
{
260
242
if ( ! implicitlyAffected . TryGetValue ( relationship , out IEnumerable affected ) )
261
243
{
262
- affected = TypeHelper . CreateListFor ( relationship . RightType ) ;
244
+ affected = CollectionConverter . CopyToList ( Array . Empty < object > ( ) , relationship . RightType ) ;
263
245
implicitlyAffected [ relationship ] = affected ;
264
246
}
265
247
266
- AddToList ( ( IList ) affected , dbRightResourceListCast ) ;
248
+ AddToList ( ( IList ) affected , dbRightResources ) ;
267
249
}
268
250
}
269
251
}
0 commit comments