BobRock
BobRock

Reputation: 3467

DDD Entity class

I'm trying to implement DDD approach in my head. So far I know that entity is unique and identified by combination of its attributes.

Entity is abstract class which will be implemented by other entity classes.

I know so far that Version property is used to manage concurrency. Need help with rest of this class.

I'm in process of learning DDD so please describe your thoughts on this or share useful concrete theme links.

public abstract class Entity<T>
    {
        #region Properties
        protected T _Id;
        private int _Version;
        #endregion

        private static bool IsTransient(Entity<T> obj)
        {
            return (obj != null) &&
                Equals(obj.Id, default(T));
        }

        private Type GetUnproxiedType()
        {
            return GetType();
        }

        public virtual T Id
        {
            get
            {
                return _Id;
            }
            protected set
            {
                _Id = value;
            }
        }

        public virtual int Version
        {
            get
            {
                return _Version;
            }
            set
            {
                _Version = value;
            }
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as Entity<T>);
        }

        public virtual bool Equals(Entity<T> other)
        {
            if (other == null) return false;
            if (ReferenceEquals(this, other)) return true;
            if (!IsTransient(this) &&
                !IsTransient(other) &&
                Equals(Id, other.Id))
            {
                var otherType = other.GetUnproxiedType();
                var thisType = GetUnproxiedType();
                return (thisType.IsAssignableFrom(otherType)
                    || otherType.IsAssignableFrom(thisType));
            }
            return false;
        }

        public override int GetHashCode()
        {
            if (Equals(Id, default(T)))
            {
                return base.GetHashCode();
            }
            return Id.GetHashCode();
        }

        public static bool operator ==(Entity<T> e1, Entity<T> e2)
        {
            return Equals(e1, e2);
        }

        public static bool operator !=(Entity<T> e1, Entity<T> e2)
        {
            return !Equals(e1, e2);
        }

    }

Updated

After some researching I'm concluded with this

Using Equals method will come handy when we try to compare two objects. For example imaging place in application where we have the same object in more than one instance. One from the database and one from the client. We need to find out if that object is identical and yet not the same.

For this we will be using Equals method.

Upvotes: 3

Views: 1855

Answers (1)

Frederik Gheysels
Frederik Gheysels

Reputation: 56934

An entity is not uniquely identified by the value of its attributes; An entity has an identity, but that does not mean that the identity is made up by the value of its attributes. That would mean that, if you change one if the attributes of an entity, it's identity will change, and you do not want that behaviour. (For instance, if your address changes, your identity doesn't change, you're still the same person, living at another address).

(I see that you've implemented identity/equality by using an Id property, which is a good thing).

Next to entities, you have value - objects. The identity of a value object is made up by the values of its attributes, and therefore, it is better that a value object is immutable, since, changing the value object would also mean that you change its identity.

But, what is your question ?

Upvotes: 3

Related Questions