Skip to main content

Repository Pattern

The Tournament Management App implements the Repository Pattern to abstract data access logic and provide a clean separation between the business logic and data persistence layers. Each domain entity has a corresponding repository interface and implementation.

Architecture

Layer Structure

Key Components

Interfaces

Define the contract for data operations (CRUD + custom queries)

Implementations

Concrete classes that interact with Entity Framework Core DbContext

DataContext

EF Core DbContext managing all DbSets and database configuration

Dependency Injection

Repositories registered as singletons in Program.cs

Available Repositories

Common Patterns

Standard CRUD Operations

All repositories implement consistent CRUD operations:

Eager Loading with Include

Repositories use .Include() to load related entities:

AsNoTracking for Read Operations

Query operations use .AsNoTracking() for better performance:
AsNoTracking() tells EF Core not to track entities in the change tracker, improving performance for read-only queries.

Duplicate Validation

All repositories implement a validateDuplicates method:

DataContext Configuration

The DataContext class inherits from DbContext and manages all entity sets:
DataContext.cs
The DataContext configures all foreign keys with DeleteBehavior.Restrict to prevent cascading deletes and maintain referential integrity.

Dependency Injection Setup

Repositories are registered in Program.cs as singletons:
Program.cs (Lines 16-21)

Using Repositories in Controllers

Error Handling

Repositories use two main error handling patterns:

1. Exception Throwing

2. Null Returns

Database Provider

The application uses SQLite as its database provider:
  • Connection String: Data Source=/app/Torneo.db
  • Provider: Microsoft.EntityFrameworkCore.Sqlite
  • Configurable: Via DATABASE_CONNECTION_STRING environment variable
The codebase includes commented-out SQL Server configuration for future migration scenarios.

Best Practices

Apply .AsNoTracking() to queries that don’t need to update entities. This improves performance by preventing EF Core from tracking changes.
Call validateDuplicates before Add/Update operations to prevent constraint violations.
Check for null returns from repository methods before accessing properties.
Always inject repositories through constructor injection rather than creating instances directly.

Next Steps

DirectorTecnico Repository

Manage technical directors

Equipo Repository

Manage teams with advanced queries

Jugador Repository

Manage players and positions

Partido Repository

Manage matches and scores