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.
43 lines
1.5 KiB
C#
43 lines
1.5 KiB
C#
using DigitalData.Core.Infrastructure;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using ReC.Application.Common.Interfaces;
|
|
using ReC.Domain.Entities;
|
|
|
|
namespace ReC.Infrastructure;
|
|
|
|
public static class DependencyInjection
|
|
{
|
|
public static IServiceCollection AddRecInfrastructure<TRecDbContext>(this IServiceCollection services, Action<ConfigurationOptions> options)
|
|
where TRecDbContext : RecDbContext
|
|
{
|
|
var configOpt = new ConfigurationOptions();
|
|
options.Invoke(configOpt);
|
|
|
|
if(configOpt.DbContextOptionsAction is null)
|
|
throw new InvalidOperationException("DbContextOptionsAction must be configured.");
|
|
|
|
services.AddDbContext<TRecDbContext>(configOpt.DbContextOptionsAction);
|
|
|
|
services.AddScoped<IRecDbContext>(provider => provider.GetRequiredService<TRecDbContext>());
|
|
|
|
services.AddDbRepository(opt => opt.RegisterFromAssembly<TRecDbContext>(typeof(RecAction).Assembly));
|
|
|
|
return services;
|
|
}
|
|
|
|
public static IServiceCollection AddRecInfrastructure(this IServiceCollection services, Action<ConfigurationOptions> options)
|
|
=> services.AddRecInfrastructure<RecDbContext>(options);
|
|
|
|
public class ConfigurationOptions
|
|
{
|
|
internal Action<DbContextOptionsBuilder>? DbContextOptionsAction { get; private set; }
|
|
|
|
public ConfigurationOptions ConfigureDbContext(Action<DbContextOptionsBuilder> optionsAction)
|
|
{
|
|
DbContextOptionsAction = optionsAction;
|
|
return this;
|
|
}
|
|
}
|
|
}
|