เมื่อเราสร้าง instance ใหม่ของ Entity เราต้องการใช้ constructor ที่รับ state ไว้มากพอที่จะระบุตัวตนของมันได้อย่างสมบูรณ์ และทำให้ client สามารถค้นหามันได้ เมื่อใช้ early identity generation constructor ที่ออกแบบอย่างถูกต้องจะรับ parameter อย่างน้อยก็คือ unique identity หาก Entity ถูกค้นหาด้วยวิธีอื่น เช่น ด้วยชื่อหรือคำอธิบาย เราก็ควรรวมสิ่งเหล่านั้นเข้าเป็น constructor parameters ด้วย
บางครั้ง Entity ต้องคง invariant หนึ่งรายการขึ้นไป Invariant คือ state ที่ต้องคงความสอดคล้องกันในเชิง transactional ตลอด life cycle ของ Entity เรื่องของ invariants เป็นความรับผิดชอบของ Aggregates แต่เนื่องจาก Aggregate Root เป็น Entity อยู่เสมอ เราจึงกล่าวถึงมันในที่นี้ หาก Entity มี invariant ที่เป็นจริงได้ด้วย state ที่ไม่เป็น null ของ object ที่มันเก็บไว้ภายใน หรือถูกคำนวณจาก state อื่น state นั้นจะต้องถูกส่งผ่าน parameter ของ constructor หนึ่งตัวหรือมากกว่า
object User ทุกตัวต้องมี tenantId , username , password และ person กล่าวอีกนัยหนึ่งคือ หลังจากการสร้างเสร็จสิ้นอย่างถูกต้องแล้ว การอ้างอิงถึง instance variables ที่ประกาศไว้เหล่านี้จะต้องไม่เป็น null ไม่ว่าในกรณีใด ๆ constructor ของ User และ instance variable setters ของมันจะช่วยรับประกันสิ่งนี้:
public class User extends Entity {
...
protected User(TenantId aTenantId, String aUsername,
String aPassword, Person aPerson) {
this();
this.setPassword(aPassword);
this.setPerson(aPerson);
this.setTenantId(aTenantId);
this.setUsername(aUsername);
this.initialize();
}
...
protected void setPassword(String aPassword) {
if (aPassword == null) {
throw new IllegalArgumentException(
"The password may not be set to null.");
}
this.password = aPassword;
}
protected void setPerson(Person aPerson) {
if (aPerson == null) {
throw new IllegalArgumentException(
"The person may not be set to null.");
}
this.person = aPerson;
}
protected void setTenantId(TenantId aTenantId) {
if (aTenantId == null) {
throw new IllegalArgumentException(
"The tenantId may not be set to null.");
}
this.tenantId = aTenantId;
}
protected void setUsername(String aUsername) {
if (this.username != null) {
throw new IllegalStateException(
"The username may not be changed.");
}
if (aUsername == null) {
throw new IllegalArgumentException(
"The username may not be set to null.");
}
this.username = aUsername;
}
...
}
การออกแบบของ class User แสดงให้เห็นถึงพลังของ self-encapsulation (การห่อหุ้มตัวเอง) constructor จะมอบหมายการกำหนดค่าให้กับ instance variables แก่ attribute/property setters ภายในของมันเอง ซึ่งให้การ self-encapsulation แก่ตัวแปรเหล่านั้น Self-encapsulation ทำให้ setter แต่ละตัวสามารถกำหนดเงื่อนไขตามสัญญาที่เหมาะสมสำหรับการกำหนดค่า state ในส่วนของตนได้ setter แต่ละตัวจะยืนยัน constraint การไม่เป็น null ในนามของ Entity ซึ่งเป็นการบังคับใช้ instance contract การยืนยันเหล่านี้เรียกว่า guards (ดู “ Validation ”) ดังที่ได้กล่าวไว้ก่อนหน้านี้ในหัวข้อ “ Identity Stability ” เทคนิค self-encapsulation ของ setter methods เหล่านี้สามารถซับซ้อนยิ่งขึ้นได้ตามความจำเป็น
ให้ใช้ Factory สำหรับการสร้าง instance ของ Entity ที่ซับซ้อน เรื่องนี้มีรายละเอียดเพิ่มเติมอยู่ใน Factories (11) ในตัวอย่างก่อนหน้านี้ คุณสังเกตไหมว่า constructor ของ User มี visibility เป็นแบบ protected? Entity Tenant ทำหน้าที่เป็น Factory สำหรับ instance ของ User และมีเพียง class ที่อยู่ใน Module เดียวกันเท่านั้นที่มองเห็น constructor ของ User ได้ ด้วยวิธีนี้ จะไม่มี object ใดนอกจาก Tenant ที่สามารถสร้าง instance ของ User ได้:
public class Tenant extends Entity {
...
public User registerUser(
String aUsername,
String aPassword,
Person aPerson) {
aPerson.setTenantId(this.tenantId());
User user =
new User(
this.tenantId(),
aUsername,
aPassword,
aPerson);
return user;
}
...
}
ในที่นี้ method registerUser() คือ Factory Factory จะช่วยให้การสร้าง default state ของ User ง่ายขึ้น และรับประกันว่า TenantId ของทั้ง Entity User และ Person จะถูกต้องเสมอ ทั้งหมดนี้เกิดขึ้นภายใต้การควบคุมของ Factory method ที่สื่อสารด้วยภาษาเดียวกับ Ubiquitous Language