Search code examples
domain-driven-designadministration

DDD: Administration and encapsulation


What are your methods to deal with the communication of an admin panel with a domain in the case of changing values of properties of an entity without breaking the encapsulation?

public class Book : Entity {

    public Book(string title, string author, string description, decimal price, short publicationYear) {

        Title = title;
        Author = author;
        Description = description;
        Price = price;
        PublicationYear = publicationYear;
    }

    public string Title { get; private set; }

    public string Author { get; private set; }

    public string Description { get; private set; }

    public decimal Price { get; private set; }

    public short PublicationYear { get; private set; }
}

Solution

  • The only way to not break encapsulation is to include some parts of the presentation logic into the object itself. Not the details, mind you, but the parts which are highly coupled to this object.

    I would do something like this (pseudo-code):

    public class Book {
        public Book(...) {
            ...
        }
    
        public InputComponent<Book> createAdminView() {
            return new FormGroup<Book>(
                new TextInput(title),
                new TextInput(author),
                ...);
        }
    }
    

    This way there is no need to publish any of the internal data fields of the object, nobody needs to know how to book looks like, and all changes related to the object will be localized.

    In fact, I've been doing this for a couple for years now, and this design results in much easier to maintain code. Have a look at my presentation about Object-Oriented Domain-Driven Design to find out more: https://speakerdeck.com/robertbraeutigam/object-oriented-domain-driven-design