From fb20c480e3764ea33fa2f620a8e5ca5ac6633e1a Mon Sep 17 00:00:00 2001 From: yv01p Date: Mon, 22 Dec 2025 23:46:59 +0000 Subject: [PATCH] refactor(core): delegate Count methods to QueryOperationService MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ContentService now delegates Count, CountPublished, CountChildren, CountDescendants to the new QueryOperationService. This is Task 5 of Phase 2 - the first delegation task that converts ContentService from direct repository calls to delegating to the specialized query operation service. All tests pass. Baseline tests confirm facade and direct service return identical results. Full ContentService test suite passes (excluding pre-existing benchmark regressions). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/Umbraco.Core/Services/ContentService.cs | 32 +------- .../ContentServiceRefactoringTests.cs | 75 +++++++++++++++++++ 2 files changed, 79 insertions(+), 28 deletions(-) diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index f69ab4867a..60a631e266 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -302,40 +302,16 @@ public class ContentService : RepositoryService, IContentService #region Count public int CountPublished(string? contentTypeAlias = null) - { - using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true)) - { - scope.ReadLock(Constants.Locks.ContentTree); - return _documentRepository.CountPublished(contentTypeAlias); - } - } + => QueryOperationService.CountPublished(contentTypeAlias); public int Count(string? contentTypeAlias = null) - { - using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true)) - { - scope.ReadLock(Constants.Locks.ContentTree); - return _documentRepository.Count(contentTypeAlias); - } - } + => QueryOperationService.Count(contentTypeAlias); public int CountChildren(int parentId, string? contentTypeAlias = null) - { - using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true)) - { - scope.ReadLock(Constants.Locks.ContentTree); - return _documentRepository.CountChildren(parentId, contentTypeAlias); - } - } + => QueryOperationService.CountChildren(parentId, contentTypeAlias); public int CountDescendants(int parentId, string? contentTypeAlias = null) - { - using (ICoreScope scope = ScopeProvider.CreateCoreScope(autoComplete: true)) - { - scope.ReadLock(Constants.Locks.ContentTree); - return _documentRepository.CountDescendants(parentId, contentTypeAlias); - } - } + => QueryOperationService.CountDescendants(parentId, contentTypeAlias); #endregion diff --git a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceRefactoringTests.cs b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceRefactoringTests.cs index fb39a28951..4e996cd09a 100644 --- a/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceRefactoringTests.cs +++ b/tests/Umbraco.Tests.Integration/Umbraco.Infrastructure/Services/ContentServiceRefactoringTests.cs @@ -673,6 +673,81 @@ internal sealed class ContentServiceRefactoringTests : UmbracoIntegrationTestWit #endregion + #region Phase 2 - Count Method Delegation Tests + + /// + /// Phase 2 Test: Verifies Count() via facade returns same result as direct service call. + /// + [Test] + public void Count_ViaFacade_ReturnsEquivalentResultToDirectService() + { + // Arrange + var queryService = GetRequiredService(); + + // Act + var facadeCount = ContentService.Count(); + var directCount = queryService.Count(); + + // Assert + Assert.That(facadeCount, Is.EqualTo(directCount)); + } + + /// + /// Phase 2 Test: Verifies CountPublished() via facade returns same result as direct service call. + /// + [Test] + public void CountPublished_ViaFacade_ReturnsEquivalentResultToDirectService() + { + // Arrange + var queryService = GetRequiredService(); + ContentService.Publish(Textpage, new[] { "*" }); + + // Act + var facadeCount = ContentService.CountPublished(); + var directCount = queryService.CountPublished(); + + // Assert + Assert.That(facadeCount, Is.EqualTo(directCount)); + } + + /// + /// Phase 2 Test: Verifies CountChildren() via facade returns same result as direct service call. + /// + [Test] + public void CountChildren_ViaFacade_ReturnsEquivalentResultToDirectService() + { + // Arrange + var queryService = GetRequiredService(); + var parentId = Textpage.Id; + + // Act + var facadeCount = ContentService.CountChildren(parentId); + var directCount = queryService.CountChildren(parentId); + + // Assert + Assert.That(facadeCount, Is.EqualTo(directCount)); + } + + /// + /// Phase 2 Test: Verifies CountDescendants() via facade returns same result as direct service call. + /// + [Test] + public void CountDescendants_ViaFacade_ReturnsEquivalentResultToDirectService() + { + // Arrange + var queryService = GetRequiredService(); + var parentId = Textpage.Id; + + // Act + var facadeCount = ContentService.CountDescendants(parentId); + var directCount = queryService.CountDescendants(parentId); + + // Assert + Assert.That(facadeCount, Is.EqualTo(directCount)); + } + + #endregion + /// /// Notification handler that tracks the order of notifications for test verification. ///