Skip to content

Commit

Permalink
Pull EF Core references out of Web into Infrastructure
Browse files Browse the repository at this point in the history
Fixes #601
  • Loading branch information
ardalis committed Nov 3, 2023
1 parent b3e8493 commit 8370dc6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 25 deletions.
9 changes: 9 additions & 0 deletions src/Clean.Architecture.Infrastructure/Data/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AppDbContext>(options =>
options.UseSqlite(connectionString));
}
}
public class AppDbContext : DbContext
{
private readonly IDomainEventDispatcher? _dispatcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
{
Expand Down
21 changes: 3 additions & 18 deletions src/Clean.Architecture.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -24,25 +22,15 @@
});

string? connectionString = builder.Configuration.GetConnectionString("SqliteConnection");
builder.Services.AddDbContext<AppDbContext>(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<FastEndpointsOperationFilter>();
//});

// add list services for diagnostic purposes - see https://github.com/ardalis/AspNetCoreStartupServices
builder.Services.Configure<ServiceConfig>(config =>
{
Expand Down Expand Up @@ -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())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Ardalis.HttpClientTestExtensions;
using Clean.Architecture.Web;
using Clean.Architecture.Infrastructure.Data;
using Clean.Architecture.Web.Endpoints.ContributorEndpoints;
using Xunit;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 8370dc6

Please sign in to comment.