Skip to content

Commit 23d8cb2

Browse files
committed
Reverted Equals changes but instead introduced an IsSameAs(...) method.
1 parent e2891d1 commit 23d8cb2

File tree

4 files changed

+25
-363
lines changed

4 files changed

+25
-363
lines changed

Src/Xer.DomainDriven/AggregateRoot.cs

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Xer.DomainDriven
66
{
7-
public abstract class AggregateRoot : Entity, IAggregateRoot, IEquatable<IAggregateRoot>
7+
public abstract class AggregateRoot : Entity, IAggregateRoot
88
{
99
#region Declarations
1010

@@ -60,67 +60,6 @@ void IAggregateRoot.MarkDomainEventsAsCommitted()
6060

6161
#endregion IAggregateRoot implementation
6262

63-
#region Methods
64-
65-
/// <summary>
66-
/// Determine if object is equal by identity.
67-
/// </summary>
68-
/// <param name="other">Other object.</param>
69-
/// <returns>True, if aggregate roots are equal by identity. Otherwise, false.</returns>
70-
public override bool Equals(object other)
71-
{
72-
return Equals(other as IAggregateRoot);
73-
}
74-
75-
/// <summary>
76-
/// Determine if aggregate root is equal by identity.
77-
/// </summary>
78-
/// <param name="other">Other aggregate root.</param>
79-
/// <returns>True, if aggregate roots are equal by identity. Otherwise, false.</returns>
80-
public virtual bool Equals(IAggregateRoot other)
81-
{
82-
return base.Equals(other);
83-
}
84-
85-
/// <summary>
86-
/// Equality operator.
87-
/// </summary>
88-
/// <param name="aggregateRoot1">First aggregate root.</param>
89-
/// <param name="aggregateRoot2">Second aggregate root.</param>
90-
/// <returns>True, if aggregate roots are equal by identity. Otherwise, false.</returns>
91-
public static bool operator ==(AggregateRoot aggregateRoot1, AggregateRoot aggregateRoot2)
92-
{
93-
if (ReferenceEquals(aggregateRoot1, null) && ReferenceEquals(aggregateRoot2, null))
94-
return true;
95-
96-
if (!ReferenceEquals(aggregateRoot1, null))
97-
return aggregateRoot1.Equals(aggregateRoot2);
98-
99-
return false;
100-
}
101-
102-
/// <summary>
103-
/// Ineuality operator.
104-
/// </summary>
105-
/// <param name="aggregateRoot1">First aggregate root.</param>
106-
/// <param name="aggregateRoot2">Second aggregate root.</param>
107-
/// <returns>True, if aggregate roots are not equal by identity. Otherwise, false.</returns>
108-
public static bool operator !=(AggregateRoot aggregateRoot1, AggregateRoot aggregateRoot2)
109-
{
110-
return !(aggregateRoot1 == aggregateRoot2);
111-
}
112-
113-
/// <summary>
114-
/// Generate hash code from ID.
115-
/// </summary>
116-
/// <returns>Hash code generated from ID.</returns>
117-
public override int GetHashCode()
118-
{
119-
return base.GetHashCode();
120-
}
121-
122-
#endregion Methods
123-
12463
#region Protected Methods
12564

12665
/// <summary>

Src/Xer.DomainDriven/Entity.cs

Lines changed: 20 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,29 @@
22

33
namespace Xer.DomainDriven
44
{
5-
public abstract class Entity : IEntity, IEquatable<IEntity>
5+
public abstract class Entity : IEntity
66
{
7+
#region Properties
8+
79
/// <summary>
810
/// Unique ID.
911
/// </summary>
1012
public Guid Id { get; protected set; }
1113

1214
/// <summary>
13-
/// Date when entitity was created.
15+
/// Date when entitity was created. This will default to <see cref="DateTime.UtcNow"/> if no value has been provided in constructor.
1416
/// </summary>
1517
public DateTime Created { get; protected set; }
1618

1719
/// <summary>
18-
/// Date when entity was last updated.
20+
/// Date when entity was last updated. This will default to <see cref="DateTime.UtcNow"/> if no value has been provided in constructor.
1921
/// </summary>
2022
public DateTime Updated { get; protected set; }
2123

24+
#endregion Properties
25+
26+
#region Constructors
27+
2228
/// <summary>
2329
/// Constructor.
2430
/// </summary>
@@ -46,72 +52,25 @@ public Entity(Guid entityId, DateTime created, DateTime updated)
4652
Updated = updated;
4753
}
4854

49-
/// <summary>
50-
/// Determine if object is equal by identity.
51-
/// </summary>
52-
/// <param name="other">Other object.</param>
53-
/// <returns>True, if entities are equal by identity. Otherwise, false.</returns>
54-
public override bool Equals(object other)
55-
{
56-
return Equals(other as IEntity);
57-
}
58-
59-
/// <summary>
60-
/// Determine if entity is equal by identity.
61-
/// </summary>
62-
/// <param name="other">Other entity.</param>
63-
/// <returns>True, if entities are equal by identity. Otherwise, false.</returns>
64-
public virtual bool Equals(IEntity other)
65-
{
66-
if (ReferenceEquals(other, null))
67-
return false;
68-
69-
if (ReferenceEquals(this, other))
70-
return true;
71-
72-
if (this.GetType() != other.GetType())
73-
return false;
74-
75-
return Id == other.Id;
76-
}
55+
#endregion Constructors
7756

57+
#region Methods
58+
7859
/// <summary>
79-
/// Equality operator.
60+
/// Check if entity has the same identity as this entity instance.
8061
/// </summary>
81-
/// <param name="entity1">First entity.</param>
82-
/// <param name="entity2">Second entity.</param>
83-
/// <returns>True, if entities are equal by identity. Otherwise, false.</returns>
84-
public static bool operator ==(Entity e1, Entity e2)
62+
/// <param name="entity">Entity.</param>
63+
/// <returns>True if entities have the same identity. Otherwise, false.</returns>
64+
public virtual bool IsSameAs(IEntity entity)
8565
{
86-
if (ReferenceEquals(e1, null) && ReferenceEquals(e2, null))
87-
return true;
88-
89-
if (!ReferenceEquals(e1, null))
66+
if (entity == null)
9067
{
91-
return e1.Equals(e2);
68+
throw new ArgumentNullException(nameof(entity));
9269
}
9370

94-
return false;
95-
}
96-
97-
/// <summary>
98-
/// Inequality operator.
99-
/// </summary>
100-
/// <param name="entity1">First entity.</param>
101-
/// <param name="entity2">Second entity.</param>
102-
/// <returns>True, if entities are not equal by identity. Otherwise, false.</returns>
103-
public static bool operator !=(Entity entity1, Entity entity2)
104-
{
105-
return !(entity1 == entity2);
71+
return Id == entity.Id;
10672
}
10773

108-
/// <summary>
109-
/// Generate hash code from ID.
110-
/// </summary>
111-
/// <returns>Hash code generated from ID.</returns>
112-
public override int GetHashCode()
113-
{
114-
return Id.GetHashCode();
115-
}
74+
#endregion Methods
11675
}
11776
}

Tests/Xer.DomainDriven.Tests/AggregateRootTests.cs

Lines changed: 4 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -9,134 +9,6 @@ namespace Xer.DomainDriven.Tests
99
{
1010
public class AggregateRootTests
1111
{
12-
#region Equality
13-
14-
public class Equality
15-
{
16-
[Fact]
17-
public void EqualsShouldBeTrueIfSameId()
18-
{
19-
var id = Guid.NewGuid();
20-
var aggregateRoot1 = new TestAggregateRoot(id);
21-
var aggregateRoot2 = new TestAggregateRoot(id);
22-
23-
// Same ID, should be equal.
24-
aggregateRoot1.Equals(aggregateRoot2).Should().BeTrue();
25-
}
26-
27-
[Fact]
28-
public void EqualsShouldBeTrueIfSameReference()
29-
{
30-
var aggregateRoot1 = new TestAggregateRoot(Guid.NewGuid());
31-
var sameReference = aggregateRoot1;
32-
33-
// Same ID, should be equal.
34-
aggregateRoot1.Equals(sameReference).Should().BeTrue();
35-
}
36-
37-
[Fact]
38-
public void ObjectEqualsShouldBeTrueIfSameId()
39-
{
40-
var id = Guid.NewGuid();
41-
var aggregateRoot1 = new TestAggregateRoot(id);
42-
var aggregateRoot2 = new TestAggregateRoot(id);
43-
44-
// Same ID, should be equal.
45-
aggregateRoot1.Equals((object)aggregateRoot2).Should().BeTrue();
46-
}
47-
48-
[Fact]
49-
public void ObjectEqualsShouldBeTrueIfSameReference()
50-
{
51-
var aggregateRoot1 = new TestAggregateRoot(Guid.NewGuid());
52-
var sameReference = aggregateRoot1;
53-
54-
// Same ID, should be equal.
55-
aggregateRoot1.Equals((object)sameReference).Should().BeTrue();
56-
}
57-
58-
[Fact]
59-
public void EqualityOperatorShouldBeTrueIfSameId()
60-
{
61-
var id = Guid.NewGuid();
62-
var aggregateRoot1 = new TestAggregateRoot(id);
63-
var aggregateRoot2 = new TestAggregateRoot(id);
64-
65-
// Same ID, should be equal.
66-
(aggregateRoot1 == aggregateRoot2).Should().BeTrue();
67-
}
68-
69-
[Fact]
70-
public void EqualityOperatorShouldBeTrueIfSameReference()
71-
{
72-
var aggregateRoot1 = new TestAggregateRoot(Guid.NewGuid());
73-
var sameReference = aggregateRoot1;
74-
75-
// Same ID, should be equal.
76-
(aggregateRoot1 == sameReference).Should().BeTrue();
77-
}
78-
79-
[Fact]
80-
public void ShouldNotBeEqualIfSameIdButDifferentType()
81-
{
82-
var id = Guid.NewGuid();
83-
var aggregateRoot1 = new TestAggregateRoot(id);
84-
var aggregateRoot2 = new NoApplierAggregateRoot(id);
85-
86-
// Same ID, should be equal.
87-
aggregateRoot1.Should().NotBe(aggregateRoot2);
88-
}
89-
}
90-
91-
#endregion Equality
92-
93-
#region GetHashCodeMethod
94-
95-
public class GetHashCodeMethod
96-
{
97-
[Fact]
98-
public void ShouldReturnTheSameValueForSameInstance()
99-
{
100-
var aggregateRoot = new TestAggregateRoot(Guid.NewGuid());
101-
int hashCode1 = aggregateRoot.GetHashCode();
102-
int hashCode2 = aggregateRoot.GetHashCode();
103-
104-
hashCode1.Should().Be(hashCode2);
105-
}
106-
107-
[Fact]
108-
public void ShouldBeSearcheableInHashSet()
109-
{
110-
var id = Guid.NewGuid();
111-
var aggregateRoot1 = new TestAggregateRoot(id);
112-
var aggregateRoot2 = new TestAggregateRoot(id);
113-
114-
var hashSet = new HashSet<TestAggregateRoot>();
115-
hashSet.Add(aggregateRoot1);
116-
117-
// Should be searcheable because aggregate roots are equal by ID.
118-
hashSet.Contains(aggregateRoot1).Should().BeTrue();
119-
hashSet.Contains(aggregateRoot2).Should().BeTrue();
120-
}
121-
122-
[Fact]
123-
public void ShouldBeSearcheableInDictionary()
124-
{
125-
var id = Guid.NewGuid();
126-
var aggregateRoot1 = new TestAggregateRoot(id);
127-
var aggregateRoot2 = new TestAggregateRoot(id);
128-
129-
var dictionary = new Dictionary<TestAggregateRoot, TestAggregateRoot>(1);
130-
dictionary[aggregateRoot1] = aggregateRoot1;
131-
132-
// Should be searcheable because aggregate roots are equal by ID.
133-
dictionary[aggregateRoot1].Should().Be(aggregateRoot1);
134-
dictionary[aggregateRoot2].Should().Be(aggregateRoot1);
135-
}
136-
}
137-
138-
#endregion GetHashCodeMethod
139-
14012
#region ApplyDomainEventMethod
14113

14214
public class ApplyDomainEventMethod
@@ -173,7 +45,7 @@ public void ShouldThrowIfNoDomainEventApplierIsRegistered()
17345

17446
#region GetUncommittedDomainEventsMethod
17547

176-
public class GetUncommittedDomainEventsMethod
48+
public class GetDomainEventsMarkedForCommitMethod
17749
{
17850
[Fact]
17951
public void ShouldIncludeAppliedDomainEvent()
@@ -190,9 +62,9 @@ public void ShouldIncludeAppliedDomainEvent()
19062
}
19163
}
19264

193-
#endregion GetUncommittedDomainEventsMethod
65+
#endregion GetDomainEventsMarkedForCommitMethod
19466

195-
#region ClearUncommitedDomainEventsMethod
67+
#region MarkDomainEventsAsCommittedMethod
19668

19769
public class ClearUncommitedDomainEventsMethod
19870
{
@@ -216,6 +88,6 @@ public void ShouldRemoveAllAppliedDomainEvents()
21688
}
21789
}
21890

219-
#endregion ClearUncommitedDomainEventsMethod
91+
#endregion MarkDomainEventsAsCommittedMethod
22092
}
22193
}

0 commit comments

Comments
 (0)