Java对象构造函数与良好实践

huangapple go评论62阅读模式
英文:

Java Object Constructors and Good Practices

问题

我目前正在开发一个Java EE应用程序,因此我的应用程序服务器端有多个层次(简要地):

  • 实体和DAO,
  • 业务对象(BO)和服务,
  • 数据传输对象(DTO)和控制器。

我的问题是,由于我总是将实体转换为BO以防止数据库覆盖并减轻工作量,是否在BO中包含一个以实体为参数的构造函数是一个良好的做法?

以下是您的代码:

Livraison.java

package my.tree.model;

导入语句

@Entity
public class Livraison {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private LocalDate date;
    @ManyToOne
    @JoinColumn(name = "FournisseurId", nullable = false)
    private Fournisseur fournisseur;
    @ManyToOne
    @JoinColumn(name = "CategorieId", nullable = false)
    private Categorie categorie;
    private Integer netTotal;
    private String prefixCode;
    private Integer compte;
    @Transient
    private List<Palette> palettes = new ArrayList<>();

    public Livraison() {}

    public Integer getId() {
        return id;
    }
    
    // 获取器和设置器
}

LivraisonBo.java

package my.tree.bo;

import Livraison;

public class LivraisonBo {
    private int id;
    private String date;
    private int fournisseurId;
    private int categorieId;
    private Integer netTotal;
    private String prefix;
    private Integer compte;

    public LivraisonBo() {
    }

    // 经典构造函数
    public LivraisonBo(int id, String date, int fournisseurId, int categorieId, Integer netTotal, String prefix, Integer compte) {
        this.id = id;
        this.date = date;
        this.fournisseurId = fournisseurId;
        this.categorieId = categorieId;
        this.netTotal = netTotal;
        this.prefix = prefix;
        this.compte = compte;
    }

    // 以实体为参数的构造函数
    public LivraisonBo(Livraison l) {
        this.id = l.getId();
        this.date = l.getDate().toString();
        this.fournisseurId = l.getFournisseur().getId();
        this.categorieId = l.getCategorie().getId();
        this.netTotal = l.getNetTotal();
        this.prefix = l.getPrefixCode();
        this.compte = l.getCompte();
    }
    
    // 获取器和设置器
}

这是否是一个良好的做法?还是应该将此方法放在一个服务类中?

感谢您的帮助。

英文:

I am currently working on a Java EE application and so I have multiple layers in my application server-side (succintly) :

  • Entities and DAO,
  • BO and services
  • DTOs and Controller

My question is, since I always convert Entities to BO to prevent DB overwrites and reducing workload, is it good practice to include a constructor in BO that takes an Entity as parameters ?

See my code below :

Livraison.java

package my.tree.model;
imports
@Entity
public class Livraison {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private LocalDate date;
@ManyToOne
@JoinColumn(name = &quot;FournisseurId&quot;, nullable = false)
private Fournisseur fournisseur;
@ManyToOne
@JoinColumn(name = &quot;CategorieId&quot;, nullable = false)
private Categorie categorie;
private Integer netTotal;
private String prefixCode;
private Integer compte;
@Transient
private List&lt;Palette&gt; palettes = new ArrayList&lt;&gt;();
public Livraison() {}
public Integer getId() {
return id;
}
//getters and setters
}

LivraisonBo.java

package my.tree.bo;
import Livraison;
public class LivraisonBo {
private int id;
private String date;
private int fournisseurId;
private int categorieId;
private Integer netTotal;
private String prefix;
private Integer compte;
public LivraisonBo() {
}
//classic constructor
public LivraisonBo(int id, String date, int fournisseurId, int categorieId, Integer netTotal, String prefix, Integer compte) {
this.id = id;
this.date = date;
this.fournisseurId = fournisseurId;
this.categorieId = categorieId;
this.netTotal = netTotal;
this.prefix = prefix;
this.compte = compte;
}
// constructor taking an entity as parameter
public LivraisonBo(Livraison l) {
this.id = l.getId();
this.date = l.getDate().toString();
this.fournisseurId = l.getFournisseur().getId();
this.categorieId = l.getCategorie().getId();
this.netTotal = l.getNetTotal();
this.prefix = l.getPrefixCode();
this.compte = l.getCompte();
}
//getters and setters
}

Is it good practice ? Or should I put this method in a service class ?
Thank you for your help.

答案1

得分: 1

依我之见,最好创建转换器类来执行从实体到业务对象(BO)的转换和相反的转换。

如果您为BO创建一个以实体为参数的构造函数,这样一来您的BO将依赖于实体。这不是一个好的做法。因为那么那些使用BO的人也需要拥有实体的定义。

而且,使用多个参数的第一种选项也不是一个好的做法。保持参数少于5个被认为是一个良好的做法。

如果您创建一个不同的转换器类,它有两个参数:实体和BO,将使代码更加清晰易读。同时,也会减少代码中不必要的依赖关系。

英文:

In my opinion rather create Converter classes that will do the conversion to and from Entities and BO.

If you create BO constructor with Entity as parameter, In that way your BO will have dependency on Entities. Which is not a good practice. Because then the one who will be using BO will need to have to have definition of Entities also available with them.

And the first option with multiple parameter is also not a good practice. Keeping parameter less than 5 is considered good practice.

If you create a different converter class with two parameters Entity and BO, will make code more cleaner and readable. As well as, will reduce unwanted dependencies in code.

huangapple
  • 本文由 发表于 2020年9月7日 17:24:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63774799.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定