9
9
namespace JsonApiDotNetCore . Serialization
10
10
{
11
11
12
- /// <summary>
13
- /// Abstract base class for serialization. Converts entities in to <see cref="ResourceObject"/>s
14
- /// given a list of attributes and relationships.
15
- /// </summary>
12
+ /// <inheritdoc/>
16
13
public class ResourceObjectBuilder : IResourceObjectBuilder
17
14
{
18
15
protected readonly IResourceGraph _resourceGraph ;
@@ -27,14 +24,7 @@ public ResourceObjectBuilder(IResourceGraph resourceGraph, IContextEntityProvide
27
24
_settings = settings ;
28
25
}
29
26
30
- /// <summary>
31
- /// Converts <paramref name="entity"/> into a <see cref="ResourceObject"/>.
32
- /// Adds the attributes and relationships that are enlisted in <paramref name="attributes"/> and <paramref name="relationships"/>
33
- /// </summary>
34
- /// <param name="entity">Entity to build a Resource Object for</param>
35
- /// <param name="attributes">Attributes to include in the building process</param>
36
- /// <param name="relationships">Relationships to include in the building process</param>
37
- /// <returns>The resource object that was built</returns>
27
+ /// <inheritdoc/>
38
28
public ResourceObject Build ( IIdentifiable entity , IEnumerable < AttrAttribute > attributes = null , IEnumerable < RelationshipAttribute > relationships = null )
39
29
{
40
30
var resourceContext = _provider . GetContextEntity ( entity . GetType ( ) ) ;
@@ -53,30 +43,6 @@ public ResourceObject Build(IIdentifiable entity, IEnumerable<AttrAttribute> att
53
43
return ro ;
54
44
}
55
45
56
- private void ProcessRelationships ( IIdentifiable entity , IEnumerable < RelationshipAttribute > relationships , ResourceObject ro )
57
- {
58
- foreach ( var rel in relationships )
59
- {
60
- var relData = GetRelationshipData ( rel , entity ) ;
61
- if ( relData != null )
62
- ( ro . Relationships = ro . Relationships ?? new Dictionary < string , RelationshipEntry > ( ) ) . Add ( rel . PublicRelationshipName , relData ) ;
63
- }
64
- }
65
-
66
- private void ProcessAttributes ( IIdentifiable entity , IEnumerable < AttrAttribute > attributes , ResourceObject ro )
67
- {
68
- ro . Attributes = new Dictionary < string , object > ( ) ;
69
- foreach ( var attr in attributes )
70
- AddAttribute ( entity , ro , attr ) ;
71
- }
72
-
73
- private void AddAttribute ( IIdentifiable entity , ResourceObject ro , AttrAttribute attr )
74
- {
75
- var value = attr . GetValue ( entity ) ;
76
- if ( ! ( value == default && _settings . OmitDefaultValuedAttributes ) && ! ( value == null && _settings . OmitDefaultValuedAttributes ) )
77
- ro . Attributes . Add ( attr . PublicAttributeName , value ) ;
78
- }
79
-
80
46
/// <summary>
81
47
/// Builds the <see cref="RelationshipEntry"/> entries of the "relationships
82
48
/// objects" The default behaviour is to just construct a resource linkage
@@ -89,6 +55,9 @@ protected virtual RelationshipEntry GetRelationshipData(RelationshipAttribute re
89
55
return new RelationshipEntry { Data = GetRelatedResourceLinkage ( relationship , entity ) } ;
90
56
}
91
57
58
+ /// <summary>
59
+ /// Gets the value for the <see cref="RelationshipEntry.Data"/> property.
60
+ /// </summary>
92
61
protected object GetRelatedResourceLinkage ( RelationshipAttribute relationship , IIdentifiable entity )
93
62
{
94
63
if ( relationship is HasOneAttribute hasOne )
@@ -150,5 +119,32 @@ private bool IsRequiredToOneRelationship(HasOneAttribute attr, IIdentifiable ent
150
119
151
120
return false ;
152
121
}
122
+
123
+ /// <summary>
124
+ /// Puts the relationships of the entity into the resource object.
125
+ /// </summary>
126
+ private void ProcessRelationships ( IIdentifiable entity , IEnumerable < RelationshipAttribute > relationships , ResourceObject ro )
127
+ {
128
+ foreach ( var rel in relationships )
129
+ {
130
+ var relData = GetRelationshipData ( rel , entity ) ;
131
+ if ( relData != null )
132
+ ( ro . Relationships = ro . Relationships ?? new Dictionary < string , RelationshipEntry > ( ) ) . Add ( rel . PublicRelationshipName , relData ) ;
133
+ }
134
+ }
135
+
136
+ /// <summary>
137
+ /// Puts the attributes of the entity into the resource object.
138
+ /// </summary>
139
+ private void ProcessAttributes ( IIdentifiable entity , IEnumerable < AttrAttribute > attributes , ResourceObject ro )
140
+ {
141
+ ro . Attributes = new Dictionary < string , object > ( ) ;
142
+ foreach ( var attr in attributes )
143
+ {
144
+ var value = attr . GetValue ( entity ) ;
145
+ if ( ! ( value == default && _settings . OmitDefaultValuedAttributes ) && ! ( value == null && _settings . OmitDefaultValuedAttributes ) )
146
+ ro . Attributes . Add ( attr . PublicAttributeName , value ) ;
147
+ }
148
+ }
153
149
}
154
150
}
0 commit comments