#if NET48 using System.Data.Entity; using Microsoft.Extensions.DependencyInjection; #else using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; #endif using ECMJobRunner.Application.Common.Interfaces; using ECMJobRunner.Domain.Interfaces; using ECMJobRunner.Infrastructure.Data; using ECMJobRunner.Infrastructure.Repositories; using ECMJobRunner.Infrastructure.Services; namespace ECMJobRunner.Infrastructure { /// /// Dependency injection extension methods for Infrastructure layer /// public static class DependencyExtension { /// /// Add Infrastructure services to dependency injection container /// /// Service collection /// Database connection string /// Service collection for chaining public static IServiceCollection AddJobRunnerInfrastructure(this IServiceCollection services, string connectionString) { #if NET48 // Register DbContext as scoped for EF6 - same pattern as EF Core // Each scope (request) gets its own DbContext instance services.AddScoped(provider => new JobRunnerDbContext(connectionString)); #else // Register DbContext as scoped for EF Core services.AddDbContext(options => options.UseSqlServer(connectionString)); #endif // Register AutoMapper services.AddAutoMapper(typeof(DependencyExtension).Assembly); // Register repositories as scoped (same lifetime as DbContext) services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Register SQL executor as scoped services.AddScoped(); return services; } #if NET48 /// /// Add Infrastructure services with InMemory database for testing (.NET Framework 4.8 - uses Effort) /// /// Service collection /// Service collection for chaining public static IServiceCollection AddInfrastructureInMemory(this IServiceCollection services) { // Register DbContext as scoped with Effort InMemory connection services.AddScoped(provider => { var connection = Effort.DbConnectionFactory.CreateTransient(); return new JobRunnerDbContext(connection); }); // Register AutoMapper services.AddAutoMapper(typeof(DependencyExtension).Assembly); // Register repositories as scoped (same lifetime as DbContext) services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Register SQL executor as scoped services.AddScoped(); return services; } #else /// /// Add Infrastructure services with InMemory database for testing (.NET 8 - uses EF Core InMemory) /// /// Service collection /// Optional database name (default: random GUID) /// Service collection for chaining public static IServiceCollection AddInfrastructureInMemory(this IServiceCollection services, string? databaseName = null) { var dbName = databaseName ?? $"TestDb_{System.Guid.NewGuid()}"; // Register DbContext as scoped with InMemory database services.AddDbContext(options => options.UseInMemoryDatabase(dbName)); // Register AutoMapper services.AddAutoMapper(typeof(DependencyExtension).Assembly); // Register repositories as scoped (same lifetime as DbContext) services.AddScoped(typeof(IRepository<>), typeof(Repository<>)); services.AddScoped(); services.AddScoped(); services.AddScoped(); // Register SQL executor as scoped services.AddScoped(); return services; } #endif } }