Overview
TheIRepositorioMunicipio 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)
GetAllMunicipios
Retrieves all municipalities with their associated teams.IEnumerable<Municipio>
Collection of all Municipio entities with eagerly loaded Equipos
Implementation (Lines 14-26)
- Uses
.Include(m => m.Equipos)to eagerly load teams - Uses
.AsNoTracking()for improved read performance - Returns complete object graph including teams
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)
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)
Nombre- Municipality name
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)
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 otherwiseImplementation (Lines 59-86)
- Case-insensitive comparison using
.ToLower() - Trims whitespace from input
- Excludes current municipality when updating (checks
municipio.Id != municipioIngresado.Id) - Returns
falseon any exception
Dependency Injection
Register the repository inProgram.cs:
Program.cs (Line 16)
Common Workflows
Creating a Municipality
Creating a Municipality
Updating a Municipality
Updating a Municipality
Listing Municipalities with Team Counts
Listing Municipalities with Team Counts
Finding Municipalities Without Teams
Finding Municipalities Without Teams
Related Entities
Municipio Entity
Domain model for municipalities
Equipo Repository
Teams associated with municipalities
Error Handling
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 performancevalidateDuplicates()callsGetAllMunicipios(), 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
Unique Municipality Names
Unique Municipality Names
Municipality names must be unique (case-insensitive). The
validateDuplicates method enforces this rule before creating or updating municipalities.Referential Integrity
Referential Integrity
Municipalities cannot be deleted if they have associated teams. This ensures data consistency and prevents orphaned team records.
Name Trimming
Name Trimming
Municipality names are trimmed during validation to prevent duplicates caused by leading/trailing whitespace.