refactor(public): in-process ITrackDataService on server prerender; HTTP on WASM

This commit is contained in:
Daniel Harvey
2026-05-20 16:49:43 -04:00
parent 35099a54e5
commit e8072f8b17
7 changed files with 104 additions and 10 deletions
@@ -38,7 +38,7 @@ public partial class TracksView : ComponentBase
private async Task SetPage(int newPage)
{
var result = await ViewModel.TrackClient.GetPage(newPage, ViewModel.PageSize, ViewModel.SortBy, ViewModel.IsDescending);
var result = await ViewModel.TrackData.GetPage(newPage, ViewModel.PageSize, ViewModel.SortBy, ViewModel.IsDescending);
if (result is { Success: true, Value: PagedResult<TrackEntity> pageResult })
{
@@ -0,0 +1,25 @@
using DeepDrftModels.Entities;
using Models.Common;
using NetBlocks.Models;
namespace DeepDrftPublic.Client.Services;
/// <summary>
/// Track metadata fetch abstraction with two render-mode-specific implementations:
///
/// - Server prerender pass: <c>TrackDirectDataService</c> in the DeepDrftPublic host
/// resolves <see cref="DeepDrftData.ITrackService"/> in-process (EF Core / SQL) and
/// avoids a loopback HTTP hop.
/// - WASM interactive pass: <c>TrackClientDataService</c> in this assembly delegates
/// to <see cref="Clients.TrackClient"/> over HTTP.
///
/// Components inject this single seam so they do not branch on render mode.
/// </summary>
public interface ITrackDataService
{
Task<ApiResult<PagedResult<TrackEntity>>> GetPage(
int pageNumber,
int pageSize,
string? sortColumn = null,
bool sortDescending = false);
}
@@ -0,0 +1,28 @@
using DeepDrftModels.Entities;
using DeepDrftPublic.Client.Clients;
using Models.Common;
using NetBlocks.Models;
namespace DeepDrftPublic.Client.Services;
/// <summary>
/// WASM-side <see cref="ITrackDataService"/> that delegates to <see cref="TrackClient"/>
/// (HTTP to the <c>DeepDrft.API</c> backend). Used on the WASM interactive render pass;
/// the server prerender pass swaps in a direct, in-process implementation.
/// </summary>
public class TrackClientDataService : ITrackDataService
{
private readonly TrackClient _trackClient;
public TrackClientDataService(TrackClient trackClient)
{
_trackClient = trackClient;
}
public Task<ApiResult<PagedResult<TrackEntity>>> GetPage(
int pageNumber,
int pageSize,
string? sortColumn = null,
bool sortDescending = false)
=> _trackClient.GetPage(pageNumber, pageSize, sortColumn, sortDescending);
}
+4 -1
View File
@@ -14,8 +14,11 @@ public static class Startup
services.AddScoped<DarkModeSettings>();
services.AddScoped<DarkModeCookieService>();
// Track Client
// Track Client. The HTTP-backed ITrackDataService registration here is the WASM
// default; the server host overrides it with an in-process implementation after
// this method runs, so SSR prerender skips the loopback hop.
services.AddScoped<TrackClient>();
services.AddScoped<ITrackDataService, TrackClientDataService>();
services.AddScoped<TracksViewModel>();
}
@@ -1,14 +1,13 @@
using DeepDrftModels.Entities;
using DeepDrftPublic.Client.Clients;
using DeepDrftModels.Entities;
using DeepDrftPublic.Client.Services;
using Models.Common;
namespace DeepDrftPublic.Client.ViewModels;
public class TracksViewModel
{
public TrackClient TrackClient { get; }
public ITrackDataService TrackData { get; }
// private int _pageNumber = 1;
public int PageNumber { get; set; } = 1;
public int PageSize
@@ -26,9 +25,9 @@ public class TracksViewModel
public string SortBy { get; set; } = string.Empty;
public bool IsDescending { get; set; } = false;
public PagedResult<TrackEntity>? Page { get; set; } = null;
public TracksViewModel(TrackClient trackClient)
public TracksViewModel(ITrackDataService trackData)
{
TrackClient = trackClient;
TrackData = trackData;
}
}
}
@@ -0,0 +1,33 @@
using DeepDrftData;
using DeepDrftModels.Entities;
using DeepDrftPublic.Client.Services;
using Models.Common;
using NetBlocks.Models;
namespace DeepDrftPublic.Services;
/// <summary>
/// Server-side <see cref="ITrackDataService"/> that calls <see cref="ITrackService"/>
/// in-process (EF Core / SQL). Replaces the loopback HTTP hop during SSR prerender:
/// the WASM interactive pass still uses <see cref="Client.Services.TrackClientDataService"/>
/// over HTTP, but on the server we already have the domain service in DI.
/// </summary>
public class TrackDirectDataService : ITrackDataService
{
private readonly ITrackService _trackService;
public TrackDirectDataService(ITrackService trackService)
{
_trackService = trackService;
}
public async Task<ApiResult<PagedResult<TrackEntity>>> GetPage(
int pageNumber,
int pageSize,
string? sortColumn = null,
bool sortDescending = false)
{
var result = await _trackService.GetPaged(pageNumber, pageSize, sortColumn, sortDescending);
return ApiResult<PagedResult<TrackEntity>>.From(result);
}
}
+6
View File
@@ -1,6 +1,7 @@
using DeepDrftData;
using DeepDrftData.Data;
using DeepDrftData.Repositories;
using DeepDrftPublic.Client.Services;
using DeepDrftPublic.Services; // DarkModeService namespace (within this host project)
using Microsoft.EntityFrameworkCore;
@@ -28,6 +29,11 @@ public static class Startup
.AddScoped<TrackRepository>()
.AddScoped<TrackManager>()
.AddScoped<ITrackService>(sp => sp.GetRequiredService<TrackManager>());
// Override the WASM HTTP-backed ITrackDataService (registered earlier by
// DeepDrftPublic.Client.Startup.ConfigureDomainServices) with an in-process
// adapter for SSR prerender. Last registration wins for single-resolution.
builder.Services.AddScoped<ITrackDataService, TrackDirectDataService>();
}
public static string GetKestrelUrl(this WebApplicationBuilder builder)