domain-driven-design - 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; }
}
解决方案
不破坏封装的唯一方法是将表示逻辑的某些部分包含到对象本身中。请注意,不是细节,而是与该对象高度耦合的部分。
我会做这样的事情(伪代码):
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
推荐阅读
- excel - Microsoft Access 数据库引擎找不到对象工作表 - ACE OLEDB
- go-templates - 连接字符串中的变量
- sql - 无法在 Informix 中传递变量
- scala - 激活后每 X 秒调用一次函数 - Scala
- node.js - 将 crypto.js 值传递给脚本 nonce
- python - 如何使用 T-sne 可视化使用训练有素的 Glove 库提取的词嵌入?
- blazor - Blazor 服务器端应用程序如何在加载时从 http://hostname 重定向到 http://hostname/route1?
- excel - 复制一系列单元格并选择一个新单元格后,PasteSpecial 失败
- python - 将 .netrc 与 pip 和 gitlab pypi 一起使用
- python - 如何让星号来回打印开始和停止?