Browse Source

Replace new() constraints by Activator.CreateInstance() to allow using abstract entities as generic parameters

pull/639/head
Vladyslav Martynets 8 years ago
committed by Kévin Chalet
parent
commit
80bfa33b36
  1. 8
      src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs
  2. 8
      src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs
  3. 23
      src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs
  4. 23
      src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
  5. 19
      src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs
  6. 23
      src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
  7. 8
      src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs
  8. 8
      src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs
  9. 16
      src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs
  10. 23
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs
  11. 23
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
  12. 19
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs
  13. 23
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs
  14. 8
      src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs
  15. 19
      src/OpenIddict.MongoDb/Stores/OpenIddictApplicationStore.cs
  16. 19
      src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs
  17. 19
      src/OpenIddict.MongoDb/Stores/OpenIddictScopeStore.cs
  18. 19
      src/OpenIddict.MongoDb/Stores/OpenIddictTokenStore.cs

8
src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkBuilder.cs

@ -94,10 +94,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <returns>The <see cref="OpenIddictEntityFrameworkBuilder"/>.</returns>
public OpenIddictEntityFrameworkBuilder ReplaceDefaultEntities<TApplication, TAuthorization, TScope, TToken, TKey>()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TScope : OpenIddictScope<TKey>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
Services.Configure<OpenIddictCoreOptions>(options =>

8
src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkExtensions.cs

@ -98,10 +98,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
/// <returns>The Entity Framework context builder.</returns>
public static DbModelBuilder UseOpenIddict<TApplication, TAuthorization, TScope, TToken, TKey>([NotNull] this DbModelBuilder builder)
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TScope : OpenIddictScope<TKey>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
if (builder == null)

23
src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs

@ -52,9 +52,9 @@ namespace OpenIddict.EntityFramework
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictApplicationStore<TApplication, TAuthorization, TToken, TContext, TKey> : IOpenIddictApplicationStore<TApplication>
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -651,7 +651,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated application, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TApplication> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TApplication>(new TApplication());
{
try
{
return new ValueTask<TApplication>(Activator.CreateInstance<TApplication>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TApplication>(Task.FromException<TApplication>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new application instance.")
.Append("Make sure that the application entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom application store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

23
src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs

@ -52,9 +52,9 @@ namespace OpenIddict.EntityFramework
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictAuthorizationStore<TAuthorization, TApplication, TToken, TContext, TKey> : IOpenIddictAuthorizationStore<TAuthorization>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -574,7 +574,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated authorization, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TAuthorization> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TAuthorization>(new TAuthorization());
{
try
{
return new ValueTask<TAuthorization>(Activator.CreateInstance<TAuthorization>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TAuthorization>(Task.FromException<TAuthorization>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new authorization instance.")
.Append("Make sure that the authorization entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom authorization store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

19
src/OpenIddict.EntityFramework/Stores/OpenIddictScopeStore.cs

@ -46,7 +46,7 @@ namespace OpenIddict.EntityFramework
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictScopeStore<TScope, TContext, TKey> : IOpenIddictScopeStore<TScope>
where TScope : OpenIddictScope<TKey>, new()
where TScope : OpenIddictScope<TKey>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -439,7 +439,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated scope, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TScope> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TScope>(new TScope());
{
try
{
return new ValueTask<TScope>(Activator.CreateInstance<TScope>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TScope>(Task.FromException<TScope>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new scope instance.")
.Append("Make sure that the scope entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom scope store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

23
src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs

@ -52,9 +52,9 @@ namespace OpenIddict.EntityFramework
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictTokenStore<TToken, TApplication, TAuthorization, TContext, TKey> : IOpenIddictTokenStore<TToken>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -582,7 +582,22 @@ namespace OpenIddict.EntityFramework
/// whose result returns the instantiated token, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TToken> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TToken>(new TToken());
{
try
{
return new ValueTask<TToken>(Activator.CreateInstance<TToken>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TToken>(Task.FromException<TToken>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new token instance.")
.Append("Make sure that the token entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom token store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

8
src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreBuilder.cs

@ -103,10 +103,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <returns>The <see cref="OpenIddictEntityFrameworkCoreBuilder"/>.</returns>
public OpenIddictEntityFrameworkCoreBuilder ReplaceDefaultEntities<TApplication, TAuthorization, TScope, TToken, TKey>()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TScope : OpenIddictScope<TKey>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
Services.Configure<OpenIddictCoreOptions>(options =>

8
src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreCustomizer.cs

@ -18,10 +18,10 @@ namespace OpenIddict.EntityFrameworkCore
/// required by the OpenIddict stack in an Entity Framework context.
/// </summary>
public class OpenIddictEntityFrameworkCoreCustomizer<TApplication, TAuthorization, TScope, TToken, TKey> : RelationalModelCustomizer
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TScope : OpenIddictScope<TKey>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
public OpenIddictEntityFrameworkCoreCustomizer([NotNull] ModelCustomizerDependencies dependencies)

16
src/OpenIddict.EntityFrameworkCore/OpenIddictEntityFrameworkCoreExtensions.cs

@ -107,10 +107,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
/// <returns>The Entity Framework context builder.</returns>
public static DbContextOptionsBuilder UseOpenIddict<TApplication, TAuthorization, TScope, TToken, TKey>([NotNull] this DbContextOptionsBuilder builder)
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TScope : OpenIddictScope<TKey>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
if (builder == null)
@ -153,10 +153,10 @@ namespace Microsoft.Extensions.DependencyInjection
/// <param name="builder">The builder used to configure the Entity Framework context.</param>
/// <returns>The Entity Framework context builder.</returns>
public static ModelBuilder UseOpenIddict<TApplication, TAuthorization, TScope, TToken, TKey>([NotNull] this ModelBuilder builder)
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TScope : OpenIddictScope<TKey>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TScope : OpenIddictScope<TKey>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TKey : IEquatable<TKey>
{
if (builder == null)

23
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs

@ -74,9 +74,9 @@ namespace OpenIddict.EntityFrameworkCore
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictApplicationStore<TApplication, TAuthorization, TToken, TContext, TKey> : IOpenIddictApplicationStore<TApplication>
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -718,7 +718,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated application, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TApplication> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TApplication>(new TApplication());
{
try
{
return new ValueTask<TApplication>(Activator.CreateInstance<TApplication>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TApplication>(Task.FromException<TApplication>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new application instance.")
.Append("Make sure that the application entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom application store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

23
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs

@ -74,9 +74,9 @@ namespace OpenIddict.EntityFrameworkCore
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictAuthorizationStore<TAuthorization, TApplication, TToken, TContext, TKey> : IOpenIddictAuthorizationStore<TAuthorization>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -662,7 +662,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated authorization, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TAuthorization> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TAuthorization>(new TAuthorization());
{
try
{
return new ValueTask<TAuthorization>(Activator.CreateInstance<TAuthorization>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TAuthorization>(Task.FromException<TAuthorization>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new authorization instance.")
.Append("Make sure that the authorization entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom authorization store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

19
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictScopeStore.cs

@ -64,7 +64,7 @@ namespace OpenIddict.EntityFrameworkCore
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictScopeStore<TScope, TContext, TKey> : IOpenIddictScopeStore<TScope>
where TScope : OpenIddictScope<TKey>, new()
where TScope : OpenIddictScope<TKey>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -480,7 +480,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated scope, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TScope> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TScope>(new TScope());
{
try
{
return new ValueTask<TScope>(Activator.CreateInstance<TScope>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TScope>(Task.FromException<TScope>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new scope instance.")
.Append("Make sure that the scope entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom scope store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

23
src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs

@ -74,9 +74,9 @@ namespace OpenIddict.EntityFrameworkCore
/// <typeparam name="TContext">The type of the Entity Framework database context.</typeparam>
/// <typeparam name="TKey">The type of the entity primary keys.</typeparam>
public class OpenIddictTokenStore<TToken, TApplication, TAuthorization, TContext, TKey> : IOpenIddictTokenStore<TToken>
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>, new()
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>, new()
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>, new()
where TToken : OpenIddictToken<TKey, TApplication, TAuthorization>
where TApplication : OpenIddictApplication<TKey, TAuthorization, TToken>
where TAuthorization : OpenIddictAuthorization<TKey, TApplication, TToken>
where TContext : DbContext
where TKey : IEquatable<TKey>
{
@ -660,7 +660,22 @@ namespace OpenIddict.EntityFrameworkCore
/// whose result returns the instantiated token, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TToken> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TToken>(new TToken());
{
try
{
return new ValueTask<TToken>(Activator.CreateInstance<TToken>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TToken>(Task.FromException<TToken>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new token instance.")
.Append("Make sure that the token entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom token store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

8
src/OpenIddict.MongoDb/OpenIddictMongoDbBuilder.cs

@ -78,7 +78,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/>.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultApplicationEntity<TApplication>()
where TApplication : OpenIddictApplication, new()
where TApplication : OpenIddictApplication
{
Services.Configure<OpenIddictCoreOptions>(options => options.DefaultApplicationType = typeof(TApplication));
@ -90,7 +90,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/>.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultAuthorizationEntity<TAuthorization>()
where TAuthorization : OpenIddictAuthorization, new()
where TAuthorization : OpenIddictAuthorization
{
Services.Configure<OpenIddictCoreOptions>(options => options.DefaultAuthorizationType = typeof(TAuthorization));
@ -102,7 +102,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/>.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultScopeEntity<TScope>()
where TScope : OpenIddictScope, new()
where TScope : OpenIddictScope
{
Services.Configure<OpenIddictCoreOptions>(options => options.DefaultScopeType = typeof(TScope));
@ -114,7 +114,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
/// <returns>The <see cref="OpenIddictMongoDbBuilder"/>.</returns>
public OpenIddictMongoDbBuilder ReplaceDefaultTokenEntity<TToken>()
where TToken : OpenIddictToken, new()
where TToken : OpenIddictToken
{
Services.Configure<OpenIddictCoreOptions>(options => options.DefaultTokenType = typeof(TToken));

19
src/OpenIddict.MongoDb/Stores/OpenIddictApplicationStore.cs

@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
/// </summary>
/// <typeparam name="TApplication">The type of the Application entity.</typeparam>
public class OpenIddictApplicationStore<TApplication> : IOpenIddictApplicationStore<TApplication>
where TApplication : OpenIddictApplication, new()
where TApplication : OpenIddictApplication
{
public OpenIddictApplicationStore(
[NotNull] IMemoryCache cache,
@ -490,7 +490,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated application, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TApplication> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TApplication>(new TApplication());
{
try
{
return new ValueTask<TApplication>(Activator.CreateInstance<TApplication>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TApplication>(Task.FromException<TApplication>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new application instance.")
.Append("Make sure that the application entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom application store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

19
src/OpenIddict.MongoDb/Stores/OpenIddictAuthorizationStore.cs

@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
/// </summary>
/// <typeparam name="TAuthorization">The type of the Authorization entity.</typeparam>
public class OpenIddictAuthorizationStore<TAuthorization> : IOpenIddictAuthorizationStore<TAuthorization>
where TAuthorization : OpenIddictAuthorization, new()
where TAuthorization : OpenIddictAuthorization
{
public OpenIddictAuthorizationStore(
[NotNull] IMemoryCache cache,
@ -489,7 +489,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated authorization, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TAuthorization> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TAuthorization>(new TAuthorization());
{
try
{
return new ValueTask<TAuthorization>(Activator.CreateInstance<TAuthorization>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TAuthorization>(Task.FromException<TAuthorization>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new authorization instance.")
.Append("Make sure that the authorization entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom authorization store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

19
src/OpenIddict.MongoDb/Stores/OpenIddictScopeStore.cs

@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
/// </summary>
/// <typeparam name="TScope">The type of the Scope entity.</typeparam>
public class OpenIddictScopeStore<TScope> : IOpenIddictScopeStore<TScope>
where TScope : OpenIddictScope, new()
where TScope : OpenIddictScope
{
public OpenIddictScopeStore(
[NotNull] IMemoryCache cache,
@ -394,7 +394,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated scope, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TScope> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TScope>(new TScope());
{
try
{
return new ValueTask<TScope>(Activator.CreateInstance<TScope>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TScope>(Task.FromException<TScope>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new scope instance.")
.Append("Make sure that the scope entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom scope store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

19
src/OpenIddict.MongoDb/Stores/OpenIddictTokenStore.cs

@ -28,7 +28,7 @@ namespace OpenIddict.MongoDb
/// </summary>
/// <typeparam name="TToken">The type of the Token entity.</typeparam>
public class OpenIddictTokenStore<TToken> : IOpenIddictTokenStore<TToken>
where TToken : OpenIddictToken, new()
where TToken : OpenIddictToken
{
public OpenIddictTokenStore(
[NotNull] IMemoryCache cache,
@ -507,7 +507,22 @@ namespace OpenIddict.MongoDb
/// whose result returns the instantiated token, that can be persisted in the database.
/// </returns>
public virtual ValueTask<TToken> InstantiateAsync(CancellationToken cancellationToken)
=> new ValueTask<TToken>(new TToken());
{
try
{
return new ValueTask<TToken>(Activator.CreateInstance<TToken>());
}
catch (MemberAccessException exception)
{
return new ValueTask<TToken>(Task.FromException<TToken>(
new InvalidOperationException(new StringBuilder()
.AppendLine("An error occurred while trying to create a new token instance.")
.Append("Make sure that the token entity is not abstract and has a public parameterless constructor ")
.Append("or create a custom token store that overrides 'InstantiateAsync()' to use a custom factory.")
.ToString(), exception)));
}
}
/// <summary>
/// Executes the specified query and returns all the corresponding elements.

Loading…
Cancel
Save