Dependency Injection & Services
Relevant source files
The following files were used as context for generating this wiki page:
The "Bazar del Venado" project utilizes a robust Dependency Injection (DI) pattern in the backend to manage component lifetimes and decoupling, while the frontend employs a service-based architecture to abstract API communication. In the backend, services are registered within the .NET Core container to handle database access, security, and session management. In the frontend, modular service objects encapsulate fetch and axios logic to provide a clean interface for UI components.
Backend Service Registration
The backend leverages the built-in .NET DI container to register services with specific lifetimes: Singleton, Scoped, and Transient. This configuration is centralized in the application entry point.
Service Lifetimes and Registrations
| Service Type | Implementation | Lifetime | Description |
|---|---|---|---|
IdentidadAjuste | Configuration Section | Singleton | Global identity settings. |
IAplicacionBdContexto | AplicacionBdContexto | Scoped | Database context tied to request lifetime. |
IHasherServicio | Sha512HasherServicio | Transient | Stateless hashing for passwords. |
ITokenIdentidadServicio | JwtTokenServicio | Transient | Logic for JWT generation/validation. |
Sources: Back/Alumnos/Api/Program.cs:15-20
Dependency Injection in Controllers
Controllers consume registered services via constructor injection. This allows controllers like UsuariosController to access database contexts and security services without manual instantiation.
The diagram above illustrates how the Program.cs configuration enables the injection of required interfaces into the UsuariosController.
Sources: Back/Alumnos/Api/Controllers/UsuariosController.cs:18-24
Frontend Service Architecture
The frontend organizes logic into dedicated service modules (e.g., usuariosService, productosService, authService). These modules handle HTTP requests, authentication header injection, and response parsing.
API Communication Flow
The frontend services utilize a centralized configuration for the base URL and interceptors to ensure every request includes necessary credentials.
This flow shows how the api.js and api.ts files process requests and manage the lifecycle of the JWT token.
Sources: Front/src/services/api.js:4-18, Front/src/services/api.ts:40-55
Service Definitions
| Service | Responsibility | Key Methods |
|---|---|---|
usuariosService | User management and profiles | obtenerUsuario, registrarUsuario, actualizarImagen |
productosService | CRUD operations for marketplace items | crearProducto, obtenerProductos, eliminarProducto |
authService | Session and authentication | login, logout |
Sources: Front/src/services/api.js:21-127, Front/src/services/api.ts:58-155
Authentication and Security Services
Security logic is abstracted into services to handle sensitive operations like password hashing and session tracking.
Backend Session Service
The UsuarioSesionServicio manages user logins by validating credentials against the database and creating new session records.
The sequence shows the interaction between the controller and the session service to establish a user session.
Sources: Back/Alumnos/Api/Servicios/UsuarioSesionServicio.cs:22-45
JWT Validation Service
In Program.cs, the DI container provides ITokenIdentidadServicio during the OnTokenValidated event to perform deep validation of incoming JWT claims.
Sources: Back/Alumnos/Api/Program.cs:78-95
Summary
The project effectively separates concerns through its service-oriented architecture. The backend's use of Dependency Injection via Program.cs ensures that components like UsuariosController and CategoriasController remain testable and decoupled from specific implementations of the database or security logic. Simultaneously, the frontend service layer provides a consistent interface for UI components to interact with the backend, managing complex tasks like token persistence and multi-part form data transparently.