Skip to main content

Overview

The IRepositorioJugador 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)
Key Features:
  • Validates team and position exist before association
  • Sets relationships to null if referenced entities don’t exist
  • Returns complete entity with relationships
Usage Example:

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)
Included Relationships:
  • Equipo - The player’s team
  • Posicion - The player’s position (e.g., Delantero, Defensa, etc.)
Usage Example:

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)
Usage Example:

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)
Updated Properties:
  • Nombre - Player name
  • Numero - Jersey number
  • Equipo - Associated team
  • Posicion - Player position
Usage Example:

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)
Usage Example:

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 otherwise
Implementation (Lines 67-90)
Validation Logic:
  1. Case-insensitive name comparison
  2. Exact number match
  3. Same team ID
  4. Same position ID
  5. Excludes current player when updating (checks jugador.Id != j.Id)
Usage Example:
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 in Program.cs:
Program.cs (Line 21)
Inject into controllers:

Common Workflows

Jugador Entity

Domain model for players

Equipo Repository

Team data access

Posicion Repository

Position data access

Business Rules

Unique Player Constraint:A player is considered unique by the combination of:
  • Name (case-insensitive)
  • Jersey number
  • Team
  • Position
This means:
  • Same player can have different numbers in different teams
  • Same number can be used by different players in different teams
  • Same player can play different positions in different teams

Error Handling

The DeleteJugador method throws an exception when the player is not found. Always handle this exception: