Blog

Filter posts by Category Or Tag of the Blog section!

How to persist ValueObjects of DDD via Fluent Nhibernate

Tuesday, 16 July 2013

I'm not going to go throw the DDD concepts again in this article as I did before here, this post is just about how to persist the Value Objects of Domain Driven Design by Fluent NHibernate. first, create the basic configuration for Domain Driven Design (I'm using the way that I've learned in Pro Asp.net Design Pattern Book). create an interface and name it IAggregateRoot

 

    public interface IAggregateRoot
    {
    }

 

Then add the BusinessRule Class

 

    public class BusinessRule
    {
        private string _property;
        private string _rule;

        public BusinessRule(string property, string rule)
        {
            _rule = rule;
            _property = property;
        }
        public string Property
        {
            get { return _property; }
            set { _property = value; }
        }
        public string Rule
        {
            get { return _rule; }
            set { _rule = value; }
        }
    }

 

Now create the EntityBase and ValueObjectBase Classes

 

    public abstract class EntityBase<TId>
    {
        private readonly List<BusinessRule> _brokenRules = new List<BusinessRule>();
        public virtual TId Id { get; set; }
        protected abstract void Validate();
        protected internal virtual IEnumerable<BusinessRule> GetBrokenRules()
        {
            _brokenRules.Clear();
            Validate();
            return _brokenRules;
        }
        protected void AddBrokenRule(BusinessRule businessRule)
        {
            _brokenRules.Add(businessRule);
        }
        public override bool Equals(object entity)
        {
            return entity != null && entity is EntityBase<TId> && this == (EntityBase<TId>)entity;
        }
        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
        public static bool operator ==(EntityBase<TId> entity1, EntityBase<TId> entity2)
        {
            if ((object)entity1 == null && (object)entity2 == null)
            {
                return true;
            }
            if ((object)entity1 == null || (object)entity2 == null)
            {
                return false;
            }
            if (entity1.Id.ToString() == entity2.Id.ToString())
            {
                return false;
            }
            return false;
        }
        public static bool operator !=(EntityBase<TId> entity1, EntityBase<TId> entity2)
        {
            return (!(entity1 == entity2));
        }
    }

 

And then Value Object Base Class

 

    public abstract class ValueObjectBase
    {
        private readonly List<BusinessRule> _brokenRules = new List<BusinessRule>();
        protected ValueObjectBase() { }
        protected static bool EqualOperator(ValueObjectBase left, ValueObjectBase right)
        {
            if (ReferenceEquals(left, null) ^ ReferenceEquals(right, null))
            {
                return false;
            }
            return ReferenceEquals(left, null) || left.Equals(right);
        }
        protected abstract void Validate();

        public void ThrowExceptionIfInvalid()
        {
            _brokenRules.Clear();
            Validate();
            if (_brokenRules.Count <= 0) return;
            var issues = new StringBuilder();
            foreach (BusinessRule businessRule in _brokenRules)
            {
                issues.AppendLine(businessRule.Rule);
            }
        }
        protected void AddBrokenRule(BusinessRule businessRule)
        {
            _brokenRules.Add(businessRule);
        }
    }

 

Sorry, I'm not going to talk about the above configurations, but you can use them as a base configuration for DDD in every enterprise application. Now Add a class and name it Address (this is the same Value Object class that we are going to know how to persist in the repository)

 

   public class Address : ValueObjectBase
    {
        public Address(string city, string street, string zipCode)
        {
            ZipCode = zipCode;
            Street = street;
            City = city;
        }

        public string City { get; private set; }
        public string Street { get; private set; }
        public string ZipCode { get; private set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }

 

In DDD we have a repository for every aggregate, and having a value object without having an entity doesn't mean anything and also we have to make the properties as a private set because Value Object should be immutable. now add the customer class, this class is an entity and also Root of the aggregate

 

   public class Customer : EntityBase<Guid>, IAggregateRoot
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public Address Address { get; set; }

        protected override void Validate()
        {
            throw new NotImplementedException();
        }
    }

 

It's time to create the CustomerMapping and the goal of this post

 

   public class CustomerMapping : ClassMap<Customer>
    {
        public CustomerMapping()
        {
            Table("Customer");
            Not.LazyLoad();
            Id(c => c.Id).GeneratedBy.GuidComb();
            Map(c => c.UserName).Not.Nullable();
            Map(c => c.Password).Not.Nullable();
            Component(c => c.Address, a =>
                {
                    a.Map(x => x.City).Column("City").Not.Nullable();
                    a.Map(x => x.Street).Column("Column").Not.Nullable();
                    a.Map(x => x.ZipCode).Column("ZipCode").Not.Nullable();
                });
        }
    }

 

By using component of fluent NHibernate you can easily map the value object of DDD. Notice that this mapping in Database will be a table named Customer with id, userName, Password, city, Street, ZipCode fields.

comments powered by Disqus