diff --git a/DbFirst.API/Program.cs b/DbFirst.API/Program.cs index bdfce03..1cb9c61 100644 --- a/DbFirst.API/Program.cs +++ b/DbFirst.API/Program.cs @@ -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(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(); builder.Services.AddScoped(); diff --git a/DbFirst.Application/DbFirst.Application.csproj b/DbFirst.Application/DbFirst.Application.csproj index 418b461..ed9e08e 100644 --- a/DbFirst.Application/DbFirst.Application.csproj +++ b/DbFirst.Application/DbFirst.Application.csproj @@ -8,6 +8,8 @@ + + diff --git a/DbFirst.Application/DependencyInjection.cs b/DbFirst.Application/DependencyInjection.cs new file mode 100644 index 0000000..1089776 --- /dev/null +++ b/DbFirst.Application/DependencyInjection.cs @@ -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; + } +} diff --git a/DbFirst.Infrastructure/DbFirst.Infrastructure.csproj b/DbFirst.Infrastructure/DbFirst.Infrastructure.csproj index 01e5904..654e162 100644 --- a/DbFirst.Infrastructure/DbFirst.Infrastructure.csproj +++ b/DbFirst.Infrastructure/DbFirst.Infrastructure.csproj @@ -15,6 +15,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DbFirst.Infrastructure/DependencyInjection.cs b/DbFirst.Infrastructure/DependencyInjection.cs new file mode 100644 index 0000000..7099eb5 --- /dev/null +++ b/DbFirst.Infrastructure/DependencyInjection.cs @@ -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(options => + options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"))); + return services; + } +}