|
| 1 | +# Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This service uses a **Layered Architecture** to keep our code clean, clear, and easy to maintain. |
| 6 | + |
| 7 | +We mostly follow the [NestJS philosophy](https://docs.nestjs.com/#philosophy), but interpret it to fit our needs. |
| 8 | + |
| 9 | +We separate the system into 3 layers: |
| 10 | + |
| 11 | +1. **Controller** – Talks to the client |
| 12 | +2. **Service** – Handles the business logic |
| 13 | +3. **Repository** – Talks to the database |
| 14 | + |
| 15 | +### 1. Controller Layer (Client-facing) |
| 16 | + |
| 17 | +- Receives data from the client (DTO) |
| 18 | +- Returns data to the client (DTO) |
| 19 | +- Validates data types |
| 20 | +- Calls the service layer |
| 21 | +- Can shape requests and responses, without performing any business logic |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +### 2. Service Layer (Business Logic) |
| 26 | + |
| 27 | +- Contains the business logic |
| 28 | +- Can perform any kind of calculation or transformation as long as it's part of the business rules |
| 29 | +- Validates logic rules (e.g., checking if a user can register) |
| 30 | +- Handles errors and logging |
| 31 | +- Calls the repository layer to get or save data |
| 32 | + |
| 33 | +--- |
| 34 | + |
| 35 | +### 3. Repository Layer (Database Access) |
| 36 | + |
| 37 | +- Talks to the database |
| 38 | +- Only responsible for saving and retrieving data |
| 39 | +- **No** assumptions about validation |
| 40 | +- **No** business logic should go here |
| 41 | + |
| 42 | +## Types we use |
| 43 | + |
| 44 | +> [!NOTE] |
| 45 | +> We will use the `User` entity as an example for the rest of this document. |
| 46 | +
|
| 47 | +To keep things clear & scalable, we separate each "entity" into three types: |
| 48 | + |
| 49 | +| Type | Associated Layer | Purpose | |
| 50 | +| --------------------------------------------------- | ---------------- | ---------------------------------------------- | |
| 51 | +| `CreateUserDto`, `UpdateUserDto`, `UserResponseDto` | Controller | Used to talk with the client | |
| 52 | +| `IUser` | All | Common contract shared between layers | |
| 53 | +| `User` | Repository | Defines how the data is stored in the database | |
| 54 | + |
| 55 | +## Type Design Principles |
| 56 | + |
| 57 | +1. **Interfaces vs Classes**: |
| 58 | + - Use interfaces (`IUser`) to define contracts between layers |
| 59 | + - Use classes (`User`) for concrete implementations. The (database) entity is a concrete implementation of the interface. |
| 60 | + - This separation allows for better testing and flexibility |
| 61 | + |
| 62 | +2. **Canonical Forms**: |
| 63 | + - Store canonical forms in the database (e.g., `birthdate`) |
| 64 | + - The canonical form is represented in the entity (`User`) *and* the interface (`IUser`) |
| 65 | + - The DTO might use a different form, e.g. `CreateUserDto` might use `age` instead of `birthdate` |
| 66 | + - Use mappers to convert between forms |
| 67 | + |
| 68 | +3. **System vs Domain Properties**: |
| 69 | + - System properties (`id`, `createdAt`, `updatedAt`) are managed by the base entity |
| 70 | + - Domain properties (e.g. `email`, `name`) are defined in the interface, enforced by the entity, and controlled by the DTOs |
| 71 | + |
| 72 | +## Examples |
| 73 | + |
| 74 | +### Example 1: Can register? |
| 75 | + |
| 76 | +```typescript |
| 77 | +canRegister(user: Partial<IUser>) { |
| 78 | + if (user.email.endsWith('@banned.com')) { |
| 79 | + throw new ForbiddenException('Email domain is not allowed'); |
| 80 | + } |
| 81 | + |
| 82 | + if (!this.isOldEnough(user.birthdate)) { |
| 83 | + throw new ForbiddenException('User must be at least 13 years old'); |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +This check lives in the service layer because: |
| 89 | + |
| 90 | +- It's business logic |
| 91 | +- It could change based on product decisions |
| 92 | +- It might be reused across different controllers (`signup`, `adminCreateUser`, etc.) |
| 93 | +- If tomorrow we add GraphQL on top of our REST, this logic will remain the same |
| 94 | + |
| 95 | +### Example 2: Normalize email |
| 96 | + |
| 97 | +```typescript |
| 98 | +normalizeEmail(email: string) { |
| 99 | + return email.toLowerCase().trim(); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +Also clearly service-level: it’s a standardized rule, not controller-specific logic. |
| 104 | + |
| 105 | +## See also |
| 106 | + |
| 107 | +- **Project structure** - see [Project Structure](#TODO) |
| 108 | +- **Contributing** - see [Developer's Guide](CONTRIBUTING.md) |
| 109 | +- **Diagrams** - see [Diagrams](#TODO) |
| 110 | +- **Documentation (for consumers)** - see [RealWorld Backend Specifications](https://realworld-docs.netlify.app/specifications/backend/introduction/) |
0 commit comments