Overview
TheRegistrosServiceTs is an Angular injectable service that manages the restaurant’s seafood dish catalog. It provides methods to retrieve, display, and delete dish records (registros), maintaining an in-memory data store of 6 signature seafood dishes.
This service is provided at the root level (
providedIn: 'root'), making it a singleton available throughout the application.Data Structure
Registro Interface
Each dish record follows theRegistro interface structure:
Unique identifier for the dish
Name of the seafood dish (e.g., “Ceviche de Pescado”)
URL to the dish’s photo (using Unsplash or Bing image CDN)
Array of ingredients or preparation notes for the dish
Default Dish Catalog
The service initializes with 6 signature seafood dishes:Methods
getRegistros()
Retrieves all dish records from the service.Returns a shallow copy of the entire registros array
The method returns a spread copy (
[...this.registros]) to prevent direct mutation of the internal array, following immutability best practices.getRegistro()
Retrieves a single dish record by its ID.The unique identifier of the dish to retrieve
Returns a shallow copy of the matching registro object, or undefined if not found
deleteRegistro()
Removes a dish record from the catalog by filtering it out of the array.The unique identifier of the dish to delete
This method does not return a value
The deletion is permanent for the current session. Since the service uses an in-memory store, deleted records will reappear when the application restarts.
addRegistro()
Registro object and add it to the internal array:
Service Injection
The service can be injected into any Angular component or service:Best Practices
Immutability
Immutability
The service returns shallow copies of data using the spread operator (
...), preventing direct mutation of the internal state. This is a good practice for maintaining data integrity.Singleton Pattern
Singleton Pattern
With
providedIn: 'root', Angular ensures only one instance of RegistrosServiceTs exists throughout the application, maintaining consistent state across all components.Data Persistence
Data Persistence
Currently, the service uses in-memory storage. For production applications, consider:
- Integrating with a backend API
- Using Ionic Storage for local persistence
- Implementing NgRx or another state management solution
Type Safety
Type Safety
Always type your component properties with the
Registro interface to leverage TypeScript’s type checking and IDE autocomplete features.Related Resources
Registro Interface
Learn about the Registro data model structure
HomePage Component
See how the home page displays all registros
DetalleRegistroPage
View and delete individual dish records
Angular Services
Official Angular documentation on services