Service hierarchy
Services are organized in a hierarchical structure with a base service providing common functionality:Base service pattern
All HTTP-based services extendBaseService for consistent HTTP configuration:
base.service.ts:1-37
The base service provides three HTTP option configurations for different response types: plain text, text with accept header, and JSON.
Modern dependency injection
Services use Angular 21’s functional injection pattern:- Functional injection
- Constructor injection
Using inject() function
The modern approach uses theinject() function instead of constructor parameters:backend.service.ts:21-34
The
inject() function provides better tree-shaking, clearer dependencies, and more flexible injection contexts.Service organization
Services are organized by function into subdirectories:Utils services
Utils services
Location:
src/app/_services/__Utils/Utility services provide cross-cutting concerns:- ConfigService: Configuration management and external config loading
- SearchService: Search functionality
- SearchCustomService: Custom search implementations
- SpeechService: Speech recognition and synthesis
- ChatService: Chat functionality
- VersionCacheService: Version and cache management
config.service.ts:12-43
Backend services
Backend services
Location:
src/app/_services/BackendService/The main backend service handles API communication:backend.service.ts:49-74
Feature services
Feature services
Location: Various subdirectoriesFeature-specific services:
- AlgorithmService (
_services/AlgorithmService/): Algorithm demonstrations - TetrisService (
_services/__Games/TetrisService/): Tetris game logic - SudokuService (
_services/__Games/SudokuService/): Sudoku game logic - PdfService (
_services/__FileGeneration/): PDF generation
AI services
AI services
Location:
src/app/_services/__AI/AI and machine learning services:- OCRService: Optical character recognition
- ComputerVisionService: Computer vision capabilities
- TensorflowService: TensorFlow integration
Service lifecycle management
Angular 21 introducesDestroyRef for cleaner subscription management:
Configuration-driven API calls
Services useConfigService to retrieve API endpoints from external configuration:
algorithm.service.ts:42-49
Benefits of configuration-driven approach
Environment flexibility
Switch between dev, staging, and production APIs without code changes
External configuration
Update API endpoints by modifying JSON files, no rebuild required
Multi-backend support
Support multiple backend technologies (.NET, Java, Node.js, Python)
Runtime configuration
Load configuration at runtime via APP_INITIALIZER
Multi-backend architecture
The application demonstrates polyglot backend support:algorithm.service.ts:42-67
This architecture demonstrates how a single frontend can communicate with multiple backend technologies, useful for microservices or technology migration scenarios.
Speech service example
TheSpeechService demonstrates browser API integration:
speech.service.ts:1-41
Key patterns demonstrated
Browser API integration
Browser API integration
Services can wrap browser APIs for Angular integration:
- Feature detection for API availability
- Event handler binding in constructor
- Error handling for unsupported browsers
Stateful services
Stateful services
Services can maintain state:
isListening: Current recording statetranscript: Latest recognized texterror: Last error message
Cross-browser compatibility
Cross-browser compatibility
Service registration
Services useprovidedIn: 'root' for singleton behavior:
Benefits of root-level provision
Singleton pattern
One instance shared across the entire application
Tree-shakable
Unused services are automatically removed from production builds
No module import
No need to add to providers array in any module
Lazy loading safe
Works correctly with lazy-loaded modules
HTTP interceptor pattern
The application uses functional HTTP interceptors:app.module.ts:39-60
Functional interceptors are lighter weight than class-based interceptors and support dependency injection via
inject().Error handling service
Global error handling is implemented as a service:app.module.ts:66-74
app.module.ts:113
Best practices demonstrated
Separation of concerns
Separation of concerns
- Services handle business logic and data access
- Components handle presentation and user interaction
- Base classes provide shared functionality
Configuration over code
Configuration over code
- API endpoints in external configuration files
- Environment-specific settings loaded at runtime
- No hardcoded URLs in service code
Consistent HTTP handling
Consistent HTTP handling
- Standardized HTTP options via base service
- Centralized error logging via interceptor
- Proper subscription cleanup with DestroyRef
Modern Angular patterns
Modern Angular patterns
- Functional dependency injection with
inject() - Functional HTTP interceptors
- Automatic cleanup with
takeUntilDestroyed() - Singleton services with
providedIn: 'root'
Next steps
Architecture overview
Review the complete application architecture
Routing system
Understand the routing configuration
PWA features
Learn about Progressive Web App capabilities
Component reference
Explore the component documentation