Browse Source

Update the MongoDB resolvers to cache the store types

pull/711/head
Kévin Chalet 8 years ago
parent
commit
2680ab2383
  1. 26
      src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs
  2. 26
      src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs
  3. 26
      src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs
  4. 26
      src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs

26
src/OpenIddict.MongoDb/Resolvers/OpenIddictApplicationStoreResolver.cs

@ -5,6 +5,7 @@
*/ */
using System; using System;
using System.Collections.Concurrent;
using System.Text; using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb
/// </summary> /// </summary>
public class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver public class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver
{ {
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider) public OpenIddictApplicationStoreResolver([NotNull] IServiceProvider provider)
@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb
return store; return store;
} }
if (!typeof(OpenIddictApplication).IsAssignableFrom(typeof(TApplication))) var type = _cache.GetOrAdd(typeof(TApplication), key =>
{ {
throw new InvalidOperationException(new StringBuilder() if (!typeof(OpenIddictApplication).IsAssignableFrom(key))
.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' ") throw new InvalidOperationException(new StringBuilder()
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") .AppendLine("The specified application type is not compatible with the MongoDB stores.")
.Append("that inherits from the 'OpenIddictApplication' entity.") .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictApplication' ")
.ToString()); .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<TApplication>) _provider.GetRequiredService( return (IOpenIddictApplicationStore<TApplication>) _provider.GetRequiredService(type);
typeof(OpenIddictApplicationStore<>).MakeGenericType(typeof(TApplication)));
} }
} }
} }

26
src/OpenIddict.MongoDb/Resolvers/OpenIddictAuthorizationStoreResolver.cs

@ -5,6 +5,7 @@
*/ */
using System; using System;
using System.Collections.Concurrent;
using System.Text; using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb
/// </summary> /// </summary>
public class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver public class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver
{ {
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public OpenIddictAuthorizationStoreResolver([NotNull] IServiceProvider provider) public OpenIddictAuthorizationStoreResolver([NotNull] IServiceProvider provider)
@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb
return store; return store;
} }
if (!typeof(OpenIddictAuthorization).IsAssignableFrom(typeof(TAuthorization))) var type = _cache.GetOrAdd(typeof(TAuthorization), key =>
{ {
throw new InvalidOperationException(new StringBuilder() if (!typeof(OpenIddictAuthorization).IsAssignableFrom(key))
.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' ") throw new InvalidOperationException(new StringBuilder()
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") .AppendLine("The specified authorization type is not compatible with the MongoDB stores.")
.Append("that inherits from the 'OpenIddictAuthorization' entity.") .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictAuthorization' ")
.ToString()); .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<TAuthorization>) _provider.GetRequiredService( return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type);
typeof(OpenIddictAuthorizationStore<>).MakeGenericType(typeof(TAuthorization)));
} }
} }
} }

26
src/OpenIddict.MongoDb/Resolvers/OpenIddictScopeStoreResolver.cs

@ -5,6 +5,7 @@
*/ */
using System; using System;
using System.Collections.Concurrent;
using System.Text; using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb
/// </summary> /// </summary>
public class OpenIddictScopeStoreResolver : IOpenIddictScopeStoreResolver public class OpenIddictScopeStoreResolver : IOpenIddictScopeStoreResolver
{ {
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public OpenIddictScopeStoreResolver([NotNull] IServiceProvider provider) public OpenIddictScopeStoreResolver([NotNull] IServiceProvider provider)
@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb
return store; return store;
} }
if (!typeof(OpenIddictScope).IsAssignableFrom(typeof(TScope))) var type = _cache.GetOrAdd(typeof(TScope), key =>
{ {
throw new InvalidOperationException(new StringBuilder() if (!typeof(OpenIddictScope).IsAssignableFrom(key))
.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' ") throw new InvalidOperationException(new StringBuilder()
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") .AppendLine("The specified scope type is not compatible with the MongoDB stores.")
.Append("that inherits from the 'OpenIddictScope' entity.") .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictScope' ")
.ToString()); .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<TScope>) _provider.GetRequiredService( return (IOpenIddictScopeStore<TScope>) _provider.GetRequiredService(type);
typeof(OpenIddictScopeStore<>).MakeGenericType(typeof(TScope)));
} }
} }
} }

26
src/OpenIddict.MongoDb/Resolvers/OpenIddictTokenStoreResolver.cs

@ -5,6 +5,7 @@
*/ */
using System; using System;
using System.Collections.Concurrent;
using System.Text; using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb
/// </summary> /// </summary>
public class OpenIddictTokenStoreResolver : IOpenIddictTokenStoreResolver public class OpenIddictTokenStoreResolver : IOpenIddictTokenStoreResolver
{ {
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
private readonly IServiceProvider _provider; private readonly IServiceProvider _provider;
public OpenIddictTokenStoreResolver([NotNull] IServiceProvider provider) public OpenIddictTokenStoreResolver([NotNull] IServiceProvider provider)
@ -37,18 +39,22 @@ namespace OpenIddict.MongoDb
return store; return store;
} }
if (!typeof(OpenIddictToken).IsAssignableFrom(typeof(TToken))) var type = _cache.GetOrAdd(typeof(TToken), key =>
{ {
throw new InvalidOperationException(new StringBuilder() if (!typeof(OpenIddictToken).IsAssignableFrom(key))
.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' ") throw new InvalidOperationException(new StringBuilder()
.Append("entity (from the 'OpenIddict.MongoDb.Models' package) or a custom entity ") .AppendLine("The specified token type is not compatible with the MongoDB stores.")
.Append("that inherits from the 'OpenIddictToken' entity.") .Append("When enabling the MongoDB stores, make sure you use the built-in 'OpenIddictToken' ")
.ToString()); .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<TToken>) _provider.GetRequiredService( return (IOpenIddictTokenStore<TToken>) _provider.GetRequiredService(type);
typeof(OpenIddictTokenStore<>).MakeGenericType(typeof(TToken)));
} }
} }
} }

Loading…
Cancel
Save