เนื่องจากการใช้ Service เป็น Factory ของผมส่วนใหญ่เกี่ยวข้องกับ Integrating Bounded Contexts (13) ผมจึงขอปล่อยเนื้อหาส่วนใหญ่ไปไว้ในบทนั้น เพราะในบทนั้นผมจะโฟกัสที่การผสานกับ Anti-Corruption Layer (3), Published Language (3) และ Open Host Service (3) เป็นหลัก ส่วนในบทนี้ผมอยากเน้นย้ำที่ตัว Factory เอง ว่าเราจะออกแบบ Service ให้ทำหน้าที่เป็น Factory ได้อย่างไร
ทีมขอยกอีกหนึ่งตัวอย่างจาก Collaboration Context มาให้ดู โดยเป็น Factory ในรูปของ CollaboratorService ที่ใช้สร้างอินสแตนซ์ของ Collaborator จาก tenant และ identity ของผู้ใช้:
package com.saasovation.collaboration.domain.model.collaborator;
import com.saasovation.collaboration.domain.model.tenant.Tenant;
public interface CollaboratorService {
public Author authorFrom(Tenant aTenant, String anIdentity);
public Creator creatorFrom(Tenant aTenant, String anIdentity);
public Moderator moderatorFrom(Tenant aTenant, String anIdentity);
public Owner ownerFrom(Tenant aTenant, String anIdentity);
public Participant participantFrom(
Tenant aTenant,
String anIdentity);
}
Service นี้ทำหน้าที่แปลง object จาก Identity and Access Context ให้กลายเป็น object ของ Collaboration Context อย่างที่กล่าวไว้ใน Bounded Contexts (2) ทีม CollabOvation จะไม่พูดถึง user เมื่อพูดถึงเรื่อง collaboration เพราะการมองมนุษย์ใน domain ของ collaborative media นี้ว่าเป็น author, creator, moderator, owner และ participant ต่างหากที่ตรงประเด็นกว่า ด้วยเหตุนี้ ทีมจึงต้องเข้าถึง Identity and Access Context ผ่าน Service ที่คอยอยู่เบื้องหลัง แล้วแปลง object user และ role จากโมเดลนั้นให้เป็น object collaborator ที่สอดคล้องกันภายใน Context ของโมเดลของตัวเอง
เมื่อ object ใหม่ซึ่งสืบทอดมาจาก base class แบบ abstract อย่าง Collaborator ถูกสร้างขึ้นโดย Service มันจึงทำหน้าที่เป็น Factory ไปในตัว และถ้าลองดูการ implement method หนึ่งของ interface นี้ ก็จะเห็นรายละเอียดบางส่วนที่เกี่ยวข้อง:
package com.saasovation.collaboration.infrastructure.services;
public class UserRoleToCollaboratorService
implements CollaboratorService {
public UserRoleToCollaboratorService() {
super();
}
@Override
public Author authorFrom(Tenant aTenant, String anIdentity) {
return
(Author)
UserInRoleAdapter
.newInstance()
.toCollaborator(
aTenant,
anIdentity,
"Author",
Author.class);
}
...
}
ด้วยความที่มันเป็นงาน implementation เชิงเทคนิค class นี้จึงถูกเก็บไว้ใน Module (9) ภายใน Infrastructure Layer
implementation ส่วนนี้ยึดติดกับ UserInRoleAdapter เพื่อแปลง Tenant และ identity (ซึ่งก็คือ username ของผู้ใช้) ให้เป็นอินสแตนซ์ของ class Author โดย Adapter [Gamma et al.] จะโต้ตอบกับ Open Host Service ของ Identity and Access Context เพื่อยืนยันว่าผู้ใช้คนดังกล่าวมี role ที่ชื่อว่า Author อยู่จริงหรือไม่ ถ้าใช่ Adapter ก็จะมอบงานให้ class CollaboratorTranslator แปล response จาก integration แบบ Published Language ให้เป็นอินสแตนซ์ของ class Author ในโมเดลท้องถิ่น ส่วน Author และ class ย่อยอื่นๆ ของ Collaborator ต่างก็เป็น Value Object ธรรมดาๆ:
package com.saasovation.collaboration.domain.model.collaborator;
public class Author extends Collaborator {
...
}
นอกเหนือจาก constructor, equals(), hashCode() และ toString() แล้ว คลาสย่อยแต่ละคลาสจะได้รับ state และ behavior ทั้งหมดมาจาก Collaborator:
package com.saasovation.collaboration.domain.model.collaborator;
public abstract class Collaborator implements Serializable {
private String emailAddress;
private String identity;
private String name;
public Collaborator(
String anIdentity,
String aName,
String anEmailAddress) {
super();
this.setEmailAddress(anEmailAddress);
this.setIdentity(anIdentity);
this.setName(aName);
}
...
}
Collaboration Context ใช้ username เป็น attribute ของ Collaborator identity ส่วน emailAddress และ name นั้นเป็นอินสแตนซ์ของ String ธรรมดาๆ ในโมเดลนี้ ทีมเลือกที่จะเก็บแนวคิดแต่ละอย่างให้เรียบง่ายที่สุดเท่าที่จะทำได้ เช่น ชื่อของผู้ใช้ก็ถูกเก็บเป็นชื่อเต็มในรูปแบบข้อความ และเรายังสามารถแยก life cycle กับคำศัพท์เชิงแนวคิดของ Bounded Context ทั้งสองออกจากกันได้ด้วย Service-Based Factory
ใน UserInRoleAdapter และ CollaboratorTranslator เองก็มีความซับซ้อนอยู่พอสมควร พูดสั้นๆ คือ UserInRoleAdapter รับผิดชอบแค่การสื่อสารกับ Context ภายนอกเท่านั้น ส่วน CollaboratorTranslator รับผิดชอบแค่การแปลผลที่นำไปสู่การสร้างออบเจกต์เท่านั้น รายละเอียดเพิ่มเติมโปรดดูจาก Integrating Bounded Contexts (13)