gpt4 book ai didi

java - hibernate 业务对象可以用作整洁架构中的实体吗?

转载 作者:行者123 更新时间:2023-12-04 11:46:46 24 4
gpt4 key购买 nike

在我们的项目中,我们使用 eclipse hibernate 插件生成的类进行持久化。
生成的类具有以下结构。

MyClass extends BaseMyClass //POJO's, that are refenced in the hbm
files
MyClassDAO extends BaseMyClassDAO //DAO objects that use hibernate
session objects to provide CRUD API's for working with DB
我的问题是,如果我们使用映射文件中使用的 POJO 类作为最内层的实体,是否会违反 Uncle Bobs 的清洁架构。
在这种情况下,hibernate 特定的 DAO 类将属于最外层,而 UseCases 层将通过提供要实现的接口(interface)与该层进行通信。

最佳答案

鲍勃叔叔在挪威的一次演讲中展示了这张幻灯片,对此发表了评论:
Entity Gateway
鲍勃叔叔说:

There is no Hibernate above the line. If you are using Hibernate it goes below the line. The application does not know that you are using that framework. It goes below the line. And Hibernate is a lovely tool. It's very good for gathering data out of the database and turning it into data structures. Very nice! But you don't want your application to know that you are using it. You put all that stuff below the line.

Robert C Martin - Clean Architecture, NDC 2012 (53:53 - 54:18)


因此,如果您在实体上使用 Hibernate 注释,则将域对象与数据库层的详细信息混合在一起。
一些开发人员认为注解不是那么强的依赖关系,因为如果它们在运行时不可用,它们就根本不存在。确实如此,但您还必须考虑其他原则。
如果您将 Hibernate 注释放在实体上,实体类现在有两个不同的更改原因:域逻辑和数据库映射。
这违反了 single responsibility principle .因此数据库映射将影响您的域对象。
我想有很多困惑,因为 重载术语实体 .当鲍勃叔叔谈论实体时,他指的是领域。在 Hibernate 中实体是指数据库记录。
这就是为什么我通常使用术语“域实体”或“数据库实体”来区分它们。

关于java - hibernate 业务对象可以用作整洁架构中的实体吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42175497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com