Skip to content

Commit

Permalink
Merge branch 'master' into dev/guidv7
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n authored Aug 6, 2024
2 parents 59add11 + a7ce845 commit ac4df08
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 44 deletions.
49 changes: 18 additions & 31 deletions src/Persistence/EntityFramework/EntityFrameworkContextBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
// </copyright>

using System.Threading;

namespace MUnique.OpenMU.Persistence.EntityFramework;

using System.Collections;
Expand Down Expand Up @@ -61,57 +63,43 @@ protected EntityFrameworkContextBase(DbContext context, IContextAwareRepositoryP
protected IContextAwareRepositoryProvider RepositoryProvider { get; }

/// <inheritdoc/>
public bool SaveChanges()
public async ValueTask<bool> SaveChangesAsync(CancellationToken cancellationToken = default)
{
using var l = this._lock.Lock();
using var l = await this._lock.LockAsync();

// when we have a change publisher attached, we want to get the changed entries before accepting them.
// Otherwise, we can accept them.
var acceptChanges = true;

object? sender = null;
SavedChangesEventArgs? args = null;
if (this._changeListener is { })
{
this.Context.SavedChanges += this.OnSavedChanges;
this.Context.SavedChanges += OnSavedChanges;
acceptChanges = false;
}

try
{
this.Context.SaveChanges(acceptChanges);
await this.Context.SaveChangesAsync(acceptChanges, cancellationToken).ConfigureAwait(false);

if (args is not null)
{
await this.OnSavedChangesAsync(sender, args).ConfigureAwait(false);
}
}
finally
{
this.Context.SavedChanges -= this.OnSavedChanges;
this.Context.SavedChanges -= OnSavedChanges;
}

return true;
}

/// <inheritdoc/>
public async ValueTask<bool> SaveChangesAsync()
{
using var l = await this._lock.LockAsync();

// when we have a change publisher attached, we want to get the changed entries before accepting them.
// Otherwise, we can accept them.
var acceptChanges = true;

if (this._changeListener is { })
void OnSavedChanges(object? s, SavedChangesEventArgs e)
{
this.Context.SavedChanges += this.OnSavedChanges;
acceptChanges = false;
sender = s;
args = e;
}

try
{
await this.Context.SaveChangesAsync(acceptChanges).ConfigureAwait(false);
}
finally
{
this.Context.SavedChanges -= this.OnSavedChanges;
}

return true;
}

/// <inheritdoc />
Expand Down Expand Up @@ -270,7 +258,6 @@ protected virtual void Dispose(bool dispose)
return;
}

this.Context.SavedChanges -= this.OnSavedChanges;
this.Context.Dispose();
}

Expand Down Expand Up @@ -319,7 +306,7 @@ private void ForEachAggregate(object obj, Action<object> action)
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "Catching all Exceptions.")]
private async void OnSavedChanges(object? sender, SavedChangesEventArgs e)
private async ValueTask OnSavedChangesAsync(object? sender, SavedChangesEventArgs e)
{
try
{
Expand Down
18 changes: 8 additions & 10 deletions src/Persistence/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MUnique.OpenMU.Persistence;

using System.Collections;
using System.Threading;

/// <summary>
/// The context for repository actions.
Expand All @@ -19,14 +20,11 @@ public interface IContext : IDisposable
/// <summary>
/// Saves the changes of the context.
/// </summary>
/// <returns><c>True</c>, if the saving was successful; <c>false</c>, otherwise.</returns>
bool SaveChanges();

/// <summary>
/// Saves the changes of the context.
/// </summary>
/// <returns><c>True</c>, if the saving was successful; <c>false</c>, otherwise.</returns>
ValueTask<bool> SaveChangesAsync();
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>
/// <c>True</c>, if the saving was successful; <c>false</c>, otherwise.
/// </returns>
ValueTask<bool> SaveChangesAsync(CancellationToken cancellationToken = default);

/// <summary>
/// Detaches the specified item from the context, if required.
Expand All @@ -35,7 +33,7 @@ public interface IContext : IDisposable
/// <param name="item">The item which should be detached.</param>
/// <returns><c>true</c>, if the object was persisted.</returns>
/// <remarks>
/// When calling this method, be sure to clear a back reference property before. Otherwise you might detach more than you intended.
/// When calling this method, be sure to clear a back reference property before. Otherwise, you might detach more than you intended.
/// </remarks>
bool Detach(object item);

Expand All @@ -45,7 +43,7 @@ public interface IContext : IDisposable
/// </summary>
/// <param name="item">The item which should be attached.</param>
/// <remarks>
/// When calling this method, be sure to clear a previous back reference property before. Otherwise you might attach more than you intended.
/// When calling this method, be sure to clear a previous back reference property before. Otherwise, you might attach more than you intended.
/// </remarks>
void Attach(object item);

Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/InMemory/InMemoryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public bool SaveChanges()
}

/// <inheritdoc/>
public async ValueTask<bool> SaveChangesAsync()
public async ValueTask<bool> SaveChangesAsync(CancellationToken cancellationToken = default)
{
var result = this.SaveChanges();
if (result)
Expand Down
4 changes: 2 additions & 2 deletions src/Startup/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ private ICollection<PlugInConfiguration> PlugInConfigurationsFactory(IServicePro
if (typesWithMissingCustomConfigs.Any())
{
typesWithMissingCustomConfigs.ForEach(c => this.CreateDefaultPlugInConfiguration(typesWithCustomConfig[c.TypeId]!, c, referenceHandler));
context.SaveChanges();
_ = context.SaveChangesAsync().AsTask().WaitAndUnwrapException();
}

var typesWithMissingConfigs = pluginManager.KnownPlugInTypes.Where(t => configs.All(c => c.TypeId != t.GUID)).ToList();
Expand Down Expand Up @@ -378,7 +378,7 @@ private IEnumerable<PlugInConfiguration> CreateMissingPlugInConfigurations(IEnum
yield return plugInConfiguration;
}

saveContext.SaveChanges();
_ = saveContext.SaveChangesAsync().AsTask().WaitAndUnwrapException();
}

private void CreateDefaultPlugInConfiguration(Type plugInType, PlugInConfiguration plugInConfiguration, ReferenceHandler referenceHandler)
Expand Down

0 comments on commit ac4df08

Please sign in to comment.