Skip to main content

Overview

The IRepositorioPartido 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)
Key Features:
  • Validates both teams exist before association
  • Sets team relationships to null if teams don’t exist
  • Throws exception if insert fails
Usage Example:

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)
Included Relationships:
  • Local - The home team
  • Visitante - The visiting team
Usage Example:

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

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)
Updated Properties:
  • Local - Home team
  • Visitante - Visiting team
  • FechaHora - Match date and time
  • MarcadorLocal - Home team score
  • MarcadorVisitante - Visiting team score
Usage Example:

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

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 otherwise
Implementation (Lines 86-118)
Validation Logic:
  1. Checks if same teams play at same date/time
  2. Also checks reverse scenario (teams swapped as home/away)
  3. Excludes current match when updating (checks p.Id != partido.Id)
  4. Returns false on 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
This ensures no two teams can play each other at the same time, regardless of home/away designation.
Usage Example:

Dependency Injection

Register the repository in Program.cs:
Program.cs (Line 19)
Inject into controllers:

Common Workflows

Partido Entity

Domain model for matches

Equipo Repository

Team data access

Business Rules

Match Validation Rules:
  1. No Duplicate Matches: Same teams cannot play at the same date/time
  2. Reverse Match Detection: Team A vs Team B = Team B vs Team A (same match)
  3. Team Existence: Both home and visiting teams must exist in the database
  4. Date/Time Required: All matches must have a valid FechaHora value

Error Handling

Multiple methods throw exceptions when entities are not found:
  • AddPartido - throws if insert fails
  • GetPartido - throws if match not found
  • UpdatePartido - throws if match not found
  • DeletePartidos - throws if match not found
Always use try-catch blocks when calling these methods.

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 FechaHora for faster date-based queries