samedi 22 septembre 2018

Separate View object for data manipulation and presentation

I have two Domain Models - Employee and Department. The class structures look like below

Employee

@Entity
@Table(name="emp")
class Employee {
@Column(name="name")
private String name;

@Id
private UUID id;

@ManyToOne
@JoinColumn(name="dept_id")
private Department dept;
....
}

Department

@Entity
@Table(name="dept")
class Department {
@Id
private UUID id;

@Column(name="name")
private String name;

@Column(name="description")
private String description;
....
}

These domain objects are used only in service and repository layers. Controllers use only View objects.

I have following two views

  1. Creates an employee object. Along with employee attributes, it also accepts department reference.
  2. Displays employee information along with department details.

Should I be creating two different view objects as below

Create/Edit

class EmployeeVO {
private String name;
private UUID id;
private UUID deptID;
}

(Intention is to avoid passing department name and other department details while creating/editing Employee. Also, I do not want API doc generators to list all irrelevant attributes.)

View

class EmployeeViewVO {
  private String name;
  private UUID id;
  private DepartmentViewVO deptView;
}

class DepartmentViewVO{
  private id;
  private name;
}

(Retrieve employee information in one API call.)

What is the recommended approach for such a scenario, is it good practice to have one View Object meant for data manipulation and another view object for presentation?

Aucun commentaire:

Enregistrer un commentaire