016
behavioral DDD

Application Service

Reference Wikipedia ↗
Application Service — class diagram
Plate 016 class diagram

The Application Service pattern defines a layer responsible for coordinating the execution of use cases within a domain-driven design (DDD) application. It acts as an intermediary between the presentation layer (UI, API) and the domain layer, encapsulating the application’s behavior and orchestrating domain objects to fulfill specific tasks. Crucially, it does not contain business rules; those reside within the domain objects themselves.

This pattern promotes separation of concerns, making the application more maintainable and testable. By containing the transaction management and coordination logic in a dedicated service layer, the domain layer remains focused on core business logic and doesn’t get cluttered with infrastructure concerns. It also provides a clear boundary for applying security, caching, and other cross-cutting concerns.

Usage

The Application Service pattern is commonly used in:

  • Web Applications: Handling user requests (e.g., creating an account, placing an order) by coordinating domain operations.
  • Microservices: Defining public interfaces for services, encapsulating internal domain logic.
  • Command Query Responsibility Segregation (CQRS): Implementing the command side, receiving commands from the UI and orchestrating actions on the domain.
  • Event-Driven Systems: Receiving events from external sources and initiating domain workflows.
  • Backend for Frontends (BFF): Tailoring application logic for specific client applications.

Examples

  1. Rails (Ruby on Rails): Rails’ concept of “Services” heavily embodies the Application Service pattern. For example, a OrderProcessingService might handle the entire process of creating an order, validating inventory, charging the customer, and updating the database. The controller receives the user request and delegates to this service.

  2. Spring (Java Spring Framework): In Spring, @Service annotated classes are frequently used as Application Services. For instance, a UserService could encapsulate the logic for registering a new user, updating user profiles, or changing passwords. It uses @Autowired to inject domain repositories and orchestrates those to fulfill user use cases.

  3. Django (Python Django Framework): Django uses services (often as functions within services.py modules or as classes) to encapsulate application-level logic. A PaymentService might interact with a third-party payment gateway and update order statuses in the database, keeping the core domain models clean.

Specimens

awaiting submission

No language specimens have been submitted for this pattern yet. Contribute one.