Dependency injection là truyền dependency (service, repository) vào class thay vì tạo chúng bên trong.
- Thay vì
class UserBloc { final repo = UserRepository(); }, hãy inject:class UserBloc { UserBloc(this.repo); final UserRepository repo; }. - Điều này cho phép test (mock repository) và linh hoạt (hoán đổi implementation).
- Dùng
GetItcho service locator pattern hoặc truyền dependency qua constructor.
Dependency injection is passing dependencies into a class instead of creating them inside.
- This enables testing with mocked dependencies and flexibility to swap implementations.
- Use
GetItfor service locator pattern or pass dependencies through constructor chains.