Gate Generator는 Flutter 및 Dart 애플리케이션용 종속성 주입 코드 생성기로, 어노테이션과 build_runner를 활용하여 봄보일러플레이트 코드를 자동으로 생성합니다. 주석이 지정된 클래스를 분석하고 컴파일 시점에 필요한 등록 및 해결 로직을 생성함으로써 종속성 주입 설정을 간소화합니다. 이는 런타임 오버헤드를 줄이고 타입 안전성을 향상시킵니다. 이 도구는 최소한의 구성으로 기존 프로젝트에 원활하게 통합되어 확장 가능하고 유지보수 가능한 애플리케이션 아키텍처에 이상적입니다.
사용 사례
의존성 주입 설정 자동화
수동 DI 블록플레이트 감소
앱 확장성 향상
형 안전한 서비스 해결 활성화
모듈형 애플리케이션 설계 지원
주요 기능
주석 기반 구성
빌드 러너 통합
컴파일 시 코드 생성
최소의 런타임 오버헤드
형 안전한 종속성 해결
적합한 대상
대규모 플러터 애플리케이션
유지보수성을 우선시하는 팀
클린 아키텍처를 사용하는 프로젝트
자동 DI 설정을 원하는 개발자
높은 테스트 가능성 요구 애플리케이션
고려 사항
build_runner 설정이 필요합니다
컴파일 시 생성에 한정
정확한 주석 사용에 의존합니다
빌드 시간을 약간 증가시킬 수 있습니다
동적 DI 시나리오에는 적합하지 않습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
💎 Features
Provide Injectable as a singleton
Provide Injectable as a dynamic service
Inject an Injectable class into another
💡 Motivation
Providing Service should be independent from your pages or other code.
The DI pattern separates the responsibility of creating an object of the service class out of the client class.
Also using code generation we can get rid of the boilerplate part.
🚀 Getting started
install gate with build_runner in you pubspec.yaml
You can inject as many dependency as you want into your class as long as their are Injectable.
part 'coffee_page.gate_inject.g.part'; will be be added to your file
this file will be genererated on next step
don't forget to import "gate_provider.dart" that is generated next
3. Generate code
Using build runner you can run the command in your shell
flutter packages pub run build_runner build --delete-conflicting-outputs
Once build runner is done, you can run your flutter app and use all injected class.
If you change your factory or wants to inject other class... just rerun the build_runner 😀
📗 Properties
Annotation
Description
Injectable
mark a class as containing Singleton or Provide factories
Singleton
mark a factory as a Singleton Injectable
Provide
mark a factory as a Dymamic Injectable. Each time you inject will create a new instance of that class
Inject
mark a class to inject dependencies
Inject annotation
Property
Description
children
a list of InjectedChild
InjectedChild annotation
Property
Description
type
The type to inject
factoryName
The factory name to use
attrName
(optionnal) The attribute name to call it from your class
✅ Tests
Run tests
dart pub global activate coverage
dart test --coverage="coverage"
format_coverage --lcov --in=coverage --out=coverage.lcov --packages=.packages --report-on=lib
Mock an injected dependency for testing
To remplace your service by mocks, you have to set them, before testing.
Gate will generate all methods for you.
First, declare your mocked services with your preferred libraries.
Then use the appProvider to set mocks.
Finally, don't forget to reset mocks by setting them to null.