Overview
TheIRepositorioPosicion interface and RepositorioPosicion implementation provide data access operations for managing player positions (e.g., Delantero, Defensa, Mediocampista, Portero). Positions are associated with players (Jugadores) in the system.
Interface Definition
IRepositorioPosicion.cs
Implementation
RepositorioPosicion.cs
Methods Reference
AddPosicion
Adds a new position to the database.Posicion
required
The Posicion entity to add
Posicion
The inserted Posicion entity with its generated ID
Implementation (Lines 8-13)
GetAllPosiciones
Retrieves all positions with their associated players.IEnumerable<Posicion>
Collection of all Posicion entities with eagerly loaded Jugadores
Exception
Throws exception with message “Posiciones not found” if the result is null
Implementation (Lines 14-26)
- Uses
.Include(p => p.Jugadores)to eagerly load all players in each position - Uses
.AsNoTracking()for improved read performance - Throws exception if no positions exist
GetPosicion
Retrieves a specific position by ID.int
required
The ID of the position to retrieve
Posicion
The Posicion entity
Exception
Throws exception with message “Posicion not found” if the ID doesn’t exist
Implementation (Lines 27-31)
UpdatePosicion
Updates an existing position’s information.Posicion
required
The Posicion entity with updated values (must include valid Id)
Posicion
The updated Posicion entity
Exception
Throws exception with message “Posicion not found” if the ID doesn’t exist
Implementation (Lines 33-40)
Nombre- Position name
DeletePosicion
Deletes a position from the database.int
required
The ID of the position to delete
Posicion
The deleted Posicion entity
Exception
Throws exception with message “Posicion not found” if the ID doesn’t exist
Implementation (Lines 42-57)
validateDuplicates
Validates whether a position name already exists in the database.Posicion
required
The Posicion entity to validate
bool
true if a duplicate position name exists (different ID with same name), false otherwiseImplementation (Lines 59-84)
- Case-insensitive comparison using
.ToLower() - Trims whitespace from input
- Excludes current position when updating (checks
posicionIngresada.Id != posicion.Id) - Returns
falseon any exception
Dependency Injection
Register the repository inProgram.cs:
Program.cs (Line 20)
Common Workflows
Setting Up Standard Positions
Setting Up Standard Positions
Listing Positions with Player Counts
Listing Positions with Player Counts
Finding Positions Without Players
Finding Positions Without Players
Renaming a Position
Renaming a Position
Related Entities
Posicion Entity
Domain model for positions
Jugador Repository
Players assigned to positions
Standard Football Positions
Portero
Goalkeeper position
Defensa
Defender position
Mediocampista
Midfielder position
Delantero
Forward/Striker position
Error Handling
Foreign Key Constraints
Cascade Delete Restriction:The DataContext configures all foreign keys with
DeleteBehavior.Restrict. This means:- You cannot delete a position that has associated players
- You must first remove or reassign all players before deleting the position
- This prevents accidental data loss and maintains referential integrity
DataContext.cs (Lines 37-40)
Performance Considerations
Optimization Tips:
GetAllPosiciones()uses.AsNoTracking()for improved read performance- The
.Include(p => p.Jugadores)eagerly loads all players - omit if not needed validateDuplicates()callsGetAllPosiciones(), which may be inefficient for large datasets- Consider implementing indexed queries for duplicate validation in production
Business Rules
Unique Position Names
Unique Position Names
Position names must be unique (case-insensitive). The
validateDuplicates method enforces this rule before creating or updating positions.Referential Integrity
Referential Integrity
Positions cannot be deleted if they have associated players. This ensures data consistency and prevents orphaned player records.
Name Trimming
Name Trimming
Position names are trimmed during validation to prevent duplicates caused by leading/trailing whitespace.
Standard Positions
Standard Positions
While not enforced at the database level, applications typically use standard football positions:
- Portero (Goalkeeper)
- Defensa (Defender)
- Mediocampista/Centrocampista (Midfielder)
- Delantero (Forward/Striker)