Skip to content
This repository has been archived by the owner on Mar 20, 2019. It is now read-only.

feat: add db context pool extension methods #6

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
203 changes: 190 additions & 13 deletions src/Configuration/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.


using System;
using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Interfaces;
using System;
using IdentityServer4.EntityFramework.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -28,6 +27,19 @@ public static IServiceCollection AddConfigurationDbContext(this IServiceCollecti
return services.AddConfigurationDbContext<ConfigurationDbContext>(storeOptionsAction);
}

/// <summary>
/// Add Configuration DbContextPool to the DI system.
/// </summary>
/// <param name="services"></param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <param name="poolSize"></param>
/// <returns></returns>
public static IServiceCollection AddConfigurationDbContextPool(this IServiceCollection services,
Action<ConfigurationStoreOptions> storeOptionsAction = null, int? poolSize = null)
{
return services.AddConfigurationDbContextPool<ConfigurationDbContext>(storeOptionsAction, poolSize);
}

/// <summary>
/// Add Configuration DbContext to the DI system.
/// </summary>
Expand All @@ -36,24 +48,101 @@ public static IServiceCollection AddConfigurationDbContext(this IServiceCollecti
/// <param name="storeOptionsAction">The store options action.</param>
/// <returns></returns>
public static IServiceCollection AddConfigurationDbContext<TContext>(this IServiceCollection services,
Action<ConfigurationStoreOptions> storeOptionsAction = null)
where TContext : DbContext, IConfigurationDbContext
Action<ConfigurationStoreOptions> storeOptionsAction = null)
where TContext : DbContext, IConfigurationDbContext
{
var options = new ConfigurationStoreOptions();
services.AddSingleton(options);
storeOptionsAction?.Invoke(options);
services.AddConfigurationInfrastructure<TContext>(storeOptionsAction, out var storeOptions);

if (options.ResolveDbContextOptions != null)
if (storeOptions.ResolveDbContextOptions != null)
{
services.AddDbContext<TContext>(options.ResolveDbContextOptions);
services.AddDbContext<TContext>(storeOptions.ResolveDbContextOptions);
}
else
{
services.AddDbContext<TContext>(dbCtxBuilder =>
{
options.ConfigureDbContext?.Invoke(dbCtxBuilder);
storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder);
});
}

return services;
}

/// <summary>
/// Add Configuration DbContextPool to the DI system.
/// </summary>
/// <typeparam name="TContext">The IConfigurationDbContext to use.</typeparam>
/// <param name="services"></param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <param name="poolSize"></param>
/// <returns></returns>
public static IServiceCollection AddConfigurationDbContextPool<TContext>(this IServiceCollection services,
Action<ConfigurationStoreOptions> storeOptionsAction = null, int? poolSize = null)
where TContext : DbContext, IConfigurationDbContext
{
services.AddConfigurationInfrastructure<TContext>(storeOptionsAction, out var storeOptions);

if (storeOptions.ResolveDbContextOptions != null)
{
if (poolSize is null)
{
services.AddDbContextPool<TContext>(storeOptions.ResolveDbContextOptions);
}
else
{
services.AddDbContextPool<TContext>(storeOptions.ResolveDbContextOptions, poolSize.Value);
}
}
else
{
if (poolSize is null)
{
services.AddDbContextPool<TContext>(dbCtxBuilder =>
{
storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder);
});
}
else
{
services.AddDbContextPool<TContext>(dbCtxBuilder =>
{
storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder);
}, poolSize.Value);
}
}

return services;
}

/// <summary>
/// Adds Configuration to the DI system, but does not register DbContext.
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="services"></param>
/// <param name="storeOptionsAction"></param>
/// <returns></returns>
public static IServiceCollection AddConfigurationInfrastructure<TContext>(this IServiceCollection services,
Action<ConfigurationStoreOptions> storeOptionsAction = null)
where TContext : DbContext, IConfigurationDbContext
{
return services.AddConfigurationInfrastructure<TContext>(storeOptionsAction, out _);
}

/// <summary>
/// Adds Configuration Store to the DI system, but does not register DbContext.
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="services"></param>
/// <param name="storeOptionsAction"></param>
/// <param name="storeOptions"></param>
/// <returns></returns>
private static IServiceCollection AddConfigurationInfrastructure<TContext>(this IServiceCollection services,
Action<ConfigurationStoreOptions> storeOptionsAction, out ConfigurationStoreOptions storeOptions)
where TContext : DbContext, IConfigurationDbContext
{
storeOptions = new ConfigurationStoreOptions();
services.AddSingleton(storeOptions);
storeOptionsAction?.Invoke(storeOptions);
services.AddScoped<IConfigurationDbContext, TContext>();

return services;
Expand All @@ -71,6 +160,18 @@ public static IServiceCollection AddOperationalDbContext(this IServiceCollection
return services.AddOperationalDbContext<PersistedGrantDbContext>(storeOptionsAction);
}

/// <summary>
/// Adds operational DbContextPool to the DI system.
/// </summary>
/// <param name="services"></param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <returns></returns>
public static IServiceCollection AddOperationalDbContextPool(this IServiceCollection services,
Action<OperationalStoreOptions> storeOptionsAction = null)
{
return services.AddOperationalDbContextPool<PersistedGrantDbContext>(storeOptionsAction);
}

/// <summary>
/// Adds operational DbContext to the DI system.
/// </summary>
Expand All @@ -82,9 +183,7 @@ public static IServiceCollection AddOperationalDbContext<TContext>(this IService
Action<OperationalStoreOptions> storeOptionsAction = null)
where TContext : DbContext, IPersistedGrantDbContext
{
var storeOptions = new OperationalStoreOptions();
services.AddSingleton(storeOptions);
storeOptionsAction?.Invoke(storeOptions);
services.AddOperationalInfrastructure<TContext>(storeOptionsAction, out var storeOptions);

if (storeOptions.ResolveDbContextOptions != null)
{
Expand All @@ -98,6 +197,84 @@ public static IServiceCollection AddOperationalDbContext<TContext>(this IService
});
}

return services;
}

/// <summary>
/// Adds operational DbContextPool to the DI system.
/// </summary>
/// <typeparam name="TContext">The IPersistedGrantDbContext to use.</typeparam>
/// <param name="services"></param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <param name="poolSize"></param>
/// <returns></returns>
public static IServiceCollection AddOperationalDbContextPool<TContext>(this IServiceCollection services,
Action<OperationalStoreOptions> storeOptionsAction = null, int? poolSize = null)
where TContext : DbContext, IPersistedGrantDbContext
{
services.AddOperationalInfrastructure<TContext>(storeOptionsAction, out var storeOptions);

if (storeOptions.ResolveDbContextOptions != null)
{
if (poolSize is null)
{
services.AddDbContextPool<TContext>(storeOptions.ResolveDbContextOptions);
}
else
{
services.AddDbContextPool<TContext>(storeOptions.ResolveDbContextOptions, poolSize.Value);
}
}
else
{
if (poolSize is null)
{
services.AddDbContextPool<TContext>(dbCtxBuilder =>
{
storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder);
});
}
else
{
services.AddDbContextPool<TContext>(dbCtxBuilder =>
{
storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder);
}, poolSize.Value);
}
}

return services;
}

/// <summary>
/// Adds Operational Store to the DI system, but does not register DbContext.
/// </summary>
/// <typeparam name="TContext">The IPersistedGrantDbContext to use.</typeparam>
/// <param name="services"></param>
/// <param name="storeOptionsAction">The store options action.</param>
/// <returns></returns>
public static IServiceCollection AddOperationalInfrastructure<TContext>(this IServiceCollection services,
Action<OperationalStoreOptions> storeOptionsAction = null)
where TContext : DbContext, IPersistedGrantDbContext
{
return services.AddOperationalInfrastructure<TContext>(storeOptionsAction, out _);
}

/// <summary>
/// Adds Configuration Store to the DI system, but does not register DbContext.
/// </summary>
/// <typeparam name="TContext"></typeparam>
/// <param name="services"></param>
/// <param name="storeOptionsAction"></param>
/// <param name="storeOptions"></param>
/// <returns></returns>
private static IServiceCollection AddOperationalInfrastructure<TContext>(this IServiceCollection services,
Action<OperationalStoreOptions> storeOptionsAction, out OperationalStoreOptions storeOptions)
where TContext : DbContext, IPersistedGrantDbContext
{
storeOptions = new OperationalStoreOptions();
services.AddSingleton(storeOptions);
storeOptionsAction?.Invoke(storeOptions);
services.AddScoped<IPersistedGrantDbContext, TContext>();
services.AddSingleton<TokenCleanup>();

Expand Down