-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueObject.cs
More file actions
175 lines (132 loc) · 5.01 KB
/
ValueObject.cs
File metadata and controls
175 lines (132 loc) · 5.01 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using BeyondNetCode.Shell.Ddd.Interfaces;
using BeyondNetCode.Shell.Ddd.Rules.Impl;
using BeyondNetCode.Shell.Ddd.Rules.PropertyChange;
using BeyondNetCode.Shell.Ddd.Services.Impl;
using BeyondNetCode.Shell.Ddd.Services.Interfaces;
namespace BeyondNetCode.Shell.Ddd
{
/// <summary>
/// Base class for value objects in the domain-driven design.
/// </summary>
/// <typeparam name="TValue">The type of the value object.</typeparam>
public abstract class ValueObject<TValue> : AbstractNotifyPropertyChanged, IProps
{
#region Members
public ITrackingStateManager TrackingState { get; private set; }
public ValidatorRuleManager<AbstractRuleValidator<ValueObject<TValue>>> ValidatorRules { get; private set; }
public IBrokenRulesManager BrokenRules { get; private set; }
public bool IsValid => !BrokenRules.GetBrokenRules().Any();
#endregion
#region Properties
/// <summary>
/// Gets the value of the value object.
/// </summary>
protected TValue InternalValue
{
get
{
return (TValue)GetValue();
}
}
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="ValueObject{TValue}"/> class.
/// </summary>
/// <param name="value">The value of the value object.</param>
protected ValueObject(TValue value)
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
BrokenRules = new BrokenRulesManager();
ValidatorRules = new ValidatorRuleManager<AbstractRuleValidator<ValueObject<TValue>>>();
TrackingState = new TrackingStateManager();
RegisterProperty(nameof(InternalValue), typeof(TValue), value, ValuePropertyChanged);
TrackingState.MarkAsNew();
#pragma warning disable CA2214 // Do not call overridable methods in constructors
AddValidators();
#pragma warning restore CA2214 // Do not call overridable methods in constructors
Validate();
}
#endregion
#region Methods
private void ValuePropertyChanged(AbstractNotifyPropertyChanged sender, NotifyPropertyChangedContextArgs e)
{
TrackingState.MarkAsDirty();
Validate();
}
public void SetValue(TValue value)
{
ArgumentNullException.ThrowIfNull(value, nameof(value));
SetValue(value, nameof(InternalValue));
}
public TValue GetValue()
{
return (TValue)GetValue(nameof(InternalValue));
}
#endregion
#region Business Rules
/// <summary>
/// Adds the validators for the value object.
/// </summary>
public virtual void AddValidators()
{
}
private void Validate()
{
BrokenRules.Clear();
BrokenRules.Add(ValidatorRules.GetBrokenRules());
}
#endregion
#region Equality
protected static bool EqualOperator(ValueObject<TValue> left, ValueObject<TValue> right)
{
if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
{
return false;
}
return ReferenceEquals(left, null) || left.Equals(right!);
}
protected static bool NotEqualOperator(ValueObject<TValue> left, ValueObject<TValue> right)
{
return !(EqualOperator(left, right));
}
protected abstract IEnumerable<object> GetEqualityComponents();
/// <summary>
/// Determines whether the current value object is equal to another value object.
/// </summary>
/// <param name="obj">The value object to compare with the current value object.</param>
/// <returns><c>true</c> if the current value object is equal to the other value object; otherwise, <c>false</c>.</returns>
public override bool Equals(object? obj)
{
if (obj == null || obj.GetType() != GetType())
{
return false;
}
var other = (ValueObject<TValue>)obj;
return this.GetEqualityComponents().SequenceEqual(other.GetEqualityComponents());
}
/// <summary>
/// Gets the hash code of the value object.
/// </summary>
/// <returns>The hash code of the value object.</returns>
public override int GetHashCode()
{
return GetEqualityComponents()
.Select(x => x != null ? x.GetHashCode() : 0)
.Aggregate((x, y) => x ^ y);
}
/// <summary>
/// Creates a copy of the value object.
/// </summary>
/// <returns>A copy of the value object.</returns>
public ValueObject<TValue> GetCopy()
{
return (ValueObject<TValue>)MemberwiseClone();
}
public object Clone()
{
return GetCopy();
}
#endregion
}
}