Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,13 @@ void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)
}

/// <summary>
/// Enriches a <see cref="IHostApplicationBuilder"/> to register the <typeparamref name="TDbContext"/> as a scoped service
/// with simplified configuration and optional OpenTelemetry instrumentation.
/// Configures retries, health check, logging and telemetry for the <see cref="DbContext" />.
/// </summary>
/// <typeparam name="TDbContext">The type of the <see cref="DbContext"/>.</typeparam>
/// <param name="builder">The <see cref="IHostApplicationBuilder"/> to read config from and add services to.</param>
/// <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>
/// <exception cref="ArgumentNullException">Thrown if mandatory <paramref name="builder"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="DbContext"/> is not registered in DI.</exception>
public static void EnrichSqliteDatabaseDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TDbContext>(
this IHostApplicationBuilder builder,
Action<SqliteEntityFrameworkCoreSettings>? configureSettings = null)
Expand All @@ -93,8 +93,8 @@ void ConfigureDbContext(DbContextOptionsBuilder dbContextOptionsBuilder)

configureSettings?.Invoke(settings);

builder.Services.AddDbContext<TDbContext>(options =>
options.UseSqlite(settings.ConnectionString));
builder.CheckDbContextRegistered<TDbContext>();

ConfigureInstrumentation<TDbContext>(builder, settings);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;

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

public class EnrichSqliteDatabaseDbContextTests
{
[Fact]
public void EnrichSqliteDatabaseDbContext_RegistersDbContext()
public void EnrichSqliteDatabaseDbContext_UsesAlreadyRegisteredDbContext()
{
// Arrange
var builder = WebApplication.CreateBuilder();
builder.Configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:")
]);
builder.Services.AddDbContext<TestDbContext>(options =>
options.UseSqlite("Data Source=:memory:"));

// Act
builder.EnrichSqliteDatabaseDbContext<TestDbContext>();
Expand All @@ -26,6 +24,17 @@ public void EnrichSqliteDatabaseDbContext_RegistersDbContext()
Assert.NotNull(dbContext);
}

[Fact]
public void EnrichSqliteDatabaseDbContext_ThrowsWhenDbContextIsNotRegistered()
{
// Arrange
var builder = WebApplication.CreateBuilder();

// Act & Assert
Assert.Throws<InvalidOperationException>(() =>
builder.EnrichSqliteDatabaseDbContext<TestDbContext>());
}

[Fact]
public void EnrichSqliteDatabaseDbContext_ThrowsWhenBuilderIsNull()
{
Expand All @@ -35,13 +44,12 @@ public void EnrichSqliteDatabaseDbContext_ThrowsWhenBuilderIsNull()
}

[Fact]
public void EnrichSqliteDatabaseDbContext_DisablesOpenTelemetryWhenFalse()
public void EnrichSqliteDatabaseDbContext_DisablesOpenTelemetryWhenConfigured()
{
// Arrange
var builder = WebApplication.CreateBuilder();
builder.Configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:")
]);
builder.Services.AddDbContextPool<TestDbContext>(options =>
options.UseSqlite("Data Source=:memory:"));

// Act
builder.EnrichSqliteDatabaseDbContext<TestDbContext>(settings => settings.DisableTracing = true);
Expand All @@ -57,9 +65,8 @@ public void EnrichSqliteDatabaseDbContext_EnablesOpenTelemetryByDefault()
{
// Arrange
var builder = WebApplication.CreateBuilder();
builder.Configuration.AddInMemoryCollection([
new KeyValuePair<string, string?>("ConnectionStrings:DefaultConnection", "Data Source=:memory:")
]);
builder.Services.AddDbContext<TestDbContext>(options =>
options.UseSqlite("Data Source=:memory:"));

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

// Verify OpenTelemetry services are registered (basic smoke test)
var services = app.Services.GetServices<IHostedService>().ToList();
Assert.True(services.Count > 0, "Services should be registered");
var healthCheckService = app.Services.GetService<HealthCheckService>();
Assert.NotNull(healthCheckService);
}
}