Skip to content

Commit 7350540

Browse files
author
Bart Koelman
committed
Cleanup SelectClauseBuilder.ToPropertySelectors
1 parent 546a8f4 commit 7350540

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

src/JsonApiDotNetCore/Queries/Internal/QueryableBuilding/SelectClauseBuilder.cs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,38 +86,57 @@ private ICollection<PropertySelector> ToPropertySelectors(IDictionary<ResourceFi
8686
{
8787
var propertySelectors = new Dictionary<PropertyInfo, PropertySelector>();
8888

89-
// If a read-only attribute is selected, its value likely depends on another property, so select all resource properties.
89+
// If a read-only attribute is selected, its calculated value likely depends on another property, so select all properties.
9090
bool includesReadOnlyAttribute = resourceFieldSelectors.Any(selector =>
9191
selector.Key is AttrAttribute attribute && attribute.Property.SetMethod == null);
9292

93+
// Only selecting relationships implicitly means to select all attributes too.
9394
bool containsOnlyRelationships = resourceFieldSelectors.All(selector => selector.Key is RelationshipAttribute);
9495

95-
foreach ((ResourceFieldAttribute resourceField, QueryLayer? queryLayer) in resourceFieldSelectors)
96+
if (includesReadOnlyAttribute || containsOnlyRelationships)
9697
{
97-
var propertySelector = new PropertySelector(resourceField.Property, queryLayer);
98-
99-
if (propertySelector.Property.SetMethod != null)
100-
{
101-
propertySelectors[propertySelector.Property] = propertySelector;
102-
}
98+
IncludeAllProperties(elementType, propertySelectors);
10399
}
104100

105-
if (includesReadOnlyAttribute || containsOnlyRelationships)
101+
IncludeFieldSelection(resourceFieldSelectors, propertySelectors);
102+
103+
IncludeEagerLoads(resourceType, propertySelectors);
104+
105+
return propertySelectors.Values;
106+
}
107+
108+
private void IncludeAllProperties(Type elementType, Dictionary<PropertyInfo, PropertySelector> propertySelectors)
109+
{
110+
IEntityType entityModel = _entityModel.GetEntityTypes().Single(type => type.ClrType == elementType);
111+
IEnumerable<IProperty> entityProperties = entityModel.GetProperties().Where(property => !property.IsShadowProperty()).ToArray();
112+
113+
foreach (IProperty entityProperty in entityProperties)
106114
{
107-
IEntityType entityModel = _entityModel.GetEntityTypes().Single(type => type.ClrType == elementType);
108-
IEnumerable<IProperty> entityProperties = entityModel.GetProperties().Where(property => !property.IsShadowProperty()).ToArray();
115+
var propertySelector = new PropertySelector(entityProperty.PropertyInfo);
116+
IncludeWritableProperty(propertySelector, propertySelectors);
117+
}
118+
}
109119

110-
foreach (IProperty entityProperty in entityProperties)
111-
{
112-
var propertySelector = new PropertySelector(entityProperty.PropertyInfo!);
120+
private static void IncludeFieldSelection(IDictionary<ResourceFieldAttribute, QueryLayer?> resourceFieldSelectors,
121+
Dictionary<PropertyInfo, PropertySelector> propertySelectors)
122+
{
123+
foreach ((ResourceFieldAttribute resourceField, QueryLayer? queryLayer) in resourceFieldSelectors)
124+
{
125+
var propertySelector = new PropertySelector(resourceField.Property, queryLayer);
126+
IncludeWritableProperty(propertySelector, propertySelectors);
127+
}
128+
}
113129

114-
if (propertySelector.Property.SetMethod != null)
115-
{
116-
propertySelectors[propertySelector.Property] = propertySelector;
117-
}
118-
}
130+
private static void IncludeWritableProperty(PropertySelector propertySelector, Dictionary<PropertyInfo, PropertySelector> propertySelectors)
131+
{
132+
if (propertySelector.Property.SetMethod != null)
133+
{
134+
propertySelectors[propertySelector.Property] = propertySelector;
119135
}
136+
}
120137

138+
private static void IncludeEagerLoads(ResourceType resourceType, Dictionary<PropertyInfo, PropertySelector> propertySelectors)
139+
{
121140
foreach (EagerLoadAttribute eagerLoad in resourceType.EagerLoads)
122141
{
123142
var propertySelector = new PropertySelector(eagerLoad.Property);
@@ -129,8 +148,6 @@ private ICollection<PropertySelector> ToPropertySelectors(IDictionary<ResourceFi
129148
propertySelectors[propertySelector.Property] = propertySelector;
130149
}
131150
}
132-
133-
return propertySelectors.Values;
134151
}
135152

136153
private MemberAssignment CreatePropertyAssignment(PropertySelector selector, LambdaScope lambdaScope)

0 commit comments

Comments
 (0)