Skip to content
Ameen Abozaid
JNR-013

Layered architecture

  • 8 Lessons
  • 4 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Controller
  • service
  • repository
  • DTO
  • domain/entity models

Learn first

4 h of practice (estimate)

Learning outcomes

  1. Assign responsibilities to controller, service, repository, and domain layers.
  2. Separate API DTOs from domain/entity models and map between them.
  3. Organize code by feature with layers inside each feature.
  4. Write a minimal @RestController that delegates to a service.

Lessons

  1. 01Why layers
  2. 02Controllers: the HTTP edge
  3. 03Services: business logic and orchestration
  4. 04Repositories: data access behind an interface
  5. 05DTOs vs domain models
  6. 06Package by feature, layer within
  7. 07Demo: building the products feature layer by layer
  8. 08Demo: separating DTOs from domain models

Lab and exercises

Lab

JNR-013-LAB1 · Products feature

Outcomes: 1–4 · Time: ~4 h

  1. In package com.shop.api.products, create Product, ProductRepository, InMemoryProductRepository, ProductService, ProductController.
  2. Implement GET /products and GET /products/{id}.
  3. Return ProductResponse DTOs through a mapper; never the domain class.
  4. Throw a not-found exception from the service for an unknown id (the HTTP mapping comes in JNR-015).

Acceptance: the controller contains no business logic; domain objects are never serialized directly; the service has no HTTP types.

Exercises (assessment: layering review of a provided feature)

IDOutcomeTask
JNR-013-EX011Sort these into layers: parse JSON, check stock, find by id, set HTTP 404, compute total with tax.
JNR-013-EX021Review a provided "fat controller" and list five violations.
JNR-013-EX032Write OrderResponse and a mapper from Order that hides internalNotes.
JNR-013-EX042Explain what breaks for clients if you rename a field of a serialized domain class.
JNR-013-EX053Reorganize a package-by-layer tree into package-by-feature.
JNR-013-EX064Write a @RestController with GET /customers/{id} that only delegates to a service.
JNR-013-EX073Decide: may orders use ProductRepository directly? Justify and write the alternative.
Show solutions
  • EX01 Controller: parse JSON, set HTTP 404. Service: check stock, compute total with tax. Repository: find by id.
  • EX02 Typical violations: repository calls in the controller, business rules in it, a returned entity, try/catch and status logic, one DTO for everything.
  • EX03 record OrderResponse(long id, List<ItemResponse> items, BigDecimal total){} and static OrderResponse toResponse(Order o).
  • EX04 The JSON field name changes, so clients break; a DTO decouples this.
  • EX05 products/{ProductController, ProductService, ProductRepository, dto/} and the same for orders.
  • EX06 @RestController @RequestMapping("/customers") class CustomerController { ... @GetMapping("/{id}") CustomerResponse get(@PathVariable long id){ return service.get(id);} }
  • EX07 No: use ProductService (or a narrow interface) so the products feature controls its own data and rules.

Quiz

Outcomes: O1 layer responsibilities · O2 DTOs vs domain · O3 package by feature · O4 minimal controller.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Where do business rules belong?O1
  2. 2What is a controller's job?O1
  3. 3Which dependency direction is correct?O1
  4. 4A service method returning ResponseEntity is:O1
  5. 5A DTO is:O2
  6. 6Why not serialize domain objects directly?O2
  7. 7Using one DTO for create, update, and response is risky because:O2
  8. 8Package by feature means:O3
  9. 9Another feature needs product data. Best:O3
  10. 10A minimal REST controller:O4

Practical part

Seven exercises JNR-013-EX01 to EX07 plus the lab. The assessment: review a provided feature's layering (EX02 is the template). Pass: quiz at least 8/10, review finds at least four violations, lab acceptance met.

Common mistakes

Fat controllers; services that return ResponseEntity; one DTO reused for everything.

Assessment

Layering review of a provided feature.

Career relevance

The "established architecture" the Junior gate asks the learner to work within; hexagonal architecture (MID-059).

Type to search levels, phases, modules and lessons.