在持久化实体中使用非持久化相关的方法和字段
我有一个与持久性相关的 java-ee 代码,我需要重写该代码才能使该应用程序在 Google App Engine 及其数据存储上运行。当我使用 java-ee 持久性提供程序时,我使用 IDE 生成持久性实体,并保持它们的原样,以防需要重新生成它们。然而,为应用程序引擎存储自动生成实体类是不可能的,我想知道是否有任何充分的理由为什么我应该保持实体类干净并且不使用与持久性不直接相关的字段和方法。如果重要的话,我使用 objectify 作为持久性提供者。
这是一个例子:
public class Dog {
@Id Long id;
@Transient Integer barkCount;
public String bark() {
barkCount++;
return "woof-woof";
}
public String getAgeEstimation() {
switch(barkCount) {
case 0: return "unborn (or very lazy)";
case 10000: return "this is very old dawg";
default: return "you get the idea :)";
}
}
}
I have a persistence related java-ee code which I need to rewrite to make the app work on the Google App Engine and its data storage. When I use java-ee persistence providers, I generate persistence entities with my IDE and I keep them the way they are in case I need to regenerate them. However autogenerating entity classes for app-engine storage is not possible and I would like to know if there are any good reasons why I should keep the entity classes clean and not use fields and methods not directly related to persistence. I use objectify as persistence provider if it matters.
Here's an example:
public class Dog {
@Id Long id;
@Transient Integer barkCount;
public String bark() {
barkCount++;
return "woof-woof";
}
public String getAgeEstimation() {
switch(barkCount) {
case 0: return "unborn (or very lazy)";
case 10000: return "this is very old dawg";
default: return "you get the idea :)";
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加与持久性不直接相关的方法,但使用从数据存储中检索的数据进行业务操作并实际实现富域模型(与 贫血域模型)。
因此,不要使用过程式服务,而是将业务逻辑移至丰富的域对象内。例如(取自域驱动设计使用 Java EE 6):
There is nothing wrong with adding methods not directly persistence related but doing business things with data retrieved from the datastore - and actually implementing a Rich Domain Model (vs an Anemic Domain Model).
So, instead of having procedural style services, move the business logic inside a rich domain object. For example (taken from Domain-driven design with Java EE 6):