Overview
TheIRepositorioPartido interface and RepositorioPartido implementation provide data access operations for managing matches (partidos). Each match involves two teams: a home team (Local) and a visiting team (Visitante).
Interface Definition
IRepositorioPartido.cs
Implementation
RepositorioPartido.cs
Methods Reference
AddPartido
Adds a new match to the database with associations to home and visiting teams.Partido
required
The Partido entity to add
int
required
The ID of the home team
int
required
The ID of the visiting team
Partido
The inserted Partido entity with its generated ID and populated team relationships
Exception
Throws exception with message “Partido not found” if the insert fails
Implementation (Lines 8-17)
- Validates both teams exist before association
- Sets team relationships to
nullif teams don’t exist - Throws exception if insert fails
GetAllPartidos
Retrieves all matches with home and visiting team relationships.IEnumerable<Partido>
Collection of all Partido entities with eagerly loaded Local and Visitante teams
Implementation (Lines 18-30)
Local- The home teamVisitante- The visiting team
GetPartido
Retrieves a specific match by ID with team relationships.int
required
The ID of the match to retrieve
Partido
The Partido entity with Local and Visitante teams loaded
Exception
Throws exception with message “Partido not found” if the ID doesn’t exist
Implementation (Lines 31-39)
UpdatePartido
Updates an existing match’s information and team associations.Partido
required
The Partido entity with updated values (must include valid Id)
int
required
The ID of the home team (new or existing)
int
required
The ID of the visiting team (new or existing)
Partido
The updated Partido entity with current relationships
Exception
Throws exception with message “Partido not found” if the ID doesn’t exist
Implementation (Lines 40-62)
Local- Home teamVisitante- Visiting teamFechaHora- Match date and timeMarcadorLocal- Home team scoreMarcadorVisitante- Visiting team score
DeletePartidos
Deletes a match from the database.int
required
The ID of the match to delete
Partido
The deleted Partido entity
Exception
Throws exception with message “Partido not found” if the ID doesn’t exist
Implementation (Lines 64-83)
validateDuplicates
Validates whether a match with the same teams and date/time already exists.Partido
required
The Partido entity to validate
int
required
The home team ID to check against
int
required
The visiting team ID to check against
bool
true if a duplicate match exists (same teams and date/time), false otherwiseImplementation (Lines 86-118)
- Checks if same teams play at same date/time
- Also checks reverse scenario (teams swapped as home/away)
- Excludes current match when updating (checks
p.Id != partido.Id) - Returns
falseon any exception
Smart Duplicate Detection:This validation prevents both:
- Exact duplicates: Team A (home) vs Team B (away) at same time
- Reverse duplicates: Team B (home) vs Team A (away) at same time
Dependency Injection
Register the repository inProgram.cs:
Program.cs (Line 19)
Common Workflows
Scheduling a Match
Scheduling a Match
Updating Match Score
Updating Match Score
Getting Team Match History
Getting Team Match History
Finding Upcoming Matches
Finding Upcoming Matches
Related Entities
Partido Entity
Domain model for matches
Equipo Repository
Team data access
Business Rules
Error Handling
Multiple methods throw exceptions when entities are not found:
AddPartido- throws if insert failsGetPartido- throws if match not foundUpdatePartido- throws if match not foundDeletePartidos- throws if match not found
Performance Considerations
Optimization Tips:
- All read operations use
.AsNoTracking()for better performance validateDuplicates()loads all matches - consider date-range queries for production- Use
.Include()only when you need team information - Consider adding indexes on
FechaHorafor faster date-based queries