From 8370dc6d5df7cb8b4647172398dc9ab54d4d0998 Mon Sep 17 00:00:00 2001 From: Steve Smith Date: Thu, 2 Nov 2023 20:06:46 -0400 Subject: [PATCH] Pull EF Core references out of Web into Infrastructure Fixes #601 --- .../Data/AppDbContext.cs | 9 ++++++++ .../Data}/SeedData.cs | 8 +++---- src/Clean.Architecture.Web/Program.cs | 21 +++---------------- .../ApiEndpoints/ContributorGetById.cs | 2 +- .../ApiEndpoints/ContributorList.cs | 2 +- .../CustomWebApplicationFactory.cs | 1 - 6 files changed, 18 insertions(+), 25 deletions(-) rename src/{Clean.Architecture.Web => Clean.Architecture.Infrastructure/Data}/SeedData.cs (77%) diff --git a/src/Clean.Architecture.Infrastructure/Data/AppDbContext.cs b/src/Clean.Architecture.Infrastructure/Data/AppDbContext.cs index 980753bfb..5100f7cb8 100644 --- a/src/Clean.Architecture.Infrastructure/Data/AppDbContext.cs +++ b/src/Clean.Architecture.Infrastructure/Data/AppDbContext.cs @@ -2,9 +2,18 @@ using Ardalis.SharedKernel; using Clean.Architecture.Core.ContributorAggregate; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; namespace Clean.Architecture.Infrastructure.Data; +public static class AppDbContextExtensions +{ + public static void AddApplicationDbContext(this IServiceCollection services, string connectionString) + { + services.AddDbContext(options => + options.UseSqlite(connectionString)); + } +} public class AppDbContext : DbContext { private readonly IDomainEventDispatcher? _dispatcher; diff --git a/src/Clean.Architecture.Web/SeedData.cs b/src/Clean.Architecture.Infrastructure/Data/SeedData.cs similarity index 77% rename from src/Clean.Architecture.Web/SeedData.cs rename to src/Clean.Architecture.Infrastructure/Data/SeedData.cs index 215cefb14..858ccec9b 100644 --- a/src/Clean.Architecture.Web/SeedData.cs +++ b/src/Clean.Architecture.Infrastructure/Data/SeedData.cs @@ -1,13 +1,13 @@ using Clean.Architecture.Core.ContributorAggregate; -using Clean.Architecture.Infrastructure.Data; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; -namespace Clean.Architecture.Web; +namespace Clean.Architecture.Infrastructure.Data; public static class SeedData { - public static readonly Contributor Contributor1 = new ("Ardalis"); - public static readonly Contributor Contributor2 = new ("Snowfrog"); + public static readonly Contributor Contributor1 = new("Ardalis"); + public static readonly Contributor Contributor2 = new("Snowfrog"); public static void Initialize(IServiceProvider serviceProvider) { diff --git a/src/Clean.Architecture.Web/Program.cs b/src/Clean.Architecture.Web/Program.cs index 452150d17..b6e9c0136 100644 --- a/src/Clean.Architecture.Web/Program.cs +++ b/src/Clean.Architecture.Web/Program.cs @@ -4,12 +4,10 @@ using Clean.Architecture.Core; using Clean.Architecture.Infrastructure; using Clean.Architecture.Infrastructure.Data; -using Clean.Architecture.Web; using FastEndpoints; using FastEndpoints.Swagger; -using FastEndpoints.ApiExplorer; using Serilog; -using Microsoft.EntityFrameworkCore; +using Ardalis.GuardClauses; var builder = WebApplication.CreateBuilder(args); @@ -24,25 +22,15 @@ }); string? connectionString = builder.Configuration.GetConnectionString("SqliteConnection"); -builder.Services.AddDbContext(options => - options.UseSqlite(connectionString)); +Guard.Against.Null(connectionString); +builder.Services.AddApplicationDbContext(connectionString); builder.Services.AddFastEndpoints(); -//builder.Services.AddFastEndpointsApiExplorer(); builder.Services.SwaggerDocument(o => { o.ShortSchemaNames = true; }); -//builder.Services.AddSwaggerGen(c => -//{ -// c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); -// c.EnableAnnotations(); -// string xmlCommentFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "swagger-docs.xml"); -// c.IncludeXmlComments(xmlCommentFilePath); -// c.OperationFilter(); -//}); - // add list services for diagnostic purposes - see https://github.com/ardalis/AspNetCoreStartupServices builder.Services.Configure(config => { @@ -76,9 +64,6 @@ app.UseHttpsRedirection(); -// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint. -//app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1")); - // Seed Database using (var scope = app.Services.CreateScope()) { diff --git a/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorGetById.cs b/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorGetById.cs index 4cf8ebc3a..883cd29c9 100644 --- a/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorGetById.cs +++ b/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorGetById.cs @@ -1,5 +1,5 @@ using Ardalis.HttpClientTestExtensions; -using Clean.Architecture.Web; +using Clean.Architecture.Infrastructure.Data; using Clean.Architecture.Web.ContributorEndpoints; using Clean.Architecture.Web.Endpoints.ContributorEndpoints; using Xunit; diff --git a/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorList.cs b/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorList.cs index 2d99e4109..e3212a3e8 100644 --- a/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorList.cs +++ b/tests/Clean.Architecture.FunctionalTests/ApiEndpoints/ContributorList.cs @@ -1,5 +1,5 @@ using Ardalis.HttpClientTestExtensions; -using Clean.Architecture.Web; +using Clean.Architecture.Infrastructure.Data; using Clean.Architecture.Web.Endpoints.ContributorEndpoints; using Xunit; diff --git a/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs b/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs index fd2a6db2b..399c66373 100644 --- a/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs +++ b/tests/Clean.Architecture.FunctionalTests/CustomWebApplicationFactory.cs @@ -1,7 +1,6 @@ using Clean.Architecture.Infrastructure.Data; using Clean.Architecture.Infrastructure.Data.Queries; using Clean.Architecture.UseCases.Contributors.List; -using Clean.Architecture.Web; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore;