Skip to content

Commit 57ce5bc

Browse files
rmunnhahn-kev
andauthored
Make IChangeContext interface (#26)
* Create IChangeContext interface in core Commit property in interface is CommitBase rather than Commit. * Include harmony core in global usings * Remove now-unneeded `using SIL.Harmony.Core` lines * Create IObjectSnapshot interface in core ObjectSnapshot relies on too many things from the DB to be moved to core easily, so we leave it in SIL.Harmony.Db and create an interface in core instead. Then the only difference is that the type of Commit will be different depending on whether you access it via the interface (in which case you'll get a CommitBase from core) or via the implementation class (in which case you'll get a Commit object from SIL.Harmony). * Tweak IChangeContext to use IObjectSnapshot * Use interface in NewEntity and ApplyChange The Change.NewEntity and Change.ApplyChange methods, as well as the overriding/implementing methods in all Change subclasses, should now take an IChangeContext so that it can be mocked for testing. * Update RemoveReference methods to use CommitBase Now that IObjectBase uses CommitBase in RemoveReference, its implementing classes need to use the base type too. * Pull GetObjectsReferencing into IChangeContext * Remove un-needed GetCurrent method from ChangeContext --------- Co-authored-by: Kevin Hahn <kevin_hahn@sil.org>
1 parent a179986 commit 57ce5bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+124
-142
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class SetWordTextChange(Guid entityId, string text) : Change<Word>(entity
6969
{
7070
public string Text { get; } = text;
7171

72-
public override ValueTask<IObjectBase> NewEntity(Commit commit, ChangeContext context)
72+
public override ValueTask<IObjectBase> NewEntity(Commit commit, IChangeContext context)
7373
{
7474
return new(new Word()
7575
{
@@ -79,7 +79,7 @@ public class SetWordTextChange(Guid entityId, string text) : Change<Word>(entity
7979
}
8080

8181

82-
public override ValueTask ApplyChange(Word entity, ChangeContext context)
82+
public override ValueTask ApplyChange(Word entity, IChangeContext context)
8383
{
8484
entity.Text = Text;
8585
return ValueTask.CompletedTask;
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace SIL.Harmony.Core;
2+
3+
public interface IChangeContext
4+
{
5+
public CommitBase Commit { get; }
6+
ValueTask<IObjectSnapshot?> GetSnapshot(Guid entityId);
7+
public async ValueTask<T?> GetCurrent<T>(Guid entityId) where T : class
8+
{
9+
var snapshot = await GetSnapshot(entityId);
10+
if (snapshot is null) return null;
11+
return (T) snapshot.Entity.DbObject;
12+
}
13+
14+
public async ValueTask<bool> IsObjectDeleted(Guid entityId) => (await GetSnapshot(entityId))?.EntityIsDeleted ?? true;
15+
internal IObjectBase Adapt(object obj);
16+
IAsyncEnumerable<object> GetObjectsReferencing(Guid entityId, bool includeDeleted = false);
17+
}

src/SIL.Harmony.Core/IObjectBase.cs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace SIL.Harmony.Core;
4+
5+
[JsonPolymorphic]
6+
public interface IObjectBase
7+
{
8+
Guid Id { get; }
9+
DateTimeOffset? DeletedAt { get; set; }
10+
11+
/// <summary>
12+
/// provides the references this object has to other objects, when those objects are deleted
13+
/// <see cref="RemoveReference"/> will be called to remove the reference
14+
/// </summary>
15+
/// <returns></returns>
16+
public Guid[] GetReferences();
17+
/// <summary>
18+
/// remove a reference to another object, in some cases this may cause this object to be deleted
19+
/// </summary>
20+
/// <param name="id">id of the deleted object</param>
21+
/// <param name="commit">
22+
/// commit where the reference was removed
23+
/// should be used to set the deleted date for this object
24+
/// </param>
25+
public void RemoveReference(Guid id, CommitBase commit);
26+
27+
public IObjectBase Copy();
28+
/// <summary>
29+
/// the name of the object type, this is used to discriminate between different types of objects in the snapshots table
30+
/// </summary>
31+
/// <returns>a stable type name of this object, should not change over time</returns>
32+
public string GetObjectTypeName();
33+
[JsonIgnore]
34+
public object DbObject { get; }
35+
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace SIL.Harmony.Core;
2+
3+
public interface IObjectSnapshot
4+
{
5+
Guid Id { get; }
6+
string TypeName { get; }
7+
IObjectBase Entity { get; }
8+
Guid[] References { get; }
9+
Guid EntityId { get; }
10+
bool EntityIsDeleted { get; }
11+
Guid CommitId { get; }
12+
CommitBase Commit { get; }
13+
bool IsRoot { get; }
14+
}

src/SIL.Harmony.Core/QueryHelpers.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.EntityFrameworkCore;
1+
using Microsoft.EntityFrameworkCore;
22

33
namespace SIL.Harmony.Core;
44

src/SIL.Harmony.Sample/Changes/AddAntonymReferenceChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class AddAntonymReferenceChange(Guid entityId, Guid antonymId)
99
{
1010
public Guid AntonymId { get; set; } = antonymId;
1111

12-
public override async ValueTask ApplyChange(Word entity, ChangeContext context)
12+
public override async ValueTask ApplyChange(Word entity, IChangeContext context)
1313
{
1414
//if the word being referenced was deleted before this change was applied (could happen after a sync)
1515
//then we don't want to apply the change

src/SIL.Harmony.Sample/Changes/AddWordImageChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class AddWordImageChange(Guid entityId, Guid imageId) : EditChange<Word>(
88
{
99
public Guid ImageId { get; } = imageId;
1010

11-
public override async ValueTask ApplyChange(Word entity, ChangeContext context)
11+
public override async ValueTask ApplyChange(Word entity, IChangeContext context)
1212
{
1313
if (!await context.IsObjectDeleted(ImageId)) entity.ImageResourceId = ImageId;
1414
}

src/SIL.Harmony.Sample/Changes/EditExampleChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public EditExampleChange(Guid entityId, string updateBlob) : base(entityId)
2626

2727
public string UpdateBlob { get; set; }
2828

29-
public override ValueTask ApplyChange(Example entity, ChangeContext context)
29+
public override ValueTask ApplyChange(Example entity, IChangeContext context)
3030
{
3131
entity.YText.Doc.ApplyUpdateV2(Convert.FromBase64String(UpdateBlob));
3232
return ValueTask.CompletedTask;

src/SIL.Harmony.Sample/Changes/NewDefinitionChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class NewDefinitionChange(Guid entityId) : CreateChange<Definition>(entit
1212
public required double Order { get; set; }
1313
public required Guid WordId { get; init; }
1414

15-
public override async ValueTask<Definition> NewEntity(Commit commit, ChangeContext context)
15+
public override async ValueTask<Definition> NewEntity(Commit commit, IChangeContext context)
1616
{
1717
return new Definition
1818
{

src/SIL.Harmony.Sample/Changes/NewExampleChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private NewExampleChange(Guid entityId) : base(entityId)
3434
{
3535
}
3636

37-
public override async ValueTask<Example> NewEntity(Commit commit, ChangeContext context)
37+
public override async ValueTask<Example> NewEntity(Commit commit, IChangeContext context)
3838
{
3939
return new Example
4040
{

src/SIL.Harmony.Sample/Changes/NewWordChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class NewWordChange(Guid entityId, string text, string? note = null, Guid
1010
public string? Note { get; } = note;
1111
public Guid? AntonymId { get; } = antonymId;
1212

13-
public override async ValueTask<Word> NewEntity(Commit commit, ChangeContext context)
13+
public override async ValueTask<Word> NewEntity(Commit commit, IChangeContext context)
1414
{
1515
var antonymShouldBeNull = AntonymId is null || (await context.IsObjectDeleted(AntonymId.Value));
1616
return (new Word { Text = Text, Note = Note, Id = EntityId, AntonymId = antonymShouldBeNull ? null : AntonymId });

src/SIL.Harmony.Sample/Changes/SetDefinitionPartOfSpeechChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class SetDefinitionPartOfSpeechChange(Guid entityId, string partOfSpeech)
88
{
99
public string PartOfSpeech { get; } = partOfSpeech;
1010

11-
public override ValueTask ApplyChange(Definition entity, ChangeContext context)
11+
public override ValueTask ApplyChange(Definition entity, IChangeContext context)
1212
{
1313
entity.PartOfSpeech = PartOfSpeech;
1414
return ValueTask.CompletedTask;

src/SIL.Harmony.Sample/Changes/SetTagChange.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class SetTagChange(Guid entityId, string text) : Change<Tag>(entityId), I
88
{
99
public string Text { get; } = text;
1010

11-
public override ValueTask<Tag> NewEntity(Commit commit, ChangeContext context)
11+
public override ValueTask<Tag> NewEntity(Commit commit, IChangeContext context)
1212
{
1313
return new(new Tag()
1414
{
@@ -18,7 +18,7 @@ public override ValueTask<Tag> NewEntity(Commit commit, ChangeContext context)
1818
}
1919

2020

21-
public override ValueTask ApplyChange(Tag entity, ChangeContext context)
21+
public override ValueTask ApplyChange(Tag entity, IChangeContext context)
2222
{
2323
entity.Text = Text;
2424
return ValueTask.CompletedTask;

src/SIL.Harmony.Sample/Changes/SetWordNoteChange.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class SetWordNoteChange(Guid entityId, string note) : EditChange<Word>(en
88
{
99
public string Note { get; } = note;
1010

11-
public override ValueTask ApplyChange(Word entity, ChangeContext context)
11+
public override ValueTask ApplyChange(Word entity, IChangeContext context)
1212
{
1313
entity.Note = Note;
1414
return ValueTask.CompletedTask;

src/SIL.Harmony.Sample/Changes/SetWordTextChange.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SetWordTextChange(Guid entityId, string text) : Change<Word>(entity
1212
{
1313
public string Text { get; } = text;
1414

15-
public override ValueTask<Word> NewEntity(Commit commit, ChangeContext context)
15+
public override ValueTask<Word> NewEntity(Commit commit, IChangeContext context)
1616
{
1717
return new(new Word()
1818
{
@@ -22,7 +22,7 @@ public override ValueTask<Word> NewEntity(Commit commit, ChangeContext context)
2222
}
2323

2424

25-
public override ValueTask ApplyChange(Word entity, ChangeContext context)
25+
public override ValueTask ApplyChange(Word entity, IChangeContext context)
2626
{
2727
entity.Text = Text;
2828
return ValueTask.CompletedTask;

src/SIL.Harmony.Sample/CrdtSampleKernel.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Data.Common;
21
using System.Diagnostics;
32
using SIL.Harmony.Changes;
43
using SIL.Harmony.Linq2db;

src/SIL.Harmony.Sample/Models/Definition.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public Guid[] GetReferences()
1818
return [WordId];
1919
}
2020

21-
public void RemoveReference(Guid id, Commit commit)
21+
public void RemoveReference(Guid id, CommitBase commit)
2222
{
2323
if (WordId == id)
2424
{

src/SIL.Harmony.Sample/Models/Example.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Guid[] GetReferences()
3131
return [DefinitionId];
3232
}
3333

34-
public void RemoveReference(Guid id, Commit commit)
34+
public void RemoveReference(Guid id, CommitBase commit)
3535
{
3636
if (DefinitionId == id)
3737
{

src/SIL.Harmony.Sample/Models/Tag.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public Guid[] GetReferences()
1414
return [];
1515
}
1616

17-
public void RemoveReference(Guid id, Commit commit)
17+
public void RemoveReference(Guid id, CommitBase commit)
1818
{
1919
}
2020

src/SIL.Harmony.Sample/Models/Word.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ IEnumerable<Guid> Refs()
2323
}
2424
}
2525

26-
public void RemoveReference(Guid id, Commit commit)
26+
public void RemoveReference(Guid id, CommitBase commit)
2727
{
2828
if (AntonymId == id) AntonymId = null;
2929
}

src/SIL.Harmony.Sample/SIL.Harmony.Sample.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@
1010
<ProjectReference Include="..\Ycs\Ycs.csproj" />
1111
</ItemGroup>
1212

13+
<ItemGroup>
14+
<Using Include="SIL.Harmony.Core"/>
15+
</ItemGroup>
1316
</Project>

src/SIL.Harmony.Sample/SampleDbContext.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using SIL.Harmony.Changes;
2-
using SIL.Harmony.Core;
32
using SIL.Harmony.Db;
43
using Microsoft.EntityFrameworkCore;
54
using Microsoft.Extensions.Options;

src/SIL.Harmony.Tests/Adapter/CustomObjectAdapterTests.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using SIL.Harmony.Changes;
77
using SIL.Harmony.Db;
88
using SIL.Harmony.Entities;
9-
using SIL.Harmony.Linq2db;
109

1110
namespace SIL.Harmony.Tests.Adapter;
1211

@@ -108,7 +107,7 @@ public DateTimeOffset? DeletedAt
108107

109108
public Guid[] GetReferences() => [];
110109

111-
public void RemoveReference(Guid id, Commit commit)
110+
public void RemoveReference(Guid id, CommitBase commit)
112111
{
113112
}
114113

@@ -124,7 +123,7 @@ public CreateMyClassChange(MyClass entity) : base(entity.Identifier)
124123
_entity = entity;
125124
}
126125

127-
public override ValueTask<MyClass> NewEntity(Commit commit, ChangeContext context)
126+
public override ValueTask<MyClass> NewEntity(Commit commit, IChangeContext context)
128127
{
129128
return ValueTask.FromResult(_entity);
130129
}
@@ -139,7 +138,7 @@ public CreateMyClass2Change(MyClass2 entity) : base(entity.Identifier)
139138
_entity = entity;
140139
}
141140

142-
public override ValueTask<MyClass2> NewEntity(Commit commit, ChangeContext context)
141+
public override ValueTask<MyClass2> NewEntity(Commit commit, IChangeContext context)
143142
{
144143
return ValueTask.FromResult(_entity);
145144
}

src/SIL.Harmony.Tests/CommitTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.IO.Hashing;
22
using System.Text.Json;
33
using SIL.Harmony.Changes;
4-
using SIL.Harmony.Core;
54
using SIL.Harmony.Sample;
65
using SIL.Harmony.Sample.Changes;
76
using Microsoft.Extensions.DependencyInjection;

src/SIL.Harmony.Tests/Core/HybridDateTimeTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using SIL.Harmony.Core;
2-
31
namespace SIL.Harmony.Tests.Core;
42

53
public class HybridDateTimeTests

src/SIL.Harmony.Tests/DataModelPerformanceTests.cs

-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,9 @@
88
using BenchmarkDotNet.Loggers;
99
using BenchmarkDotNet.Running;
1010
using JetBrains.Profiler.SelfApi;
11-
using Microsoft.Data.Sqlite;
1211
using SIL.Harmony.Changes;
13-
using SIL.Harmony.Core;
1412
using SIL.Harmony.Db;
1513
using SIL.Harmony.Sample.Changes;
16-
using SIL.Harmony.Sample.Models;
1714
using Xunit.Abstractions;
1815

1916
namespace SIL.Harmony.Tests;

src/SIL.Harmony.Tests/DataModelTestBase.cs

-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using System.Data.Common;
21
using Microsoft.Data.Sqlite;
32
using SIL.Harmony.Changes;
4-
using SIL.Harmony.Core;
53
using SIL.Harmony.Sample;
64
using SIL.Harmony.Sample.Changes;
75
using SIL.Harmony.Sample.Models;
86
using SIL.Harmony.Tests.Mocks;
97
using Microsoft.EntityFrameworkCore;
108
using Microsoft.Extensions.DependencyInjection;
119
using Microsoft.Extensions.DependencyInjection.Extensions;
12-
using Microsoft.Extensions.Options;
1310
using SIL.Harmony.Db;
1411

1512
namespace SIL.Harmony.Tests;

src/SIL.Harmony.Tests/Db/QueryHelperTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using SIL.Harmony.Core;
21
using SIL.Harmony.Db;
32
using SIL.Harmony.Tests.Mocks;
43

src/SIL.Harmony.Tests/DbContextTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using SIL.Harmony.Core;
21
using LinqToDB;
32
using LinqToDB.EntityFrameworkCore;
43
using Microsoft.EntityFrameworkCore;

src/SIL.Harmony.Tests/Mocks/MockTimeProvider.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using SIL.Harmony.Core;
2-
31
namespace SIL.Harmony.Tests.Mocks;
42

53
public class MockTimeProvider: IHybridDateTimeProvider

src/SIL.Harmony.Tests/ModuleInit.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
using System.Linq.Expressions;
1+
using System.Linq.Expressions;
22
using System.Runtime.CompilerServices;
33
using Argon;
4-
using SIL.Harmony.Core;
54
using SIL.Harmony.Sample;
65
using Microsoft.Extensions.DependencyInjection;
76

src/SIL.Harmony.Tests/ObjectBaseTestingHelpers.cs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using SIL.Harmony.Entities;
2-
31
namespace SIL.Harmony.Tests;
42

53
public static class ObjectBaseTestingHelpers

src/SIL.Harmony.Tests/PersistExtraDataTests.cs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
2-
using Microsoft.Extensions.DependencyInjection.Extensions;
32
using SIL.Harmony.Changes;
4-
using SIL.Harmony.Core;
53
using SIL.Harmony.Entities;
6-
using SIL.Harmony.Sample;
74

85
namespace SIL.Harmony.Tests;
96

@@ -13,7 +10,7 @@ public class PersistExtraDataTests
1310

1411
public class CreateExtraDataModelChange(Guid entityId) : CreateChange<ExtraDataModel>(entityId), ISelfNamedType<CreateExtraDataModelChange>
1512
{
16-
public override ValueTask<ExtraDataModel> NewEntity(Commit commit, ChangeContext context)
13+
public override ValueTask<ExtraDataModel> NewEntity(Commit commit, IChangeContext context)
1714
{
1815
return ValueTask.FromResult(new ExtraDataModel()
1916
{
@@ -35,7 +32,7 @@ public Guid[] GetReferences()
3532
return [];
3633
}
3734

38-
public void RemoveReference(Guid id, Commit commit)
35+
public void RemoveReference(Guid id, CommitBase commit)
3936
{
4037
}
4138

src/SIL.Harmony.Tests/RepositoryTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using SIL.Harmony.Core;
21
using SIL.Harmony.Db;
32
using SIL.Harmony.Sample;
43
using SIL.Harmony.Sample.Models;

0 commit comments

Comments
 (0)