แม้ว่า JSON representation ที่ผลิตโดย Identity and Access Context จะมีประโยชน์ต่อเหล่า client integrators พอสมควร แต่เมื่อเรามุ่งเน้นไปที่เป้าหมายของ DDD แล้ว representation ดังกล่าวจะไม่ถูกบริโภคตามที่เป็นอยู่ใน client Bounded Context อย่างที่ได้กล่าวไว้ในบทก่อนหน้า หาก consumer เป็น Collaboration Context ทีมก็ไม่ได้สนใจ users ระดับพื้นฐานพร้อมกับ roles ของพวกเขา ทีมที่พัฒนาอยู่ใน collaboration model กลับสนใจ roles ที่จำเพาะต่อ domain มากกว่า ความจริงที่ว่าในโมเดลอื่นๆ มีชุดของ User objects ที่สามารถ assign ให้กับ roles ได้หนึ่งตัวหรือหลายตัวตามที่ Role object จำลองไว้นั้น ไม่ได้เป็นสิ่งที่ collaboration ให้ความสำคัญโดยตรงเลย

ถ้าอย่างนั้น เราจะทำให้ user-in-role representation รับใช้จุดประสงค์ด้าน collaboration เฉพาะของเราได้อย่างไร? ลองกลับไปดู Context Map ที่เคยวาดไว้ก่อนหน้านี้อีกครั้ง คราวนี้อยู่ใน Figure 13.1 ส่วนสำคัญของ UserResource Adapter ถูกแสดงไปแล้วในหัวข้อย่อยก่อนหน้า จึงเหลือเพียง interfaces และ classes ที่ต้องพัฒนาขึ้นโดยเฉพาะสำหรับ Collaboration Context ซึ่งได้แก่ CollaboratorService, UserInRoleAdapter และ CollaboratorTranslator นอกจากนี้ยังมี HttpClient อยู่ด้วย แต่ตัวนั้นมาจาก JAX-RS implementation ผ่าน classes ClientRequest และ ClientResponse

Image

Figure 13.1. Open Host Service ของ Identity and Access Context และ Anticorruption Layer ของ Collaboration Context ที่ใช้สำหรับ integration ระหว่างสอง context นี้

ทั้งสามอย่าง ได้แก่ CollaboratorService, UserInRoleAdapter และ CollaboratorTranslator ถูกนำมารวมกันเพื่อสร้างเป็น Anticorruption Layer (3) ซึ่งเป็นกลไกที่ Collaboration Context ใช้โต้ตอบกับ Identity and Access Context และแปลความหมายของ user-in-role representation ให้กลายเป็น Value Object สำหรับ Collaborator ประเภทเฉพาะเจาะจง

ต่อไปคือ interface CollaboratorService ซึ่งรวบรวม operation แบบง่ายๆ ของ Anticorruption Layer ไว้ดังนี้:

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);
}

จากมุมมองของเหล่า clients ของ CollaboratorService แล้ว interface นี้ช่วยซ่อนความซับซ้อนทั้งหมดของการเข้าถึงระบบระยะไกลและการแปลความหมายที่ตามมา ตั้งแต่ Published Language ไปจนถึง objects ที่ยึดตาม Ubiquitous Language ในท้องถิ่นได้อย่างสิ้นเชิง ในกรณีนี้เราจึงใช้ Separated Interface [Fowler, P of EAA] ร่วมกับ implementation class เนื่องจาก implementation เป็นเรื่องเชิงเทคนิคและไม่ควรอยู่ใน Domain Layer

Factories (11) ทั้งหมดนี้คล้ายคลึงกันมาก พวกมันสร้าง subclass ของ abstract Collaborator Value type เหมือนกันทุกตัว แต่จะสร้างก็ต่อเมื่อ user ที่อยู่ใน aTenant และมี anIdentity นั้นครอบครอง security role หนึ่งในห้าประเภท ได้แก่ Author, Creator, Moderator, Owner และ Participant เนื่องจากมันคล้ายกันมาก ลองมาดู implementation ของ method เพียงตัวเดียวคือ authorFrom():

package com.saasovation.collaboration.infrastructure.services;

import com.saasovation.collaboration.domain.model.collaborator.Author;
...
public class TranslatingCollaboratorService
       implements CollaboratorService  {
    ...
    @Override
    public Author authorFrom(Tenant aTenant, String anIdentity) {
        Author author =
            this.userInRoleAdapter
                .toCollaborator(
                        aTenant,
                        anIdentity,
                        "Author",
                        Author.class);

        return author;
    }
    ...
}

ก่อนอื่นขอให้สังเกตว่า TranslatingCollaboratorService อยู่ใน Module (9) ของ Infrastructure เราแยก Separated Interface ไว้ใน hexagon ชั้นในซึ่งเป็นส่วนหนึ่งของ domain model แต่ implementation เป็นเรื่องเชิงเทคนิค จึงถูกเก็บไว้ด้านนอกของ Hexagonal architecture ซึ่งเป็นที่อยู่ของ Ports and Adapters

ในส่วนของการ implement ทางเทคนิคนั้น โดยทั่วไปแล้ว Anticorruption Layer จะประกอบด้วย Adapter [Gamma et al.] แบบเฉพาะทาง และ translator หนึ่งตัว ลองย้อนกลับไปดู Figure 13.1 อีกครั้ง จะเห็นว่า Adapter เฉพาะของเราคือ UserInRoleAdapter ส่วน translator ก็คือ CollaboratorTranslator และ Adapter เฉพาะทาง UserInRoleAdapter ของ Anticorruption Layer นี้มีหน้าที่ติดต่อกับระบบระยะไกล เพื่อขอ user-in-role resource ที่จำเป็นมา:

package com.saasovation.collaboration.infrastructure.services;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
...
public class UserInRoleAdapter {
    ...
    public <T extends Collaborator> T toCollaborator(
            Tenant aTenant,
            String anIdentity,
            String aRoleName,
            Class<T> aCollaboratorClass) {

        T collaborator = null;

        try {
            ClientRequest request =
                    this.buildRequest(aTenant, anIdentity, aRoleName);

            ClientResponse<String> response =
                    request.get(String.class);

            if (response.getStatus() == 200) {
                collaborator =
                    new CollaboratorTranslator()
                        .toCollaboratorFromRepresentation(
                            response.getEntity(),
                            aCollaboratorClass);
            } else if (response.getStatus() != 204) {
                throw new IllegalStateException(
                        "There was a problem requesting the user: "
                        + anIdentity
                        + " in role: "
                        + aRoleName
                        + " with resulting status: "
                        + response.getStatus());
            }

        } catch (Throwable t) {
            throw new IllegalStateException(
                    "Failed because: " + t.getMessage(), t);
        }

        return collaborator;
    }
    ...
}

ถ้า response ของ GET request สำเร็จ (status 200) นั่นก็หมายความว่า UserInRoleAdapter ได้รับ user-in-role resource มาแล้ว ซึ่งตอนนี้ก็พร้อมจะถูกแปลงเป็น Collaborator subclass ได้:

package com.saasovation.collaboration.infrastructure.services;

import java.lang.reflect.Constructor;
import com.saasovation.common.media.RepresentationReader;
...
public class CollaboratorTranslator {
    public CollaboratorTranslator() {
        super();
    }

    public <T extends Collaborator> T toCollaboratorFromRepresentation(
            String aUserInRoleRepresentation,
            Class<T> aCollaboratorClass)
    throws Exception {

        RepresentationReader reader =
                new RepresentationReader(aUserInRoleRepresentation);

        String username = reader.stringValue("username");
        String firstName = reader.stringValue("firstName");
        String lastName = reader.stringValue("lastName");
        String emailAddress = reader.stringValue("emailAddress");

        T collaborator =
            this.newCollaborator(
                    username,
                    firstName,
                    lastName,
                    emailAddress,
                    aCollaboratorClass);

        return collaborator;
    }

    private <T extends Collaborator> T newCollaborator(
            String aUsername,
            String aFirstName,
            String aLastName,
            String aEmailAddress,
            Class<T> aCollaboratorClass)
    throws Exception {

        Constructor<T> ctor =
            aCollaboratorClass.getConstructor(
                    String.class, String.class, String.class);

        T collaborator =
            ctor.newInstance(
                    aUsername,
                    (aFirstName + " " + aLastName).trim(),
                    aEmailAddress);

        return collaborator;
    }
}

translator ตัวนี้จะรับข้อความ user-in-role representation ที่เป็น String พร้อมกับ Class ที่จะใช้สร้าง instance ของ subclass ของ Collaborator โดยขั้นแรก RepresentationReader ซึ่งค่อนข้างคล้ายกับ NotificationReader ที่ได้แนะนำไปก่อนหน้านี้ จะถูกใช้เพื่ออ่าน attribute สี่ตัวจาก JSON representation เช่นเดิม เราสามารถทำเช่นนี้ได้อย่างมั่นใจและเชื่อถือได้ เพราะ custom media type ของ SaaSOvation ทำหน้าที่เป็น binding contract ระหว่าง producer และ consumer เมื่อ translator ได้ค่า String ที่จำเป็นครบแล้ว มันก็จะใช้ค่าเหล่านั้นเพื่อ instantiate Collaborator Value Object และในกรณีของตัวอย่างนี้ก็คือ Author:

package com.saasovation.collaboration.domain.model.collaborator;

public final class Author
       extends Collaborator  {

    public Author(
            String anIdentity,
            String aName,
            String anEmailAddress) {
        super(anIdentity, aName, anEmailAddress);
    }
    ...
}

เราไม่พยายามที่จะทำให้ instance ของ Collaborator Value ยังคง synchronized กับ Identity and Access Context เลย เพราะมันเป็น immutable ที่ทำได้เพียงถูกแทนที่ด้วยค่าใหม่ทั้งก้อนเท่านั้น ไม่ใช่การแก้ไข นี่คือวิธีที่ Application Service ดึง Author ออกมา แล้วส่งต่อให้ Forum เพื่อเริ่ม Discussion ใหม่:

package com.saasovation.collaboration.application;
...
public class ForumService ... {
    ...
    @Transactional
    public Discussion startDiscussion(
            String aTenantId,
            String aForumId,
            String anAuthorId,
            String aSubject) {

        Tenant tenant = new Tenant(aTenantId);
        ForumId forumId = new ForumId(aForumId);

        Forum forum = this.forum(tenant, forumId);

        if (forum == null) {
            throw new IllegalStateException("Forum does not exist.");
        }

        Author author =
                this.collaboratorService.authorFrom(
                        tenant, anAuthorId);

        Discussion newDiscussion =
                forum.startDiscussion(
                        this.forumNavigationService(),
                        author,
                        aSubject);

        this.discussionRepository.add(newDiscussion);

        return newDiscussion;
    }
    ...
}

ถ้าชื่อหรือที่อยู่อีเมลของ Collaborator คนใดคนหนึ่งเปลี่ยนไปใน Identity and Access Context การเปลี่ยนแปลงเช่นนั้นก็จะไม่ถูก update ไปยัง Collaboration Context โดยอัตโนมัติ การเปลี่ยนแปลงลักษณะนี้เกิดขึ้นได้น้อยมาก ทีมจึงตัดสินใจรักษาการออกแบบเฉพาะจุดนี้ให้เรียบง่าย โดยไม่พยายาม synchronize การเปลี่ยนแปลงใน remote Context กับ object ใน local Context ของตนเอง อย่างไรก็ตาม เราจะได้เห็นว่า Agile Project Management Context มีเป้าหมายการออกแบบที่แตกต่างออกไป

ยังมีวิธีอื่น ๆ ในการ implement Anticorruption Layer เช่น การใช้ Repository (12) อย่างไรก็ตาม เนื่องจาก Repository โดยปกติถูกใช้เพื่อ persist และ reconstitute Aggregate การจะสร้าง Value Object ด้วยวิธีนั้นจึงดูไม่ค่อยเหมาะสมเท่าไรนัก แต่ถ้าเป้าหมายของเราคือการสร้าง Aggregate จาก Anticorruption Layer แล้วล่ะก็ Repository ก็น่าจะเป็นแหล่งที่มาที่เป็นธรรมชาติมากกว่า