Blog

Filter posts by Category Or Tag of the Blog section!

How to persist a list of value objects in Domain Driven Design

Thursday, 06 February 2014

I'm really interested in to write about domain driven design, but it doesn't come into a simple blog post! real-world application in Domain-driven design approach is usually big, complex and hard to understand the business. I'm gonna to give an example about the persisting the list of value objects, it's just an implementation example that everybody knows the business.

Suppose that we are going to persist the list of tags with a blog post. You know, in DDD  value objects should persist with aggregate root and to doing any kind of operation with value object you should talk to aggregate root.

 Fire up visual studio and create a console application or a class library, and put these below infrastructural classes of DDD in it (you can find the implementation of these class on the web, it's not the purpose of this post):

 

 public class EntityBase<T>
    {
        public T Identity { get; set; }
    }

 public interface IAggegateRoot
    {
    }

  public class ValueObjectBase
    {
    }

 

 Now, create the aggregateRoot class:

 

  public class Post : EntityBase<int>, IAggegateRoot
    {
    }

 

as I mentioned, I'm gonna to persist a list of value object(BlogTag). so create the blogTag with this definition: 

 

public class Tag : ValueObjectBase
    {
        private readonly IList<Tag> _tags;

        public Tag(IEnumerable<Tag> tags)
        {
            _tags = new List<Tag>(tags);
        }

        public string Name { get; private set; }

        public IEnumerable<Tag> Tags
        {
            get { return _tags; }
        }
    }

 

This class has a property named Name and a constructor which initialize the list of Tags, Now refer to Post class (AggregateRoot) and change implement it like this:

 

 public class Post : EntityBase<int>, IAggegateRoot
    {
        private readonly IList<Tag> _tags;
        public Post(IEnumerable<Tag> tags, string postTitle, string postBody)
        {
            _tags = new List<Tag>(tags);
            PostTitle = postTitle;
            PostBody = postBody;
        }
        public IEnumerable<Tag> Tags
        {
            get { return _tags; }
        }

        public string PostTitle { get; set; }
        public string PostBody { get; set; }
    }

 

This class is an entity and also the root of our aggregate, this constructor will be used in the service layer, before starting the service implementation create your repository stuff :

 

public interface IPostRepository
    {
        void CreatePost(Post post);
    }

 public class PostRepository : IPostRepository
    {
        public void CreatePost(Post post)
        {
            //Porsist on Dataabase
        }
    }

 

now it's time jump on service implementation, first create your PostViewModel Class:

 

 public class PostViewModel
    {
        public string PostTitle { get; set; }
        public string PostBody { get; set; }
    }

 

 as I'm interested in request-response pattern so let's create the messages of  CreatingPost:

 

 public class CreatePostRequest
    {
        public PostViewModel PostViewModel { get; set; }
        public IEnumerable<Tag> Tags { get; set; }

        public CreatePostRequest(IEnumerable<Tag> tags)
        {
            Tags = new List<Tag>(tags);
        }
    }

 

As you saw above when I wanted to create the Tag class I created the constructor with accepting the IEnumerable<T> as I want to send a list of tags to persisting, create the response message: 

 

 public class CreatePostResponse : Response
    {
    }
    public class Response
    {
        public Status Status { get; set; }
    }
    public enum Status
    {
        Success,
        Fail
    }

 

Finally, it's time to implement the Service class and the goal of this article:

 

  public interface IPostService
    {
        CreatePostResponse CreatePost(CreatePostRequest request);
    }

 public class PostService : IPostService
    {

        private readonly IPostRepository _postRepository;

        public PostService(IPostRepository postRepository)
        {
            _postRepository = postRepository;
        }

        public CreatePostResponse CreatePost(CreatePostRequest request)
        {
            var response = new CreatePostResponse();
            try
            {
                var tag = new Tag(request.Tags);
                var post = new Post(tag.Tags, request.PostViewModel.PostTitle, request.PostViewModel.PostBody);
                _postRepository.CreatePost(post);
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.Message);
            }

            return response;
        }
    }

 

Take a deeper look at the CreatePost method of this Service class, by creating the constructor in Tag and Blog you can pass a list of tags via request and put them in Post class constructor and finally send the object to the repository for persisting.

you can get the source code from here, Cheers!

Category: Software

Tags: DDDesign

comments powered by Disqus