Skip to content

Commit

Permalink
Merge pull request #4 from invocative/v6
Browse files Browse the repository at this point in the history
add factory and pools
  • Loading branch information
0xF6 authored Jan 13, 2024
2 parents 44cf502 + de5cadf commit 022c6e2
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 {

Check warning on line 23 in src/features/Database/DatabaseFeature.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(Database.DatabaseSetupKind)4' is not covered.

Check warning on line 23 in src/features/Database/DatabaseFeature.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(Database.DatabaseSetupKind)4' is not covered.

Check warning on line 23 in src/features/Database/DatabaseFeature.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(Database.DatabaseSetupKind)4' is not covered.

Check warning on line 23 in src/features/Database/DatabaseFeature.cs

View workflow job for this annotation

GitHub Actions / build

The switch expression does not handle some values of its input type (it is not exhaustive) involving an unnamed enum value. For example, the pattern '(Database.DatabaseSetupKind)4' is not covered.
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 022c6e2

Please sign in to comment.