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:
@@ -1,3 +1,4 @@
|
||||
using DbFirst.Application;
|
||||
using DbFirst.Application.Catalogs;
|
||||
using DbFirst.Domain.Repositories;
|
||||
using DbFirst.Infrastructure;
|
||||
@@ -5,8 +6,6 @@ using DbFirst.Infrastructure.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DbFirst.API.Middleware;
|
||||
|
||||
//TODO: create and add exception handling middleware
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// 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.AddDbContext<ApplicationDbContext>(options =>
|
||||
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.AddInfrastructure(builder.Configuration);
|
||||
builder.Services.AddApplication();
|
||||
|
||||
builder.Services.AddScoped<ICatalogRepository, CatalogRepository>();
|
||||
builder.Services.AddScoped<ICatalogService, CatalogService>();
|
||||
|
||||
Reference in New Issue
Block a user