4

I am using ASP.NET MVC with EntityFramework code first.

I am attempting to create a base class for all objects that will be saved to my database (here called the Entity class).

Is there a good way of implementing IEquatable in the base class? In my implementation, I check the type and ID - is this a good way of doing it? Will this always return the ultimate derived type? What about intermediate parents?

Objects

public abstract class Entity : IEquatable<Entity>
{
    public int ID { get; set; }
    public string Name { get; set; }

    public Entity(string Name)
    {
        this.Name = Name;
    }

    public override string ToString() { return Name; }
    public override int GetHashCode() { return ID.GetHashCode(); }
    public override bool Equals(object obj) { return this == (Entity)obj; }
    public bool Equals(Entity other) { return this == other; }
    public static bool operator ==(Entity x, Entity y) { return Object.ReferenceEquals(x, y) ? true : ((object)x == null || (object)y == null) ? false : (x.GetType() == y.GetType() && x.ID.Equals(y.ID)); }
    public static bool operator !=(Entity x, Entity y) { return !(x == y); }
}

public abstract class Content : Entity
{
    public Content(string Name) : base(Name)
    {

    }
}

public class Page : Content
{
            public string Body { get; set; }
    public Page(string Name) : base(Name)
    {

    }
}

public class User : Entity
{
    public User(string Name) : base(Name)
    {

    }
}

Database

Each derived object type will normally exist in a separate database. However, there will be some objects with intermediate inherited parents which share primary keys.

Let's say that there are three tables with the following columns: Entity

  • Content
    • ID
    • Name
  • Page
    • ID
    • Body
  • User
    • ID
    • Name

Therefore, the Page and Content tables share a Primary Key.

1
  • You should only do this if ID is read only
    – paparazzo
    Mar 6, 2018 at 16:36

2 Answers 2

1
  1. There's no need to implement IEquitable, it doesn't give any value (neither performance value, no readability value).
  2. In your comparison, you rely on reference equality check only. You should check ID equality as well.
  3. Your GetHashCode() implementation is sub-optimal, you should include entity's type as well.
  4. Are you sure that all of your entities will have the Name property? In the base class, you should include properties that are presented in every entity of the bounded context.

Here I describe this topic in detail: Domain object base class.

3
  • 2
    "There's no need to implement IEquitable" - Why do you say that? There are several reasons to.
    – user585968
    Jun 24, 2020 at 5:53
  • @MickyD it only makes sense when the type is a struct. Otherwise, there's no significant performance gains. Unless you work on a software with high performance requirements and need all micro-optimizations you can get, you won't notice the difference.
    – Vladimir
    Jun 24, 2020 at 12:19
  • 1
    It's not done for performance reasons, but to help you write better code. If you have a class named Entity, and it has an Id property which is what identifies that entity in your program, and you want multiple instances of the Entity class that have the same Id, also be considered equal (for example when checking if a collection contains that object), the best way to do it would be to make the class implement IEquatable.
    – Sasino
    Jan 8, 2021 at 19:18
0

Your implementation is correct.

Be careful doing this though. If you could have objects that are partially loaded, then they will be considered equal but may not have all of the same values.

If you want to sometimes use default equals and sometimes use ID based equals, create a separate helper class implementing IEqualityComparer<Entity>.

2
  • What do you mean by partially loaded?
    – William
    Mar 24, 2014 at 2:47
  • @William, with EF this normally would only come up with relationships. If a Page has Content then you can load a Page instance from the db and not load the referenced Content. Then you may separately load the same Page instance somewhere, but do load referenced Content. Now you have two Page instances that are different, one that has Content and one that doesn't. Are they equal? The answer can be yes and no depending on the needs of the particular circumstance. There are circumstances that make them equal and some that make them not. I have run into this problem specifically. Mar 24, 2014 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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