injectable | Dart package
Injectable is a convenient code generator for get_it. Inspired by Angular DI, Guice DI and inject.dart.
pub.dev
DI(Dependency Injection) 패키지인 get_it 의 짝궁인 injectable 패캐지를 소개합니다.
IoC(Inversion of Control)의 이점이 있는 get_it을 더 편리하게 사용할 수 있습니다.
그렇기에 필수적으로 get_it 패키지와 함께 사용해야합니다.
Annotation을 사용하기에 백엔드 프레임워크인 Spring을 해보신 분들이라면 익숙하길꺼라 생각듭니다.
1. 사용하기
- pubspec.yaml에 get_it과 injectable 관련 패키지를 추가합니다.
- build_runner를 통해 코드 생성을 하기 위해 injectable_generator 패키지도 함께 추가합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies: | |
injectable: | |
get_it: | |
dev_dependencies: | |
injectable_generator: | |
build_runner: |
- dart 파일을 생성해 초기화 메소드를 생성해줍니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'di.config.dart'; | |
final getIt = GetIt.instance; | |
@InjectableInit( | |
initializerName: 'init', // default | |
preferRelativeImports: true, // default | |
asExtension: true, // default | |
) | |
void configureDependencies() => getIt.init(); |
- main 에서 초기화 매소드를 호출해 injectable 패키지를 사용할 수 있도록 합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
configureDependencies(); | |
runApp(MyApp()); | |
} |
2. Factory
- @injectable을 사용해 Factory 등록을 합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:injectable/injectable.dart'; | |
@injectable | |
class ServiceA { | |
void serviceCheck(){ | |
print("ServiceA"); | |
} | |
} | |
@injectable | |
class ServiceB { | |
final ServiceA _serviceA; | |
ServiceB(this._serviceA); | |
void serviceCheck(){ | |
_serviceA.serviceCheck(); | |
} | |
} |
- ServiceB 클래스 내 ServiceA 클래스가 DI 되어 있어있는 것을 확인할 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main(){ | |
final serviceB = getIt<ServiceB>(); | |
serviceB.serviceCheck(); //ServiceA | |
} |
3. Singletone
- @singleton 와@lazySingleton을 사용해 싱클톤 등록을 합니다.
- @disposeMethod 을 활용해 싱글톤을 직접 폐기 시킬 수 있습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@singleton // or lazySingleton | |
class DataSource { | |
@disposeMethod | |
void dispose(){ | |
// logic to dispose instance | |
} | |
} |
4. Module
- ThirdParty 의 종속성을 부여하기 위해 사용합니다.
- module로 등록하기 위해선 abstict class 여야합니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@module | |
abstract class RegisterModule { | |
Future<SharedPreferences> get prefs => SharedPreferences.getInstance(); | |
} |
간단한 사용법에 대해 설명하였습니다.
이 외에도 DI 관련 다양한 활용법이 있습니다. 패키지 README를 꼭 한 번 확인 후 간단하게 DI, IoC를 활용해보세요~!
'Flutter' 카테고리의 다른 글
에뮬레이터 localhost(127.0.0.1) 안될 때 (0) | 2025.01.05 |
---|---|
WillPopScope이 안된다면?? (PopScope 사용하기) (0) | 2023.08.28 |
Flutter - Supported OS (0) | 2023.08.07 |
Flutter - invalid_annotation_target 경고 없애기 (0) | 2023.06.02 |
font_awesome_flutter (다양한 아이콘 사용) (0) | 2023.06.01 |