-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCrdtSampleKernel.cs
More file actions
88 lines (85 loc) · 3.39 KB
/
CrdtSampleKernel.cs
File metadata and controls
88 lines (85 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Diagnostics;
using SIL.Harmony.Changes;
using SIL.Harmony.Linq2db;
using SIL.Harmony.Sample.Changes;
using SIL.Harmony.Sample.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace SIL.Harmony.Sample;
public static class CrdtSampleKernel
{
public static IServiceCollection AddCrdtDataSample(this IServiceCollection services, string dbPath)
{
return services.AddCrdtDataSample(builder => builder.UseSqlite($"Data Source={dbPath}"));
}
public static IServiceCollection AddCrdtDataSample(this IServiceCollection services,
Action<DbContextOptionsBuilder> optionsBuilder, bool performanceTest = false)
{
services.AddDbContext<SampleDbContext>((provider, builder) =>
{
//this ensures that Ef Conversion methods will not be cached across different IoC containers
//this can show up as the second instance using the JsonSerializerOptions from the first container
//only needed for testing scenarios
builder.EnableServiceProviderCaching(performanceTest);
builder.UseLinqToDbCrdt(provider);
optionsBuilder(builder);
builder.EnableDetailedErrors();
builder.EnableSensitiveDataLogging();
#if DEBUG
builder.LogTo(s => Debug.WriteLine(s));
#endif
});
services.AddCrdtData<SampleDbContext>(config =>
{
config.EnableProjectedTables = true;
config.AddRemoteResourceEntity();
config.ChangeTypeListBuilder
.Add<NewWordChange>()
.Add<NewDefinitionChange>()
.Add<NewExampleChange>()
.Add<EditExampleChange>()
.Add<SetWordTextChange>()
.Add<SetWordNoteChange>()
.Add<SetAntonymReferenceChange>()
.Add<AddWordImageChange>()
.Add<SetOrderChange<Definition>>()
.Add<SetDefinitionPartOfSpeechChange>()
.Add<SetTagChange>()
.Add<TagWordChange>()
.Add<DeleteChange<Word>>()
.Add<DeleteChange<Definition>>()
.Add<DeleteChange<Example>>()
.Add<DeleteChange<Tag>>()
;
config.ObjectTypeListBuilder.DefaultAdapter()
.Add<Word>(builder =>
{
builder.HasMany(w => w.Tags)
.WithMany()
.UsingEntity<WordTag>();
builder.HasOne((w) => w.Antonym)
.WithMany()
.HasForeignKey(w => w.AntonymId)
.OnDelete(DeleteBehavior.SetNull);
})
.Add<Definition>(builder =>
{
builder.HasOne<Word>()
.WithMany()
.HasForeignKey(d => d.WordId)
.OnDelete(DeleteBehavior.Cascade);
})
.Add<Example>()
.Add<Tag>(builder =>
{
builder.HasIndex(tag => tag.Text).IsUnique();
})
.Add<WordTag>(builder =>
{
builder.HasKey(wt => wt.Id);
builder.HasIndex(wt => new { wt.WordId, wt.TagId }).IsUnique();
});
});
return services;
}
}