-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathOneToOnePropertyConstraint.cs
More file actions
59 lines (51 loc) · 1.76 KB
/
OneToOnePropertyConstraint.cs
File metadata and controls
59 lines (51 loc) · 1.76 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
using LazyEntityGraph.Core.Extensions;
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace LazyEntityGraph.Core.Constraints
{
public class OneToOnePropertyConstraint<THost, TProperty> : IPropertyConstraint<THost, TProperty>
where THost : class
where TProperty : class
{
private readonly PropertyInfo _inverse;
public OneToOnePropertyConstraint(PropertyInfo propInfo, PropertyInfo inverse)
{
PropInfo = propInfo;
_inverse = inverse;
}
public void Rebind(THost host, TProperty previousValue, TProperty value)
{
Property.Set(value, _inverse, host);
}
public PropertyInfo PropInfo { get; }
#region Equality
protected bool Equals(OneToOnePropertyConstraint<THost, TProperty> other)
{
return _inverse.PropertyEquals(other._inverse) && PropInfo.PropertyEquals(other.PropInfo);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != GetType())
return false;
return Equals((OneToOnePropertyConstraint<THost, TProperty>)obj);
}
public override int GetHashCode()
{
unchecked
{
return ((_inverse != null ? _inverse.GetHashCode() : 0) * 397) ^
(PropInfo != null ? PropInfo.GetHashCode() : 0);
}
}
#endregion
public override string ToString()
{
return $"{typeof(THost).Name}.{PropInfo.Name} 1..1 {typeof(TProperty).Name}.{_inverse.Name}";
}
}
}