An in-process dependency executes within the application process, such as another domain class or a plain value. Domain classes should generally depend on these rather than external systems. section 7.2
Use the distinction to keep responsibilities and tests manageable:
- Keep communication with databases and message buses in an application service or controller, leaving domain classes focused on domain behavior. section 7.2
- Avoid single-implementation interfaces for domain classes introduced only to enable mocking; interaction-based tests couple to implementation details and become brittle. section 8.4
Listing 7.6. User after refactoring
public class User
{
public int UserId { get; private set; }
public string Email { get; private set; }
public UserType Type { get; private set; }
public void ChangeEmail(string newEmail, Company company)
{
if (Email == newEmail)
return;
UserType newType = company.IsEmailCorporate(newEmail)
? UserType.Employee
: UserType.Customer;
if (Type != newType)
{
int delta = newType == UserType.Employee ? 1 : -1;
company.ChangeNumberOfEmployees(delta);
}
Email = newEmail;
Type = newType;
}
}
The refactored User collaborates with Company in process while leaving database and message-bus communication to the controller. section 7.2
