1
- using System . Collections . Concurrent ;
2
1
using JsonApiDotNetCore . Internal . Generics ;
3
2
using JsonApiDotNetCore . Models . Operations ;
4
3
using JsonApiDotNetCore . Services . Operations . Processors ;
5
4
6
5
namespace JsonApiDotNetCore . Services . Operations
7
6
{
7
+ /// <summary>
8
+ /// Used to resolve <see cref="IOpProcessor"/> at runtime based on the required operation
9
+ /// </summary>
8
10
public interface IOperationProcessorResolver
9
11
{
12
+ /// <summary>
13
+ /// Locates the correct <see cref="CreateOpProcessor{T, TId}"/>
14
+ /// </summary>
10
15
IOpProcessor LocateCreateService ( Operation operation ) ;
16
+
17
+ /// <summary>
18
+ /// Locates the correct <see cref="GetOpProcessor{T, TId}"/>
19
+ /// </summary>
11
20
IOpProcessor LocateGetService ( Operation operation ) ;
21
+
22
+ /// <summary>
23
+ /// Locates the correct <see cref="RemoveOpProcessor{T, TId}"/>
24
+ /// </summary>
12
25
IOpProcessor LocateRemoveService ( Operation operation ) ;
26
+
27
+ /// <summary>
28
+ /// Locates the correct <see cref="UpdateOpProcessor{T, TId}"/>
29
+ /// </summary>
13
30
IOpProcessor LocateUpdateService ( Operation operation ) ;
14
31
}
15
32
33
+ /// <inheritdoc />
16
34
public class OperationProcessorResolver : IOperationProcessorResolver
17
35
{
18
36
private readonly IGenericProcessorFactory _processorFactory ;
19
37
private readonly IJsonApiContext _context ;
20
38
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 />
28
40
public OperationProcessorResolver (
29
41
IGenericProcessorFactory processorFactory ,
30
42
IJsonApiContext context )
@@ -33,73 +45,55 @@ public OperationProcessorResolver(
33
45
_context = context ;
34
46
}
35
47
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 />
38
49
public IOpProcessor LocateCreateService ( Operation operation )
39
50
{
40
51
var resource = operation . GetResourceTypeName ( ) ;
41
52
42
- if ( _createOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
43
- return cachedProcessor ;
44
-
45
53
var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
46
54
var processor = _processorFactory . GetProcessor < IOpProcessor > (
47
55
typeof ( ICreateOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
48
56
) ;
49
57
50
- _createOpProcessors [ resource ] = processor ;
51
-
52
58
return processor ;
53
59
}
54
60
61
+ /// <inheritdoc />
55
62
public IOpProcessor LocateGetService ( Operation operation )
56
63
{
57
64
var resource = operation . GetResourceTypeName ( ) ;
58
65
59
- if ( _getOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
60
- return cachedProcessor ;
61
-
62
66
var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
63
67
var processor = _processorFactory . GetProcessor < IOpProcessor > (
64
68
typeof ( IGetOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
65
69
) ;
66
70
67
- _getOpProcessors [ resource ] = processor ;
68
-
69
71
return processor ;
70
72
}
71
73
74
+ /// <inheritdoc />
72
75
public IOpProcessor LocateRemoveService ( Operation operation )
73
76
{
74
77
var resource = operation . GetResourceTypeName ( ) ;
75
78
76
- if ( _removeOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
77
- return cachedProcessor ;
78
-
79
79
var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
80
80
var processor = _processorFactory . GetProcessor < IOpProcessor > (
81
81
typeof ( IRemoveOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
82
82
) ;
83
83
84
- _removeOpProcessors [ resource ] = processor ;
85
-
86
84
return processor ;
87
85
}
88
86
87
+ /// <inheritdoc />
89
88
public IOpProcessor LocateUpdateService ( Operation operation )
90
89
{
91
90
var resource = operation . GetResourceTypeName ( ) ;
92
91
93
- if ( _updateOpProcessors . TryGetValue ( resource , out IOpProcessor cachedProcessor ) )
94
- return cachedProcessor ;
95
-
96
92
var contextEntity = _context . ContextGraph . GetContextEntity ( resource ) ;
97
93
var processor = _processorFactory . GetProcessor < IOpProcessor > (
98
94
typeof ( IUpdateOpProcessor < , > ) , contextEntity . EntityType , contextEntity . IdentityType
99
95
) ;
100
96
101
- _updateOpProcessors [ resource ] = processor ;
102
-
103
97
return processor ;
104
98
}
105
99
}
0 commit comments