Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
use asynchronous EF methods #2067
Browse files Browse the repository at this point in the history
  • Loading branch information
brockallen committed Dec 10, 2019
1 parent 417737a commit f8b29b3
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 82 deletions.
26 changes: 13 additions & 13 deletions src/EntityFramework.Storage/src/Stores/ClientStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,30 +49,30 @@ public ClientStore(IConfigurationDbContext context, ILogger<ClientStore> logger)
/// <returns>
/// The client
/// </returns>
public virtual Task<Client> FindClientByIdAsync(string clientId)
public virtual async Task<Client> FindClientByIdAsync(string clientId)
{
IQueryable<Entities.Client> baseQuery = Context.Clients
.Where(x => x.ClientId == clientId)
.Take(1);

var client = baseQuery.FirstOrDefault();
if (client == null) return Task.FromResult<Client>(null);
var client = await baseQuery.FirstOrDefaultAsync();
if (client == null) return null;

baseQuery.Include(x => x.AllowedCorsOrigins).SelectMany(c => c.AllowedCorsOrigins).Load();
baseQuery.Include(x => x.AllowedGrantTypes).SelectMany(c => c.AllowedGrantTypes).Load();
baseQuery.Include(x => x.AllowedScopes).SelectMany(c => c.AllowedScopes).Load();
baseQuery.Include(x => x.Claims).SelectMany(c => c.Claims).Load();
baseQuery.Include(x => x.ClientSecrets).SelectMany(c => c.ClientSecrets).Load();
baseQuery.Include(x => x.IdentityProviderRestrictions).SelectMany(c => c.IdentityProviderRestrictions).Load();
baseQuery.Include(x => x.PostLogoutRedirectUris).SelectMany(c => c.PostLogoutRedirectUris).Load();
baseQuery.Include(x => x.Properties).SelectMany(c => c.Properties).Load();
baseQuery.Include(x => x.RedirectUris).SelectMany(c => c.RedirectUris).Load();
await baseQuery.Include(x => x.AllowedCorsOrigins).SelectMany(c => c.AllowedCorsOrigins).LoadAsync();
await baseQuery.Include(x => x.AllowedGrantTypes).SelectMany(c => c.AllowedGrantTypes).LoadAsync();
await baseQuery.Include(x => x.AllowedScopes).SelectMany(c => c.AllowedScopes).LoadAsync();
await baseQuery.Include(x => x.Claims).SelectMany(c => c.Claims).LoadAsync();
await baseQuery.Include(x => x.ClientSecrets).SelectMany(c => c.ClientSecrets).LoadAsync();
await baseQuery.Include(x => x.IdentityProviderRestrictions).SelectMany(c => c.IdentityProviderRestrictions).LoadAsync();
await baseQuery.Include(x => x.PostLogoutRedirectUris).SelectMany(c => c.PostLogoutRedirectUris).LoadAsync();
await baseQuery.Include(x => x.Properties).SelectMany(c => c.Properties).LoadAsync();
await baseQuery.Include(x => x.RedirectUris).SelectMany(c => c.RedirectUris).LoadAsync();

var model = client.ToModel();

Logger.LogDebug("{clientId} found in database: {clientIdFound}", clientId, model != null);

return Task.FromResult(model);
return model;
}
}
}
34 changes: 14 additions & 20 deletions src/EntityFramework.Storage/src/Stores/DeviceFlowStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,43 +60,41 @@ public DeviceFlowStore(
/// <param name="userCode">The user code.</param>
/// <param name="data">The data.</param>
/// <returns></returns>
public virtual Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, DeviceCode data)
public virtual async Task StoreDeviceAuthorizationAsync(string deviceCode, string userCode, DeviceCode data)
{
Context.DeviceFlowCodes.Add(ToEntity(data, deviceCode, userCode));

Context.SaveChanges();

return Task.FromResult(0);
await Context.SaveChangesAsync();
}

/// <summary>
/// Finds device authorization by user code.
/// </summary>
/// <param name="userCode">The user code.</param>
/// <returns></returns>
public virtual Task<DeviceCode> FindByUserCodeAsync(string userCode)
public virtual async Task<DeviceCode> FindByUserCodeAsync(string userCode)
{
var deviceFlowCodes = Context.DeviceFlowCodes.AsNoTracking().FirstOrDefault(x => x.UserCode == userCode);
var deviceFlowCodes = await Context.DeviceFlowCodes.AsNoTracking().FirstOrDefaultAsync(x => x.UserCode == userCode);
var model = ToModel(deviceFlowCodes?.Data);

Logger.LogDebug("{userCode} found in database: {userCodeFound}", userCode, model != null);

return Task.FromResult(model);
return model;
}

/// <summary>
/// Finds device authorization by device code.
/// </summary>
/// <param name="deviceCode">The device code.</param>
/// <returns></returns>
public virtual Task<DeviceCode> FindByDeviceCodeAsync(string deviceCode)
public virtual async Task<DeviceCode> FindByDeviceCodeAsync(string deviceCode)
{
var deviceFlowCodes = Context.DeviceFlowCodes.AsNoTracking().FirstOrDefault(x => x.DeviceCode == deviceCode);
var deviceFlowCodes = await Context.DeviceFlowCodes.AsNoTracking().FirstOrDefaultAsync(x => x.DeviceCode == deviceCode);
var model = ToModel(deviceFlowCodes?.Data);

Logger.LogDebug("{deviceCode} found in database: {deviceCodeFound}", deviceCode, model != null);

return Task.FromResult(model);
return model;
}

/// <summary>
Expand All @@ -105,9 +103,9 @@ public virtual Task<DeviceCode> FindByDeviceCodeAsync(string deviceCode)
/// <param name="userCode">The user code.</param>
/// <param name="data">The data.</param>
/// <returns></returns>
public virtual Task UpdateByUserCodeAsync(string userCode, DeviceCode data)
public virtual async Task UpdateByUserCodeAsync(string userCode, DeviceCode data)
{
var existing = Context.DeviceFlowCodes.SingleOrDefault(x => x.UserCode == userCode);
var existing = await Context.DeviceFlowCodes.SingleOrDefaultAsync(x => x.UserCode == userCode);
if (existing == null)
{
Logger.LogError("{userCode} not found in database", userCode);
Expand All @@ -122,24 +120,22 @@ public virtual Task UpdateByUserCodeAsync(string userCode, DeviceCode data)

try
{
Context.SaveChanges();
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
Logger.LogWarning("exception updating {userCode} user code in database: {error}", userCode, ex.Message);
}

return Task.FromResult(0);
}

/// <summary>
/// Removes the device authorization, searching by device code.
/// </summary>
/// <param name="deviceCode">The device code.</param>
/// <returns></returns>
public virtual Task RemoveByDeviceCodeAsync(string deviceCode)
public virtual async Task RemoveByDeviceCodeAsync(string deviceCode)
{
var deviceFlowCodes = Context.DeviceFlowCodes.FirstOrDefault(x => x.DeviceCode == deviceCode);
var deviceFlowCodes = await Context.DeviceFlowCodes.FirstOrDefaultAsync(x => x.DeviceCode == deviceCode);

if(deviceFlowCodes != null)
{
Expand All @@ -149,7 +145,7 @@ public virtual Task RemoveByDeviceCodeAsync(string deviceCode)

try
{
Context.SaveChanges();
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
Expand All @@ -160,8 +156,6 @@ public virtual Task RemoveByDeviceCodeAsync(string deviceCode)
{
Logger.LogDebug("no {deviceCode} device code found in database", deviceCode);
}

return Task.FromResult(0);
}

/// <summary>
Expand Down
46 changes: 19 additions & 27 deletions src/EntityFramework.Storage/src/Stores/PersistedGrantStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public PersistedGrantStore(IPersistedGrantDbContext context, ILogger<PersistedGr
/// </summary>
/// <param name="token">The token.</param>
/// <returns></returns>
public virtual Task StoreAsync(PersistedGrant token)
public virtual async Task StoreAsync(PersistedGrant token)
{
var existing = Context.PersistedGrants.SingleOrDefault(x => x.Key == token.Key);
var existing = await Context.PersistedGrants.SingleOrDefaultAsync(x => x.Key == token.Key);
if (existing == null)
{
Logger.LogDebug("{persistedGrantKey} not found in database", token.Key);
Expand All @@ -65,54 +65,52 @@ public virtual Task StoreAsync(PersistedGrant token)

try
{
Context.SaveChanges();
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
Logger.LogWarning("exception updating {persistedGrantKey} persisted grant in database: {error}", token.Key, ex.Message);
}

return Task.FromResult(0);
}

/// <summary>
/// Gets the grant.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public virtual Task<PersistedGrant> GetAsync(string key)
public virtual async Task<PersistedGrant> GetAsync(string key)
{
var persistedGrant = Context.PersistedGrants.AsNoTracking().FirstOrDefault(x => x.Key == key);
var persistedGrant = await Context.PersistedGrants.AsNoTracking().FirstOrDefaultAsync(x => x.Key == key);
var model = persistedGrant?.ToModel();

Logger.LogDebug("{persistedGrantKey} found in database: {persistedGrantKeyFound}", key, model != null);

return Task.FromResult(model);
return model;
}

/// <summary>
/// Gets all grants for a given subject id.
/// </summary>
/// <param name="subjectId">The subject identifier.</param>
/// <returns></returns>
public virtual Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
public virtual async Task<IEnumerable<PersistedGrant>> GetAllAsync(string subjectId)
{
var persistedGrants = Context.PersistedGrants.Where(x => x.SubjectId == subjectId).AsNoTracking().ToList();
var persistedGrants = await Context.PersistedGrants.Where(x => x.SubjectId == subjectId).AsNoTracking().ToListAsync();
var model = persistedGrants.Select(x => x.ToModel());

Logger.LogDebug("{persistedGrantCount} persisted grants found for {subjectId}", persistedGrants.Count, subjectId);

return Task.FromResult(model);
return model;
}

/// <summary>
/// Removes the grant by key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public virtual Task RemoveAsync(string key)
public virtual async Task RemoveAsync(string key)
{
var persistedGrant = Context.PersistedGrants.FirstOrDefault(x => x.Key == key);
var persistedGrant = await Context.PersistedGrants.FirstOrDefaultAsync(x => x.Key == key);
if (persistedGrant!= null)
{
Logger.LogDebug("removing {persistedGrantKey} persisted grant from database", key);
Expand All @@ -121,7 +119,7 @@ public virtual Task RemoveAsync(string key)

try
{
Context.SaveChanges();
await Context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException ex)
{
Expand All @@ -132,8 +130,6 @@ public virtual Task RemoveAsync(string key)
{
Logger.LogDebug("no {persistedGrantKey} persisted grant found in database", key);
}

return Task.FromResult(0);
}

/// <summary>
Expand All @@ -142,24 +138,22 @@ public virtual Task RemoveAsync(string key)
/// <param name="subjectId">The subject identifier.</param>
/// <param name="clientId">The client identifier.</param>
/// <returns></returns>
public virtual Task RemoveAllAsync(string subjectId, string clientId)
public virtual async Task RemoveAllAsync(string subjectId, string clientId)
{
var persistedGrants = Context.PersistedGrants.Where(x => x.SubjectId == subjectId && x.ClientId == clientId).ToList();
var persistedGrants = await Context.PersistedGrants.Where(x => x.SubjectId == subjectId && x.ClientId == clientId).ToListAsync();

Logger.LogDebug("removing {persistedGrantCount} persisted grants from database for subject {subjectId}, clientId {clientId}", persistedGrants.Count, subjectId, clientId);

Context.PersistedGrants.RemoveRange(persistedGrants);

try
{
Context.SaveChanges();
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
Logger.LogInformation("removing {persistedGrantCount} persisted grants from database for subject {subjectId}, clientId {clientId}: {error}", persistedGrants.Count, subjectId, clientId, ex.Message);
}

return Task.FromResult(0);
}

/// <summary>
Expand All @@ -169,27 +163,25 @@ public virtual Task RemoveAllAsync(string subjectId, string clientId)
/// <param name="clientId">The client identifier.</param>
/// <param name="type">The type.</param>
/// <returns></returns>
public virtual Task RemoveAllAsync(string subjectId, string clientId, string type)
public virtual async Task RemoveAllAsync(string subjectId, string clientId, string type)
{
var persistedGrants = Context.PersistedGrants.Where(x =>
var persistedGrants = await Context.PersistedGrants.Where(x =>
x.SubjectId == subjectId &&
x.ClientId == clientId &&
x.Type == type).ToList();
x.Type == type).ToListAsync();

Logger.LogDebug("removing {persistedGrantCount} persisted grants from database for subject {subjectId}, clientId {clientId}, grantType {persistedGrantType}", persistedGrants.Count, subjectId, clientId, type);

Context.PersistedGrants.RemoveRange(persistedGrants);

try
{
Context.SaveChanges();
await Context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
Logger.LogInformation("exception removing {persistedGrantCount} persisted grants from database for subject {subjectId}, clientId {clientId}, grantType {persistedGrantType}: {error}", persistedGrants.Count, subjectId, clientId, type, ex.Message);
}

return Task.FromResult(0);
}
}
}
Loading

0 comments on commit f8b29b3

Please sign in to comment.