DDD – Business rule that depends on a service

I got confused about if a domain is required to access an external service in some state of it’s life cycle.

The business rule is this :

If an Employee is accepting a JobSeeker’s JobApplication, then the
Employee must create an Appointment and have it e-mailed to the
JobSeeker.

My current solutions are:

1 – Leave the mailing responsibility to the JobApplication domain

class JobApplication {
    public function makeAppointment(IMailService $mailService, IMailGenerator $mailGen, DateTime $time, Address $address = null) {    
        $employer = $this->getVacancy()->getEmployer();

        $this->accept();

        $appointment = new Appointment($employer,
            $this->applicant,
            'Interview Invitation - '.$employer,
            $time,
            $address ?: $employer->getAddress()
        );

        $mailService->send(
            $appointment->getJobSeeker()->getMail(), 
            $appointment->getTitle(),
            $mailGen->generateAppointmentMail($appointment)
        );

        return $appointment;
    }    
}

I kinda feel awkward about this option, since it leaves the JobApplication entity to depends on the Mailing services.

2 – Create a new AppointmentService

class AppointmentService {
    private $mailService;
    private $mailGen;

    public function __construct(IMailService $mailService, IMailGenerator $mailGen) {
        $this->mailService = $mailService;
        $this->mailGen = $mailGen;
    }

    public function makeAppointmentFromJobApplication(JobApplication $application, DateTime $time, Address $address = null) {
        $jobApplication->accept();

        $appointment = new Appointment($application->getCompany(),
            $application->getApplicant(),
            'Interview Invitation - '.$application->getCompany(),
            $time,
            $address ?: $application->getCompany()->getAddress()
        );

        $mailService->send(
            $appointment->getJobSeeker()->getMail(), 
            $appointment->getTitle(),
            $mailGen->generateAppointmentMail($appointment)
        );

        return $appointment;
    }
}

This option is nice since I only need to inject AppointmentService to the controller, but it leaves the Appointment class anemic.

I once read to never leave your domain to be dependent to domain services, the other way is fine. But some business rules do require services that existed out of the domain scope.

Is there another options?

Update – 1

A third option would be like this:

class Employer {
    public function acceptApplication(JobApplication $application) {
        if($application->isRejected()) throw new Exception('Cannot make appointment for rejected application');

        $application->setAccepted();
    }

    public function makeAppointment(IAppointmentService $appointmentService, JobApplication $application, DateTime $time, Address $address = null) {
        if(!$application->isAccepted()) $this->acceptApplication($application);

        return $appointmentService->makeAppointment($this, $application->getApplicant(), $time, $address ?: $this->getAddress());
    }
}

class AppointmentService {
    private $mailService;
    private $mailGenerator;

    public function makeAppointment(Employer $company, JobSeeker $jobSeeker, DateTime $time, Address $address = null) {
        $appointment = new Appointment($company, 
            $jobSeeker,
            'Interview Invitation - '.$application->getCompany(),
            $time,
            $address
        );

        $this->mailAppointment($appointment);

        return $appointment;
    }

    private function mailAppointment(Appointment $appointment) {
        return $mailService->send($appointment->getJobSeeker, $appointment->getTitle, $mailGenerator->generateApplicationAppointment());
    }
}

3

The rule is not to depend on infrastructure, but instead you can create an interface in terms of the business to communicate with that infrastructure. These are your services.

Sending mail is a business requirement, or service, but how you send mail is up to you. You may want to create your own business-specific mail interface, and one of the implementations of could bridge whatever mail library you are using.

Having said that, given your two options, I think #2 is more correct.

One major point which you’ve alluded to is to separate data from behaviour. You should be passing data around, and typically services are dependencies rather than business objects being passed around.

However, the current design still feels too focused on the services rather than the business object. Nothing about converting a job application into appointment mentions mail.

This leaves you with something like

class AppointmentScheduler {
    public function makeAppointmentFromJobApplication(JobApplication $application, DateTime $time, Address $address = null) {
        $jobApplication->accept();

        return new Appointment($application->getCompany(),
            $application->getApplicant(),
            'Interview Invitation - '.$application->getCompany(),
            $time,
            $address ?: $application->getCompany()->getAddress()
        );
    }
}

The next question is, can a $jobApplication really accept itself? Who is accepting it? The applicant? the company?

I’d rather see the company explictly interacting with the application rather than hiding that internally. That’s the real interaction that is happening here.

It seems there are a few richer interactions hiding within your code:

  • a Company accepting a JobApplication
  • the Company scheduling an Interview

and, as side effects from that:

  • the Company mailing the Applicant the Invitation
  • the Application being accepted

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật