Skip to content

Layered architecture and dependency injection

Introduction

Divide code by responsibility into layers (API, business, data), each doing only its own job; with dependency injection, whatever a component needs is handed in from outside rather than built by itself.

Why use it

  • Clear responsibilities: changing a page does not touch business rules; changing the database does not touch business rules.
  • Easy to test: when testing business logic, you can hand it a “fake data layer” with no real database.
  • Easy to replace: swapping a payment provider or storage approach means changing one place.

How to do it

  1. The common three layers:
    • API layer (Controller / routes): receive the request, validate input, return the result; no business rules here.
    • Business layer (Service): rules and flows, such as “placing an order must check stock and compute discounts”.
    • Data layer (Repository, or directly an ORM): only responsible for persistence.
  2. Dependencies point one way: the API layer calls the business layer, the business layer calls the data layer; never the other way around.
  3. Dependency injection: declare “what I need” in the constructor (or function parameters); the framework creates and passes it in at runtime.
  4. When the business layer fails, throw a meaningful error and let the API layer turn it into a response (see error handling).

Component comparison

Classic three-layer (API / business / data layer)

Hand-rolledOpen sourceOpsLow

The most common split: the API layer takes requests, the business layer holds rules, the data layer stores and fetches data, and upper layers call lower layers.

Best forSmall and medium projects, mostly CRUD, with a not-very-experienced team.

Avoid whenBusiness rules are complex and must evolve for a long time, and you want business logic fully independent of the database and framework.

Pros

The structure is simple, and a newcomer can understand it at a glance

Most framework tutorials and samples use this kind of layering

A small project only needs two or three layers, so it is quick to start

Cons

The business layer often depends on the data layer, so changing databases still affects it

As the project grows, code for one feature spreads across the layers

It easily turns into empty layers that only forward the call

Clean Architecture

Hand-rolledOpen sourceOpsLow

Put business rules at the very center with the database and web framework in outer rings; dependencies may only point inward, making replacement easy.

Best forSystems with complex business rules, long lifecycles, and high testability requirements.

Avoid whenSmall projects, prototypes, and CRUD-centric admin backends.

Pros

The business core does not depend on the framework or database and can be tested on its own

Swapping databases or external services barely touches the core code

Clear boundaries, suited to complex, long-lived systems

Cons

Noticeably more projects, interfaces, and mapping code, so even simple features need plenty of boilerplate

The team must understand and obey the dependency rule, otherwise it quickly deforms

Over-engineering for simple CRUD projects

Vertical slice (organized by feature)

Hand-rolledOpen sourceOpsLow

Do not split folders by technical layer; split by feature instead: the endpoint, rules, and data access of a "place order" feature live together.

Best forProjects with many features of widely varying complexity, and teams that want to reduce cross-layer changes.

Avoid whenThe team has no experience with this style of organization and nobody leads in setting rules.

Pros

Changing a feature means working in one place, with no jumping between layers

Each feature can choose the implementation that suits it, keeping simple things simple

Features affect each other little, which suits parallel development by several people

Cons

Duplicate code across features may increase, and you must deliberately extract shared parts

Without common rules, features may be written inconsistently

Teams used to traditional layering need an adjustment period

Checklist

  • The API layer has no business rules; the business layer does not depend on HTTP objects
  • Dependencies point one way; there are no cycles
  • External dependencies (database, third-party APIs) come in through injection so tests can replace them
  • The number of layers matches the project size; do not layer for its own sake