Skip to content

Commit

Permalink
🐛 Bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jasontaylordev committed Nov 28, 2024
1 parent 9f4ba58 commit c29aa41
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/AppHost/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
// Note: To run without Docker, simply remove sql and database:
// builder.AddProject<Projects.Web>("web");

#if (!UseSqlite)
var sql = builder.AddSqlServer("sql");

var database = sql.AddDatabase("CleanArchitectureDb");

builder.AddProject<Projects.Web>("web")
.WaitFor(database)
.WithReference(database);
#else
builder.AddProject<Projects.Web>("web");
#endif

builder.Build().Run();
28 changes: 12 additions & 16 deletions src/Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,30 @@ public static class DependencyInjection
{
public static void AddInfrastructureServices(this IHostApplicationBuilder builder)
{
builder.Services.AddScoped<ISaveChangesInterceptor, AuditableEntityInterceptor>();
builder.Services.AddScoped<ISaveChangesInterceptor, DispatchDomainEventsInterceptor>();

var connectionString = builder.Configuration.GetConnectionString("CleanArchitectureDb");
Guard.Against.Null(connectionString, message: "Connection string 'CleanArchitectureDb' not found.");

#if (UseSqlite)

builder.Services.AddScoped<ISaveChangesInterceptor, AuditableEntityInterceptor>();
builder.Services.AddScoped<ISaveChangesInterceptor, DispatchDomainEventsInterceptor>();

#if (UseSqlite)
builder.Services.AddDbContext<ApplicationDbContext>((sp, options) =>
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
options.UseSqlite(connectionString);
});
#else
#if (UseAspire)
builder.AddSqlServerDbContext<ApplicationDbContext>("CleanArchitectureDb", null, options =>
{
var sp = builder.Services.BuildServiceProvider();
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
});
#else
});
#else
builder.Services.AddDbContext<ApplicationDbContext>((sp, options) =>
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
options.UseSqlServer(connectionString);
});
#endif
#endif
#if (UseAspire)

builder.EnrichSqlServerDbContext<ApplicationDbContext>();
#endif
#endif

builder.Services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());

builder.Services.AddScoped<ApplicationDbContextInitialiser>();
Expand Down
12 changes: 8 additions & 4 deletions tests/Application.FunctionalTests/CustomWebApplicationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,27 @@ public CustomWebApplicationFactory(DbConnection connection)

protected override void ConfigureWebHost(IWebHostBuilder builder)
{
#if (UseAspire)
builder.UseSetting("ConnectionStrings:CleanArchitectureDb", _connection.ConnectionString);
#endif
builder.ConfigureTestServices(services =>
{
services
.RemoveAll<IUser>()
.AddTransient(provider => Mock.Of<IUser>(s => s.Id == GetUserId()));

#if (!UseAspire || UseSqlite)
services
.RemoveAll<DbContextOptions<ApplicationDbContext>>()
.AddDbContext<ApplicationDbContext>((sp, options) =>
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
#if (UseSqlite)
#if (UseSqlite)
options.UseSqlite(_connection);
#else
#else
options.UseSqlServer(_connection);
#endif
#endif
});
#endif
});
}
}

0 comments on commit c29aa41

Please sign in to comment.