Browse Source

Update the MongoDB resolvers to cache the store types

pull/711/head
Kévin Chalet 7 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.Collections.Concurrent;
using System.Text;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection;
@ -18,6 +19,7 @@ namespace OpenIddict.MongoDb
/// </summary>
public class OpenIddictApplicationStoreResolver : IOpenIddictApplicationStoreResolver
{
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
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<TApplication>) _provider.GetRequiredService(
typeof(OpenIddictApplicationStore<>).MakeGenericType(typeof(TApplication)));
return (IOpenIddictApplicationStore<TApplication>) _provider.GetRequiredService(type);
}
}
}

26
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
/// </summary>
public class OpenIddictAuthorizationStoreResolver : IOpenIddictAuthorizationStoreResolver
{
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
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<TAuthorization>) _provider.GetRequiredService(
typeof(OpenIddictAuthorizationStore<>).MakeGenericType(typeof(TAuthorization)));
return (IOpenIddictAuthorizationStore<TAuthorization>) _provider.GetRequiredService(type);
}
}
}

26
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
/// </summary>
public class OpenIddictScopeStoreResolver : IOpenIddictScopeStoreResolver
{
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
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<TScope>) _provider.GetRequiredService(
typeof(OpenIddictScopeStore<>).MakeGenericType(typeof(TScope)));
return (IOpenIddictScopeStore<TScope>) _provider.GetRequiredService(type);
}
}
}

26
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
/// </summary>
public class OpenIddictTokenStoreResolver : IOpenIddictTokenStoreResolver
{
private readonly ConcurrentDictionary<Type, Type> _cache = new ConcurrentDictionary<Type, Type>();
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<TToken>) _provider.GetRequiredService(
typeof(OpenIddictTokenStore<>).MakeGenericType(typeof(TToken)));
return (IOpenIddictTokenStore<TToken>) _provider.GetRequiredService(type);
}
}
}

Loading…
Cancel
Save