liveBook · Manning

Application service

As explained in Simple Object-Oriented Design: Create clean, maintainable applications

An application service coordinates application tasks and interactions between components without containing the domain's business rules.

An application service coordinates an application operation across repositories, validators, and domain objects. It controls the flow of a request—load data, reject invalid input, invoke domain behavior, and return or raise the result—without owning the business rules themselves. Keeping that control flow separate from domain logic makes a growing design easier to change and test. section 3.2.4

Coordinate enrollment without absorbing its rules

An add-employee operation loads an offering and employee, validates the request, then calls offering.addEmployee(employee). The service prevents direct access to the operation, while the validator holds rules such as capacity, repeat-participation limits, and duplicate enrollment. The service is therefore an orchestrator, not the place to decide those rules. section 3.2.4 section 3.2.4

Listing 3.5 AddEmployeeToOfferingService class
class AddEmployeeToOfferingService {
 
  private OfferingRepository offerings;
  private EmployeeRepository employees;
  private AddEmployeeToOfferingValidator validator;
 
  public void addEmployee(int offeringId, int employeeId) {
 
    var offering = offerings.findById(offeringId);
    var employee = employees.findById(employeeId);
 
    if(offering == null || employe == null)                  #1
      throw new InvalidRequestException(
        "Offering and employee IDs should be valid");
 
    var validation = validator.validate(offering, employee); #2
    if(validation.hasErrors()) {                             #3
      throw new ValidationException(validation);
    }
 
    offering.addEmployee(employee);                          #4
  }
}

Trace the boundary: the service retrieves objects, checks validation results, and delegates the state change to the offering. section 3.2.4

Do not move domain invariants into a service merely to avoid loading aggregate state. A cancellation service that directly cancels an enrollment and increments available spots weakens the aggregate root’s control and can leave loaded objects inconsistent. When performance pressure changes the design, refactor deliberately: keep the service coordinating the cancellation and move availability calculation to an appropriate repository query rather than exposing a mutable spot counter. section 6.2.2 section 6.2.2

Listing 6.5 CancelEnrollmentService application service
class CancelEnrollmentService {
  private OfferingRepository offerings;
  private EnrollmentRepository enrollments;
  public void cancel(int offeringId, int employeeId) {     #1
    if(offeringId==null || employeeId==null)
      throw new InvalidArgumentException();
    Offering offering = offerings.getById(offeringId);
    if(offering == null)
        throw new OfferingDoesntExistException();
    Enrollment enrollment =
      offerings.getEnrollment(offeringId, employeeId);
    if(enrollment == null)
        throw new EnrollmentDoesntExistException();
    Calendar now = Calendar.getInstance();
    enrollment.cancel(now);                                #2
  }
}
class Offering {                                           #3
  // private int availableSpots;
  // public void cancel(Enrollment enrollmentToCancel) { ... }
}

Compare this cancellation flow with a service that increments spots directly: it coordinates lookup and cancellation while avoiding a public aggregate mutation. section 6.2.2

Use the distinction pragmatically:

  • Start simply when an operation is small, even if one service temporarily mixes orchestration and rules. section 3.2.4
  • Refactor once growing complexity makes the separation useful; place business rules in domain services or domain objects and retain coordination in the application service. section 3.2.4
  • Protect aggregate invariants rather than exposing convenience methods that let callers alter related state independently. section 6.2.2

How Simple Object-Oriented Design: Create clean, maintainable applications covers it

How deep it goes
Works through concrete enrollment and cancellation flows, contrasting orchestration with validation rules and aggregate-invariant control.
Examples
Uses adding an employee to a training offering and cancelling an enrollment, including service, validator, and repository-query listings.
What it assumes
Assumes object-oriented services, repositories, and domain objects; introduces the application-versus-domain-service distinction directly.
What you can do after
Evaluate/understand when a service is coordinating work versus taking over business rules or weakening aggregate invariants.
How it connects
Connects application services to domain services, aggregate roots, repositories, validation, and invariants.

Also explained in

Compare how 3 books explain application service →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Simple Object-Oriented Design: Create clean, maintainable applications. The section links point to the parts of the book it draws on.