Files
ReC/src/ReC.Infrastructure/DependencyInjection.cs
TekH 0d30b5ff87 Register default repository for TRecDbContext
Also register the default repository for TRecDbContext in dependency injection, ensuring both custom and default repositories are available for use.
2026-01-14 13:32:09 +01:00

51 lines
1.8 KiB
C#

using DigitalData.Core.Infrastructure;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using ReC.Application.Common.Interfaces;
using ReC.Application.Common.Validations;
using ReC.Domain.Views;
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);
opt.RegisterDefaultRepository<TRecDbContext>();
});
services.AddValidatorsFromAssembly(typeof(AuthScopedValidator).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;
}
}
}