Skip to main content

Overview

The IRepositorioMunicipio interface and RepositorioMunicipio implementation provide data access operations for managing municipalities. Municipalities are associated with teams (Equipos) in the system.

Interface Definition

IRepositorioMunicipio.cs

Implementation

RepositorioMunicipio.cs

Methods Reference

AddMunicipio

Adds a new municipality to the database.
Municipio
required
The Municipio entity to add
Municipio
The inserted Municipio entity with its generated ID
Implementation (Lines 8-13)
Usage Example:

GetAllMunicipios

Retrieves all municipalities with their associated teams.
IEnumerable<Municipio>
Collection of all Municipio entities with eagerly loaded Equipos
Implementation (Lines 14-26)
Key Features:
  • Uses .Include(m => m.Equipos) to eagerly load teams
  • Uses .AsNoTracking() for improved read performance
  • Returns complete object graph including teams
Usage Example:

GetMunicipio

Retrieves a specific municipality by ID.
int
required
The ID of the municipality to retrieve
Municipio
The Municipio entity, or null if not found
Implementation (Lines 27-31)
Usage Example:

UpdateMunicipio

Updates an existing municipality’s information.
Municipio
required
The Municipio entity with updated values (must include valid Id)
Municipio
The updated Municipio entity
Exception
Throws exception with message “Municipio not found” if the ID doesn’t exist
Implementation (Lines 32-44)
Updated Properties:
  • Nombre - Municipality name
Usage Example:

DeleteMunicipio

Deletes a municipality from the database.
int
required
The ID of the municipality to delete
Municipio
The deleted Municipio entity
Exception
Throws exception with message “Municipio not found” if the ID doesn’t exist
Implementation (Lines 45-57)
Deleting a municipality will fail if it has associated teams due to foreign key constraints configured with DeleteBehavior.Restrict. Remove all teams from the municipality before deleting.
Usage Example:

validateDuplicates

Validates whether a municipality name already exists in the database.
Municipio
required
The Municipio entity to validate
bool
true if a duplicate municipality name exists (different ID with same name), false otherwise
Implementation (Lines 59-86)
Validation Logic:
  1. Case-insensitive comparison using .ToLower()
  2. Trims whitespace from input
  3. Excludes current municipality when updating (checks municipio.Id != municipioIngresado.Id)
  4. Returns false on any exception
Usage Example:

Dependency Injection

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

Common Workflows

Municipio Entity

Domain model for municipalities

Equipo Repository

Teams associated with municipalities

Error Handling

Both UpdateMunicipio and DeleteMunicipio throw exceptions when the entity is not found. Always handle these exceptions:

Foreign Key Constraints

Cascade Delete Restriction:The DataContext configures all foreign keys with DeleteBehavior.Restrict. This means:
  • You cannot delete a municipality that has associated teams
  • You must first remove or reassign all teams before deleting the municipality
  • This prevents accidental data loss and maintains referential integrity
DataContext.cs (Lines 37-40)

Performance Considerations

Optimization Tips:
  • GetAllMunicipios() uses .AsNoTracking() for improved read performance
  • validateDuplicates() calls GetAllMunicipios(), which may be inefficient for large datasets
  • Consider implementing indexed queries for duplicate validation in production
  • The .Include(m => m.Equipos) loads all teams - omit if not needed

Business Rules

Municipality names must be unique (case-insensitive). The validateDuplicates method enforces this rule before creating or updating municipalities.
Municipalities cannot be deleted if they have associated teams. This ensures data consistency and prevents orphaned team records.
Municipality names are trimmed during validation to prevent duplicates caused by leading/trailing whitespace.