Overview
TheIRepositorioJugador interface and RepositorioJugador implementation provide data access operations for managing players. Each player is associated with a team (Equipo) and a position (Posicion).
Interface Definition
IRepositorioJugador.cs
Implementation
RepositorioJugador.cs
Methods Reference
AddJugador
Adds a new player to the database with associations to team and position.Jugador
required
The Jugador entity to add
int
required
The ID of the team the player belongs to
int
required
The ID of the player’s position
Jugador
The inserted Jugador entity with its generated ID and populated relationships
Implementation (Lines 8-17)
- Validates team and position exist before association
- Sets relationships to
nullif referenced entities don’t exist - Returns complete entity with relationships
GetAllJugadores
Retrieves all players with their team and position relationships.IEnumerable<Jugador>
Collection of all Jugador entities with eagerly loaded Equipo and Posicion
Implementation (Lines 18-28)
Equipo- The player’s teamPosicion- The player’s position (e.g., Delantero, Defensa, etc.)
GetJugador
Retrieves a specific player by ID with team and position relationships.int
required
The ID of the player to retrieve
Jugador
The Jugador entity with Equipo and Posicion loaded, or null if not found
Implementation (Lines 30-38)
UpdateJugador
Updates an existing player’s information and relationships.Jugador
required
The Jugador entity with updated values (must include valid Id)
int
required
The ID of the new or existing team
int
required
The ID of the new or existing position
Jugador
The updated Jugador entity with current relationships
Implementation (Lines 40-52)
Nombre- Player nameNumero- Jersey numberEquipo- Associated teamPosicion- Player position
DeleteJugador
Deletes a player from the database.int
required
The ID of the player to delete
Jugador
The deleted Jugador entity
Exception
Throws exception with message “Jugador not found” if the ID doesn’t exist
Implementation (Lines 54-65)
validateDuplicates
Validates whether a player with the same name, number, team, and position already exists.Jugador
required
The Jugador entity to validate
int
required
The team ID to check against
int
required
The position ID to check against
bool
true if a duplicate player exists (different ID but same name, number, team, and position), false otherwiseImplementation (Lines 67-90)
- Case-insensitive name comparison
- Exact number match
- Same team ID
- Same position ID
- Excludes current player when updating (checks
jugador.Id != j.Id)
The duplicate validation checks ALL four fields (name, number, team, position). A player can have the same name or number if they play in a different team or position.
Dependency Injection
Register the repository inProgram.cs:
Program.cs (Line 21)
Common Workflows
Adding a Player to a Team
Adding a Player to a Team
Transferring a Player to Another Team
Transferring a Player to Another Team
Listing Players by Position
Listing Players by Position
Finding Players by Team
Finding Players by Team
Related Entities
Jugador Entity
Domain model for players
Equipo Repository
Team data access
Posicion Repository
Position data access
Business Rules
Error Handling
The
DeleteJugador method throws an exception when the player is not found. Always handle this exception: