问题描述
在Google的 RequestFactory
教程中,他们建议将我的持久性逻辑(在本例中为Hibernate)放入Entity类中。然后他们提出了这样一个问题:如果你不想在一个实体中实现持久化代码呢?并继续解释另一种方法。
我的问题:哪种更好,将持久性逻辑放在Entity类中,还是将所有持久性逻辑放在单独的类中? p>
感谢任何信息。
-tjw
将持久性代码放入实体本身是一种活动记录模式方法,同时将所有持久性逻辑保存在单独的类中是 Repository Pattern 方法。您可以使用关键字存储库模式与activerecord
来搜索有关您的问题的更多信息。 例如,活动记录模式在没有数据库的情况下对其可测试性提出批评,您可以参考这个获取更多信息。
对我来说,我更喜欢Repository Pattern,因为它是可测试的,我不喜欢持久性代码和domian业务逻辑混合在一个类中这违反了关注分离的重点。
In Google's RequestFactory
tutorials, they recommend putting my persistence logic (in my case, Hibernate) inside the Entity classes. Then they pose the question: "What if you don't want to implement persistence code in an entity itself?" and proceed to explain an alternate method.
My question: which is better, putting persistence logic inside the Entity class, or keeping all persistence logic in a separate class?
Any info is appreciated, thanks.
-tjw
Putting the persistence code in an entity itself is an Active Record Pattern approach while keeping all persistence logic in a separate class is the Repository Pattern approach. You can use the keyword repository pattern vs activerecord
to search more information about your questions.
For example ,active record pattern has a criticism about its testability without a database , you can refer to this for more info .
For me , I prefer the Repository Pattern more as it is testable and I don't like persistence codes and domian business logic are mixed in one class which violates the emphasis on separation of concerns.
这篇关于最佳实践 - 实体本身内的Hibernate持久性代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!