1- using System . Collections . Concurrent ;
21using JsonApiDotNetCore . Internal . Generics ;
32using JsonApiDotNetCore . Models . Operations ;
43using JsonApiDotNetCore . Services . Operations . Processors ;
54
65namespace JsonApiDotNetCore . Services . Operations
76{
7+ /// <summary>
8+ /// Used to resolve <see cref="IOpProcessor"/> at runtime based on the required operation
9+ /// </summary>
810 public interface IOperationProcessorResolver
911 {
12+ /// <summary>
13+ /// Locates the correct <see cref="CreateOpProcessor{T, TId}"/>
14+ /// </summary>
1015 IOpProcessor LocateCreateService ( Operation operation ) ;
16+
17+ /// <summary>
18+ /// Locates the correct <see cref="GetOpProcessor{T, TId}"/>
19+ /// </summary>
1120 IOpProcessor LocateGetService ( Operation operation ) ;
21+
22+ /// <summary>
23+ /// Locates the correct <see cref="RemoveOpProcessor{T, TId}"/>
24+ /// </summary>
1225 IOpProcessor LocateRemoveService ( Operation operation ) ;
26+
27+ /// <summary>
28+ /// Locates the correct <see cref="UpdateOpProcessor{T, TId}"/>
29+ /// </summary>
1330 IOpProcessor LocateUpdateService ( Operation operation ) ;
1431 }
1532
33+ /// <inheritdoc />
1634 public class OperationProcessorResolver : IOperationProcessorResolver
1735 {
1836 private readonly IGenericProcessorFactory _processorFactory ;
1937 private readonly IJsonApiContext _context ;
2038
21- // processor caches -- since there is some associated cost with creating the processors, we store them in memory
22- // to reduce the cost of subsequent requests. in the future, this may be moved into setup code run at startup
23- private ConcurrentDictionary < string , IOpProcessor > _createOpProcessors = new ConcurrentDictionary < string , IOpProcessor > ( ) ;
24- private ConcurrentDictionary < string , IOpProcessor > _getOpProcessors = new ConcurrentDictionary < string , IOpProcessor > ( ) ;
25- private ConcurrentDictionary < string , IOpProcessor > _removeOpProcessors = new ConcurrentDictionary < string , IOpProcessor > ( ) ;
26- private ConcurrentDictionary < string , IOpProcessor > _updateOpProcessors = new ConcurrentDictionary < string , IOpProcessor > ( ) ;
27-
39+ /// <nodoc />
2840 public OperationProcessorResolver (
2941 IGenericProcessorFactory processorFactory ,
3042 IJsonApiContext context )
@@ -33,73 +45,55 @@ public OperationProcessorResolver(
3345 _context = context ;
3446 }
3547
36- // TODO: there may be some optimizations here around the cache such as not caching processors
37- // if the request only contains a single op
48+ /// <inheritdoc />
3849 public IOpProcessor LocateCreateService ( Operation operation )
3950 {
4051 var resource = operation . GetResourceTypeName ( ) ;
4152
42- if ( _createOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
43- return cachedProcessor ;
44-
4553 var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
4654 var processor = _processorFactory . GetProcessor < IOpProcessor > (
4755 typeof ( ICreateOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
4856 ) ;
4957
50- _createOpProcessors [ resource ] = processor ;
51-
5258 return processor ;
5359 }
5460
61+ /// <inheritdoc />
5562 public IOpProcessor LocateGetService ( Operation operation )
5663 {
5764 var resource = operation . GetResourceTypeName ( ) ;
5865
59- if ( _getOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
60- return cachedProcessor ;
61-
6266 var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
6367 var processor = _processorFactory . GetProcessor < IOpProcessor > (
6468 typeof ( IGetOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
6569 ) ;
6670
67- _getOpProcessors [ resource ] = processor ;
68-
6971 return processor ;
7072 }
7173
74+ /// <inheritdoc />
7275 public IOpProcessor LocateRemoveService ( Operation operation )
7376 {
7477 var resource = operation . GetResourceTypeName ( ) ;
7578
76- if ( _removeOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
77- return cachedProcessor ;
78-
7979 var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
8080 var processor = _processorFactory . GetProcessor < IOpProcessor > (
8181 typeof ( IRemoveOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
8282 ) ;
8383
84- _removeOpProcessors [ resource ] = processor ;
85-
8684 return processor ;
8785 }
8886
87+ /// <inheritdoc />
8988 public IOpProcessor LocateUpdateService ( Operation operation )
9089 {
9190 var resource = operation . GetResourceTypeName ( ) ;
9291
93- if ( _updateOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
94- return cachedProcessor ;
95-
9692 var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
9793 var processor = _processorFactory . GetProcessor < IOpProcessor > (
9894 typeof ( IUpdateOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
9995 ) ;
10096
101- _updateOpProcessors [ resource ] = processor ;
102-
10397 return processor ;
10498 }
10599 }
0 commit comments