# Angular Dependency Injection, Services, and HTTP Apply these rules when creating Angular services, configuring dependency injection, or calling HTTP APIs. The TypeScript modeling and runtime-safety rules cover contracts and validation; this file adds Angular specifics. ## Dependency injection and services * Prefer the `inject()` function over constructor parameter injection, as the Angular style guide recommends since v20; keep constructor injection where the surrounding code consistently uses it. * Scope services intentionally. Use root singletons only for truly shared, stateless, or coordinated state, and never store request- or component-specific mutable state in them. * Use injection tokens for configuration or real abstractions; do not add interfaces only to satisfy dependency injection. * Keep services focused, expose readonly state to consumers, and separate API access, domain logic, and UI state where practical. Do not use services merely to move poorly structured component code elsewhere. ## HTTP and APIs * Keep API access centralized in typed data-access services, and keep DTOs separate from domain models when their responsibilities differ. * The type parameter of `HttpClient.get()` or `httpResource()` is a compile-time assertion, not validation. Treat responses as `unknown` and validate them at the data-access boundary before they drive authorization, persistence, navigation, or rendering. * Configure HTTP with `provideHttpClient(withInterceptors([...]), withFetch())` and functional interceptors in new code, not `HttpClientModule` or `HTTP_INTERCEPTORS` classes. Use interceptors only for cross-cutting concerns. * Avoid duplicate requests, cache only with a clear invalidation strategy, cancel requests the user no longer needs, and handle expected error states explicitly. * Avoid manual URL string construction when typed helpers or centralized endpoints are available. ## Errors * Route unhandled errors to one `ErrorHandler`; `provideBrowserGlobalErrorListeners()` forwards uncaught errors and unhandled promise rejections to it. Report them without secrets or personal data, and show users a recoverable state instead of a broken view.