From bccefed35e0567a9fc9db287ff150b9d81f9eb2d Mon Sep 17 00:00:00 2001 From: kjh2064 Date: Mon, 13 Jul 2026 00:44:43 +0900 Subject: [PATCH] refactor(dotnet): separate collection read model service --- .../Interfaces/ICollectionReadModelService.cs | 13 +++++ .../Services/CollectionReadModelService.cs | 21 ++++++++ .../Endpoints/CollectionEndpoints.cs | 48 +++++++++---------- .../Pages/Admin/Collection/Index.cshtml.cs | 11 +++-- .../Pages/Admin/Dashboard/Index.cshtml.cs | 9 ++-- src/dotnet/QuantEngine.Web/Program.cs | 1 + 6 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 src/dotnet/QuantEngine.Application/Interfaces/ICollectionReadModelService.cs create mode 100644 src/dotnet/QuantEngine.Application/Services/CollectionReadModelService.cs diff --git a/src/dotnet/QuantEngine.Application/Interfaces/ICollectionReadModelService.cs b/src/dotnet/QuantEngine.Application/Interfaces/ICollectionReadModelService.cs new file mode 100644 index 00000000..6057130c --- /dev/null +++ b/src/dotnet/QuantEngine.Application/Interfaces/ICollectionReadModelService.cs @@ -0,0 +1,13 @@ +using QuantEngine.Core.Interfaces; + +namespace QuantEngine.Application.Interfaces; + +public interface ICollectionReadModelService +{ + Task GetDashboardStateAsync(); + Task> GetRecentRunsAsync(int limit = 20); + Task> GetRunSnapshotsAsync(string runId); + Task> GetRunErrorsAsync(string runId, int limit = 50); + Task> GetLatestSnapshotsForTickerAsync(string ticker, int limit = 10); + Task> GetPriceHistorySummaryAsync(); +} diff --git a/src/dotnet/QuantEngine.Application/Services/CollectionReadModelService.cs b/src/dotnet/QuantEngine.Application/Services/CollectionReadModelService.cs new file mode 100644 index 00000000..89760d3b --- /dev/null +++ b/src/dotnet/QuantEngine.Application/Services/CollectionReadModelService.cs @@ -0,0 +1,21 @@ +using QuantEngine.Application.Interfaces; +using QuantEngine.Core.Interfaces; + +namespace QuantEngine.Application.Services; + +public sealed class CollectionReadModelService : ICollectionReadModelService +{ + private readonly ICollectionRepository _repository; + + public CollectionReadModelService(ICollectionRepository repository) + { + _repository = repository; + } + + public Task GetDashboardStateAsync() => _repository.GetDashboardStateAsync(); + public Task> GetRecentRunsAsync(int limit = 20) => _repository.GetRecentRunsAsync(limit); + public Task> GetRunSnapshotsAsync(string runId) => _repository.GetRunSnapshotsAsync(runId); + public Task> GetRunErrorsAsync(string runId, int limit = 50) => _repository.GetRunErrorsAsync(runId, limit); + public Task> GetLatestSnapshotsForTickerAsync(string ticker, int limit = 10) => _repository.GetLatestSnapshotsForTickerAsync(ticker, limit); + public Task> GetPriceHistorySummaryAsync() => _repository.GetPriceHistorySummaryAsync(); +} diff --git a/src/dotnet/QuantEngine.Web/Endpoints/CollectionEndpoints.cs b/src/dotnet/QuantEngine.Web/Endpoints/CollectionEndpoints.cs index 6ef963d9..43ad8668 100644 --- a/src/dotnet/QuantEngine.Web/Endpoints/CollectionEndpoints.cs +++ b/src/dotnet/QuantEngine.Web/Endpoints/CollectionEndpoints.cs @@ -7,11 +7,11 @@ namespace QuantEngine.Web.Endpoints; public class GetCollectionStateEndpoint : EndpointWithoutRequest { - private readonly ICollectionRepository _repo; + private readonly ICollectionReadModelService _readModelService; - public GetCollectionStateEndpoint(ICollectionRepository repo) + public GetCollectionStateEndpoint(ICollectionReadModelService readModelService) { - _repo = repo; + _readModelService = readModelService; } public override void Configure() @@ -27,7 +27,7 @@ public class GetCollectionStateEndpoint : EndpointWithoutRequest { - private readonly ICollectionRepository _repo; + private readonly ICollectionReadModelService _readModelService; - public GetRecentRunsEndpoint(ICollectionRepository repo) + public GetRecentRunsEndpoint(ICollectionReadModelService readModelService) { - _repo = repo; + _readModelService = readModelService; } public override void Configure() @@ -70,7 +70,7 @@ public class GetRecentRunsEndpoint : Endpoint { - private readonly ICollectionRepository _repo; + private readonly ICollectionReadModelService _readModelService; - public GetRunSnapshotsEndpoint(ICollectionRepository repo) + public GetRunSnapshotsEndpoint(ICollectionReadModelService readModelService) { - _repo = repo; + _readModelService = readModelService; } public override void Configure() @@ -115,7 +115,7 @@ public class GetRunSnapshotsEndpoint : Endpoint { - private readonly ICollectionRepository _repo; + private readonly ICollectionReadModelService _readModelService; - public GetRunErrorsEndpoint(ICollectionRepository repo) + public GetRunErrorsEndpoint(ICollectionReadModelService readModelService) { - _repo = repo; + _readModelService = readModelService; } public override void Configure() @@ -161,7 +161,7 @@ public class GetRunErrorsEndpoint : Endpoint { - private readonly ICollectionRepository _repo; + private readonly ICollectionReadModelService _readModelService; - public GetLatestSnapshotsEndpoint(ICollectionRepository repo) + public GetLatestSnapshotsEndpoint(ICollectionReadModelService readModelService) { - _repo = repo; + _readModelService = readModelService; } public override void Configure() @@ -206,7 +206,7 @@ public class GetLatestSnapshotsEndpoint : Endpoint { - private readonly ICollectionRepository _repo; + private readonly ICollectionReadModelService _readModelService; private readonly ILogger _logger; - public GetPriceHistorySummaryEndpoint(ICollectionRepository repo, ILogger logger) + public GetPriceHistorySummaryEndpoint(ICollectionReadModelService readModelService, ILogger logger) { - _repo = repo; + _readModelService = readModelService; _logger = logger; } @@ -245,7 +245,7 @@ public class GetPriceHistorySummaryEndpoint : EndpointWithoutRequest _logger; public List? Runs { get; set; } public List? HistorySummary { get; set; } public string? Message { get; set; } - public IndexModel(ICollectionRepository collectionRepository, ILogger logger) + public IndexModel(ICollectionReadModelService collectionReadModelService, ILogger logger) { - _collectionRepository = collectionRepository; + _collectionReadModelService = collectionReadModelService; _logger = logger; } @@ -25,8 +26,8 @@ public class IndexModel : PageModel { try { - Runs = await _collectionRepository.GetRecentRunsAsync(limit: 20); - HistorySummary = await _collectionRepository.GetPriceHistorySummaryAsync(); + Runs = await _collectionReadModelService.GetRecentRunsAsync(limit: 20); + HistorySummary = await _collectionReadModelService.GetPriceHistorySummaryAsync(); } catch (Exception ex) { diff --git a/src/dotnet/QuantEngine.Web/Pages/Admin/Dashboard/Index.cshtml.cs b/src/dotnet/QuantEngine.Web/Pages/Admin/Dashboard/Index.cshtml.cs index f6ae4314..ddb8e495 100644 --- a/src/dotnet/QuantEngine.Web/Pages/Admin/Dashboard/Index.cshtml.cs +++ b/src/dotnet/QuantEngine.Web/Pages/Admin/Dashboard/Index.cshtml.cs @@ -2,6 +2,7 @@ using System.IO; using System.Reflection; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc.RazorPages; +using QuantEngine.Application.Interfaces; using QuantEngine.Core.Interfaces; using QuantEngine.Web.Services; @@ -11,7 +12,7 @@ namespace QuantEngine.Web.Pages.Admin.Dashboard; public class IndexModel : PageModel { private readonly IWorkspaceRepository _workspaceRepository; - private readonly ICollectionRepository _collectionRepository; + private readonly ICollectionReadModelService _collectionReadModelService; private readonly IWebHostEnvironment _environment; private readonly ILogger _logger; @@ -95,12 +96,12 @@ public class IndexModel : PageModel public IndexModel( IWorkspaceRepository workspaceRepository, - ICollectionRepository collectionRepository, + ICollectionReadModelService collectionReadModelService, IWebHostEnvironment environment, ILogger logger) { _workspaceRepository = workspaceRepository; - _collectionRepository = collectionRepository; + _collectionReadModelService = collectionReadModelService; _environment = environment; _logger = logger; } @@ -112,7 +113,7 @@ public class IndexModel : PageModel var accounts = await _workspaceRepository.GetAccountsAsync(); ActiveUsersCount = accounts.Count(a => string.Equals(a.IsActive, "true", StringComparison.OrdinalIgnoreCase)); - var dashboard = await _collectionRepository.GetDashboardStateAsync(); + var dashboard = await _collectionReadModelService.GetDashboardStateAsync(); RecentRunsCount = string.IsNullOrEmpty(dashboard?.LastRunId) ? 0 : 1; // These two queries only complete if the DB round-trip actually diff --git a/src/dotnet/QuantEngine.Web/Program.cs b/src/dotnet/QuantEngine.Web/Program.cs index c3aa764d..05ebf959 100644 --- a/src/dotnet/QuantEngine.Web/Program.cs +++ b/src/dotnet/QuantEngine.Web/Program.cs @@ -108,6 +108,7 @@ try builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); + builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHttpClient();