1
+ using System ;
1
2
using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using System . Linq ;
@@ -14,22 +15,29 @@ public class DocumentBuilder : IDocumentBuilder
14
15
private readonly IContextGraph _contextGraph ;
15
16
private readonly IRequestMeta _requestMeta ;
16
17
private readonly DocumentBuilderOptions _documentBuilderOptions ;
18
+ private readonly IScopedServiceProvider _scopedServiceProvider ;
17
19
18
- public DocumentBuilder ( IJsonApiContext jsonApiContext , IRequestMeta requestMeta = null , IDocumentBuilderOptionsProvider documentBuilderOptionsProvider = null )
20
+ public DocumentBuilder (
21
+ IJsonApiContext jsonApiContext ,
22
+ IRequestMeta requestMeta = null ,
23
+ IDocumentBuilderOptionsProvider documentBuilderOptionsProvider = null ,
24
+ IScopedServiceProvider scopedServiceProvider = null )
19
25
{
20
26
_jsonApiContext = jsonApiContext ;
21
27
_contextGraph = jsonApiContext . ContextGraph ;
22
28
_requestMeta = requestMeta ;
23
- _documentBuilderOptions = documentBuilderOptionsProvider ? . GetDocumentBuilderOptions ( ) ?? new DocumentBuilderOptions ( ) ; ;
29
+ _documentBuilderOptions = documentBuilderOptionsProvider ? . GetDocumentBuilderOptions ( ) ?? new DocumentBuilderOptions ( ) ;
30
+ _scopedServiceProvider = scopedServiceProvider ;
24
31
}
25
32
26
33
public Document Build ( IIdentifiable entity )
27
34
{
28
35
var contextEntity = _contextGraph . GetContextEntity ( entity . GetType ( ) ) ;
29
36
37
+ var resourceDefinition = _scopedServiceProvider ? . GetService ( contextEntity . ResourceType ) as IResourceDefinition ;
30
38
var document = new Document
31
39
{
32
- Data = GetData ( contextEntity , entity ) ,
40
+ Data = GetData ( contextEntity , entity , resourceDefinition ) ,
33
41
Meta = GetMeta ( entity )
34
42
} ;
35
43
@@ -44,8 +52,8 @@ public Document Build(IIdentifiable entity)
44
52
public Documents Build ( IEnumerable < IIdentifiable > entities )
45
53
{
46
54
var entityType = entities . GetElementType ( ) ;
47
-
48
55
var contextEntity = _contextGraph . GetContextEntity ( entityType ) ;
56
+ var resourceDefinition = _scopedServiceProvider ? . GetService ( contextEntity . ResourceType ) as IResourceDefinition ;
49
57
50
58
var enumeratedEntities = entities as IList < IIdentifiable > ?? entities . ToList ( ) ;
51
59
var documents = new Documents
@@ -59,7 +67,7 @@ public Documents Build(IEnumerable<IIdentifiable> entities)
59
67
60
68
foreach ( var entity in enumeratedEntities )
61
69
{
62
- documents . Data . Add ( GetData ( contextEntity , entity ) ) ;
70
+ documents . Data . Add ( GetData ( contextEntity , entity , resourceDefinition ) ) ;
63
71
documents . Included = AppendIncludedObject ( documents . Included , contextEntity , entity ) ;
64
72
}
65
73
@@ -98,7 +106,11 @@ private List<DocumentData> AppendIncludedObject(List<DocumentData> includedObjec
98
106
return includedObject ;
99
107
}
100
108
109
+ [ Obsolete ( "You should specify an IResourceDefinition implementation using the GetData/3 overload." ) ]
101
110
public DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity )
111
+ => GetData ( contextEntity , entity , resourceDefinition : null ) ;
112
+
113
+ public DocumentData GetData ( ContextEntity contextEntity , IIdentifiable entity , IResourceDefinition resourceDefinition = null )
102
114
{
103
115
var data = new DocumentData
104
116
{
@@ -111,7 +123,8 @@ public DocumentData GetData(ContextEntity contextEntity, IIdentifiable entity)
111
123
112
124
data . Attributes = new Dictionary < string , object > ( ) ;
113
125
114
- contextEntity . Attributes . ForEach ( attr =>
126
+ var resourceAttributes = resourceDefinition ? . GetOutputAttrs ( entity ) ?? contextEntity . Attributes ;
127
+ resourceAttributes . ForEach ( attr =>
115
128
{
116
129
var attributeValue = attr . GetValue ( entity ) ;
117
130
if ( ShouldIncludeAttribute ( attr , attributeValue ) )
@@ -219,8 +232,9 @@ private DocumentData GetIncludedEntity(IIdentifiable entity)
219
232
if ( entity == null ) return null ;
220
233
221
234
var contextEntity = _jsonApiContext . ContextGraph . GetContextEntity ( entity . GetType ( ) ) ;
235
+ var resourceDefinition = _scopedServiceProvider . GetService ( contextEntity . ResourceType ) as IResourceDefinition ;
222
236
223
- var data = GetData ( contextEntity , entity ) ;
237
+ var data = GetData ( contextEntity , entity , resourceDefinition ) ;
224
238
225
239
data . Attributes = new Dictionary < string , object > ( ) ;
226
240
0 commit comments