8

I'm now starting out on DDD, I've already found a nice implementation for ValueObject but I cant seem to find any good implementation for Entities, i want a generic base entity type which will have an ID(needed by the specification) and implement currect equality operations.

Whats the most elegent solution?

3 Answers 3

19

The only characteristic of an Entity is that it has a long-lived and (semi-)permanent identity. You can encapsulate and express that by implementing IEquatable<T>. Here's one way to do it:

public abstract class Entity<TId> : IEquatable<Entity<TId>>
{
    private readonly TId id;

    protected Entity(TId id)
    {
        if (object.Equals(id, default(TId)))
        {
            throw new ArgumentException("The ID cannot be the default value.", "id");
        }

        this.id = id;
    }

    public TId Id
    {
        get { return this.id; }
    }

    public override bool Equals(object obj)
    {
        var entity = obj as Entity<TId>;
        if (entity != null)
        {
            return this.Equals(entity);
        }
        return base.Equals(obj);
    }

    public override int GetHashCode()
    {
        return this.Id.GetHashCode();
    }

    #region IEquatable<Entity> Members

    public bool Equals(Entity<TId> other)
    {
        if (other == null)
        {
            return false;
        }
        return this.Id.Equals(other.Id);
    }

    #endregion
}
Sign up to request clarification or add additional context in comments.

10 Comments

+1. Is this a normal approach for a large app? A question I asked today is attracting criticism of the Entity base class approach: softwareengineering.stackexchange.com/questions/364870/…. However, it works very well for us. It was even described as anaemic in one of the comments.
@w0051977 I wouldn't write it like that today. It's true that the characteristic of an Entity is that it's the same Entity if it has the same ID, but that implementation of Equals isn't helpful in actual code bases.
@Mark Seemann, what is the modern day alternative? I was watching a Pluralsight course recently, which built this: github.com/vkhorikov/DddInAction/blob/master/… for the entity and this: github.com/vkhorikov/DddInAction/blob/master/… for the ValueObject. Is there another alternative? Thanks.
@w0051977 I shan't pretend to speak for anyone but myself, but certainly not something like that. For me, the alternative's FP.
@AndresHernandez As I've written in a comment from 2018, I wouldn't write code like this today, but if you need a Guid ID, set the type argument TId to Guid in the above. If that doesn't answer your question, consider posting a new question here on Stack Overflow. You can ping me with the link if you do.
|
3

For implementation of correct equality operations I recommend to have a look on a base class of domain entities in Sharparchitecture - https://github.com/sharparchitecture/Sharp-Architecture/blob/master/Solutions/SharpArch.Domain/DomainModel/EntityWithTypedId.cs . It has implementation of all required functionality. And have a look on some other code there, IMO, it will be very useful for you and your case.

Comments

0

I am not sure if you are after a specific library/sample code or guidelines. A good DDD solution will use factory for instantiation, persistency separated from the domain model (most ORM tends to bundle the two together), clearly define domain boundary, enforcing fields and operations through interface.

I would strongly recommend the book Applying DDD and Patterns book by Jimmy Nilson. It discusses in depth about DDD and best practices. The examples are in C# as well which will suit your project.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.