【发布时间】:2021-07-13 04:16:02
【问题描述】:
在我看到的 95% 的示例中,人们将 @Entity 或 @Document 注释添加到他们的域对象中。
我想创建一个可以轻松更改持久层的应用程序。
应该可以将设置从SQL DB 切换到 e.x. MongoDB等
我想让我的领域对象完全独立于持久层。
Item 是域对象。
public interface ItemsRepository {
List<Item> getItems();
}
每个ItemsRepository 实现都有自己的专用持久层对象。对于 SQL,假设是 ItemEntity 类,对于 Mongo ItemDocument 类。并且每个持久化对象都有与领域对象之间的转换。
这样的方法可以接受吗?如果不是,解决该问题的最佳行业模式是什么?
【问题讨论】:
-
Spring Data 为你做这件事。
-
"这样的方法可以接受吗?" - 是的。这是interface segregation principle,属于SOLID principles。
-
更正:是Separation of Concerns principle,不是接口隔离原则。本质上,持久性是一个单独的关注点,因此应该被分离出来(完全包括特定持久层的数据类)。
-
@BoristheSpider 但要使用
Spring Data JPA或Spring Data MongoDb,您仍然需要拥有这些专用实体/文档。
标签: java design-patterns persistence data-access-layer domain-object