Overview
The Business Logic Layer (capa_negocio) sits between the presentation and data layers, orchestrating operations and enforcing business rules. This layer ensures that all business logic is centralized and reusable across different presentation interfaces.Responsibilities
The business layer handles:- Orchestrating calls to the data layer
- Implementing business rules and validations
- Coordinating operations across multiple data access classes
- Providing a clean API for the presentation layer
- Ensuring data integrity and business constraints
Business Layer Classes
Each domain entity has a corresponding business logic class:CN_Canchas
Soccer field management logic
CN_Clientes
Client management logic
CN_Reservas
Reservation management logic
CN_Canchas Class
TheCN_Canchas class manages business logic for soccer fields.
Complete Implementation
Fromcapa_negocio/CN_canchas.cs:
Key Characteristics
Data Layer Abstraction
Data Layer Abstraction
The business layer creates instances of data access classes and delegates database operations:
Simplified Method Names
Simplified Method Names
Business layer methods have cleaner names than data layer methods:
- Business:
ListarCanchas()→ Data:Listar() - Business:
AgregarCancha()→ Data:AgregarCancha() - Business:
Actualizar()→ Data:ActualizarCancha()
Single Responsibility
Single Responsibility
Each method handles one specific business operation, making the code easy to understand and maintain.
CN_Clientes Class
TheCN_Clientes class manages business logic for clients.
Complete Implementation
Fromcapa_negocio/CN_Clientes.cs:
Business Logic Features
Null Safety
FromCN_Clientes.cs:21-30, the Listar method includes null checking:
This prevents null reference exceptions in the presentation layer by always returning a valid list (even if empty).
CN_Reservas Class
TheCN_Reservas class handles reservation business logic, including filtering and search functionality.
Complete Implementation
Fromcapa_negocio/CN_Reservas.cs:
Advanced Features
Search Functionality
TheListarNombre method provides filtered search capabilities:
- Accepts a search term from the presentation layer
- Delegates the filtered query to the data layer
- Returns only reservations matching the search criteria
ViewModel Support
The insert method accepts aReservaViewModel instead of a simple entity:
The
ReservaViewModel contains not just the reservation entity, but also lists of available clients and fields for dropdown menus.Layer Communication Pattern
The business layer follows a consistent pattern for communicating with the data layer:Dependency Injection Pattern
Each business class creates an instance of its corresponding data access class:Method Naming Conventions
Business layer methods follow clear naming patterns:| Operation | Method Name | Example |
|---|---|---|
| Retrieve all | Listar() or Listar[Entity]() | ListarCanchas() |
| Search/Filter | Listar[Criteria]() | ListarNombre(string) |
| Create | Insertar() or Agregar[Entity]() | AgregarCancha() |
| Update | Actualizar() | Actualizar(CE_Canchas) |
| Delete | Eliminar() | Eliminar(int id) |
Entity Objects
Business methods accept and return entity objects fromcapa_entidad:
Business Logic Examples
Example 1: Adding a Soccer Field
Example 2: Searching Reservations
Example 3: Updating a Client
Where to Add Business Rules
This layer is the ideal place to add:Validation Logic: Verify data meets business requirements
Authorization Checks: Ensure users have permission
Calculations: Compute prices, totals, or derived values
Cross-Entity Operations: Coordinate multiple data operations
Transaction Management: Ensure data consistency
Example: Adding Price Calculation
Benefits of the Business Layer
Centralized Business Logic
Centralized Business Logic
All business rules are in one place, making them easy to find, update, and test.
Presentation Independence
Presentation Independence
The same business logic can serve web controllers, APIs, console apps, or mobile apps.
Testability
Testability
Business logic can be tested independently of database or UI concerns.
Consistency
Consistency
All parts of the application use the same business rules, preventing discrepancies.
Layer Dependencies
The business layer depends on:- capa_dato: Data access classes for database operations
- capa_entidad: Entity classes for data transfer
- System.Collections.Generic: For working with lists
- System.Linq: For data querying and filtering
- capa_presentacion: Controllers that handle HTTP requests
Next Steps
Presentation Layer
Learn how controllers use business layer classes
Data Layer
Understand the data access methods being called