Skip to content

Commit

Permalink
add factory and pools
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed Jan 13, 2024
1 parent 44cf502 commit de5cadf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<Version>1.0-rc.5</Version>
<Version>1.0-rc.6</Version>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<Deterministic>true</Deterministic>
<Features>strict</Features>
Expand Down
23 changes: 21 additions & 2 deletions src/features/Database/DatabaseFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,30 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;

public enum DatabaseSetupKind
{
Classic,
Factory,
Pool,
FactoryPool
}

[PublicAPI]
public record DatabaseFeature<T>(Action<IDatabaseAdapter> Setup, IAppCreationContext BuildContext) : AppFeature, IAppFeatureWithHealthCheck where T : DbContext
public record DatabaseFeature<T>(Action<IDatabaseAdapter> Setup, IAppCreationContext BuildContext, DatabaseSetupKind Kind) : AppFeature, IAppFeatureWithHealthCheck where T : DbContext
{
public override void BeforeRegistrations(WebApplicationBuilder webBuilder)
=> webBuilder.Services.AddDbContext<T>(x => this.Setup(new DatabaseAdapter(x, BuildContext)));
{
_ = this.Kind switch {
DatabaseSetupKind.Classic => webBuilder.Services.AddDbContext<T>(x =>
this.Setup(new DatabaseAdapter(x, this.BuildContext))),
DatabaseSetupKind.Factory => webBuilder.Services.AddDbContextFactory<T>(x =>
this.Setup(new DatabaseAdapter(x, this.BuildContext))),
DatabaseSetupKind.Pool => webBuilder.Services.AddDbContextPool<T>(x =>
this.Setup(new DatabaseAdapter(x, this.BuildContext))),
DatabaseSetupKind.FactoryPool => webBuilder.Services.AddPooledDbContextFactory<T>(x =>
this.Setup(new DatabaseAdapter(x, this.BuildContext)))
};
}

public override void BeforeRun(WebApplication webBuilder)
=> webBuilder.WarmUp<T>();
Expand Down
9 changes: 8 additions & 1 deletion src/features/Database/DatabaseFeatureIAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@
public static class DatabaseFeatureIAppBuilder
{
public static IAppBuilder Database<T>(this IAppBuilder builder, Action<IDatabaseAdapter> setup) where T : DbContext
=> builder.InjectFeature((x) => new DatabaseFeature<T>(setup, x));
=> builder.InjectFeature((x) => new DatabaseFeature<T>(setup, x, DatabaseSetupKind.Classic));
public static IAppBuilder PooledDatabase<T>(this IAppBuilder builder, Action<IDatabaseAdapter> setup) where T : DbContext
=> builder.InjectFeature((x) => new DatabaseFeature<T>(setup, x, DatabaseSetupKind.Pool));

public static IAppBuilder FactoryPooledDatabase<T>(this IAppBuilder builder, Action<IDatabaseAdapter> setup) where T : DbContext
=> builder.InjectFeature((x) => new DatabaseFeature<T>(setup, x, DatabaseSetupKind.FactoryPool));
public static IAppBuilder FactoryDatabase<T>(this IAppBuilder builder, Action<IDatabaseAdapter> setup) where T : DbContext
=> builder.InjectFeature((x) => new DatabaseFeature<T>(setup, x, DatabaseSetupKind.Factory));
}

0 comments on commit de5cadf

Please sign in to comment.