如何处理“已计算" Play框架模型中的字段 [英] How to handle the "calculated" fields in a Play Framework model

查看:96
本文介绍了如何处理“已计算" Play框架模型中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Play Framework中,定义具有某些计​​算"字段而不是直接映射到与实体相关的任何字段的对象的最佳做法"是什么?

例如,我想为具有标价"和实际价格"的产品"定义模型.

就数据库而言,清单价格"直接映射到模型类中的"int listPrice".但是,实际价格"是使用与产品"本身无关的其他数据(例如,一般商店折扣,部门特定折扣等,其中涉及某些业务逻辑")从标价"计算得出的.

由于我的应用程序需要公开REST API(而不仅仅是Web应用程序)-我真的很想扩展"Product"类以支持"calculated"字段,因此"MyProduct.listPrice"和"MyProduct"都应扩展. finalPrice".

是否可以将临时"成员添加到模型类?如果不是,是否应该定义一个从模型派生的类并使用它?

感谢任何提示.

最大

解决方案

这是解决方法

@Entity public class Product extends Model {
  ...
  public int listPrice;
  @Transient int realPrice;
  public int getRealPrice() {
    return calcRealPrice(listPrice);
  }
  ...
}

What are the "best practices" to define - in Play Framework - objects that have some fields that are "calculated", rather than directly mapped to any of the fields related to an entity?

For example, I would like to define a Model for a "product" having a "list price" and "real price".

In terms of the DB, the "list price" is directly mapped to the "int listPrice" in the model class. However, the "real price" is calculated from the "list price" using additional data not related to the "product" itself (like, general store discount, department-specific discount etc. - some "business logic" involved).

Since my app needs to expose REST API (and not just a web app) - I would really want to extend the "Product" class to support the "calculated" field, so both "MyProduct.listPrice" and "MyProduct.finalPrice" will be supported.

Is it possible to add "transient" members to the model class? If not, should define a class derived from the model, and use it?

Thanks for any hint.

Max

解决方案

Here is the way to go

@Entity public class Product extends Model {
  ...
  public int listPrice;
  @Transient int realPrice;
  public int getRealPrice() {
    return calcRealPrice(listPrice);
  }
  ...
}

这篇关于如何处理“已计算" Play框架模型中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆