-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModelBuilderExtensions.cs
152 lines (139 loc) · 8.88 KB
/
ModelBuilderExtensions.cs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ValueGeneration;
using TimeSlice;
[assembly: InternalsVisibleTo("TimeSliceTests")]
namespace TimeSliceEntityFrameworkExtensions
{
/// <summary>
/// Extensions to <see cref="ModelBuilder" />
/// </summary>
public static class ModelBuilderExtensions
{
/// <summary>
/// Set default keys using
/// <see cref="EntityTypeBuilderExtensions.HasDefaultKeys{TTimeSliceCollection,TPersistableRelation,TPersistableParent,TParentKey,TPersistableChild,TChildKey}" />.
/// </summary>
/// <param name="modelBuilder">a model builder instance</param>
/// <param name="collectionKeyExpression">each collection has to have a key defined</param>
/// <param name="relationKeyExpression">optional key for each <typeparamref name="TPersistableRelation" /></param>
/// <typeparam name="TTimeSliceCollection">
/// <see cref="TimeDependentCollection{TRelation,TParent,TChild}" />
/// </typeparam>
/// <typeparam name="TPersistableRelation">
/// <see cref="TimeDependentRelation{TParent,TChild}" />
/// </typeparam>
/// <typeparam name="TPersistableParent">
/// <see cref="TimeDependentRelation{TParent,TChild}.Parent" />
/// </typeparam>
/// <typeparam name="TPersistableChild">
/// <see cref="TimeDependentRelation{TParent,TChild}.Child" />
/// </typeparam>
/// <typeparam name="TParentKey"><see cref="IHasKey{TKey}.Id" /> of <typeparamref name="TPersistableParent" /></typeparam>
/// <typeparam name="TChildKey"><see cref="IHasKey{TKey}.Id" /> of <typeparamref name="TPersistableChild" /></typeparam>
public static void SetupCollectionAndRelations<TTimeSliceCollection, TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>(
this ModelBuilder modelBuilder,
Expression<Func<TTimeSliceCollection, object?>> collectionKeyExpression,
Expression<Func<TPersistableRelation, object?>>? relationKeyExpression = null)
where TTimeSliceCollection : PersistableTimeDependentCollection<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>
where TPersistableRelation : class, ITimeSlice, IPersistableRelation<TPersistableParent, TParentKey, TPersistableChild, TChildKey>
where TPersistableParent : class, IHasKey<TParentKey>
where TPersistableChild : class, IHasKey<TChildKey>
{
// set primary keys for parent and child
modelBuilder.Entity<TPersistableParent>().HasKey(parent => parent.Id);
modelBuilder.Entity<TPersistableChild>().HasKey(child => child.Id);
// set primary key for relation
modelBuilder.Entity<TPersistableRelation>().HasKey(x => new { x.Discriminator, x.ParentId, x.ChildId, x.Start, x.End });
modelBuilder.Entity<TPersistableRelation>()
.HasDefaultKeys<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>(relationKeyExpression);
// autogenerate parent and child id in relation
modelBuilder.Entity<TPersistableRelation>().Property(x => x.ParentId)
.HasValueGenerator<ParentIdValueGenerator<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild,
TChildKey>>();
modelBuilder.Entity<TPersistableRelation>().Property(x => x.ChildId)
.HasValueGenerator<ChildIdValueGenerator<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild,
TChildKey>>();
// set primary key for collection
modelBuilder.Entity<TTimeSliceCollection>()
.HasDefaultKeys<TTimeSliceCollection, TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>(collectionKeyExpression);
// autogenerate common parent id property for collection
modelBuilder.Entity<TTimeSliceCollection>().Property(x => x.CommonParentId)
.HasValueGenerator<CommonParentIdValueGenerator<TTimeSliceCollection, TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>>();
modelBuilder.Entity<TTimeSliceCollection>(collection => collection.ToTable(t => t.HasCheckConstraint("validity_check_constraint", "IsValid == true")));
}
}
/// <summary>
/// The CommonParentIdValueGenerator extracts the
/// <see cref="PersistableTimeDependentCollection{TPersistableRelation,TPersistableParent,TParentKey,TPersistableChild,TChildKey}.CommonParentId" /> from the
/// <see cref="TimeDependentCollection{TRelation,TParent,TChild}.CommonParent" />
/// </summary>
/// <typeparam name="TTimeSliceCollection"></typeparam>
/// <typeparam name="TPersistableRelation"></typeparam>
/// <typeparam name="TPersistableParent"></typeparam>
/// <typeparam name="TParentKey"></typeparam>
/// <typeparam name="TPersistableChild"></typeparam>
/// <typeparam name="TChildKey"></typeparam>
internal class CommonParentIdValueGenerator<TTimeSliceCollection, TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild,
TChildKey> : ValueGenerator<TParentKey>
where TTimeSliceCollection : PersistableTimeDependentCollection<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>
where TPersistableRelation : class, ITimeSlice, IPersistableRelation<TPersistableParent, TParentKey, TPersistableChild, TChildKey>
where TPersistableParent : class, IHasKey<TParentKey>
where TPersistableChild : class, IHasKey<TChildKey>
{
public override bool GeneratesTemporaryValues => false;
public override TParentKey Next(EntityEntry entry)
{
if (entry.Entity is TTimeSliceCollection collection) return collection.CommonParent!.Id;
throw new NotImplementedException($"Generating a default value for {entry?.Entity?.GetType()} is not implemented.");
}
}
/// <summary>
/// The ParentIdValueGenerator extracts the <see cref="PersistableTimeDependentRelation{TPersistableParent,TParentKey,TPersistableChild,TChildKey}.ParentId" /> from the
/// <see cref="TimeDependentRelation{TParent,TChild}.Parent" />
/// </summary>
/// <typeparam name="TPersistableRelation"></typeparam>
/// <typeparam name="TPersistableParent"></typeparam>
/// <typeparam name="TParentKey"></typeparam>
/// <typeparam name="TPersistableChild"></typeparam>
/// <typeparam name="TChildKey"></typeparam>
internal class ParentIdValueGenerator<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild,
TChildKey> : ValueGenerator<TParentKey>
where TPersistableRelation : class, ITimeSlice, IPersistableRelation<TPersistableParent, TParentKey, TPersistableChild, TChildKey>
where TPersistableParent : class, IHasKey<TParentKey>
where TPersistableChild : class, IHasKey<TChildKey>
{
public override bool GeneratesTemporaryValues => false;
public override TParentKey Next(EntityEntry entry)
{
if (entry.Entity is TPersistableRelation relation) return relation.Parent!.Id;
throw new NotImplementedException($"Generating a default value for {entry?.Entity?.GetType()} is not implemented.");
}
}
/// <summary>
/// The ParentIdValueGenerator extracts the <see cref="PersistableTimeDependentRelation{TPersistableParent,TParentKey,TPersistableChild,TChildKey}.ChildId" /> from the
/// <see cref="TimeDependentRelation{TParent,TChild}.Child" />
/// </summary>
/// <typeparam name="TPersistableRelation"></typeparam>
/// <typeparam name="TPersistableParent"></typeparam>
/// <typeparam name="TParentKey"></typeparam>
/// <typeparam name="TPersistableChild"></typeparam>
/// <typeparam name="TChildKey"></typeparam>
internal class ChildIdValueGenerator<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild,
TChildKey> : ValueGenerator<TChildKey>
where TPersistableRelation : class, ITimeSlice, IPersistableRelation<TPersistableParent, TParentKey, TPersistableChild, TChildKey>
where TPersistableParent : class, IHasKey<TParentKey>
where TPersistableChild : class, IHasKey<TChildKey>
{
public override bool GeneratesTemporaryValues => false;
public override TChildKey Next(EntityEntry entry)
{
if (entry.Entity is TPersistableRelation relation) return relation.Child!.Id;
throw new NotImplementedException($"Generating a default value for {entry?.Entity?.GetType()} is not implemented.");
}
}
}