@@ -196,24 +196,18 @@ public virtual async Task<TResource> GetRelationshipAsync(TId id, string relatio
196196 _hookExecutor ? . BeforeRead < TResource > ( ResourcePipeline . GetRelationship , id . ToString ( ) ) ;
197197
198198 var secondaryLayer = _queryLayerComposer . Compose ( _request . SecondaryResource ) ;
199-
200- var secondaryIdAttribute = _request . SecondaryResource . Attributes . Single ( a => a . Property . Name == nameof ( Identifiable . Id ) ) ;
201-
199+ secondaryLayer . Projection = _queryLayerComposer . GetSecondaryProjectionForRelationshipEndpoint ( _request . SecondaryResource ) ;
202200 secondaryLayer . Include = null ;
203- secondaryLayer . Projection = new Dictionary < ResourceFieldAttribute , QueryLayer >
204- {
205- [ secondaryIdAttribute ] = null
206- } ;
207-
208- var primaryLayer = GetPrimaryLayerForSecondaryEndpoint ( secondaryLayer , id ) ;
201+
202+ var primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResource , id , _request . Relationship ) ;
209203
210204 var primaryResources = await _repository . GetAsync ( primaryLayer ) ;
211-
205+
212206 var primaryResource = primaryResources . SingleOrDefault ( ) ;
213207 AssertPrimaryResourceExists ( primaryResource ) ;
214208
215209 if ( _hookExecutor != null )
216- {
210+ {
217211 _hookExecutor . AfterRead ( AsList ( primaryResource ) , ResourcePipeline . GetRelationship ) ;
218212 primaryResource = _hookExecutor . OnReturn ( AsList ( primaryResource ) , ResourcePipeline . GetRelationship ) . Single ( ) ;
219213 }
@@ -233,7 +227,7 @@ public virtual async Task<object> GetSecondaryAsync(TId id, string relationshipN
233227 _hookExecutor ? . BeforeRead < TResource > ( ResourcePipeline . GetRelationship , id . ToString ( ) ) ;
234228
235229 var secondaryLayer = _queryLayerComposer . Compose ( _request . SecondaryResource ) ;
236- var primaryLayer = GetPrimaryLayerForSecondaryEndpoint ( secondaryLayer , id ) ;
230+ var primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResource , id , _request . Relationship ) ;
237231
238232 var primaryResources = await _repository . GetAsync ( primaryLayer ) ;
239233
@@ -249,35 +243,6 @@ public virtual async Task<object> GetSecondaryAsync(TId id, string relationshipN
249243 return _request . Relationship . GetValue ( primaryResource ) ;
250244 }
251245
252- private QueryLayer GetPrimaryLayerForSecondaryEndpoint ( QueryLayer secondaryLayer , TId primaryId )
253- {
254- var innerInclude = secondaryLayer . Include ;
255- secondaryLayer . Include = null ;
256-
257- var primaryIdAttribute =
258- _request . PrimaryResource . Attributes . Single ( a => a . Property . Name == nameof ( Identifiable . Id ) ) ;
259-
260- return new QueryLayer ( _request . PrimaryResource )
261- {
262- Include = RewriteIncludeForSecondaryEndpoint ( innerInclude ) ,
263- Filter = CreateFilterById ( primaryId ) ,
264- Projection = new Dictionary < ResourceFieldAttribute , QueryLayer >
265- {
266- [ primaryIdAttribute ] = null ,
267- [ _request . Relationship ] = secondaryLayer
268- }
269- } ;
270- }
271-
272- private IncludeExpression RewriteIncludeForSecondaryEndpoint ( IncludeExpression relativeInclude )
273- {
274- var parentElement = relativeInclude != null
275- ? new IncludeElementExpression ( _request . Relationship , relativeInclude . Elements )
276- : new IncludeElementExpression ( _request . Relationship ) ;
277-
278- return new IncludeExpression ( new [ ] { parentElement } ) ;
279- }
280-
281246 /// <inheritdoc />
282247 public virtual async Task < TResource > UpdateAsync ( TId id , TResource requestResource )
283248 {
@@ -320,7 +285,7 @@ public virtual async Task UpdateRelationshipAsync(TId id, string relationshipNam
320285 AssertRelationshipExists ( relationshipName ) ;
321286
322287 var secondaryLayer = _queryLayerComposer . Compose ( _request . SecondaryResource ) ;
323- var primaryLayer = GetPrimaryLayerForSecondaryEndpoint ( secondaryLayer , id ) ;
288+ var primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResource , id , _request . Relationship ) ;
324289 primaryLayer . Projection = null ;
325290
326291 var primaryResources = await _repository . GetAsync ( primaryLayer ) ;
0 commit comments