@@ -62,7 +62,10 @@ public virtual async Task<IReadOnlyCollection<TResource>> GetAsync(CancellationT
6262
6363 using IDisposable _ = CodeTimingSessionManager . Current . Measure ( "Service - Get resources" ) ;
6464
65- if ( _options . IncludeTotalResourceCount )
65+ QueryLayer queryLayer = _queryLayerComposer . ComposeFromConstraints ( _request . PrimaryResourceType ) ;
66+ bool isPaginationDisabled = queryLayer . Pagination ? . PageSize == null ;
67+
68+ if ( _options . IncludeTotalResourceCount && ! isPaginationDisabled )
6669 {
6770 FilterExpression ? topFilter = _queryLayerComposer . GetPrimaryFilterFromConstraints ( _request . PrimaryResourceType ) ;
6871 _paginationContext . TotalResourceCount = await _repositoryAccessor . CountAsync ( _request . PrimaryResourceType , topFilter , cancellationToken ) ;
@@ -73,14 +76,18 @@ public virtual async Task<IReadOnlyCollection<TResource>> GetAsync(CancellationT
7376 }
7477 }
7578
76- QueryLayer queryLayer = _queryLayerComposer . ComposeFromConstraints ( _request . PrimaryResourceType ) ;
7779 IReadOnlyCollection < TResource > resources = await _repositoryAccessor . GetAsync < TResource > ( queryLayer , cancellationToken ) ;
7880
7981 if ( queryLayer . Pagination ? . PageSize ? . Value == resources . Count )
8082 {
8183 _paginationContext . IsPageFull = true ;
8284 }
8385
86+ if ( _options . IncludeTotalResourceCount && _paginationContext . TotalResourceCount == null )
87+ {
88+ _paginationContext . TotalResourceCount = resources . Count ;
89+ }
90+
8491 return resources ;
8592 }
8693
@@ -112,15 +119,17 @@ public virtual async Task<TResource> GetAsync([DisallowNull] TId id, Cancellatio
112119
113120 using IDisposable _ = CodeTimingSessionManager . Current . Measure ( "Service - Get secondary resource(s)" ) ;
114121
115- if ( _options . IncludeTotalResourceCount && _request . IsCollection )
122+ QueryLayer secondaryLayer = _queryLayerComposer . ComposeFromConstraints ( _request . SecondaryResourceType ! ) ;
123+ bool isPaginationDisabled = secondaryLayer . Pagination ? . PageSize == null ;
124+
125+ if ( _options . IncludeTotalResourceCount && ! isPaginationDisabled )
116126 {
117127 await RetrieveResourceCountForNonPrimaryEndpointAsync ( id , ( HasManyAttribute ) _request . Relationship , cancellationToken ) ;
118128
119129 // We cannot return early when _paginationContext.TotalResourceCount == 0, because we don't know whether
120130 // the parent resource exists. In case the parent does not exist, an error is produced below.
121131 }
122132
123- QueryLayer secondaryLayer = _queryLayerComposer . ComposeFromConstraints ( _request . SecondaryResourceType ! ) ;
124133 QueryLayer primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResourceType , id , _request . Relationship ) ;
125134 IReadOnlyCollection < TResource > primaryResources = await _repositoryAccessor . GetAsync < TResource > ( primaryLayer , cancellationToken ) ;
126135
@@ -134,6 +143,11 @@ public virtual async Task<TResource> GetAsync([DisallowNull] TId id, Cancellatio
134143 _paginationContext . IsPageFull = true ;
135144 }
136145
146+ if ( _options . IncludeTotalResourceCount && _paginationContext . TotalResourceCount == null && rightValue is ICollection rightResourcesForCount )
147+ {
148+ _paginationContext . TotalResourceCount = rightResourcesForCount . Count ;
149+ }
150+
137151 return rightValue ;
138152 }
139153
@@ -152,15 +166,17 @@ public virtual async Task<TResource> GetAsync([DisallowNull] TId id, Cancellatio
152166
153167 using IDisposable _ = CodeTimingSessionManager . Current . Measure ( "Service - Get relationship" ) ;
154168
155- if ( _options . IncludeTotalResourceCount && _request . IsCollection )
169+ QueryLayer secondaryLayer = _queryLayerComposer . ComposeSecondaryLayerForRelationship ( _request . SecondaryResourceType ! ) ;
170+ bool isPaginationDisabled = secondaryLayer . Pagination ? . PageSize == null ;
171+
172+ if ( _options . IncludeTotalResourceCount && ! isPaginationDisabled )
156173 {
157174 await RetrieveResourceCountForNonPrimaryEndpointAsync ( id , ( HasManyAttribute ) _request . Relationship , cancellationToken ) ;
158175
159176 // We cannot return early when _paginationContext.TotalResourceCount == 0, because we don't know whether
160177 // the parent resource exists. In case the parent does not exist, an error is produced below.
161178 }
162179
163- QueryLayer secondaryLayer = _queryLayerComposer . ComposeSecondaryLayerForRelationship ( _request . SecondaryResourceType ! ) ;
164180 QueryLayer primaryLayer = _queryLayerComposer . WrapLayerForSecondaryEndpoint ( secondaryLayer , _request . PrimaryResourceType , id , _request . Relationship ) ;
165181 IReadOnlyCollection < TResource > primaryResources = await _repositoryAccessor . GetAsync < TResource > ( primaryLayer , cancellationToken ) ;
166182
@@ -174,6 +190,11 @@ public virtual async Task<TResource> GetAsync([DisallowNull] TId id, Cancellatio
174190 _paginationContext . IsPageFull = true ;
175191 }
176192
193+ if ( _options . IncludeTotalResourceCount && _paginationContext . TotalResourceCount == null && rightValue is ICollection rightResourcesForCount )
194+ {
195+ _paginationContext . TotalResourceCount = rightResourcesForCount . Count ;
196+ }
197+
177198 return rightValue ;
178199 }
179200
0 commit comments