From 2680ab23833670101c0cb87feee2ef79244eae98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Mon, 29 Oct 2018 17:36:18 +0100 Subject: [PATCH] Update the MongoDB resolvers to cache the store types --- .../OpenIddictApplicationStoreResolver.cs | 26 ++++++++++++------- .../OpenIddictAuthorizationStoreResolver.cs | 26 ++++++++++++------- .../Resolvers/OpenIddictScopeStoreResolver.cs | 26 ++++++++++++------- .../Resolvers/OpenIddictTokenStoreResolver.cs | 26 ++++++++++++------- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs b/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs index 3f30d555..65bf330e 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs @@ -5,6 +5,7 @@ */ using System; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider) @@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb return store; } - if (!typeof(OpenIddictApplication).IsAssignableFrom(typeof(TApplication))) + var type = _cache.GetOrAdd(typeof(TApplication), key => { - throw new InvalidOperationException(new StringBuilder() - .AppendLine("The specified application type is not compatible with the MongoDB stores.") - .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictApplication' ") - .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") - .Append("that inherits from the 'OpenIddictApplication' entity.") - .ToString()); - } + if (!typeof(OpenIddictApplication).IsAssignableFrom(key)) + { + throw new InvalidOperationException(new StringBuilder() + .AppendLine("The specified application type is not compatible with the MongoDB stores.") + .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictApplication' ") + .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") + .Append("that inherits from the 'OpenIddictApplication' entity.") + .ToString()); + } + + return typeof(OpenIddictApplicationStore<>).MakeGenericType(key); + }); - return (IOpenIddictApplicationStore) _provider.GetRequiredService( - typeof(OpenIddictApplicationStore<>).MakeGenericType(typeof(TApplication))); + return (IOpenIddictApplicationStore) _provider.GetRequiredService(type); } } } diff --git a/src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs b/src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs index 7e6d6893..2ea3773e 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs @@ -5,6 +5,7 @@ */ using System; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictAuthorizationStoreResolver([NotNull] IServiceProvider provider) @@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb return store; } - if (!typeof(OpenIddictAuthorization).IsAssignableFrom(typeof(TAuthorization))) + var type = _cache.GetOrAdd(typeof(TAuthorization), key => { - throw new InvalidOperationException(new StringBuilder() - .AppendLine("The specified authorization type is not compatible with the MongoDB stores.") - .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictAuthorization' ") - .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") - .Append("that inherits from the 'OpenIddictAuthorization' entity.") - .ToString()); - } + if (!typeof(OpenIddictAuthorization).IsAssignableFrom(key)) + { + throw new InvalidOperationException(new StringBuilder() + .AppendLine("The specified authorization type is not compatible with the MongoDB stores.") + .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictAuthorization' ") + .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") + .Append("that inherits from the 'OpenIddictAuthorization' entity.") + .ToString()); + } + + return typeof(OpenIddictAuthorizationStore<>).MakeGenericType(key); + }); - return (IOpenIddictAuthorizationStore) _provider.GetRequiredService( - typeof(OpenIddictAuthorizationStore<>).MakeGenericType(typeof(TAuthorization))); + return (IOpenIddictAuthorizationStore) _provider.GetRequiredService(type); } } } diff --git a/src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs b/src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs index 1d9671a1..557725f4 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs @@ -5,6 +5,7 @@ */ using System; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictScopeStoreResolver : IOpenIddictScopeStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictScopeStoreResolver([NotNull] IServiceProvider provider) @@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb return store; } - if (!typeof(OpenIddictScope).IsAssignableFrom(typeof(TScope))) + var type = _cache.GetOrAdd(typeof(TScope), key => { - throw new InvalidOperationException(new StringBuilder() - .AppendLine("The specified scope type is not compatible with the MongoDB stores.") - .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictScope' ") - .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") - .Append("that inherits from the 'OpenIddictScope' entity.") - .ToString()); - } + if (!typeof(OpenIddictScope).IsAssignableFrom(key)) + { + throw new InvalidOperationException(new StringBuilder() + .AppendLine("The specified scope type is not compatible with the MongoDB stores.") + .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictScope' ") + .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") + .Append("that inherits from the 'OpenIddictScope' entity.") + .ToString()); + } + + return typeof(OpenIddictScopeStore<>).MakeGenericType(key); + }); - return (IOpenIddictScopeStore) _provider.GetRequiredService( - typeof(OpenIddictScopeStore<>).MakeGenericType(typeof(TScope))); + return (IOpenIddictScopeStore) _provider.GetRequiredService(type); } } } diff --git a/src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs b/src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs index 9d5be3ed..8ee68132 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs @@ -5,6 +5,7 @@ */ using System; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictTokenStoreResolver : IOpenIddictTokenStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictTokenStoreResolver([NotNull] IServiceProvider provider) @@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb return store; } - if (!typeof(OpenIddictToken).IsAssignableFrom(typeof(TToken))) + var type = _cache.GetOrAdd(typeof(TToken), key => { - throw new InvalidOperationException(new StringBuilder() - .AppendLine("The specified token type is not compatible with the MongoDB stores.") - .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictToken' ") - .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") - .Append("that inherits from the 'OpenIddictToken' entity.") - .ToString()); - } + if (!typeof(OpenIddictToken).IsAssignableFrom(key)) + { + throw new InvalidOperationException(new StringBuilder() + .AppendLine("The specified token type is not compatible with the MongoDB stores.") + .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictToken' ") + .Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") + .Append("that inherits from the 'OpenIddictToken' entity.") + .ToString()); + } + + return typeof(OpenIddictTokenStore<>).MakeGenericType(key); + }); - return (IOpenIddictTokenStore) _provider.GetRequiredService( - typeof(OpenIddictTokenStore<>).MakeGenericType(typeof(TToken))); + return (IOpenIddictTokenStore) _provider.GetRequiredService(type); } } }