43 lines
1.2 KiB
Cheetah
43 lines
1.2 KiB
Cheetah
---
|
|
description: "Injectable dependency injection conventions for {{PROJECT_NAME}} — Pillar 4"
|
|
alwaysApply: true
|
|
---
|
|
|
|
# Injectable (get_it) Standards — {{PROJECT_NAME}}
|
|
|
|
## Setup
|
|
```dart
|
|
// lib/core/di/injection.dart
|
|
@InjectableInit()
|
|
void configureDependencies() => getIt.init();
|
|
```
|
|
|
|
## Annotations
|
|
```dart
|
|
@singleton // One instance for app lifetime
|
|
@lazySingleton // Created on first access (preferred for most services)
|
|
@injectable // New instance each time (use sparingly)
|
|
@factoryMethod // Custom factory logic
|
|
```
|
|
|
|
## Example
|
|
```dart
|
|
@lazySingleton
|
|
class ProductRepository {
|
|
final DioClient _client;
|
|
const ProductRepository(this._client); // Constructor injection
|
|
}
|
|
|
|
@injectable
|
|
class GetProductsUseCase {
|
|
final ProductRepository _repo;
|
|
const GetProductsUseCase(this._repo);
|
|
}
|
|
```
|
|
|
|
## Rules
|
|
- Run `dart run build_runner build` after adding/modifying `@injectable` annotations
|
|
- **NEVER** use `getIt<T>()` in widget `build()` methods — inject via constructor or provider
|
|
- Use `@module` for third-party registrations (Dio, SharedPreferences, etc.)
|
|
- Register environment-specific implementations with `@Environment('dev')` / `@Environment('prod')`
|