#if NET48
using System.Data.Entity;
using Microsoft.Extensions.DependencyInjection;
#else
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
#endif
using ECMJobRunner.Domain.Interfaces;
using ECMJobRunner.Infrastructure.Data;
using ECMJobRunner.Infrastructure.Repositories;
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 AddInfrastructure(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();
return services;
}
}
}