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

62 lines
1.6 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Database
{
public class DatabaseWriteUsage : IDisposable
{
public readonly OsuDbContext Context;
private readonly Action<DatabaseWriteUsage> usageCompleted;
public DatabaseWriteUsage(OsuDbContext context, Action<DatabaseWriteUsage> onCompleted)
{
Context = context;
usageCompleted = onCompleted;
}
public bool PerformedWrite { get; private set; }
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 = false;
2018-04-13 17:19:50 +08:00
protected void Dispose(bool disposing)
{
if (isDisposed) return;
isDisposed = true;
try
{
PerformedWrite |= Context.SaveChanges() > 0;
}
catch (Exception e)
{
Errors.Add(e);
}
finally
{
usageCompleted?.Invoke(this);
}
2018-04-13 17:19:50 +08:00
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~DatabaseWriteUsage()
{
Dispose(false);
}
}
}