DDD:管理和封装

DDD: Administration and 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; }
}

不破坏封装的唯一方法是将表示逻辑的某些部分包含到对象本身中。请注意,不是细节,而是与该对象高度耦合的部分。

我会做这样的事情(伪代码):

public class Book {
    public Book(...) {
        ...
    }

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

这样就不需要发布对象的任何内部数据字段,没有人需要知道书的样子,所有与对象相关的更改都将本地化。

事实上,我已经这样做了好几年了,这种设计使代码更容易维护。查看我关于面向对象领域驱动设计的演示文稿以了解更多信息:https://speakerdeck.com/robertbraeutigam/object-oriented-domain-driven-design