Files
ReC/src/ReC.Infrastructure/DependencyInjection.cs
TekH 60e5adbf1a Refactor DbContext configuration for flexibility
Updated `ConfigureDbContext` to accept `IServiceProvider`, enabling dependency injection during database context setup. Modified `DependencyInjection.cs` to align with this change by updating `DbContextOptionsAction` and its related method signature.

Removed unused `System.IO` and `System.Text.Json` namespaces from `RecActionController.cs` to improve code cleanliness.
2025-12-03 11:47:04 +01:00

43 lines
1.6 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(RecActionView).Assembly));
return services;
}
public static IServiceCollection AddRecInfrastructure(this IServiceCollection services, Action<ConfigurationOptions> options)
=> services.AddRecInfrastructure<RecDbContext>(options);
public class ConfigurationOptions
{
internal Action<IServiceProvider, DbContextOptionsBuilder>? DbContextOptionsAction { get; private set; }
public ConfigurationOptions ConfigureDbContext(Action<IServiceProvider, DbContextOptionsBuilder> optionsAction)
{
DbContextOptionsAction = optionsAction;
return this;
}
}
}