Refactor DbContext and add IRecDbContext interface

Refactored `DependencyInjection` to use a generic `TRecDbContext`
for flexibility and added scoped registration for `IRecDbContext`.
Updated `RecDbContext` to implement the new `IRecDbContext`
interface, ensuring adherence to the application-layer contract.

Introduced the `IRecDbContext` interface in the application layer,
defining `DbSet` properties for key entities and a `SaveChangesAsync`
method. Updated `ReC.Infrastructure.csproj` to reference the
application project for interface access.
This commit is contained in:
tekh 2025-11-27 16:51:49 +01:00
parent 8ea7d47868
commit f8581deaf9
4 changed files with 26 additions and 2 deletions

View File

@ -0,0 +1,19 @@
using Microsoft.EntityFrameworkCore;
using ReC.Domain.Entities;
namespace ReC.Application.Common.Interfaces;
public interface IRecDbContext
{
public DbSet<EndpointParam> EndpointParams { get; }
public DbSet<RecAction> Actions { get; }
public DbSet<OutRes> OutRes { get; }
public DbSet<HeaderQueryResult> HeaderQueryResults { get; }
public DbSet<BodyQueryResult> BodyQueryResults { get; }
public Task<int> SaveChangesAsync(CancellationToken cancel = default);
}

View File

@ -1,6 +1,7 @@
using DigitalData.Core.Infrastructure; using DigitalData.Core.Infrastructure;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using ReC.Application.Common.Interfaces;
using ReC.Domain.Entities; using ReC.Domain.Entities;
namespace ReC.Infrastructure; namespace ReC.Infrastructure;
@ -16,7 +17,9 @@ public static class DependencyInjection
if(configOpt.DbContextOptionsAction is null) if(configOpt.DbContextOptionsAction is null)
throw new InvalidOperationException("DbContextOptionsAction must be configured."); throw new InvalidOperationException("DbContextOptionsAction must be configured.");
services.AddDbContext<RecDbContext>(configOpt.DbContextOptionsAction); services.AddDbContext<TRecDbContext>(configOpt.DbContextOptionsAction);
services.AddScoped<IRecDbContext>(provider => provider.GetRequiredService<TRecDbContext>());
services.AddDbRepository(opt => opt.RegisterFromAssembly<TRecDbContext>(typeof(RecAction).Assembly)); services.AddDbRepository(opt => opt.RegisterFromAssembly<TRecDbContext>(typeof(RecAction).Assembly));

View File

@ -13,6 +13,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ReC.Application\ReC.Application.csproj" />
<ProjectReference Include="..\ReC.Domain\ReC.Domain.csproj" /> <ProjectReference Include="..\ReC.Domain\ReC.Domain.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,9 +1,10 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using ReC.Application.Common.Interfaces;
using ReC.Domain.Entities; using ReC.Domain.Entities;
namespace ReC.Infrastructure; namespace ReC.Infrastructure;
public class RecDbContext(DbContextOptions<RecDbContext> options) : DbContext(options) public class RecDbContext(DbContextOptions<RecDbContext> options) : DbContext(options), IRecDbContext
{ {
public DbSet<EndpointParam> EndpointParams { get; set; } public DbSet<EndpointParam> EndpointParams { get; set; }