Purpose
The Data Access Layer (DAL) in SMAF provides a centralized abstraction for all database operations, ensuring consistent data access patterns across the application. Located in theInapescaWeb.DAL namespace, the DAL implements a repository-like pattern with specialized manager classes for different business domains.
Architecture
Layer Separation
The DAL maintains strict separation between business logic and data access:- Business entities are passed as parameters but never modified by DAL methods
- SQL queries are constructed and executed exclusively in DAL classes
- Connection management is centralized through
MngConexion - No business logic resides in DAL - only data retrieval and persistence
Manager Classes Structure
The DAL is organized into specialized manager classes following the naming conventionMngDatos*:
| Manager Class | Responsibility |
|---|---|
MngConexion | Database connection management and encryption |
MngDatosComision | Travel commission data operations |
MngDatosUsuarios | User data retrieval and management |
MngDatosViaticos | Travel allowance calculations |
MngDatosProyecto | Project data operations |
MngDatosCatalogos | Catalog and lookup tables |
MngDatosReportes | Report data aggregation |
MngActualizaDatos | User profile update operations |
Each manager class focuses on a specific business domain, containing only static methods for data access. This design simplifies dependency management and ensures stateless operations.
Data Access Patterns
Query Pattern
Most read operations follow this standard pattern:Insert/Update Pattern
Write operations return boolean success indicators:Exception Handling Strategy
Current Implementation
The DAL uses a fail-safe approach where exceptions are caught at higher layers:- DAL methods do not throw exceptions
- Failed operations return
null, empty collections, orfalse - Connection disposal occurs in
finallyblocks (implicit in using statements) - The presentation layer checks return values to determine success
Stored Procedure Usage
Current State
SMAF’s DAL does not use stored procedures. All data access is performed through dynamically constructed SQL statements within the C# code. Rationale:- Direct SQL provides better visibility into query logic
- Easier debugging during development
- Simplified deployment (no database-side code changes)
- Query construction can adapt dynamically to business rules
- Less database-side optimization
- SQL logic distributed across multiple DAL classes
- Potential for query duplication
Database Technology
SMAF uses MySQL as its database backend, accessed via theMySql.Data.MySqlClient library.
Key characteristics:
- All queries target MySQL-specific syntax
- Connection strings stored encrypted in
Web.config - Multiple database connections supported (SMAF, Contratos, DGAIPP modules)
- Connection pooling handled by MySqlConnection provider
Common Conventions
Method Naming
| Prefix | Purpose | Return Type |
|---|---|---|
Obtiene_ | Retrieve single value | string, int, or entity |
Lista_ | Retrieve collection | List<T> |
Inserta_ | Insert new record | Boolean |
Update_ | Modify existing record | Boolean |
Obtiene_Max_ | Get next sequence number | string |
Data Conversion
All database values are retrieved as strings and converted as needed:Null Handling
Empty values are typically represented as empty strings rather than null:Best Practices
When working with SMAF’s DAL:
- Always dispose connections using
MngConexion.disposeConexionSMAF() - Close DataReaders before disposing connections
- Use
clsFunciones.FormatFecha()for date formatting - Check for empty string results from query methods
- Follow existing naming conventions for consistency
- Keep business logic out of DAL methods