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(this IServiceCollection services, Action 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(configOpt.DbContextOptionsAction); services.AddScoped(provider => provider.GetRequiredService()); services.AddDbRepository(opt => opt.RegisterFromAssembly(typeof(RecActionView).Assembly)); return services; } public static IServiceCollection AddRecInfrastructure(this IServiceCollection services, Action options) => services.AddRecInfrastructure(options); public class ConfigurationOptions { internal Action? DbContextOptionsAction { get; private set; } public ConfigurationOptions ConfigureDbContext(Action optionsAction) { DbContextOptionsAction = optionsAction; return this; } } }