นี่เป็นอีกตัวอย่างหนึ่ง คราวนี้มาจาก Core Domain (2) ในปัจจุบัน นั่นคือ Agile Project Management Context Service นี้จะคำนวณผลลัพธ์จาก Value บน Aggregate ชนิดใดชนิดหนึ่งจำนวนเท่าไรก็ได้ ในกรณีนี้ผมเห็นว่าไม่มีเหตุผลดีพอที่จะใช้ Separated Interface อย่างน้อยก็ในตอนนี้ เพราะการคำนวณเหล่านี้ถูกดำเนินการด้วยวิธีเดิมเสมอ หากสถานการณ์เช่นนี้ไม่เปลี่ยนไป เราก็ไม่ควรวุ่นวายใจกับการแยก interface ออกจาก implementation

Cowboy Logic

LB: “ม้าตัวเก่งของผมทำเงินได้ $5,000 ต่อ service หนึ่งครั้ง และผมก็มีแม่ม้าเตรียมไว้รอแล้วล่ะ”

AJ: “ทีนี้เจ้าม้า ตัวนั้น ก็อยู่ใน domain ของเขาแล้วล่ะ”

Image

ย้อนกลับไป ตอนแรกนักพัฒนา SaaSOvation ได้สร้าง static method แบบละเอียดย่อย (fine-grained) ไว้บน Product เพื่อใช้คำนวณตามที่ต้องการ และนี่คือสิ่งที่เกิดขึ้นต่อมา . . .


Image

ที่ปรึกษาของทีมยังชี้ให้เห็นถึงความน่าปรารถนาของการใช้ Domain Service แทน static method ด้วย แนวคิดเบื้องหลัง Service นี้จะคล้ายกับดีไซน์ปัจจุบันเป็นอย่างมาก นั่นคือคำนวณและส่งคืน BusinessPriorityTotals ซึ่งเป็น Value instance หนึ่ง แต่ Service จะต้องทำงานเพิ่มขึ้นอีกเล็กน้อย ซึ่งรวมถึงการค้นหา backlog items ที่ยัง outstanding ทั้งหมดของ Scrum product ที่กำหนด แล้วจึงรวมค่า BusinessPriority Value ของแต่ละรายการเข้าด้วยกัน นี่คือ implementation:

package com.saasovation.agilepm.domain.model.product;

import com.saasovation.agilepm.domain.model.DomainRegistry;
import com.saasovation.agilepm.domain.model.tenant.Tenant;

public class BusinessPriorityCalculator {

    public BusinessPriorityCalculator() {
        super();
    }


    public BusinessPriorityTotals businessPriorityTotals(
            Tenant aTenant,
            ProductId aProductId) {
        int totalBenefit = 0;
        int totalPenalty = 0;
        int totalCost = 0;
        int totalRisk = 0;

        java.util.Collection<BacklogItem> outstandingBacklogItems =
            DomainRegistry
                .backlogItemRepository()
                .allOutstandingProductBacklogItems(
                        aTenant,
                        aProductId);

        for (BacklogItem backlogItem : outstandingBacklogItems) {
            if (backlogItem.hasBusinessPriority()) {
                BusinessPriorityRatings ratings =
                    backlogItem.businessPriority().ratings();

                totalBenefit += ratings.benefit();
                totalPenalty += ratings.penalty();
                totalCost += ratings.cost();
                totalRisk += ratings.risk();
            }
        }

        BusinessPriorityTotals businessPriorityTotals =
            new BusinessPriorityTotals(
                    totalBenefit,
                    totalPenalty,
                    totalBenefit + totalPenalty,
                    totalCost,
                    totalRisk);

        return businessPriorityTotals;
    }
}

เราใช้ BacklogItemRepository เพื่อดึง outstanding BacklogItem ทั้งหมดมา ซึ่ง BacklogItem ที่ outstanding นี้หมายถึงรายการที่มีสถานะเป็น Planned , Scheduled หรือ Committed ไม่ใช่ Done หรือ Removed สำหรับ Service ใน domain แล้ว การใช้ Repository ตามที่จำเป็นถือเป็นเรื่องที่ทำได้ตามสะดวก แต่การเข้าถึง Repository จาก instance ของ Aggregate นั้นไม่ใช่แนวทางปฏิบัติที่แนะนำ

เมื่อได้รายการที่ outstanding ทั้งหมดของ product หนึ่งแล้ว เราจะวนซ้ำผ่านมันและรวมค่า ratings ของ BusinessPriority ของแต่ละรายการ ค่ายอดรวมที่ได้จากการคำนวณแบบวนซ้ำนี้จะถูกนำไปใช้สร้าง instance ใหม่ของ BusinessPriorityTotals ซึ่งจะถูกส่งคืนให้กับ client กระบวนการคำนวณของ Service ไม่จำเป็นต้องซับซ้อนเสมอไป แม้ว่าในบางกรณีความซับซ้อนอาจเป็นสิ่งที่เลี่ยงไม่ได้ก็ตาม แต่ตัวอย่างนี้ถือว่าง่ายพอสมควร

สังเกตจากตัวอย่างนี้ว่าคุณ ไม่ควรอย่างยิ่ง ที่จะให้ logic นี้อยู่ใน Application Service แม้คุณจะมองว่าการคำนวณผลรวมใน for loop เป็นเรื่องเล็กน้อย มันก็ยังเป็น business logic อยู่ดี แต่นั่นไม่ใช่เหตุผลเดียว เพราะยังมีเหตุผลอีกข้อหนึ่ง:

BusinessPriorityTotals businessPriorityTotals =
    new BusinessPriorityTotals(
            totalBenefit,
            totalPenalty,
             totalBenefit + totalPenalty,
            totalCost,
            totalRisk);

เมื่อ BusinessPriorityTotals ถูกสร้างขึ้น attribute totalValue ของมันจะถูกคำนวณมาจากการรวม totalBenefit และ totalPenalty เข้าด้วยกัน logic ตรงนี้เป็นเรื่องเฉพาะของ domain (domain specific) และต้องไม่รั่วไหลลงไปใน Application Layer เราอาจเถียงได้ว่า constructor ของ BusinessPriorityTotals ควรจัดการให้ค่าดังกล่าวถูกคำนวณจาก parameter ที่ส่งเข้ามาทั้งสองตัวด้วยตัวเอง ซึ่งถ้าทำได้ก็อาจเป็นหนทางหนึ่งในการปรับปรุง model แต่การทำเช่นนั้นก็ไม่ใช่ข้ออ้างที่จะย้ายการคำนวณที่เหลือลงไปไว้ใน Application Service

แม้เราจะไม่ได้เก็บ business logic นี้ไว้ใน Application Service แต่ Application Service ก็ยังทำหน้าที่เป็น client ของ Domain Service อยู่ดี:

public class ProductService ... {
    ...
    private BusinessPriorityTotals productBusinessPriority(
            String aTenantId,
            String aProductId) {
        BusinessPriorityTotals productBusinessPriority =
                DomainRegistry
                    .businessPriorityCalculator()
                    .businessPriorityTotals(
                            new TenantId(aTenantId),
                            new ProductId(aProductId));

        return productBusinessPriority;
    }
}

ในกรณีนี้ private method ใน Application Service จะทำหน้าที่ร้องขอค่าผลรวม business priority ของ product ที่นี่ method ดังกล่าวอาจเป็นเพียงการจัดหาส่วนหนึ่งของ payload ที่จะส่งกลับไปให้ client ของ ProductService อย่างเช่น user interface ก็เป็นได้