Add equality support to ILive types

This commit is contained in:
Dean Herbert
2021-11-26 14:38:39 +09:00
parent 3bc8f21935
commit 13612c0d02
5 changed files with 13 additions and 7 deletions

View File

@ -6,7 +6,6 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
using osu.Game.Beatmaps;
using osu.Game.Database; using osu.Game.Database;
using osu.Game.Models; using osu.Game.Models;
using Realms; using Realms;
@ -18,15 +17,15 @@ namespace osu.Game.Tests.Database
public class RealmLiveTests : RealmTest public class RealmLiveTests : RealmTest
{ {
[Test] [Test]
public void TestLiveCastability() public void TestLiveEquality()
{ {
RunTestWithRealm((realmFactory, _) => RunTestWithRealm((realmFactory, _) =>
{ {
RealmLive<RealmBeatmap> beatmap = realmFactory.CreateContext().Write(r => r.Add(new RealmBeatmap(CreateRuleset(), new RealmBeatmapDifficulty(), new RealmBeatmapMetadata()))).ToLive(); ILive<RealmBeatmap> beatmap = realmFactory.CreateContext().Write(r => r.Add(new RealmBeatmap(CreateRuleset(), new RealmBeatmapDifficulty(), new RealmBeatmapMetadata()))).ToLive();
ILive<IBeatmapInfo> iBeatmap = beatmap; ILive<RealmBeatmap> beatmap2 = realmFactory.CreateContext().All<RealmBeatmap>().First().ToLive();
Assert.AreEqual(0, iBeatmap.Value.Length); Assert.AreEqual(beatmap, beatmap2);
}); });
} }

View File

@ -3,6 +3,8 @@
using System; using System;
#nullable enable
namespace osu.Game.Database namespace osu.Game.Database
{ {
public class EntityFrameworkLive<T> : ILive<T> where T : class public class EntityFrameworkLive<T> : ILive<T> where T : class
@ -30,5 +32,7 @@ namespace osu.Game.Database
} }
public T Value { get; } public T Value { get; }
public bool Equals(ILive<T>? other) => ID == other?.ID;
} }
} }

View File

@ -9,7 +9,8 @@ namespace osu.Game.Database
/// A wrapper to provide access to database backed classes in a thread-safe manner. /// A wrapper to provide access to database backed classes in a thread-safe manner.
/// </summary> /// </summary>
/// <typeparam name="T">The databased type.</typeparam> /// <typeparam name="T">The databased type.</typeparam>
public interface ILive<out T> where T : class // TODO: Add IHasGuidPrimaryKey once we don't need EF support any more. public interface ILive<T> : IEquatable<ILive<T>>
where T : class // TODO: Add IHasGuidPrimaryKey once we don't need EF support any more.
{ {
Guid ID { get; } Guid ID { get; }

View File

@ -8,7 +8,7 @@ using System.Collections.Generic;
namespace osu.Game.Database namespace osu.Game.Database
{ {
public interface IPostImports<out TModel> public interface IPostImports<TModel>
where TModel : class where TModel : class
{ {
/// <summary> /// <summary>

View File

@ -107,5 +107,7 @@ namespace osu.Game.Database
// this matches realm's internal thread validation (see https://github.com/realm/realm-dotnet/blob/903b4d0b304f887e37e2d905384fb572a6496e70/Realm/Realm/Native/SynchronizationContextScheduler.cs#L72) // this matches realm's internal thread validation (see https://github.com/realm/realm-dotnet/blob/903b4d0b304f887e37e2d905384fb572a6496e70/Realm/Realm/Native/SynchronizationContextScheduler.cs#L72)
private bool isCorrectThread private bool isCorrectThread
=> (fetchedContext != null && SynchronizationContext.Current == fetchedContext) || fetchedThreadId == Thread.CurrentThread.ManagedThreadId; => (fetchedContext != null && SynchronizationContext.Current == fetchedContext) || fetchedThreadId == Thread.CurrentThread.ManagedThreadId;
public bool Equals(ILive<T>? other) => ID == other?.ID;
} }
} }