Browse Source

Update the Entity Framework/Entity Framework Core stores to use FindAsync() instead of SingleOrDefaultAsync() when possible

pull/474/head
Kévin Chalet 9 years ago
parent
commit
1b63058b6d
  1. 14
      src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs
  2. 18
      src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs
  3. 38
      src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs
  4. 14
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs
  5. 18
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs
  6. 36
      src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs

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

@ -156,6 +156,20 @@ namespace OpenIddict.EntityFramework
catch (DbUpdateConcurrencyException) { } catch (DbUpdateConcurrencyException) { }
} }
/// <summary>
/// Retrieves an application using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the application.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the client application corresponding to the identifier.
/// </returns>
public override Task<TApplication> FindByIdAsync(string identifier, CancellationToken cancellationToken)
{
return Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier));
}
/// <summary> /// <summary>
/// Executes the specified query. /// Executes the specified query.
/// </summary> /// </summary>

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

@ -133,9 +133,7 @@ namespace OpenIddict.EntityFramework
// Bind the authorization to the specified application, if applicable. // Bind the authorization to the specified application, if applicable.
if (!string.IsNullOrEmpty(descriptor.ApplicationId)) if (!string.IsNullOrEmpty(descriptor.ApplicationId))
{ {
var key = ConvertIdentifierFromString(descriptor.ApplicationId); var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.ApplicationId));
var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key));
if (application == null) if (application == null)
{ {
throw new InvalidOperationException("The application associated with the authorization cannot be found."); throw new InvalidOperationException("The application associated with the authorization cannot be found.");
@ -147,6 +145,20 @@ namespace OpenIddict.EntityFramework
return await CreateAsync(authorization, cancellationToken); return await CreateAsync(authorization, cancellationToken);
} }
/// <summary>
/// Retrieves an authorization using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the authorization.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the authorization corresponding to the identifier.
/// </returns>
public override Task<TAuthorization> FindByIdAsync(string identifier, CancellationToken cancellationToken)
{
return Authorizations.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier));
}
/// <summary> /// <summary>
/// Executes the specified query. /// Executes the specified query.
/// </summary> /// </summary>

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

@ -142,9 +142,7 @@ namespace OpenIddict.EntityFramework
// Bind the token to the specified client application, if applicable. // Bind the token to the specified client application, if applicable.
if (!string.IsNullOrEmpty(descriptor.ApplicationId)) if (!string.IsNullOrEmpty(descriptor.ApplicationId))
{ {
var key = ConvertIdentifierFromString(descriptor.ApplicationId); var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.ApplicationId));
var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key));
if (application == null) if (application == null)
{ {
throw new InvalidOperationException("The application associated with the token cannot be found."); throw new InvalidOperationException("The application associated with the token cannot be found.");
@ -156,9 +154,7 @@ namespace OpenIddict.EntityFramework
// Bind the token to the specified authorization, if applicable. // Bind the token to the specified authorization, if applicable.
if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) if (!string.IsNullOrEmpty(descriptor.AuthorizationId))
{ {
var key = ConvertIdentifierFromString(descriptor.AuthorizationId); var authorization = await Authorizations.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.AuthorizationId));
var authorization = await Authorizations.SingleOrDefaultAsync(entity => entity.Id.Equals(key));
if (authorization == null) if (authorization == null)
{ {
throw new InvalidOperationException("The authorization associated with the token cannot be found."); throw new InvalidOperationException("The authorization associated with the token cannot be found.");
@ -193,6 +189,20 @@ namespace OpenIddict.EntityFramework
catch (DbUpdateConcurrencyException) { } catch (DbUpdateConcurrencyException) { }
} }
/// <summary>
/// Retrieves an token using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the token.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the token corresponding to the unique identifier.
/// </returns>
public override Task<TToken> FindByIdAsync(string identifier, CancellationToken cancellationToken)
{
return Tokens.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier));
}
/// <summary> /// <summary>
/// Executes the specified query. /// Executes the specified query.
/// </summary> /// </summary>
@ -251,15 +261,13 @@ namespace OpenIddict.EntityFramework
if (!string.IsNullOrEmpty(identifier)) if (!string.IsNullOrEmpty(identifier))
{ {
var key = ConvertIdentifierFromString(identifier); var authorization = await Authorizations.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier));
var authorization = await Authorizations.SingleOrDefaultAsync(element => element.Id.Equals(key));
if (authorization == null) if (authorization == null)
{ {
throw new InvalidOperationException("The authorization associated with the token cannot be found."); throw new InvalidOperationException("The authorization associated with the token cannot be found.");
} }
authorization.Tokens.Add(token); token.Authorization = authorization;
} }
else else
@ -268,7 +276,7 @@ namespace OpenIddict.EntityFramework
// Try to retrieve the authorization associated with the token. // Try to retrieve the authorization associated with the token.
// If none can be found, assume that no authorization is attached. // If none can be found, assume that no authorization is attached.
var authorization = await Authorizations.SingleOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key))); var authorization = await Authorizations.FirstOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key)));
if (authorization != null) if (authorization != null)
{ {
authorization.Tokens.Remove(token); authorization.Tokens.Remove(token);
@ -294,15 +302,13 @@ namespace OpenIddict.EntityFramework
if (!string.IsNullOrEmpty(identifier)) if (!string.IsNullOrEmpty(identifier))
{ {
var key = ConvertIdentifierFromString(identifier); var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier));
var application = await Applications.SingleOrDefaultAsync(element => element.Id.Equals(key));
if (application == null) if (application == null)
{ {
throw new InvalidOperationException("The application associated with the token cannot be found."); throw new InvalidOperationException("The application associated with the token cannot be found.");
} }
application.Tokens.Add(token); token.Application = application;
} }
else else
@ -311,7 +317,7 @@ namespace OpenIddict.EntityFramework
// Try to retrieve the application associated with the token. // Try to retrieve the application associated with the token.
// If none can be found, assume that no application is attached. // If none can be found, assume that no application is attached.
var application = await Applications.SingleOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key))); var application = await Applications.FirstOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key)));
if (application != null) if (application != null)
{ {
application.Tokens.Remove(token); application.Tokens.Remove(token);

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

@ -155,6 +155,20 @@ namespace OpenIddict.EntityFrameworkCore
catch (DbUpdateConcurrencyException) { } catch (DbUpdateConcurrencyException) { }
} }
/// <summary>
/// Retrieves an application using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the application.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the client application corresponding to the identifier.
/// </returns>
public override Task<TApplication> FindByIdAsync(string identifier, CancellationToken cancellationToken)
{
return Applications.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken);
}
/// <summary> /// <summary>
/// Executes the specified query. /// Executes the specified query.
/// </summary> /// </summary>

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

@ -132,9 +132,7 @@ namespace OpenIddict.EntityFrameworkCore
// Bind the authorization to the specified application, if applicable. // Bind the authorization to the specified application, if applicable.
if (!string.IsNullOrEmpty(descriptor.ApplicationId)) if (!string.IsNullOrEmpty(descriptor.ApplicationId))
{ {
var key = ConvertIdentifierFromString(descriptor.ApplicationId); var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken);
var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key));
if (application == null) if (application == null)
{ {
throw new InvalidOperationException("The application associated with the authorization cannot be found."); throw new InvalidOperationException("The application associated with the authorization cannot be found.");
@ -146,6 +144,20 @@ namespace OpenIddict.EntityFrameworkCore
return await CreateAsync(authorization, cancellationToken); return await CreateAsync(authorization, cancellationToken);
} }
/// <summary>
/// Retrieves an authorization using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the authorization.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the authorization corresponding to the identifier.
/// </returns>
public override Task<TAuthorization> FindByIdAsync(string identifier, CancellationToken cancellationToken)
{
return Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken);
}
/// <summary> /// <summary>
/// Executes the specified query. /// Executes the specified query.
/// </summary> /// </summary>

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

@ -141,9 +141,7 @@ namespace OpenIddict.EntityFrameworkCore
// Bind the token to the specified client application, if applicable. // Bind the token to the specified client application, if applicable.
if (!string.IsNullOrEmpty(descriptor.ApplicationId)) if (!string.IsNullOrEmpty(descriptor.ApplicationId))
{ {
var key = ConvertIdentifierFromString(descriptor.ApplicationId); var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken);
var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key));
if (application == null) if (application == null)
{ {
throw new InvalidOperationException("The application associated with the token cannot be found."); throw new InvalidOperationException("The application associated with the token cannot be found.");
@ -155,9 +153,7 @@ namespace OpenIddict.EntityFrameworkCore
// Bind the token to the specified authorization, if applicable. // Bind the token to the specified authorization, if applicable.
if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) if (!string.IsNullOrEmpty(descriptor.AuthorizationId))
{ {
var key = ConvertIdentifierFromString(descriptor.AuthorizationId); var authorization = await Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.AuthorizationId) }, cancellationToken);
var authorization = await Authorizations.SingleOrDefaultAsync(entity => entity.Id.Equals(key));
if (authorization == null) if (authorization == null)
{ {
throw new InvalidOperationException("The authorization associated with the token cannot be found."); throw new InvalidOperationException("The authorization associated with the token cannot be found.");
@ -192,6 +188,20 @@ namespace OpenIddict.EntityFrameworkCore
catch (DbUpdateConcurrencyException) { } catch (DbUpdateConcurrencyException) { }
} }
/// <summary>
/// Retrieves an token using its unique identifier.
/// </summary>
/// <param name="identifier">The unique identifier associated with the token.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> that can be used to abort the operation.</param>
/// <returns>
/// A <see cref="Task"/> that can be used to monitor the asynchronous operation,
/// whose result returns the token corresponding to the unique identifier.
/// </returns>
public override Task<TToken> FindByIdAsync(string identifier, CancellationToken cancellationToken)
{
return Tokens.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken);
}
/// <summary> /// <summary>
/// Executes the specified query. /// Executes the specified query.
/// </summary> /// </summary>
@ -250,15 +260,13 @@ namespace OpenIddict.EntityFrameworkCore
if (!string.IsNullOrEmpty(identifier)) if (!string.IsNullOrEmpty(identifier))
{ {
var key = ConvertIdentifierFromString(identifier); var authorization = await Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken);
var authorization = await Authorizations.SingleOrDefaultAsync(element => element.Id.Equals(key));
if (authorization == null) if (authorization == null)
{ {
throw new InvalidOperationException("The authorization associated with the token cannot be found."); throw new InvalidOperationException("The authorization associated with the token cannot be found.");
} }
authorization.Tokens.Add(token); token.Authorization = authorization;
} }
else else
@ -267,7 +275,7 @@ namespace OpenIddict.EntityFrameworkCore
// Try to retrieve the authorization associated with the token. // Try to retrieve the authorization associated with the token.
// If none can be found, assume that no authorization is attached. // If none can be found, assume that no authorization is attached.
var authorization = await Authorizations.SingleOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key))); var authorization = await Authorizations.FirstOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key)));
if (authorization != null) if (authorization != null)
{ {
authorization.Tokens.Remove(token); authorization.Tokens.Remove(token);
@ -295,13 +303,13 @@ namespace OpenIddict.EntityFrameworkCore
{ {
var key = ConvertIdentifierFromString(identifier); var key = ConvertIdentifierFromString(identifier);
var application = await Applications.SingleOrDefaultAsync(element => element.Id.Equals(key)); var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken);
if (application == null) if (application == null)
{ {
throw new InvalidOperationException("The application associated with the token cannot be found."); throw new InvalidOperationException("The application associated with the token cannot be found.");
} }
application.Tokens.Add(token); token.Application = application;
} }
else else
@ -310,7 +318,7 @@ namespace OpenIddict.EntityFrameworkCore
// Try to retrieve the application associated with the token. // Try to retrieve the application associated with the token.
// If none can be found, assume that no application is attached. // If none can be found, assume that no application is attached.
var application = await Applications.SingleOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key))); var application = await Applications.FirstOrDefaultAsync(element => element.Tokens.Any(t => t.Id.Equals(key)));
if (application != null) if (application != null)
{ {
application.Tokens.Remove(token); application.Tokens.Remove(token);

Loading…
Cancel
Save