Say I have a class like this:

public abstract class Product 
        public Guid id {get; set;}
        public string description {get; set;}

This class is populated from the database. I am writing a unit test to decide if two products are the same. How do I establish if two entities are equal:

1) Guid only - this is the primary key from the database so is unique. Not even sure that this member should be in my class

2) Description only - this is always unique

3) GUID and Description

  • It might interest you link
    – Laiv
    Aug 17, 2017 at 12:53

2 Answers 2


When two objects are semantically the same, a comparison with .equals() should lead to a positive result. Note, that it doesn't have to be the exact same object/instance.

In a database an entity is defined by its primary key, but I think it is still possible to have two different entities, which are semantically equal. So when comparing them with .equals(), I'd expect a positive result.

Therefore, I'd go with option 2). However, it is difficult to answer this in general, because you always have to think about when two object are equal, which might vary depending on the domain you are in. So in general, this should be also part of your requirements engineering.

  • If this is "weird" then how do you compare two entities in a unit test?
    – w0051977
    Aug 17, 2017 at 12:37
  • @w0051977: By creating two entities that are equal, calling .equals() and verifying that the returned result is true? Aug 17, 2017 at 15:44

In DDD, two objects refer to the same entity if they have the same ID; so a valid question is this: how to test if two C# objects refer to the same entity?.

How to check if two entities are equal? is not really a valid question, at least not a generic one. What means for you domain that two entities are equal? Please note that this question is a perfect valid one in case of value objects but a weird one in case of entities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.