From 2e62ca7ce7ccc123fbe08620fca79135f64d578e Mon Sep 17 00:00:00 2001 From: Pavel Levchuk Date: Sat, 29 Dec 2018 16:23:00 +0300 Subject: [PATCH 1/3] feat: add db context pool extension methods --- .../ServiceCollectionExtensions.cs | 95 ++++++++++++++++++- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/src/Configuration/ServiceCollectionExtensions.cs b/src/Configuration/ServiceCollectionExtensions.cs index 2366ac9..77d099c 100644 --- a/src/Configuration/ServiceCollectionExtensions.cs +++ b/src/Configuration/ServiceCollectionExtensions.cs @@ -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; @@ -28,6 +27,18 @@ public static IServiceCollection AddConfigurationDbContext(this IServiceCollecti return services.AddConfigurationDbContext(storeOptionsAction); } + /// + /// Add Configuration DbContextPool to the DI system. + /// + /// + /// The store options action. + /// + public static IServiceCollection AddConfigurationDbContextPool(this IServiceCollection services, + Action storeOptionsAction = null) + { + return services.AddConfigurationDbContextPool(storeOptionsAction); + } + /// /// Add Configuration DbContext to the DI system. /// @@ -36,8 +47,8 @@ public static IServiceCollection AddConfigurationDbContext(this IServiceCollecti /// The store options action. /// public static IServiceCollection AddConfigurationDbContext(this IServiceCollection services, - Action storeOptionsAction = null) - where TContext : DbContext, IConfigurationDbContext + Action storeOptionsAction = null) + where TContext : DbContext, IConfigurationDbContext { var options = new ConfigurationStoreOptions(); services.AddSingleton(options); @@ -59,6 +70,37 @@ public static IServiceCollection AddConfigurationDbContext(this IServi return services; } + /// + /// Add Configuration DbContextPool to the DI system. + /// + /// The IConfigurationDbContext to use. + /// + /// The store options action. + /// + public static IServiceCollection AddConfigurationDbContextPool(this IServiceCollection services, + Action storeOptionsAction = null) + where TContext : DbContext, IConfigurationDbContext + { + var options = new ConfigurationStoreOptions(); + services.AddSingleton(options); + storeOptionsAction?.Invoke(options); + + if (options.ResolveDbContextOptions != null) + { + services.AddDbContextPool(options.ResolveDbContextOptions); + } + else + { + services.AddDbContextPool(dbCtxBuilder => + { + options.ConfigureDbContext?.Invoke(dbCtxBuilder); + }); + } + services.AddScoped(); + + return services; + } + /// /// Adds operational DbContext to the DI system. /// @@ -71,6 +113,18 @@ public static IServiceCollection AddOperationalDbContext(this IServiceCollection return services.AddOperationalDbContext(storeOptionsAction); } + /// + /// Adds operational DbContextPool to the DI system. + /// + /// + /// The store options action. + /// + public static IServiceCollection AddOperationalDbContextPool(this IServiceCollection services, + Action storeOptionsAction = null) + { + return services.AddOperationalDbContextPool(storeOptionsAction); + } + /// /// Adds operational DbContext to the DI system. /// @@ -104,6 +158,39 @@ public static IServiceCollection AddOperationalDbContext(this IService return services; } + /// + /// Adds operational DbContextPool to the DI system. + /// + /// The IPersistedGrantDbContext to use. + /// + /// The store options action. + /// + public static IServiceCollection AddOperationalDbContextPool(this IServiceCollection services, + Action storeOptionsAction = null) + where TContext : DbContext, IPersistedGrantDbContext + { + var storeOptions = new OperationalStoreOptions(); + services.AddSingleton(storeOptions); + storeOptionsAction?.Invoke(storeOptions); + + if (storeOptions.ResolveDbContextOptions != null) + { + services.AddDbContextPool(storeOptions.ResolveDbContextOptions); + } + else + { + services.AddDbContextPool(dbCtxBuilder => + { + storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); + }); + } + + services.AddScoped(); + services.AddSingleton(); + + return services; + } + /// /// Adds an implementation of the IOperationalStoreNotification to the DI system. /// From f6da2cf6742d6fa1ef3b62e36d203a75fa5254b6 Mon Sep 17 00:00:00 2001 From: Pavel Levchuk Date: Sat, 29 Dec 2018 17:38:26 +0300 Subject: [PATCH 2/3] feat: add extension methods that allow to register stores in DI without adding DbContext --- .../ServiceCollectionExtensions.cs | 97 ++++++++++++++----- 1 file changed, 75 insertions(+), 22 deletions(-) diff --git a/src/Configuration/ServiceCollectionExtensions.cs b/src/Configuration/ServiceCollectionExtensions.cs index 77d099c..5cfc98a 100644 --- a/src/Configuration/ServiceCollectionExtensions.cs +++ b/src/Configuration/ServiceCollectionExtensions.cs @@ -50,22 +50,19 @@ public static IServiceCollection AddConfigurationDbContext(this IServi Action storeOptionsAction = null) where TContext : DbContext, IConfigurationDbContext { - var options = new ConfigurationStoreOptions(); - services.AddSingleton(options); - storeOptionsAction?.Invoke(options); + services.AddConfigurationInfrastructure(storeOptionsAction, out var storeOptions); - if (options.ResolveDbContextOptions != null) + if (storeOptions.ResolveDbContextOptions != null) { - services.AddDbContext(options.ResolveDbContextOptions); + services.AddDbContext(storeOptions.ResolveDbContextOptions); } else { services.AddDbContext(dbCtxBuilder => { - options.ConfigureDbContext?.Invoke(dbCtxBuilder); + storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); }); } - services.AddScoped(); return services; } @@ -81,21 +78,52 @@ public static IServiceCollection AddConfigurationDbContextPool(this IS Action storeOptionsAction = null) where TContext : DbContext, IConfigurationDbContext { - var options = new ConfigurationStoreOptions(); - services.AddSingleton(options); - storeOptionsAction?.Invoke(options); + services.AddConfigurationInfrastructure(storeOptionsAction, out var storeOptions); - if (options.ResolveDbContextOptions != null) + if (storeOptions.ResolveDbContextOptions != null) { - services.AddDbContextPool(options.ResolveDbContextOptions); + services.AddDbContextPool(storeOptions.ResolveDbContextOptions); } else { services.AddDbContextPool(dbCtxBuilder => { - options.ConfigureDbContext?.Invoke(dbCtxBuilder); + storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); }); } + + return services; + } + + /// + /// Adds Configuration to the DI system, but does not register DbContext. + /// + /// + /// + /// + /// + public static IServiceCollection AddConfigurationInfrastructure(this IServiceCollection services, + Action storeOptionsAction = null) + where TContext : DbContext, IConfigurationDbContext + { + return services.AddConfigurationInfrastructure(storeOptionsAction, out _); + } + + /// + /// Adds Configuration Store to the DI system, but does not register DbContext. + /// + /// + /// + /// + /// + /// + private static IServiceCollection AddConfigurationInfrastructure(this IServiceCollection services, + Action storeOptionsAction, out ConfigurationStoreOptions storeOptions) + where TContext : DbContext, IConfigurationDbContext + { + storeOptions = new ConfigurationStoreOptions(); + services.AddSingleton(storeOptions); + storeOptionsAction?.Invoke(storeOptions); services.AddScoped(); return services; @@ -136,9 +164,7 @@ public static IServiceCollection AddOperationalDbContext(this IService Action storeOptionsAction = null) where TContext : DbContext, IPersistedGrantDbContext { - var storeOptions = new OperationalStoreOptions(); - services.AddSingleton(storeOptions); - storeOptionsAction?.Invoke(storeOptions); + services.AddOperationalInfrastructure(storeOptionsAction, out var storeOptions); if (storeOptions.ResolveDbContextOptions != null) { @@ -152,9 +178,6 @@ public static IServiceCollection AddOperationalDbContext(this IService }); } - services.AddScoped(); - services.AddSingleton(); - return services; } @@ -169,9 +192,7 @@ public static IServiceCollection AddOperationalDbContextPool(this ISer Action storeOptionsAction = null) where TContext : DbContext, IPersistedGrantDbContext { - var storeOptions = new OperationalStoreOptions(); - services.AddSingleton(storeOptions); - storeOptionsAction?.Invoke(storeOptions); + services.AddOperationalInfrastructure(storeOptionsAction, out var storeOptions); if (storeOptions.ResolveDbContextOptions != null) { @@ -185,6 +206,38 @@ public static IServiceCollection AddOperationalDbContextPool(this ISer }); } + return services; + } + + /// + /// Adds Operational Store to the DI system, but does not register DbContext. + /// + /// The IPersistedGrantDbContext to use. + /// + /// The store options action. + /// + public static IServiceCollection AddOperationalInfrastructure(this IServiceCollection services, + Action storeOptionsAction = null) + where TContext : DbContext, IPersistedGrantDbContext + { + return services.AddOperationalInfrastructure(storeOptionsAction, out _); + } + + /// + /// Adds Configuration Store to the DI system, but does not register DbContext. + /// + /// + /// + /// + /// + /// + private static IServiceCollection AddOperationalInfrastructure(this IServiceCollection services, + Action storeOptionsAction, out OperationalStoreOptions storeOptions) + where TContext : DbContext, IPersistedGrantDbContext + { + storeOptions = new OperationalStoreOptions(); + services.AddSingleton(storeOptions); + storeOptionsAction?.Invoke(storeOptions); services.AddScoped(); services.AddSingleton(); From 9f4cb9d51f169c895bcd04ba106e44107066c2a1 Mon Sep 17 00:00:00 2001 From: Pavel Levchuk Date: Sat, 29 Dec 2018 18:15:19 +0300 Subject: [PATCH 3/3] feat: add optional poolSize parameters --- .../ServiceCollectionExtensions.cs | 61 +++++++++++++++---- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/src/Configuration/ServiceCollectionExtensions.cs b/src/Configuration/ServiceCollectionExtensions.cs index 5cfc98a..d31a5ad 100644 --- a/src/Configuration/ServiceCollectionExtensions.cs +++ b/src/Configuration/ServiceCollectionExtensions.cs @@ -32,11 +32,12 @@ public static IServiceCollection AddConfigurationDbContext(this IServiceCollecti /// /// /// The store options action. + /// /// public static IServiceCollection AddConfigurationDbContextPool(this IServiceCollection services, - Action storeOptionsAction = null) + Action storeOptionsAction = null, int? poolSize = null) { - return services.AddConfigurationDbContextPool(storeOptionsAction); + return services.AddConfigurationDbContextPool(storeOptionsAction, poolSize); } /// @@ -73,23 +74,41 @@ public static IServiceCollection AddConfigurationDbContext(this IServi /// The IConfigurationDbContext to use. /// /// The store options action. + /// /// public static IServiceCollection AddConfigurationDbContextPool(this IServiceCollection services, - Action storeOptionsAction = null) + Action storeOptionsAction = null, int? poolSize = null) where TContext : DbContext, IConfigurationDbContext { services.AddConfigurationInfrastructure(storeOptionsAction, out var storeOptions); if (storeOptions.ResolveDbContextOptions != null) { - services.AddDbContextPool(storeOptions.ResolveDbContextOptions); + if (poolSize is null) + { + services.AddDbContextPool(storeOptions.ResolveDbContextOptions); + } + else + { + services.AddDbContextPool(storeOptions.ResolveDbContextOptions, poolSize.Value); + } } else { - services.AddDbContextPool(dbCtxBuilder => + if (poolSize is null) { - storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); - }); + services.AddDbContextPool(dbCtxBuilder => + { + storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); + }); + } + else + { + services.AddDbContextPool(dbCtxBuilder => + { + storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); + }, poolSize.Value); + } } return services; @@ -187,23 +206,41 @@ public static IServiceCollection AddOperationalDbContext(this IService /// The IPersistedGrantDbContext to use. /// /// The store options action. + /// /// public static IServiceCollection AddOperationalDbContextPool(this IServiceCollection services, - Action storeOptionsAction = null) + Action storeOptionsAction = null, int? poolSize = null) where TContext : DbContext, IPersistedGrantDbContext { services.AddOperationalInfrastructure(storeOptionsAction, out var storeOptions); if (storeOptions.ResolveDbContextOptions != null) { - services.AddDbContextPool(storeOptions.ResolveDbContextOptions); + if (poolSize is null) + { + services.AddDbContextPool(storeOptions.ResolveDbContextOptions); + } + else + { + services.AddDbContextPool(storeOptions.ResolveDbContextOptions, poolSize.Value); + } } else { - services.AddDbContextPool(dbCtxBuilder => + if (poolSize is null) { - storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); - }); + services.AddDbContextPool(dbCtxBuilder => + { + storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); + }); + } + else + { + services.AddDbContextPool(dbCtxBuilder => + { + storeOptions.ConfigureDbContext?.Invoke(dbCtxBuilder); + }, poolSize.Value); + } } return services;