1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-28 06:22:54 +08:00
osu-lazer/osu.Game/Database/DatabaseWriteUsage.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.6 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
2018-02-12 16:55:11 +08:00
using System;
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
2018-02-12 16:55:11 +08:00
namespace osu.Game.Database
{
public class DatabaseWriteUsage : IDisposable
{
public readonly OsuDbContext Context;
private readonly Action<DatabaseWriteUsage> usageCompleted;
2018-04-13 17:19:50 +08:00
2018-02-12 16:55:11 +08:00
public DatabaseWriteUsage(OsuDbContext context, Action<DatabaseWriteUsage> onCompleted)
{
Context = context;
usageCompleted = onCompleted;
}
2018-04-13 17:19:50 +08:00
public bool PerformedWrite { get; private set; }
2018-04-13 17:19:50 +08:00
private bool isDisposed;
public List<Exception> Errors = new List<Exception>();
/// <summary>
/// Whether this write usage will commit a transaction on completion.
/// If false, there is a parent usage responsible for transaction commit.
/// </summary>
public bool IsTransactionLeader;
2018-04-13 17:19:50 +08:00
protected void Dispose(bool disposing)
2018-02-12 16:55:11 +08:00
{
if (isDisposed) return;
2019-02-28 12:31:40 +08:00
isDisposed = true;
2018-04-13 17:19:50 +08:00
try
{
PerformedWrite |= Context.SaveChanges() > 0;
}
catch (Exception e)
{
Errors.Add(e);
2018-05-29 13:25:27 +08:00
throw;
}
finally
{
usageCompleted?.Invoke(this);
}
2018-02-12 16:55:11 +08:00
}
2018-04-13 17:19:50 +08:00
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
2018-02-12 16:55:11 +08:00
}
}