-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.cs
More file actions
253 lines (192 loc) · 7.06 KB
/
Entity.cs
File metadata and controls
253 lines (192 loc) · 7.06 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using BeyondNetCode.Shell.Ddd.Rules;
using BeyondNetCode.Shell.Ddd.Interfaces;
using BeyondNetCode.Shell.Ddd.Extensions;
using BeyondNetCode.Shell.Ddd.Rules.Impl;
using BeyondNetCode.Shell.Ddd.Services.Impl;
namespace BeyondNetCode.Shell.Ddd
{
/// <summary>
/// Represents an abstract base class for entities in the domain-driven design.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TProps">The type of the entity properties.</typeparam>
public abstract class Entity<TEntity, TProps> : IEntity<TEntity, TProps> where TEntity : class
where TProps : class, IProps
{
#region Members
/// <summary>
/// The properties of the entity.
/// </summary>
private TProps _props;
#endregion
#region Properties
public IdValueObject Id { get; private set; }
public IdValueObject SetId(string id)
{
return IdValueObject.Load(id);
}
public BrokenRulesManager BrokenRules { get; }
public TrackingStateManager TrackingState { get; }
public ValidatorRuleManager<AbstractRuleValidator<TEntity>> ValidatorRules { get; }
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Entity{TEntity, TProps}"/> class.
/// </summary>
/// <param name="props">The properties of the entity.</param>
protected Entity(TProps props)
{
BrokenRules = new BrokenRulesManager();
TrackingState = new TrackingStateManager();
ValidatorRules = new ValidatorRuleManager<AbstractRuleValidator<TEntity>>();
Id = IdValueObject.Create();
_props = props;
Validate();
TrackingState.MarkAsNew();
}
#endregion
#region Methods
/// <summary>
/// Gets a copy of the entity properties.
/// </summary>
/// <returns>A copy of the entity properties.</returns>
public TProps GetPropsCopy()
{
var copyProps = _props!.Clone();
return (TProps)copyProps;
}
/// <summary>
/// Gets the properties of the entity.
/// </summary>
public TProps Props
{
get { return _props!; }
}
/// <summary>
/// Sets the properties of the entity.
/// </summary>
/// <param name="props">The properties to set.</param>
public void SetProps(TProps props)
{
_props = props;
TrackingState.MarkAsDirty();
}
#endregion
#region BusinessRules
/// <summary>
/// Gets a value indicating whether the entity is valid.
/// </summary>
/// <returns><c>true</c> if the entity is valid; otherwise, <c>false</c>.</returns>
public bool IsValid()
{
Validate();
return !BrokenRules.GetBrokenRules().Any();
}
/// <summary>
/// Validates the entity.
/// </summary>
public void Validate()
{
Guard();
// Add validators for Entity
AddValidators();
// Get broken rules for Entity
BrokenRules.Add(ValidatorRules.GetBrokenRules().ToList());
if (BrokenRules.GetBrokenRules().Any())
{
TrackingState.MarkAsDirty();
return;
}
// Explore broken rules for properties
var props = GetPropsCopy().GetType().GetProperties();
var propsBrokenRules = props.GetPropertiesBrokenRules(_props);
if (propsBrokenRules.Any())
{
BrokenRules.Add(propsBrokenRules);
TrackingState.MarkAsDirty();
}
}
private void Guard()
{
if (!(this is TEntity))
throw new InvalidOperationException($"Entity '{GetType().Print()}' specifies '{typeof(TEntity).Print()}' as generic argument, it should be its own type");
}
/// <summary>
/// Adds the validators for the value object.
/// </summary>
public virtual void AddValidators()
{
}
#endregion
#region Equality
/// <inheritdoc/>
public override bool Equals(object? obj)
{
if (obj == null || !(obj is Entity<TEntity, TProps>))
return false;
if (ReferenceEquals(this, obj))
return true;
if (GetType() != obj.GetType())
return false;
return ReferenceEntityPropertiesEquals(obj);
}
private bool ReferenceEntityPropertiesEquals(object? obj)
{
if (obj is not Entity<TEntity, TProps> entity)
return false;
return Id.Equals(entity.Id);
}
/// <inheritdoc/>
public override int GetHashCode()
{
return Id.GetHashCode();
}
/// <summary>
/// Determines whether two entities are equal.
/// </summary>
/// <param name="left">The left entity.</param>
/// <param name="right">The right entity.</param>
/// <returns><c>true</c> if the entities are equal; otherwise, <c>false</c>.</returns>
public static bool operator ==(Entity<TEntity, TProps> left, Entity<TEntity, TProps> right)
{
if (Equals(left, null))
return Equals(right, null) ? true : false;
else
return left.Equals(right);
}
/// <summary>
/// Determines whether two entities are not equal.
/// </summary>
/// <param name="left">The left entity.</param>
/// <param name="right">The right entity.</param>
/// <returns><c>true</c> if the entities are not equal; otherwise, <c>false</c>.</returns>
public static bool operator !=(Entity<TEntity, TProps> left, Entity<TEntity, TProps> right)
{
return !(left == right);
}
#endregion
#region TransitionStatus
/// <summary>
/// Finite State Machine (FSM) Pattern
/// </summary>
private readonly Dictionary<object, List<object>> _validTransitions = new();
protected void DefineValidTransitions(Dictionary<object, List<object>> transitions)
{
ArgumentNullException.ThrowIfNull(transitions, nameof(transitions));
_validTransitions.Clear();
foreach (var transition in transitions)
{
_validTransitions[transition.Key] = transition.Value;
}
}
protected bool CanTransitionTo(object currentState, object newState)
{
if (!_validTransitions.ContainsKey(currentState))
{
throw new InvalidOperationException($"No transitions defined for state {currentState}.");
}
return _validTransitions[currentState].Contains(newState);
}
#endregion
}
}