liveBook · Manning

Service layer

As explained in Grokking Software Architecture

The architectural layer that coordinates application use cases and orchestrates interactions with domain and infrastructure components.

A service layer is the conductor of an application use case: it receives a request, coordinates the work, and returns the result. Also called the application layer, it translates actions such as “create order” into domain operations and may handle transactions. It directs business work without owning the core business rules. section 4.2.3

Without clear boundaries, a controller can grow into a catch-all for HTTP handling, business rules, database access, and email. Keep HTTP concerns in presentation, orchestration in the service layer, business rules in the domain, and external details in infrastructure. A rich domain model keeps its rules testable in isolation. section 4.7.4 section 4.2.3

Create an order

A request to create an order reaches an application service. The service coordinates domain objects or services and any needed infrastructure work; the domain owns the business rules, while infrastructure handles details such as database access or sending email. If database and email components need to work together, the application layer can coordinate them rather than making those infrastructure siblings depend on each other. section 4.2.3 section 4.4.1

/my-project/src/
  /api/                 # Application Layer (part 1)
    controllers.js  
    routes.js           # Handles HTTP, validation, calls service 
  /app/                 # Presentation layer
    app.js              # Main UI (React/Angular/Vue, etc) code
    Index.html
  /domain/                # Domain Layer
    order.js
    widget.js           # Widget class with business logic
  /lib/                 # Infrastructure Layer (part 2)
    emailClient.js      # Other external services
  /repositories/        # Infrastructure Layer (part 1)
    widgetRepository.js # Database code
  /services/            # Application Layer (part 2)
    orderService.js     # Orchestrates the “create order” use case
    widgetService.js

See one folder-based arrangement: services orchestrate use cases, domain objects hold business logic, and repositories and clients handle infrastructure. section 4.4.1

In the layered blueprint, commands and dependencies point downward; result data can flow back up. Lower layers should not initiate commands to higher layers. Infrastructure can supply concrete implementations of interfaces defined above it, helping keep external details out of the core. section 4.6.1 section 4.2.3

The boundary matters in production, too. A raw HTTP call buried in checkout logic couples the workflow to a library and URL while hiding risks such as timeouts, outages, and double charges. Inject a service rather than hardcoding the dependency, and make network assumptions and failure risks visible. Protect layer boundaries with automated architectural tests, such as a test preventing controllers from communicating directly with the database. section 10.4.1 section 4.7.4

How Grokking Software Architecture covers it

How deep it goes
Explains layer responsibilities and dependency direction, then illustrates enforcement with an architectural test.
Examples
Illustrates order creation, a folder-based project structure, and a whiteboard exercise assigning components to layers.
What you can do after
Apply the pattern by assigning orchestration, business rules, and external concerns to appropriate layers.
How it connects
Connects layered architecture to separation of concerns, dependency inversion, and the rich domain model.

Also explained in

Compare how 5 books explain service layer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Software Architecture. The section links point to the parts of the book it draws on.