Files
ReC/src/ReC.Infrastructure/DependencyInjection.cs

47 lines
1.7 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));
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;
}
}
}