diff --git a/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs b/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs index fb86b347..300e415d 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs @@ -6,6 +6,7 @@ using System; using System.Reflection; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -19,6 +20,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider) @@ -38,18 +40,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 672a8c2a..ec5af0ff 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs @@ -6,6 +6,7 @@ using System; using System.Reflection; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -19,6 +20,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictAuthorizationStoreResolver([NotNull] IServiceProvider provider) @@ -38,18 +40,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 2174dbc1..e1bbfc8c 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs @@ -6,6 +6,7 @@ using System; using System.Reflection; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -19,6 +20,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictScopeStoreResolver : IOpenIddictScopeStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictScopeStoreResolver([NotNull] IServiceProvider provider) @@ -38,18 +40,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 f27195d8..506a9716 100644 --- a/src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs +++ b/src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs @@ -6,6 +6,7 @@ using System; using System.Reflection; +using System.Collections.Concurrent; using System.Text; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -19,6 +20,7 @@ namespace OpenIddict.MongoDb /// public class OpenIddictTokenStoreResolver : IOpenIddictTokenStoreResolver { + private readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private readonly IServiceProvider _provider; public OpenIddictTokenStoreResolver([NotNull] IServiceProvider provider) @@ -38,18 +40,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); } } }