Refactor DI setup with extension methods for modularity

Refactored dependency injection by introducing AddApplication and AddInfrastructure extension methods for service registration. Moved DbContext and AutoMapper setup out of Program.cs to improve modularity and reusability. Added required NuGet packages to .csproj files.
This commit is contained in:
OlgunR
2026-01-19 08:46:52 +01:00
parent 28bab05980
commit 8c31784a5a
5 changed files with 33 additions and 8 deletions

View File

@@ -1,3 +1,4 @@
using DbFirst.Application;
using DbFirst.Application.Catalogs; using DbFirst.Application.Catalogs;
using DbFirst.Domain.Repositories; using DbFirst.Domain.Repositories;
using DbFirst.Infrastructure; using DbFirst.Infrastructure;
@@ -5,8 +6,6 @@ using DbFirst.Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using DbFirst.API.Middleware; using DbFirst.API.Middleware;
//TODO: create and add exception handling middleware
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
@@ -35,12 +34,8 @@ builder.Services.AddCors(options =>
}); });
}); });
// TODO: Create extension method for this in Infrastructure layer in case of using in multiple projects builder.Services.AddInfrastructure(builder.Configuration);
builder.Services.AddDbContext<ApplicationDbContext>(options => builder.Services.AddApplication();
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
// TODO: Create extension method for this in Application layer in case of using in multiple projects
builder.Services.AddAutoMapper(typeof(CatalogProfile).Assembly, typeof(ApplicationDbContext).Assembly);
builder.Services.AddScoped<ICatalogRepository, CatalogRepository>(); builder.Services.AddScoped<ICatalogRepository, CatalogRepository>();
builder.Services.AddScoped<ICatalogService, CatalogService>(); builder.Services.AddScoped<ICatalogService, CatalogService>();

View File

@@ -8,6 +8,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.1" /> <PackageReference Include="AutoMapper" Version="12.0.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,12 @@
using Microsoft.Extensions.DependencyInjection;
namespace DbFirst.Application;
public static class DependencyInjection
{
public static IServiceCollection AddApplication(this IServiceCollection services)
{
services.AddAutoMapper(typeof(DependencyInjection).Assembly);
return services;
}
}

View File

@@ -15,6 +15,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace DbFirst.Infrastructure;
public static class DependencyInjection
{
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));
return services;
}
}