Files
ECMJobRunner/ECMJobRunner.Infrastructure/DependencyExtension.cs
TekH 65a51482dd Add ECMJobRunner.Tests and enhance DI for multi-frameworks
Added a new `ECMJobRunner.Tests` project to the solution for unit
testing, including build configurations for Debug and Release.

Enhanced the `DependencyExtension` class to support dependency
injection for both .NET Framework 4.8 (EF6) and .NET 8 (EF Core)
using conditional compilation. Registered `JobRunnerDbContext`
differently based on the target framework and added AutoMapper
registration for both frameworks.

Updated `ECMJobRunner.Infrastructure.csproj` to include new
package references for DI and AutoMapper in .NET Framework 4.8.
Improved code structure and readability by removing redundant
directives and aligning DI patterns across frameworks.
2026-07-09 13:48:55 +02:00

50 lines
1.9 KiB
C#

#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
{
/// <summary>
/// Dependency injection extension methods for Infrastructure layer
/// </summary>
public static class DependencyExtension
{
/// <summary>
/// Add Infrastructure services to dependency injection container
/// </summary>
/// <param name="services">Service collection</param>
/// <param name="connectionString">Database connection string</param>
/// <returns>Service collection for chaining</returns>
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<JobRunnerDbContext>(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<ICfgProfileRepository, CfgProfileRepository>();
services.AddScoped<IProfileSqlJobRepository, ProfileSqlJobRepository>();
services.AddScoped<IProfileHistoryRepository, ProfileHistoryRepository>();
return services;
}
}
}