-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersistableTimeDependentCollection.cs
71 lines (64 loc) · 3.13 KB
/
PersistableTimeDependentCollection.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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using TimeSlice;
namespace TimeSliceEntityFrameworkExtensions
{
/// <summary>
/// Similar to <see cref="TimeDependentCollection{TRelation,TParent,TChild}" /> but with persistable parent and child
/// </summary>
/// <typeparam name="TPersistableRelation"></typeparam>
/// <typeparam name="TPersistableParent"></typeparam>
/// <typeparam name="TParentKey"></typeparam>
/// <typeparam name="TPersistableChild"></typeparam>
/// <typeparam name="TChildKey"></typeparam>
public abstract class PersistableTimeDependentCollection<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey> :
TimeDependentCollection<TPersistableRelation, TPersistableParent, TPersistableChild>,
IEquatable<PersistableTimeDependentCollection<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>>
where TPersistableRelation : IPersistableRelation<TPersistableParent, TParentKey, TPersistableChild, TChildKey>, ITimeSlice, IValidatableObject,
IRelation<TPersistableParent, TPersistableChild>
where TPersistableParent : class, IHasKey<TParentKey>
where TPersistableChild : class, IHasKey<TChildKey>
{
/// <inheritdoc />
protected PersistableTimeDependentCollection(TPersistableParent commonParent, IEnumerable<TPersistableRelation>? relations = null)
: base(commonParent, relations)
{
}
/// <inheritdoc />
protected PersistableTimeDependentCollection()
{
}
/// <summary>
/// ID of <see cref="TimeDependentCollection{TRelation,TParent,TChild}.CommonParent" />
/// </summary>
public TParentKey? CommonParentId { get; set; }
/// <summary>
/// This property is mapped to a column on the database.
/// This allows us to enforce only valid collections on the database using a Check constraint.
/// The setter is only fake.
/// </summary>
public bool IsValid
{
get => this.IsValid();
set { } // not necessary
}
/// <inheritdoc />
public bool Equals(PersistableTimeDependentCollection<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>? other)
{
return base.Equals(other) && ((other.CommonParentId == null && CommonParentId == null) || (other.CommonParentId != null && CommonParentId != null) && other.CommonParentId.Equals(CommonParentId));
}
/// <inheritdoc />
public override bool Equals(object? obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == GetType() && Equals((PersistableTimeDependentCollection<TPersistableRelation, TPersistableParent, TParentKey, TPersistableChild, TChildKey>)obj);
}
/// <inheritdoc />
public override int GetHashCode()
{
return HashCode.Combine(CommonParentId, base.GetHashCode());
}
}
}