Skip to content

Commit 9587cb6

Browse files
committed
Align Sqlite enrich behavior
Make EnrichSqliteDatabaseDbContext follow the Aspire enrich pattern by requiring the DbContext to already be registered instead of registering it itself. Update the tests to reflect the new contract, including coverage for missing DbContext registration and successful enrichment of an existing registration.
1 parent 0e44c1e commit 9587cb6

2 files changed

Lines changed: 26 additions & 19 deletions

File tree

src/CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite/AspireEFSqliteExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
7171
}
7272

7373
/// <summary>
74-
/// Enriches a <see cref="IHostApplicationBuilder"/> to register the <typeparamref name="TDbContext"/> as a scoped service
75-
/// with simplified configuration and optional OpenTelemetry instrumentation.
74+
/// Configures retries, health check, logging and telemetry for the <see cref="DbContext" />.
7675
/// </summary>
7776
/// <typeparam name="TDbContext">The type of the <see cref="DbContext"/>.</typeparam>
7877
/// <param name="builder">The <see cref="IHostApplicationBuilder"/> to read config from and add services to.</param>
7978
/// <param name="configureSettings">An optional delegate that can be used for customizing options. It's invoked after the settings are read from the configuration.</param>
8079
/// <exception cref="ArgumentNullException">Thrown if mandatory <paramref name="builder"/> is null.</exception>
80+
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="DbContext"/> is not registered in DI.</exception>
8181
public static void EnrichSqliteDatabaseDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TDbContext>(
8282
this IHostApplicationBuilder builder,
8383
Action<SqliteEntityFrameworkCoreSettings>? configureSettings = null)
@@ -93,8 +93,8 @@ void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
9393

9494
configureSettings?.Invoke(settings);
9595

96-
builder.Services.AddDbContext<TDbContext>(options =>
97-
options.UseSqlite(settings.ConnectionString));
96+
builder.CheckDbContextRegistered<TDbContext>();
97+
9898
ConfigureInstrumentation<TDbContext>(builder, settings);
9999
}
100100

tests/CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests/EnrichSqliteDatabaseDbContextTests.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.EntityFrameworkCore;
3-
using Microsoft.Extensions.Configuration;
4-
using Microsoft.Extensions.DependencyInjection;
3+
using Microsoft.Extensions.Diagnostics.HealthChecks;
54
using Microsoft.Extensions.Hosting;
65

76
namespace CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite.Tests;
87

98
public class EnrichSqliteDatabaseDbContextTests
109
{
1110
[Fact]
12-
public void EnrichSqliteDatabaseDbContext_RegistersDbContext()
11+
public void EnrichSqliteDatabaseDbContext_UsesAlreadyRegisteredDbContext()
1312
{
1413
// Arrange
1514
var builder = WebApplication.CreateBuilder();
16-
builder.Configuration.AddInMemoryCollection([
17-
new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:")
18-
]);
15+
builder.Services.AddDbContext<TestDbContext>(options =>
16+
options.UseSqlite("Data Source=:memory:"));
1917

2018
// Act
2119
builder.EnrichSqliteDatabaseDbContext<TestDbContext>();
@@ -26,6 +24,17 @@ public void EnrichSqliteDatabaseDbContext_RegistersDbContext()
2624
Assert.NotNull(dbContext);
2725
}
2826

27+
[Fact]
28+
public void EnrichSqliteDatabaseDbContext_ThrowsWhenDbContextIsNotRegistered()
29+
{
30+
// Arrange
31+
var builder = WebApplication.CreateBuilder();
32+
33+
// Act & Assert
34+
Assert.Throws<InvalidOperationException>(() =>
35+
builder.EnrichSqliteDatabaseDbContext<TestDbContext>());
36+
}
37+
2938
[Fact]
3039
public void EnrichSqliteDatabaseDbContext_ThrowsWhenBuilderIsNull()
3140
{
@@ -35,13 +44,12 @@ public void EnrichSqliteDatabaseDbContext_ThrowsWhenBuilderIsNull()
3544
}
3645

3746
[Fact]
38-
public void EnrichSqliteDatabaseDbContext_DisablesOpenTelemetryWhenFalse()
47+
public void EnrichSqliteDatabaseDbContext_DisablesOpenTelemetryWhenConfigured()
3948
{
4049
// Arrange
4150
var builder = WebApplication.CreateBuilder();
42-
builder.Configuration.AddInMemoryCollection([
43-
new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:")
44-
]);
51+
builder.Services.AddDbContextPool<TestDbContext>(options =>
52+
options.UseSqlite("Data Source=:memory:"));
4553

4654
// Act
4755
builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = true);
@@ -57,9 +65,8 @@ public void EnrichSqliteDatabaseDbContext_EnablesOpenTelemetryByDefault()
5765
{
5866
// Arrange
5967
var builder = WebApplication.CreateBuilder();
60-
builder.Configuration.AddInMemoryCollection([
61-
new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:")
62-
]);
68+
builder.Services.AddDbContext<TestDbContext>(options =>
69+
options.UseSqlite("Data Source=:memory:"));
6370

6471
// Act
6572
builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = false);
@@ -70,7 +77,7 @@ public void EnrichSqliteDatabaseDbContext_EnablesOpenTelemetryByDefault()
7077
Assert.NotNull(dbContext);
7178

7279
// Verify OpenTelemetry services are registered (basic smoke test)
73-
var services = app.Services.GetServices<IHostedService>().ToList();
74-
Assert.True(services.Count > 0, "Services should be registered");
80+
var healthCheckService = app.Services.GetService<HealthCheckService>();
81+
Assert.NotNull(healthCheckService);
7582
}
7683
}

0 commit comments

Comments
 (0)