Entities across bounded contexts in Domain-Driven Design

Asher picture Asher · Nov 23, 2011 · Viewed 8.2k times · Source

I am trying to understand how entities operate in multiple bounded contexts.

Given an Employee of a Company. In (for example) the Human Resources context, this person has a name, surname, address, salary reference number, and bank account. But in the Accounting context all that is relevant is the salary reference number and bank account.

Do you have an Employee entity in the HR context and a Value-Type (e.g. SalariedEmployee) in the Accounting context?

class Employee
{
    public BankAccount BankAcountDetails { get; set; }
    public string FullName { get; set; }
    public Address ResidentialAddress { get; set; }
    public string SalaryRef { get; set; }
}

SalariedEmployee class (??) : Employee's value-type

class SalariedEmployee
{
    public SalariedEmployee(string salaryRef, BankAccount bankAcountDetails)
    {
        ...
    }

    public string SalaryRef { get; }
    public BankAccount BankAcountDetails { get; }
}

Does the HRService in the bounded context return this information? Or do you use the Employee class in both contexts?

Answer

Mohsen Alikhani picture Mohsen Alikhani · Sep 10, 2013

From http://msdn.microsoft.com/en-us/library/jj554200.aspx :

Bounded contexts are autonomous components, with their own domain models and their own ubiquitous language. They should not have any dependencies on each other at run time and should be capable of running in isolation.However they are a part of the same overall system and do need to exchange data with one another.

If you are implementing the CQRS pattern in a bounded context, you should use events for this type of communication: your bounded context can respond to events that are raised outside of the bounded context, and your bounded context can publish events that other bounded contexts may subscribe to. Events (one-way, asynchronous messages that publish information about something that has already happened), enable you to maintain the loose coupling between your bounded contexts.