Habitacion entity represents a hotel room with state management capabilities. It maintains a complete history of state changes through the HabitacionHistorial pattern, enabling tracking of availability, occupancy, maintenance, and cleaning states.
Overview
Habitacion is a sealed entity that manages room information and maintains an immutable history of state transitions. The current state is determined by the latest open history record.
Namespace: SGRH.Domain.Entities.Habitaciones
Source: ~/workspace/source/SGRH.Domain/Entities/Habitaciones/Habitacion.cs
Properties
Unique identifier for the room (primary key).
Reference to the room category (determines capacity, pricing, amenities).
Room number (must be > 0).
Floor number where the room is located (must be > 0).
Read-only collection of all state change records. Modifications only through
CambiarEstado().Calculated property: Returns the current active state record (where
FechaFin is null).Constructor
Disponible state.
Parameters
- categoriaHabitacionId: Valid category ID (must be > 0)
- numeroHabitacion: Room number (must be > 0)
- piso: Floor number (must be > 0)
Initial State
Automatically creates a history record with:EstadoHabitacion=DisponibleFechaInicio=DateTime.UtcNowFechaFin=null(open/active)MotivoCambio=null
Example
Room States
EstadoHabitacion Enum
State Transition Rules
Disponible
Disponible
Available for Reservation
- Room is ready for guests
- Can be reserved
- No motivo required
- Cannot have a motivo specified
Ocupada
Ocupada
Currently Occupied
- Guest is checked in
- Cannot be reserved (except by current reservation)
- No motivo required
- Cannot have a motivo specified
Mantenimiento
Mantenimiento
Under Maintenance
- Room needs repairs or upgrades
- Cannot be reserved
- Motivo REQUIRED: Must specify reason for maintenance
- Prevents reservations during maintenance period
Limpieza
Limpieza
Being Cleaned
- Room is being prepared
- Cannot be reserved
- Motivo REQUIRED: Must specify cleaning reason/type
- Temporary state before returning to Disponible
Methods
CambiarEstado
- Validates the new state is different from current
- Closes the current history record (
FechaFin = DateTime.UtcNow) - Creates a new open history record with the new state
- nuevoEstado: Target state
- motivo: Required for
MantenimientoandLimpieza, forbidden for others
BusinessRuleViolationExceptionif already in the target stateValidationExceptionif motivo rules are violated (see HabitacionHistorial)
CambiarCategoria
- nuevaCategoriaId: Valid category ID (must be > 0)
Category changes affect future pricing but do not modify existing reservations.
HabitacionHistorial
Each state change creates a history record:History Record Lifecycle
- Created:
FechaInicio = DateTime.UtcNow,FechaFin = null(open) - Active: The current state (only one record has
FechaFin = null) - Closed: When state changes,
FechaFin = DateTime.UtcNow
Motivo Validation Rules
Implemented inHabitacionHistorial constructor:
Business Rules
State Change Validation
State Change Validation
- Cannot transition to the same state (throws
BusinessRuleViolationException) - Each state change must follow motivo rules
- All state changes are tracked with timestamps
- Current state is always the record with
FechaFin = null
History Immutability
History Immutability
- History cannot be modified directly from outside the entity
- Only
CambiarEstado()can add history records - Closed records cannot be reopened (enforced by
HabitacionHistorial.Cerrar())
Maintenance Blocking
Maintenance Blocking
Rooms in
Mantenimiento state cannot be reserved. The reservation policy (IReservaDomainPolicy.EnsureHabitacionNoEnMantenimiento) validates this at the application layer.Usage Patterns
Checking Current State
State History Audit
Typical Workflow
Related Entities
- CategoriaHabitacion: Defines room type, capacity, and base pricing
- TarifaTemporada: Pricing per category per season (see TarifaTemporada)
- Reserva: References rooms through
DetalleReserva(see Reserva)
Related Documentation
- TarifaTemporada - Room pricing by season
- Reserva - Reservation management
- Validation Guards - Validation mechanisms