# DI-001: Arquitectura Backend

**Proyecto:** Sistema de Registro de Estudiantes
**Fecha:** 2026-01-07

---

## 1. Patrón: Clean Architecture + CQRS

```
┌────────────────────────────────────────────────────────────┐
│                         HOST                                │
│              (Program.cs, DI, Configuration)                │
└─────────────────────────┬──────────────────────────────────┘
                          │
┌─────────────────────────▼──────────────────────────────────┐
│                       ADAPTERS                              │
│  ┌──────────────────────┬─────────────────────────────┐    │
│  │   DRIVING (Primary)  │     DRIVEN (Secondary)      │    │
│  │   ──────────────────  │     ─────────────────────   │    │
│  │   GraphQL API        │     Persistence (EF Core)   │    │
│  │   (HotChocolate)     │     DataLoaders             │    │
│  └──────────────────────┴─────────────────────────────┘    │
└─────────────────────────┬──────────────────────────────────┘
                          │
┌─────────────────────────▼──────────────────────────────────┐
│                     APPLICATION                             │
│        Commands / Queries / Handlers / Validators           │
└─────────────────────────┬──────────────────────────────────┘
                          │
┌─────────────────────────▼──────────────────────────────────┐
│                        DOMAIN                               │
│     Entities / Value Objects / Services / Ports             │
└────────────────────────────────────────────────────────────┘
```

---

## 2. Regla de Dependencia

```
Host → Adapters → Application → Domain
                                   ↑
            Adapters implementa Ports
```

**INVIOLABLE:** Domain NO depende de nada externo.

---

## 3. Responsabilidades por Capa

| Capa | Responsabilidad | Tecnología |
|------|-----------------|------------|
| **Domain** | Entidades, Value Objects, reglas de negocio puras, Ports (interfaces) | C# puro |
| **Application** | Casos de uso (Commands/Queries), DTOs, Validators, orquestación | MediatR, FluentValidation, Mapster |
| **Adapters/Driving** | GraphQL API (Types, Resolvers, Mutations) | HotChocolate |
| **Adapters/Driven** | Repositorios, DbContext, DataLoaders | EF Core, SQL Server |
| **Host** | Composition Root, DI, Middleware | ASP.NET Core |

---

## 4. Estructura de Proyectos

```
src/backend/
├── Domain/
│   ├── Entities/           # Student, Subject, Professor, Enrollment
│   ├── ValueObjects/       # Email, Credits
│   ├── Services/           # EnrollmentDomainService
│   ├── Ports/
│   │   └── Repositories/   # IStudentRepository, ISubjectRepository
│   └── Exceptions/         # MaxEnrollmentsException, SameProfessorException
│
├── Application/
│   ├── Students/
│   │   ├── Commands/       # CreateStudentCommand, UpdateStudentCommand
│   │   ├── Queries/        # GetStudentsQuery, GetStudentByIdQuery
│   │   └── DTOs/           # StudentDto, CreateStudentInput
│   ├── Enrollments/
│   │   ├── Commands/       # EnrollCommand, UnenrollCommand
│   │   ├── Queries/        # GetClassmatesQuery
│   │   └── DTOs/           # EnrollmentDto, EnrollInput
│   └── Common/
│       ├── Behaviors/      # ValidationBehavior
│       └── Mappings/       # MapsterConfig
│
├── Adapters/
│   ├── Driving/
│   │   └── Api/
│   │       ├── Types/      # StudentType, SubjectType
│   │       ├── Queries/    # QueryResolvers
│   │       └── Mutations/  # MutationResolvers
│   └── Driven/
│       └── Persistence/
│           ├── Context/    # AppDbContext
│           ├── Configs/    # StudentConfiguration
│           ├── Repos/      # StudentRepository
│           └── Seeding/    # DataSeeder
│
└── Host/
    └── Program.cs          # Entry point + DI
```

---

## 5. Flujo de una Request

```
1. GraphQL Request → HotChocolate Resolver
2. Resolver → Application Handler (via MediatR)
3. Handler → Validator (FluentValidation)
4. Handler → Domain Service (reglas de negocio)
5. Handler → Repository (persistencia)
6. Repository → DbContext → SQL Server
7. Response ← DTO ← Mapper ← Entity
```

---

## 6. Decisiones de Arquitectura

| Decisión | Razón |
|----------|-------|
| GraphQL vs REST | Flexibilidad en queries, evita over/under-fetching |
| HotChocolate | Mejor soporte .NET, Code-First, DataLoaders |
| MediatR | Desacopla handlers, facilita CQRS |
| Mapster | Más rápido que AutoMapper |
| FluentValidation | Validaciones declarativas y testeables |
