That's typical equivalence problem and looks like accepted answer is not a good one. I'll try to explain why.
Imagine the following - you have to write integration test on your backend to assure it stores your domain object correcty.
You have a code:
[TestMethod]
[Description(@"Sequentially perform operations
1. Save new item in DB
2. Get same Item from DB
Ensure that saved and get Items are equivalent")]
public void Repository_Create_Test()
{
var initialItem = GetTestItem();
//create item and check it is created correct
initialItem.ID = repository.Create(initialItem, userID, ownerID);
Item resultItem = repository.GetById(initialItem.ID, ownerID);
resultItem.Should().NotBeNull();
Assert.AreEqual(initialItem, resultItem);
}
So, you need to verify that the object read from storage is absolute equivalent of the object that we've send to the storage. Overriding Equals
is an easy first guess here. For this case we need to setup Equals
to compare all objects fields. But from DDD perspective thats just plain wrong. Domain Entities are distinguished by immutable key (or primary key) and not by all the mutable fields. So, if we are modelling HR domain and hypothetical 'Mister X' has a new phone number, he still remains the very same 'Mister X'.
All that said, I've currently using FluentAssertions Framework that has rather powerful facility for equivalence check. Like this:
resultItem.ShouldBeEquivalentTo(initialItem);