Skip to content

Commit

Permalink
Replace unnecesary calls to SaveChangesAsync by FlushAsync (#17084)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienros authored Nov 28, 2024
1 parent 00b6efd commit d016259
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 24 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ dotnet_diagnostic.IDE0130.severity = silent # Na
dotnet_style_prefer_conditional_expression_over_assignment = silent # Remove redundant equality, e.g. Disabled == false

dotnet_diagnostic.CA2263.severity = warning # Prefer generic overload when type is known
dotnet_diagnostic.CA1816.severity = none # Dispose methods should call SuppressFinalize

# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true
Expand Down
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Html/Migrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public async Task<int> UpdateFrom3Async()
lastDocumentId = contentItemVersion.Id;
}

await _session.SaveChangesAsync();
await _session.FlushAsync();
}

static bool UpdateBody(JsonNode content)
Expand Down
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Title/Migrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public async Task<int> UpdateFrom1()
lastDocumentId = contentItemVersion.Id;
}

await _session.SaveChangesAsync();
await _session.FlushAsync();
}

static bool UpdateTitle(JsonNode content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public virtual async ValueTask CreateAsync(TApplication application, Cancellatio
cancellationToken.ThrowIfCancellationRequested();

await _session.SaveAsync(application, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand All @@ -61,7 +61,7 @@ public virtual async ValueTask DeleteAsync(TApplication application, Cancellatio
cancellationToken.ThrowIfCancellationRequested();

_session.Delete(application, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public virtual async ValueTask CreateAsync(TAuthorization authorization, Cancell
cancellationToken.ThrowIfCancellationRequested();

await _session.SaveAsync(authorization, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand All @@ -55,7 +55,7 @@ public virtual async ValueTask DeleteAsync(TAuthorization authorization, Cancell
cancellationToken.ThrowIfCancellationRequested();

_session.Delete(authorization, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -319,7 +319,7 @@ public virtual async ValueTask<long> PruneAsync(DateTimeOffset threshold, Cancel

try
{
await _session.SaveChangesAsync();
await _session.FlushAsync();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -440,7 +440,7 @@ public virtual async ValueTask UpdateAsync(TAuthorization authorization, Cancell

try
{
await _session.SaveChangesAsync();
await _session.FlushAsync();
}
catch (ConcurrencyException exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public virtual async ValueTask CreateAsync(TScope scope, CancellationToken cance
cancellationToken.ThrowIfCancellationRequested();

await _session.SaveAsync(scope, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand All @@ -54,7 +54,7 @@ public virtual async ValueTask DeleteAsync(TScope scope, CancellationToken cance
cancellationToken.ThrowIfCancellationRequested();

_session.Delete(scope, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -326,7 +326,7 @@ public virtual async ValueTask UpdateAsync(TScope scope, CancellationToken cance

try
{
await _session.SaveChangesAsync();
await _session.FlushAsync();
}
catch (ConcurrencyException exception)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public virtual async ValueTask CreateAsync(TToken token, CancellationToken cance
cancellationToken.ThrowIfCancellationRequested();

await _session.SaveAsync(token, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand All @@ -55,7 +55,7 @@ public virtual async ValueTask DeleteAsync(TToken token, CancellationToken cance
cancellationToken.ThrowIfCancellationRequested();

_session.Delete(token, collection: OpenIdCollection);
await _session.SaveChangesAsync();
await _session.FlushAsync();
}

/// <inheritdoc/>
Expand Down Expand Up @@ -354,7 +354,7 @@ public virtual async ValueTask<long> PruneAsync(DateTimeOffset threshold, Cancel

try
{
await _session.SaveChangesAsync();
await _session.FlushAsync();
}
catch (Exception exception)
{
Expand Down Expand Up @@ -399,7 +399,7 @@ public virtual async ValueTask<long> RevokeByAuthorizationIdAsync(string identif
await _session.SaveAsync(token, checkConcurrency: false, collection: OpenIdCollection);
}

await _session.SaveChangesAsync();
await _session.FlushAsync();

return tokens.Count;
}
Expand Down Expand Up @@ -546,7 +546,7 @@ public virtual async ValueTask UpdateAsync(TToken token, CancellationToken cance

try
{
await _session.SaveChangesAsync();
await _session.FlushAsync();
}
catch (ConcurrencyException exception)
{
Expand Down
7 changes: 3 additions & 4 deletions src/OrchardCore/OrchardCore.Users.Core/Services/UserStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public UserStore(ISession session,

public void Dispose()
{
GC.SuppressFinalize(this);
}

public string NormalizeKey(string key)
Expand Down Expand Up @@ -111,7 +110,7 @@ public async Task<IdentityResult> CreateAsync(IUser user, CancellationToken canc
}

await _session.SaveAsync(user);
await _session.SaveChangesAsync();
await _session.FlushAsync();
await Handlers.InvokeAsync((handler, context) => handler.CreatedAsync(context), context, _logger);
}
catch (Exception e)
Expand Down Expand Up @@ -139,7 +138,7 @@ public async Task<IdentityResult> DeleteAsync(IUser user, CancellationToken canc
}

_session.Delete(user);
await _session.SaveChangesAsync();
await _session.FlushAsync();
await Handlers.InvokeAsync((handler, context) => handler.DeletedAsync(context), context, _logger);
}
catch (Exception e)
Expand Down Expand Up @@ -232,7 +231,7 @@ public async Task<IdentityResult> UpdateAsync(IUser user, CancellationToken canc
}

await _session.SaveAsync(user);
await _session.SaveChangesAsync();
await _session.FlushAsync();
await Handlers.InvokeAsync((handler, context) => handler.UpdatedAsync(context), context, _logger);
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,7 @@ public void Build()
_inner = _services.BuildServiceProvider();
}

#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
public void Dispose()
#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
{
(_inner as IDisposable)?.Dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,7 @@ public async Task DerivedThemesCanOverrideBaseThemeShapeBindings()
Assert.Equal("DerivedTheme", table.Descriptors["OverriddenShape"].BindingSource);
}

#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
public void Dispose()
#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
{
(_serviceProvider as IDisposable)?.Dispose();
}
Expand Down

0 comments on commit d016259

Please sign in to comment.