diff --git a/Directory.Build.props b/Directory.Build.props index f17c725..3b6fdf8 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,7 +1,7 @@ net8.0 - 1.0-rc.5 + 1.0-rc.6 false true strict diff --git a/src/features/Database/DatabaseFeature.cs b/src/features/Database/DatabaseFeature.cs index 8db27a3..2a88acf 100644 --- a/src/features/Database/DatabaseFeature.cs +++ b/src/features/Database/DatabaseFeature.cs @@ -7,11 +7,30 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; +public enum DatabaseSetupKind +{ + Classic, + Factory, + Pool, + FactoryPool +} + [PublicAPI] -public record DatabaseFeature(Action Setup, IAppCreationContext BuildContext) : AppFeature, IAppFeatureWithHealthCheck where T : DbContext +public record DatabaseFeature(Action Setup, IAppCreationContext BuildContext, DatabaseSetupKind Kind) : AppFeature, IAppFeatureWithHealthCheck where T : DbContext { public override void BeforeRegistrations(WebApplicationBuilder webBuilder) - => webBuilder.Services.AddDbContext(x => this.Setup(new DatabaseAdapter(x, BuildContext))); + { + _ = this.Kind switch { + DatabaseSetupKind.Classic => webBuilder.Services.AddDbContext(x => + this.Setup(new DatabaseAdapter(x, this.BuildContext))), + DatabaseSetupKind.Factory => webBuilder.Services.AddDbContextFactory(x => + this.Setup(new DatabaseAdapter(x, this.BuildContext))), + DatabaseSetupKind.Pool => webBuilder.Services.AddDbContextPool(x => + this.Setup(new DatabaseAdapter(x, this.BuildContext))), + DatabaseSetupKind.FactoryPool => webBuilder.Services.AddPooledDbContextFactory(x => + this.Setup(new DatabaseAdapter(x, this.BuildContext))) + }; + } public override void BeforeRun(WebApplication webBuilder) => webBuilder.WarmUp(); diff --git a/src/features/Database/DatabaseFeatureIAppBuilder.cs b/src/features/Database/DatabaseFeatureIAppBuilder.cs index e330f17..9b8afd7 100644 --- a/src/features/Database/DatabaseFeatureIAppBuilder.cs +++ b/src/features/Database/DatabaseFeatureIAppBuilder.cs @@ -8,5 +8,12 @@ public static class DatabaseFeatureIAppBuilder { public static IAppBuilder Database(this IAppBuilder builder, Action setup) where T : DbContext - => builder.InjectFeature((x) => new DatabaseFeature(setup, x)); + => builder.InjectFeature((x) => new DatabaseFeature(setup, x, DatabaseSetupKind.Classic)); + public static IAppBuilder PooledDatabase(this IAppBuilder builder, Action setup) where T : DbContext + => builder.InjectFeature((x) => new DatabaseFeature(setup, x, DatabaseSetupKind.Pool)); + + public static IAppBuilder FactoryPooledDatabase(this IAppBuilder builder, Action setup) where T : DbContext + => builder.InjectFeature((x) => new DatabaseFeature(setup, x, DatabaseSetupKind.FactoryPool)); + public static IAppBuilder FactoryDatabase(this IAppBuilder builder, Action setup) where T : DbContext + => builder.InjectFeature((x) => new DatabaseFeature(setup, x, DatabaseSetupKind.Factory)); }