using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using MyNewProjectName.Application.Interfaces; using MyNewProjectName.Domain.Interfaces; using MyNewProjectName.Infrastructure.Options; using MyNewProjectName.Infrastructure.Persistence.Context; using MyNewProjectName.Infrastructure.Persistence.Repositories; using MyNewProjectName.Infrastructure.Services; namespace MyNewProjectName.Infrastructure; /// /// Dependency Injection for Infrastructure Layer /// public static class DependencyInjection { public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration) { // Register Options Pattern services.Configure(configuration.GetSection(DatabaseOptions.SectionName)); services.Configure(configuration.GetSection(JwtOptions.SectionName)); services.Configure(configuration.GetSection(RedisOptions.SectionName)); services.Configure(configuration.GetSection(SerilogOptions.SectionName)); // Validate required Options on startup services.AddOptions() .Bind(configuration.GetSection(DatabaseOptions.SectionName)) .ValidateDataAnnotations() .ValidateOnStart(); // Validate JwtOptions only if section exists (optional for now) var jwtSection = configuration.GetSection(JwtOptions.SectionName); if (jwtSection.Exists() && jwtSection.GetChildren().Any()) { services.AddOptions() .Bind(jwtSection) .ValidateDataAnnotations() .ValidateOnStart(); } // Register DbContext using Options Pattern services.AddDbContext((serviceProvider, options) => { var dbOptions = serviceProvider.GetRequiredService>().Value; options.UseSqlServer( dbOptions.DefaultConnection, b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)); }); // Register repositories services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); services.AddScoped(); // Register HttpContextAccessor (required for CurrentUserService) services.AddHttpContextAccessor(); // Register services services.AddScoped(); services.AddScoped(); return services; } }