Skip to content

Commit df679bb

Browse files
author
elotoja
committed
Add EntityId
1 parent 0ecd766 commit df679bb

File tree

14 files changed

+46
-1224
lines changed

14 files changed

+46
-1224
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
namespace Quizer.Domain.Common.Models;
22

3-
public abstract class AggregateRootId<TId> : ValueObject
3+
public abstract class AggregateRootId<TId> : EntityId<TId>
44
{
5-
public abstract TId Value { get; protected set; }
5+
protected AggregateRootId(TId value) : base(value)
6+
{
7+
}
68
}

src/Quizer.Domain/Common/Models/Entity.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using ErrorOr;
22
using FluentValidation.Results;
3-
using Quizer.Domain.QuizAggregate.Validation;
43

54
namespace Quizer.Domain.Common.Models;
65

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace Quizer.Domain.Common.Models;
2+
3+
public abstract class EntityId<TId> : ValueObject
4+
{
5+
public TId Value { get; }
6+
7+
protected EntityId(TId value)
8+
{
9+
Value = value;
10+
}
11+
12+
public override IEnumerable<object?> GetEqualityComponents()
13+
{
14+
yield return Value;
15+
}
16+
17+
public override string? ToString() => Value?.ToString() ?? base.ToString();
18+
19+
#pragma warning disable CS8618
20+
protected EntityId()
21+
{
22+
}
23+
#pragma warning restore CS8618
24+
}

src/Quizer.Domain/Common/Models/ValueObject.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,29 @@
22

33
public abstract class ValueObject : IEquatable<ValueObject>
44
{
5-
public abstract IEnumerable<object> GetEqualityComponents();
5+
public abstract IEnumerable<object?> GetEqualityComponents();
66

77
public override bool Equals(object? obj)
88
{
99
if (obj is null || obj.GetType() != GetType())
10+
{
1011
return false;
12+
}
1113

1214
var valueObject = (ValueObject)obj;
1315

1416
return GetEqualityComponents()
1517
.SequenceEqual(valueObject.GetEqualityComponents());
1618
}
1719

18-
public static bool operator ==(ValueObject? a, ValueObject? b)
20+
public static bool operator ==(ValueObject left, ValueObject right)
1921
{
20-
return Equals(a, b);
22+
return Equals(left, right);
2123
}
2224

23-
public static bool operator !=(ValueObject? a, ValueObject? b)
25+
public static bool operator !=(ValueObject left, ValueObject right)
2426
{
25-
return !Equals(a, b);
27+
return !Equals(left, right);
2628
}
2729

2830
public override int GetHashCode()
@@ -34,6 +36,6 @@ public override int GetHashCode()
3436

3537
public bool Equals(ValueObject? other)
3638
{
37-
return Equals(other);
39+
return Equals((object?)other);
3840
}
39-
}
41+
}

src/Quizer.Domain/QuestionAggregate/Entities/Answer.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@ public ErrorOr<bool> Update(string answer, bool isCorrect)
2929
{
3030
base.Update();
3131
Text = answer;
32-
IsCorrect = true;
32+
IsCorrect = isCorrect;
3333

3434
return true;
3535
}
3636

3737
#pragma warning disable CS8618
38-
private Answer()
39-
{
40-
}
38+
private Answer() {}
4139
#pragma warning restore CS8618
4240
}

src/Quizer.Domain/QuestionAggregate/Question.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public void Delete()
7272
}
7373

7474
#pragma warning disable CS8618
75-
private Question()
76-
{
77-
}
75+
private Question() {}
7876
#pragma warning restore CS8618
7977
}

src/Quizer.Domain/QuestionAggregate/QuestionId.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ namespace Quizer.Domain.QuestionAggregate;
44

55
public sealed class QuestionId : AggregateRootId<Guid>
66
{
7-
public override Guid Value { get; protected set; }
87

9-
private QuestionId(Guid value)
8+
private QuestionId(Guid value) : base(value)
109
{
11-
Value = value;
1210
}
1311

1412
public static QuestionId CreateUnique()

src/Quizer.Domain/QuestionAggregate/ValueObjects/AnswerId.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
namespace Quizer.Domain.QuestionAggregate.ValueObjects;
44

5-
public sealed class AnswerId : ValueObject
5+
public sealed class AnswerId : EntityId<Guid>
66
{
7-
public Guid Value { get; private set; }
8-
9-
private AnswerId(Guid value)
7+
public AnswerId(Guid value) : base(value)
108
{
11-
Value = value;
129
}
1310

1411
public static AnswerId CreateUnique()

src/Quizer.Domain/QuizAggregate/Quiz.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ public void DeleteQuestion(Question question)
100100
}
101101

102102
#pragma warning disable CS8618
103-
private Quiz()
104-
{
105-
}
103+
private Quiz() {}
106104
#pragma warning restore CS8618
107105
}

src/Quizer.Domain/QuizAggregate/QuizId.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ namespace Quizer.Domain.QuizAggregate;
44

55
public sealed class QuizId : AggregateRootId<Guid>
66
{
7-
public override Guid Value { get; protected set; }
8-
9-
private QuizId(Guid value)
7+
private QuizId(Guid value) : base(value)
108
{
11-
Value = value;
129
}
1310

1411
public static QuizId CreateUnique()

0 commit comments

Comments
 (0)