From 1b63058b6d6cc98815b912e337b6e35d815e7820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sat, 30 Sep 2017 10:30:24 +0200 Subject: [PATCH] Update the Entity Framework/Entity Framework Core stores to use FindAsync() instead of SingleOrDefaultAsync() when possible --- .../Stores/OpenIddictApplicationStore.cs | 14 +++++++ .../Stores/OpenIddictAuthorizationStore.cs | 18 +++++++-- .../Stores/OpenIddictTokenStore.cs | 38 +++++++++++-------- .../Stores/OpenIddictApplicationStore.cs | 14 +++++++ .../Stores/OpenIddictAuthorizationStore.cs | 18 +++++++-- .../Stores/OpenIddictTokenStore.cs | 36 +++++++++++------- 6 files changed, 102 insertions(+), 36 deletions(-) diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs index 89629b5c..5330656f 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictApplicationStore.cs @@ -156,6 +156,20 @@ namespace OpenIddict.EntityFramework catch (DbUpdateConcurrencyException) { } } + /// + /// Retrieves an application using its unique identifier. + /// + /// The unique identifier associated with the application. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the client application corresponding to the identifier. + /// + public override Task FindByIdAsync(string identifier, CancellationToken cancellationToken) + { + return Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier)); + } + /// /// Executes the specified query. /// diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs index cfdadabf..deb68fbe 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictAuthorizationStore.cs @@ -133,9 +133,7 @@ namespace OpenIddict.EntityFramework // Bind the authorization to the specified application, if applicable. if (!string.IsNullOrEmpty(descriptor.ApplicationId)) { - var key = ConvertIdentifierFromString(descriptor.ApplicationId); - - var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key)); + var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.ApplicationId)); if (application == null) { throw new InvalidOperationException("The application associated with the authorization cannot be found."); @@ -147,6 +145,20 @@ namespace OpenIddict.EntityFramework return await CreateAsync(authorization, cancellationToken); } + /// + /// Retrieves an authorization using its unique identifier. + /// + /// The unique identifier associated with the authorization. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the authorization corresponding to the identifier. + /// + public override Task FindByIdAsync(string identifier, CancellationToken cancellationToken) + { + return Authorizations.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier)); + } + /// /// Executes the specified query. /// diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs index 3443c103..e3817327 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictTokenStore.cs @@ -142,9 +142,7 @@ namespace OpenIddict.EntityFramework // Bind the token to the specified client application, if applicable. if (!string.IsNullOrEmpty(descriptor.ApplicationId)) { - var key = ConvertIdentifierFromString(descriptor.ApplicationId); - - var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key)); + var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.ApplicationId)); if (application == null) { 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. if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) { - var key = ConvertIdentifierFromString(descriptor.AuthorizationId); - - var authorization = await Authorizations.SingleOrDefaultAsync(entity => entity.Id.Equals(key)); + var authorization = await Authorizations.FindAsync(cancellationToken, ConvertIdentifierFromString(descriptor.AuthorizationId)); if (authorization == null) { throw new InvalidOperationException("The authorization associated with the token cannot be found."); @@ -193,6 +189,20 @@ namespace OpenIddict.EntityFramework catch (DbUpdateConcurrencyException) { } } + /// + /// Retrieves an token using its unique identifier. + /// + /// The unique identifier associated with the token. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the token corresponding to the unique identifier. + /// + public override Task FindByIdAsync(string identifier, CancellationToken cancellationToken) + { + return Tokens.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier)); + } + /// /// Executes the specified query. /// @@ -251,15 +261,13 @@ namespace OpenIddict.EntityFramework if (!string.IsNullOrEmpty(identifier)) { - var key = ConvertIdentifierFromString(identifier); - - var authorization = await Authorizations.SingleOrDefaultAsync(element => element.Id.Equals(key)); + var authorization = await Authorizations.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier)); if (authorization == null) { throw new InvalidOperationException("The authorization associated with the token cannot be found."); } - authorization.Tokens.Add(token); + token.Authorization = authorization; } else @@ -268,7 +276,7 @@ namespace OpenIddict.EntityFramework // Try to retrieve the authorization associated with the token. // 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) { authorization.Tokens.Remove(token); @@ -294,15 +302,13 @@ namespace OpenIddict.EntityFramework if (!string.IsNullOrEmpty(identifier)) { - var key = ConvertIdentifierFromString(identifier); - - var application = await Applications.SingleOrDefaultAsync(element => element.Id.Equals(key)); + var application = await Applications.FindAsync(cancellationToken, ConvertIdentifierFromString(identifier)); if (application == null) { throw new InvalidOperationException("The application associated with the token cannot be found."); } - application.Tokens.Add(token); + token.Application = application; } else @@ -311,7 +317,7 @@ namespace OpenIddict.EntityFramework // Try to retrieve the application associated with the token. // 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) { application.Tokens.Remove(token); diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs index 0ba5a373..c33728a2 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictApplicationStore.cs @@ -155,6 +155,20 @@ namespace OpenIddict.EntityFrameworkCore catch (DbUpdateConcurrencyException) { } } + /// + /// Retrieves an application using its unique identifier. + /// + /// The unique identifier associated with the application. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the client application corresponding to the identifier. + /// + public override Task FindByIdAsync(string identifier, CancellationToken cancellationToken) + { + return Applications.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken); + } + /// /// Executes the specified query. /// diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs index bf938412..6acea907 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictAuthorizationStore.cs @@ -132,9 +132,7 @@ namespace OpenIddict.EntityFrameworkCore // Bind the authorization to the specified application, if applicable. if (!string.IsNullOrEmpty(descriptor.ApplicationId)) { - var key = ConvertIdentifierFromString(descriptor.ApplicationId); - - var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key)); + var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); if (application == null) { throw new InvalidOperationException("The application associated with the authorization cannot be found."); @@ -146,6 +144,20 @@ namespace OpenIddict.EntityFrameworkCore return await CreateAsync(authorization, cancellationToken); } + /// + /// Retrieves an authorization using its unique identifier. + /// + /// The unique identifier associated with the authorization. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the authorization corresponding to the identifier. + /// + public override Task FindByIdAsync(string identifier, CancellationToken cancellationToken) + { + return Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken); + } + /// /// Executes the specified query. /// diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs index 516d784f..34c593f4 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictTokenStore.cs @@ -141,9 +141,7 @@ namespace OpenIddict.EntityFrameworkCore // Bind the token to the specified client application, if applicable. if (!string.IsNullOrEmpty(descriptor.ApplicationId)) { - var key = ConvertIdentifierFromString(descriptor.ApplicationId); - - var application = await Applications.SingleOrDefaultAsync(entity => entity.Id.Equals(key)); + var application = await Applications.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.ApplicationId) }, cancellationToken); if (application == null) { 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. if (!string.IsNullOrEmpty(descriptor.AuthorizationId)) { - var key = ConvertIdentifierFromString(descriptor.AuthorizationId); - - var authorization = await Authorizations.SingleOrDefaultAsync(entity => entity.Id.Equals(key)); + var authorization = await Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(descriptor.AuthorizationId) }, cancellationToken); if (authorization == null) { throw new InvalidOperationException("The authorization associated with the token cannot be found."); @@ -192,6 +188,20 @@ namespace OpenIddict.EntityFrameworkCore catch (DbUpdateConcurrencyException) { } } + /// + /// Retrieves an token using its unique identifier. + /// + /// The unique identifier associated with the token. + /// The that can be used to abort the operation. + /// + /// A that can be used to monitor the asynchronous operation, + /// whose result returns the token corresponding to the unique identifier. + /// + public override Task FindByIdAsync(string identifier, CancellationToken cancellationToken) + { + return Tokens.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken); + } + /// /// Executes the specified query. /// @@ -250,15 +260,13 @@ namespace OpenIddict.EntityFrameworkCore if (!string.IsNullOrEmpty(identifier)) { - var key = ConvertIdentifierFromString(identifier); - - var authorization = await Authorizations.SingleOrDefaultAsync(element => element.Id.Equals(key)); + var authorization = await Authorizations.FindAsync(new object[] { ConvertIdentifierFromString(identifier) }, cancellationToken); if (authorization == null) { throw new InvalidOperationException("The authorization associated with the token cannot be found."); } - authorization.Tokens.Add(token); + token.Authorization = authorization; } else @@ -267,7 +275,7 @@ namespace OpenIddict.EntityFrameworkCore // Try to retrieve the authorization associated with the token. // 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) { authorization.Tokens.Remove(token); @@ -295,13 +303,13 @@ namespace OpenIddict.EntityFrameworkCore { 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) { throw new InvalidOperationException("The application associated with the token cannot be found."); } - application.Tokens.Add(token); + token.Application = application; } else @@ -310,7 +318,7 @@ namespace OpenIddict.EntityFrameworkCore // Try to retrieve the application associated with the token. // 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) { application.Tokens.Remove(token);