liveBook · Manning

In-process dependency

As explained in Unit Testing Principles, Practices, and Patterns

An in-process dependency is a dependency that executes within the application's process and can provide behavior or data without external communication.

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

How Unit Testing Principles, Practices, and Patterns covers it

How deep it goes
Defines the classification, contrasts interface and mocking choices, and demonstrates refactoring from direct external calls to domain collaboration.
Examples
Uses User, Company, UserController, Database, MessageBus, factories, and single-implementation interfaces as contrasting examples.
What it assumes
Assumes familiarity with domain classes, controllers, interfaces, mocking, and unit-test fragility.
What you can do after
Apply it via domain boundaries that keep external communication in controllers and avoid brittle interaction tests for domain collaborators.
How it connects
Connects domain models with application services, hexagonal architecture, out-of-process dependencies, interfaces, mocking, and test fragility.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Unit Testing Principles, Practices, and Patterns. The section links point to the parts of the book it draws on.