การสร้าง Repository ไม่ว่าจะเป็นแนวทางไหนก็ตาม ล้วนมีขั้นตอนหลักอยู่สองขั้นตอนด้วยกัน นั่นคือการนิยาม public interface และการสร้าง implementation อย่างน้อยหนึ่งตัว
โดยเฉพาะอย่างยิ่งในกรณีของการออกแบบแนว collection-oriented ขั้นตอนแรกคือการนิยาม interface ที่เลียนแบบพฤติกรรมของ collection ส่วนขั้นตอนที่สองคือการสร้าง implementation ที่จัดการกับกลไก primary storage ที่อยู่เบื้องหลัง เช่น Hibernate ส่วน interface เองก็เหมือนกับ collection ตรงที่มักมี method พื้นฐานทั่ว ๆ ไป ดังที่จะเห็นในตัวอย่างต่อไปนี้
package com.saasovation.collaboration.domain.model.calendar;
public interface CalendarEntryRepository {
public void add(CalendarEntry aCalendarEntry);
public void addAll(
Collection<CalendarEntry> aCalendarEntryCollection);
public void remove(CalendarEntry aCalendarEntry);
public void removeAll(
Collection<CalendarEntry> aCalendarEntryCollection);
...
}
ให้วางนิยามของ interface ไว้ใน Module (9) เดียวกันกับ Aggregate type ที่มันจัดเก็บ ในกรณีนี้ interface CalendarEntryRepository จะถูกวางไว้ใน Module (Java package) เดียวกันกับ CalendarEntry ส่วน class ที่เป็น implementation จะถูกแยกไปไว้อีก package หนึ่ง ตามที่จะกล่าวถึงในภายหลัง
Interface CalendarEntryRepository มี method ที่คล้ายกับที่ collection ทั่วไปมีให้ใช้เป็นอย่างมาก เช่น java.util.Collection ซึ่งเป็นมาตรฐานอยู่แล้ว โดยเราสามารถเพิ่ม CalendarEntry ใหม่หนึ่งตัวเข้าไปใน Repository นี้ได้ด้วย add() และเพิ่ม instance ใหม่หลายตัวพร้อมกันได้ด้วย addAll() เมื่อ instance ถูกเพิ่มเข้าไปแล้ว มันก็จะถูก persist ลงใน data store บางรูปแบบ และเรียกคืนได้ด้วย identity ที่ไม่ซ้ำกันนับจากนั้นเป็นต้นไป ส่วน method ที่ตรงข้ามกับสองตัวนั้นก็คือ remove() และ removeAll() ซึ่งใช้สำหรับลบ instance หนึ่งตัวหรือหลายตัวออกจาก collection
ส่วนตัวผมไม่ชอบให้ method พวกนี้ตอบกลับเป็น Boolean เหมือนกับที่ collection เต็มรูปแบบทำกัน เนื่องจาก ในบางกรณี การตอบ true ให้กับ operation แบบ add ไม่ได้การันตีความสำเร็จเสมอไป ผลลัพธ์ true อาจยังต้องขึ้นอยู่กับการ commit transaction บน data store อยู่ดี ด้วยเหตุนี้ void จึงเป็น return type ที่แม่นยำกว่าในกรณีของ Repository
อาจมีบางกรณีที่การเพิ่มและ/หรือลบ instance ของ Aggregate หลายตัวใน transaction เดียวกันนั้นไม่เหมาะสม เมื่อกรณีเช่นนั้นเกิดขึ้นจริงใน domain ของคุณ ก็ไม่จำเป็นต้องมี method addAll() และ removeAll() อย่างไรก็ตาม method ทั้งสองนี้มีไว้เพื่อความสะดวกเท่านั้น เพราะ client สามารถใช้ loop เรียก add() หรือ remove() ซ้ำ ๆ ได้หลายครั้งระหว่างวนลูปผ่าน collection ด้วยตัวเองอยู่แล้ว ดังนั้นการตัด addAll() และ removeAll() ออกไปจึงเป็นเพียงสัญลักษณ์เชิงนโยบายที่ไม่สามารถบังคับใช้ได้จริงด้วยการออกแบบ เว้นแต่คุณจะสร้างกลไกตรวจจับ การเพิ่มและการลบ object หลายตัวใน transaction เดียวกันขึ้นมาด้วย ซึ่งการทำเช่นนั้นน่าจะต้องให้ Repository แบบนี้ถูก instantiate ขึ้นมาใหม่ทุก ๆ transaction ซึ่งเป็นข้อเสนอที่อาจมีค่าใช้จ่ายสูงทีเดียว และผมจะไม่พูดถึงเรื่องนี้ต่อ
เป็นไปได้ว่า instance ของ Aggregate type บางประเภทไม่ควรถูกลบผ่าน use cases ปกติของแอปพลิเคชันเลย และมัน อาจจำเป็นต้องเก็บ instance นั้นไว้อีกนาน หลังจากที่มันใช้งานในแอปพลิเคชันไม่ได้อีกต่อไป เช่น เพื่อการอ้างอิง และ/หรือเหตุผลทางประวัติศาสตร์ ในแง่การอ้างอิงแล้ว object บางอย่างอาจลบได้ยากมากหรือแทบเป็นไปไม่ได้เลย ในมุมธุรกิจ การลบ object บางอย่างอาจไม่ฉลาด ไม่ควรทำ หรือผิดกฎหมายด้วยซ้ำ ในกรณีเช่นนั้น คุณอาจตัดสินใจเพียงแค่ mark สถานะของ Aggregate instance ว่า ปิดใช้งาน (disabled) หรือ ใช้งานไม่ได้ (unusable) หรืออาจ mark ด้วยวิธีอื่นตามความเฉพาะของ domain ว่า ถูกลบในเชิงตรรกะ (logically removed) ถ้าเป็นเช่นนั้น คุณอาจเลือกที่จะไม่รวม method การลบใด ๆ ไว้ใน public interface ของ Repository หรือคุณอาจตัดสินใจ ที่จะ implement method การลบโดยให้มันตั้งสถานะใช้งานไม่ได้ (unusable) ให้กับ instance ของ Aggregate แทน คุณอาจป้องกันการลบ object ทั้งก้อนด้วยการ code review โดยตรวจสอบ client อย่างละเอียดเพื่อให้แน่ใจว่าไม่มีการใช้ พฤติกรรมการลบเช่นนั้นอยู่จริง เป็นการตัดสินใจที่ต้องไตร่ตรอง แต่คุณอาจพบว่าการไม่อนุญาตให้ลบใด ๆ เลยง่ายกว่า ก็อย่าลืมว่า method ใด ๆ ใน public interface โดยทั่วไปถือว่าพร้อมใช้งานได้เสมอ ถ้าการลบเปิดให้ใช้ต่อสาธารณะในขณะที่ ไม่อนุญาตในเชิงตรรกะ คุณควรพิจารณา implement การลบเชิงตรรกะ (logical removal) แทนการลบทางกายภาพ (physical removal)
อีกหนึ่งส่วนสำคัญของ Repository interface ก็คือการนิยาม finder methods ต่าง ๆ
public interface CalendarEntryRepository {
...
public CalendarEntry calendarEntryOfId(
Tenant aTenant,
CalendarEntryId aCalendarEntryId);
public Collection<CalendarEntry> calendarEntriesOfCalendar(
Tenant aTenant,
CalendarId aCalendarId);
public Collection<CalendarEntry> overlappingCalendarEntries(
Tenant aTenant,
CalendarId aCalendarId,
TimeSpan aTimeSpan);
}
คำนิยาม method แรกคือ calendarEntryOfId() ใช้ดึง instance เฉพาะของ CalendarEntry Aggregate ตาม identity ที่ไม่ซ้ำกัน โดย type นี้ใช้ identity type แบบ explicit นั่นคือ CalendarEntryId สำหรับคำนิยาม method ที่สอง calendarEntriesOfCalendar() ใช้ดึง collection ของ instance ทั้งหมดของ CalendarEntry ที่เป็นของ Calendar เฉพาะตาม identity ที่ไม่ซ้ำกันของมัน และสุดท้าย คำนิยามของ finder method ตัวที่สาม overlapping-CalendarEntries() จะคืน collection ของ instance ทั้งหมดของ CalendarEntry ที่เป็นของ Calendar เฉพาะ ในช่วง TimeSpan ที่กำหนด โดยเฉพาะอย่างยิ่ง method นี้รองรับการดึงข้อมูลว่ารายการใดถูกจัดตารางไว้บ้าง ตลอดช่วงระยะเวลาที่ต่อเนื่องกันของวันที่และเวลา
สุดท้ายนี้ เราอาจสงสัยว่า CalendarEntry ถูกกำหนด identity ที่ไม่ซ้ำกันในแบบ global ได้อย่างไร เรื่องนี้ Repository ก็สามารถจัดหาให้ได้อย่างสะดวกเช่นกัน:
public interface CalendarEntryRepository {
public CalendarEntryId nextIdentity();
...
}
code ใดก็ตามที่ทำหน้าที่สร้าง instance ใหม่ของ CalendarEntry จะใช้ nextIdentity() เพื่อขอ instance ใหม่ของ CalendarEntryId :
CalendarEntry calendarEntry =
new CalendarEntry(tenant, calendarId,
calendarEntryRepository.nextIdentity(),
owner, subject, description, timeSpan, alarm,
repetition, location, invitees);
ดู Entities (5) สำหรับการอภิปรายอย่างละเอียดถี่ถ้วนเกี่ยวกับเทคนิคการสร้าง identity การใช้ domain-specific identity และ surrogate identity และความสำคัญของการกำหนด identity ในจังหวะเวลาที่เหมาะสม
ต่อไปเรามาดู implementation class ของ Repository แบบดั้งเดิมนี้กัน มีตัวเลือกอยู่สองสามแบบสำหรับการเลือก Module ที่จะใช้วาง class นี้ บางคนนิยมใช้ Module (Java package) ที่อยู่ใต้ Aggregate และ Repository Module โดยตรง ซึ่งในกรณีนี้ก็จะหมายถึง
package com.saasovation.collaboration.domain.model.calendar.impl;
public class HibernateCalendarEntryRepository
implements CalendarEntryRepository {
...
}
การวาง class ไว้ตรงนี้ช่วยให้เราจัดการ implementation ได้ใน Domain Layer แต่ใน package พิเศษสำหรับ implementation วิธีนี้ช่วยให้แนวคิดของ domain ถูกแยกออกจากสิ่งที่เกี่ยวข้องกับการ persistence โดยตรงอย่างชัดเจน การประกาศ interface ใน package ที่ตั้งชื่ออย่างมีความหมาย แล้ววาง implementation ของมันใน sub-package ที่ชื่อ impl ซึ่งอยู่ใต้ package นั้นโดยตรง เป็นแนวทางที่ปฏิบัติกันอย่างแพร่หลายใน Java projects อย่างไรก็ตาม ในกรณีของ Collaboration Context ทีมเลือกที่จะวาง class ที่เป็น technical implementation ทั้งหมดไว้ใน Infrastructure Layer:
package com.saasovation.collaboration.infrastructure.persistence;
public class HibernateCalendarEntryRepository
implements CalendarEntryRepository {
...
}
การทำเช่นนี้ใช้หลักการ Dependency Inversion Principle (4) หรือ DIP สำหรับการจัด layer ของสิ่งที่เกี่ยวกับ infrastructure โดย Infrastructure Layer อยู่เหนือชั้นอื่นๆ ทั้งหมดในเชิงตรรกะ ทำให้ references เป็นแบบทิศทางเดียว และชี้ลงไปยัง Domain Layer
Class HibernateCalendarEntryRepository เป็น Spring bean ที่ลงทะเบียนไว้ โดยมี zero-argument constructor และมี dependency ซึ่งเป็น infrastructure bean object อีกตัวหนึ่งถูก inject เข้ามา:
import com.saasovation.collaboration.infrastructure
.persistence.SpringHibernateSessionProvider;
public class HibernateCalendarEntryRepository
implements CalendarEntryRepository {
public HibernateCalendarEntryRepository() {
super();
}
...
private SpringHibernateSessionProvider sessionProvider;
public void setSessionProvider(
SpringHibernateSessionProvider aSessionProvider) {
this.sessionProvider = aSessionProvider;
}
private org.hibernate.Session session() {
return this.sessionProvider.session();
}
}
Class SpringHibernateSessionProvider ก็ถูกเก็บไว้ใน Infrastructure Layer เช่นกัน ใน com.saasovation.collaboration.infrastructure.persistence Module และถูก inject เข้าไปใน Hibernate-based Repository ทุกตัว โดยแต่ละ method ที่ต้องใช้ Session object ของ Hibernate จะเรียก method session() ของตัวเองขึ้นมาเพื่อขอ object นั้น โดย method session() จะใช้ sessionProvider instance ที่ถูก inject ผ่าน dependency เพื่อขอ Session instance ที่ผูกกับ thread (thread-bound) ซึ่งเราจะได้เห็นในภายหลังในบทนี้
method add() , addAll() , remove() และ removeAll() ถูก implement ไว้ดังนี้:
package com.saasovation.collaboration.infrastructure.persistence;
public class HibernateCalendarEntryRepository
implements CalendarEntryRepository {
...
@Override
public void add(CalendarEntry aCalendarEntry) {
try {
this.session().saveOrUpdate(aCalendarEntry);
} catch (ConstraintViolationException e) {
throw new IllegalStateException(
"CalendarEntry is not unique.", e);
}
}
@Override
public void addAll(
Collection<CalendarEntry> aCalendarEntryCollection) {
try {
for (CalendarEntry instance : aCalendarEntryCollection) {
this.session().saveOrUpdate(instance);
}
} catch (ConstraintViolationException e) {
throw new IllegalStateException(
"CalendarEntry is not unique.", e);
}
}
@Override
public void remove(CalendarEntry aCalendarEntry) {
this.session().delete(aCalendarEntry);
}
@Override
public void removeAll(
Collection<CalendarEntry> aCalendarEntryCollection) {
for (CalendarEntry instance : aCalendarEntryCollection) {
this.session().delete(instance);
}
}
...
}
method เหล่านี้มี implementation ที่ค่อนข้างเรียบง่าย โดยแต่ละ method จะเรียกใช้ session() ของตัวเองเพื่อให้ได้ instance ของ Session ของ Hibernate (ตามที่ได้อธิบายไปก่อนหน้านี้แล้ว)
อาจฟังดูน่าแปลกใจที่ method add() และ addAll() ใช้ Session โดยเรียกใช้ method saveOrUpdate() ของมัน ซึ่งถือเป็นการสนับสนุนเพิ่มเติมสำหรับการเพิ่มแบบ Set หาก client เผลอเพิ่ม CalendarEntry ตัวเดิมมากกว่าหนึ่งครั้ง พฤติกรรมของ saveOr-Update() จะทำให้การเพิ่มครั้งหลังดูเหมือนเป็น no-op ที่ไม่ก่อให้เกิดอันตราย ที่จริงแล้ว ตั้งแต่ Hibernate เวอร์ชัน 3 เป็นต้นมา การ update ในรูปแบบใดก็ตามล้วนเป็น no-op เนื่องจาก ดังที่ได้กล่าวไว้ก่อนหน้านี้ การ update จะถูกติดตามโดยนัยผ่านการเปลี่ยนแปลงสถานะของ object ดังนั้น ตราบใดที่ object ที่ method ทั้งสองนี้เพิ่มเข้าไปไม่ได้เป็น object ใหม่ทั้งหมด พฤติกรรมก็จะไม่ทำอะไรเลย
การเพิ่ม object อาจก่อให้เกิด ConstraintViolationException ได้ แทนที่จะปล่อยให้ exception ของ Hibernate รั่วไหลออกไปถึง client ตรง ๆ เราจะทำการ catch exception เหล่านั้นแล้วห่อหุ้มด้วย exception ที่เป็นมิตรกับ client มากกว่า นั่นคือ IllegalStateException เราอาจประกาศ exception ที่เฉพาะเจาะจงกับ domain แล้ว throw มันออกไปก็ได้ ซึ่งเป็นทางเลือกของแต่ละทีมโปรเจกต์ แต่ประเด็นหลักคือ การที่เรายอมลำบากเพื่อซ่อนรายละเอียดการ implement ของ persistence framework ที่อยู่ข้างใต้ เราจึงต้องการกัน client ออกจากรายละเอียดทั้งหมดนั้น ไม่เว้นแม้แต่ exception
method remove() และ removeAll() นั้นง่ายมาก โดยทั้งคู่เพียงแค่ใช้ Session delete() เพื่อทำการลบ object ออกจาก data store ที่อยู่ข้างใต้ มีรายละเอียดเพิ่มเติมอีกข้อหนึ่งเกี่ยวกับการลบ Aggregate ที่ใช้ one-to-one mapping ซึ่งเป็นจริงในกรณีหนึ่งใน Identity and Access Context เนื่องจากคุณไม่สามารถ cascade การเปลี่ยนแปลงบนความสัมพันธ์แบบนี้ได้ คุณจึงจำเป็นต้องลบ object ทั้งสองฝั่งของ association อย่างชัดเจน:
package com.saasovation.identityaccess.infrastructure.persistence;
public class HibernateUserRepository implements UserRepository {
...
@Override
public void remove(User aUser) {
this.session().delete(aUser.person());
this.session().delete(aUser);
}
@Override
public void removeAll(Collection<User> aUserCollection) {
for (User instance : aUserCollection) {
this.session().delete(instance.person());
this.session().delete(instance);
}
}
...
}
ต้องลบ object Person ที่อยู่ข้างในก่อนเสมอ แล้วจึงค่อยลบ User ซึ่งเป็น Aggregate Root หากคุณไม่ลบ object Person ที่อยู่ข้างใน มันจะกลายเป็น orphan ในตาราง database ที่สอดคล้องกับมัน โดยทั่วไปแล้ว นี่เป็นเหตุผลที่ดีในการหลีกเลี่ยง ความสัมพันธ์แบบ one-to-one แล้วหันไปใช้ constrained singular many-to-one unidirectional association แทน อย่างไรก็ตาม ผม เลือกที่จะ implement ความสัมพันธ์แบบ one-to-one bidirectional นี้โดยตั้งใจ เพื่อสาธิตว่าการทำงานกับ mapping ที่ยุ่งยากกว่าแบบนี้ต้องเจอกับอะไรบ้าง
โปรดสังเกตว่ามีแนวทางที่แตกต่างกันซึ่งต่างก็เป็นที่นิยมสำหรับจัดการกับสถานการณ์แบบนี้ บางคนอาจเลือกพึ่งพา ORM life cycle events เพื่อทำให้เกิดการลบแบบ cascade ของ part object ผมหลีกเลี่ยงแนวทางเหล่านั้นโดยตั้งใจ เพราะผมเป็น ผู้ที่คัดค้านการทำ persistence แบบ Aggregate-managed อย่างแข็งขัน และสนับสนุนการทำ persistence แบบ Repository-only อย่างเต็มที่ ข้อถกเถียงในเรื่องนี้ดำเนินไปอย่างเผ็ดร้อนและไม่มีวันจบสิ้น จนน่าเบื่อหน่าย (ad nauseam) คุณควรตัดสินใจอย่างมีข้อมูล แต่จงเข้าใจว่า เหล่า expert ด้าน DDD มักหลีกเลี่ยงการทำ persistence แบบ Aggregate-managed โดยยึดถือเป็น rule of thumb
ทีนี้กลับมาที่ HibernateCalendarEntryRepository และ implementation ของ finder method ต่างๆ ของมันกันต่อ:
public class HibernateCalendarEntryRepository
implements CalendarEntryRepository {
...
@Override
@SuppressWarnings("unchecked")
public Collection<CalendarEntry> overlappingCalendarEntries(
Tenant aTenant, CalendarId aCalendarId, TimeSpan aTimeSpan) {
Query query =
this.session().createQuery(
"from CalendarEntry as _obj_ " +
"where _obj_.tenant = :tenant and " +
"_obj_.calendarId = :calendarId and " +
"((_obj_.repetition.timeSpan.begins between " +
":tsb and :tse) or " +
" (_obj_.repetition.timeSpan.ends between " +
":tsb and :tse))");
query.setParameter("tenant", aTenant);
query.setParameter("calendarId", aCalendarId);
query.setParameter("tsb", aTimeSpan.begins(), Hibernate.DATE);
query.setParameter("tse", aTimeSpan.ends(), Hibernate.DATE);
return (Collection<CalendarEntry>) query.list();
}
@Override
public CalendarEntry calendarEntryOfId(
Tenant aTenant,
CalendarEntryId aCalendarEntryId) {
Query query =
this.session().createQuery(
"from CalendarEntry as _obj_ " +
"where _obj_.tenant = ? and _obj_.calendarEntryId = ?");
query.setParameter(0, aTenant);
query.setParameter(1, aCalendarEntryId);
return (CalendarEntry) query.uniqueResult();
}
@Override
@SuppressWarnings("unchecked")
public Collection<CalendarEntry> calendarEntriesOfCalendar(
Tenant aTenant, CalendarId aCalendarId) {
Query query =
this.session().createQuery(
"from CalendarEntry as _obj_ " +
"where _obj_.tenant = ? and _obj_.calendarId = ?");
query.setParameter(0, aTenant);
query.setParameter(1, aCalendarId);
return (Collection<CalendarEntry>) query.list();
}
...
}
method finder ทั้งสามตัวนี้ต่างก็สร้าง Query ขึ้นมาผ่าน Session ของมันเอง ซึ่งเป็นเรื่องปกติของ query ใน Hibernate ที่ทีมจะใช้ HQL เพื่ออธิบาย criteria แล้วก็โหลด parameter objects เข้ามา จากนั้นจึงรัน query โดยขอผลลัพธ์แบบใดแบบหนึ่ง คือผลลัพธ์เดี่ยวแบบ unique result หรือ collection ที่เป็น list ของ objects ในบรรดา query ทั้งสามตัวนี้ ตัวที่ซับซ้อนที่สุดก็คือ overlappingCalendarEntries() ซึ่งในกรณีนี้เราต้องค้นหา CalendarEntry instance ทั้งหมดที่ทับซ้อนกับช่วงวันที่และเวลาที่กำหนดไว้ หรือก็คือ TimeSpan นั่นเอง
สุดท้ายเราจะมาดู implementation ของ method nextIdentity() กัน:
public class HibernateCalendarEntryRepository
implements CalendarEntryRepository {
...
public CalendarEntryId nextIdentity() {
return new CalendarEntryId(
UUID.randomUUID().toString().toUpperCase());
}
...
}
implementation เฉพาะนี้ไม่ได้ใช้กลไก persistence หรือ data store ในการสร้าง identity ที่ไม่ซ้ำกัน แต่กลับใช้ UUID generator ซึ่งค่อนข้างเร็วและน่าเชื่อถือได้สูงมากแทน