在持久化实体中使用非持久化相关的方法和字段

发布于 2024-09-29 09:24:19 字数 639 浏览 0 评论 0原文

我有一个与持久性相关的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-10-06 09:24:19

我想知道是否有任何充分的理由为什么我应该保持实体类干净并且不使用与持久性不直接相关的字段和方法。

添加与持久性不直接相关的方法,但使用从数据存储中检索的数据进行业务操作并实际实现富域模型(与 贫血域模型)。

因此,不要使用过程式服务,而是将业务逻辑移至丰富的域对象内。例如(取自域驱动设计使用 Java EE 6):

@Entity
public class Load {

    @OneToMany(cascade = CascadeType.ALL)
    private List<OrderItem> orderItems;
    @Id
    private Long id;

    protected Load() {
        this.orderItems = new ArrayList<OrderItem>();
    }

    public int getShippingCosts() {
        int shippingCosts = 0;
        for (OrderItem orderItem : orderItems) {
            shippingCosts += orderItem.getShippingCost();
        }
        return shippingCosts;
    }
//...
}

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.

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):

@Entity
public class Load {

    @OneToMany(cascade = CascadeType.ALL)
    private List<OrderItem> orderItems;
    @Id
    private Long id;

    protected Load() {
        this.orderItems = new ArrayList<OrderItem>();
    }

    public int getShippingCosts() {
        int shippingCosts = 0;
        for (OrderItem orderItem : orderItems) {
            shippingCosts += orderItem.getShippingCost();
        }
        return shippingCosts;
    }
//...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文