首页 > 解决方案 > DDD:管理和封装

问题描述

在不破坏封装的情况下更改实体属性值的情况下,您有哪些方法来处理管理面板与域的通信?

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; }
}

标签: domain-driven-designadministration

解决方案


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

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

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


推荐阅读