Stage 1 - Layered Architecture

Layered Architecture is the first architectural shape most backend developers meet. It divides code by responsibility: controllers speak HTTP, services coordinate use cases, repositories talk to storage, and domain objects represent business concepts. The point is not to create folders named controller, service, and repository. The point is to stop every class from knowing every detail of the system.

Stage 1 - Layered Architecture

Why This Matters

Architecture is the part of backend development that decides where code is allowed to live and what it is allowed to know. Without architecture, every feature becomes a direct path from HTTP to database to external service and back. That may work for a small demo, but in a real product the same shortcut creates fragile code. A change in the database leaks into the API. A validation rule appears in three services. A transaction starts in the wrong place. A class becomes impossible to test without starting the whole application.

Good architecture is not about drawing impressive diagrams. It is about reducing the cost of change. A developer should be able to add a field, change a rule, replace an external provider, or test a use case without rewriting unrelated parts of the system. The design should make the common path obvious and the dangerous path difficult.

How To Think About It

Start with responsibility. Ask what decision a piece of code owns. A controller owns HTTP details. A use case owns application flow. A repository owns persistence access. A mapper owns data conversion. A transaction boundary owns atomicity. When those responsibilities are mixed, bugs become harder to locate because one class is doing several jobs for several reasons.

Then look at direction. Dependencies should usually point from outer details toward stable inner rules, or from high-level policy toward replaceable details through interfaces. If a domain rule imports a web controller, the dependency direction is wrong. If a service creates an HTTP client directly inside a method, the dependency is hidden. If a mapper loads lazy relations from the database by accident, the boundary is leaking.

Concrete Example

@Service
public class CreateOrderUseCase {
  private final OrderRepository orders;
  private final PaymentPort payments;

  @Transactional
  public OrderResult create(CreateOrderCommand command) {
    Order order = Order.create(command.customerId(), command.items());
    payments.reserve(order.total());
    return OrderResult.from(orders.save(order));
  }
}

This example is small, but it shows several architectural choices. The use case receives dependencies instead of constructing them. The payment system is represented by a port, not a concrete SDK class. The transaction boundary surrounds the business operation. The API layer can map a request DTO into CreateOrderCommand before this code runs, and the response DTO can be built after the use case returns.

Useful Reference

Concept Meaning
Controller HTTP input and output
Use case Application flow
Repository Persistence access
Boundary Clear ownership line

Common Mistakes

  • Treating folders as architecture while dependencies still point everywhere.
  • Adding abstractions before there is a real reason to change an implementation.
  • Letting controllers contain business rules because it is faster today.
  • Returning entities from API endpoints and calling that simple.
  • Placing transaction boundaries around helper methods instead of use cases.
Glossary
architecture
The set of boundaries, dependency directions, and design choices that control how a system changes.
layer
A group of code with one broad responsibility, such as web, application, domain, or persistence.
use case
Application operation that coordinates a business action from input to result.
port
Interface owned by the application core to describe needed or offered behavior.
adapter
Technology-specific implementation that connects the core to the outside world.
SOLID
Five object-oriented design principles for maintainable and replaceable code.
DRY
Principle that avoids duplicating the same knowledge or business rule in multiple places.
KISS
Principle that favors the simplest design that clearly solves the current problem.
YAGNI
Principle that avoids building features or extension points before they are needed.
design pattern
Named reusable solution to a recurring design problem.
DTO
Data Transfer Object used to carry data across a boundary without exposing internal models.
mapper
Code that converts data between models such as entity, DTO, command, and response.
dependency injection
Technique where objects receive required dependencies from the outside.
transaction boundary
The start and end of a database transaction around one consistent unit of work.