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.
61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using DbFirst.Application;
|
|
using DbFirst.Application.Catalogs;
|
|
using DbFirst.Domain.Repositories;
|
|
using DbFirst.Infrastructure;
|
|
using DbFirst.Infrastructure.Repositories;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using DbFirst.API.Middleware;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
// TODO: allow listed origins configured in appsettings.json
|
|
builder.Services.AddCors(options =>
|
|
{
|
|
var origins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? Array.Empty<string>();
|
|
options.AddDefaultPolicy(policy =>
|
|
{
|
|
if (origins.Length > 0)
|
|
{
|
|
policy.WithOrigins(origins)
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
}
|
|
else
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
}
|
|
});
|
|
});
|
|
|
|
builder.Services.AddInfrastructure(builder.Configuration);
|
|
builder.Services.AddApplication();
|
|
|
|
builder.Services.AddScoped<ICatalogRepository, CatalogRepository>();
|
|
builder.Services.AddScoped<ICatalogService, CatalogService>();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
|
|
app.UseMiddleware<ExceptionHandlingMiddleware>();
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseCors();
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|