dependency injection
μ±μμμ μμ‘΄μ± μ£Όμ
μ¬μ€ μ¬κΈ°μ μμ‘΄μ± μ£Όμ μ΄λΌλ λ¨μ΄λ₯Ό μ¨λ λ μ§ κ°μΈμ μΌλ‘ μ λ§€νλ€λ μκ°μ λ λ€. μλνλ©΄ μμ‘΄μ± μ£Όμ μ μ¬μ©νλ μͺ½μμ νμν κ²μ μμ±νμ¬ μ¬μ©νλλ‘ νλ κ²μ΄ μλλΌ λ°μλλ‘ μ¬μ©νκ² λλ 'μ μ΄μ μμ ' μ΄λΌλ νμμ λ°μμν€λ νμλΌκ³ μκ°νλ€. κ·Έλ¦¬κ³ μ΄ νμμ μ μ λ μΆμνλ μΈν°νμ΄μ€ μλμμ μ¬λ¬ ꡬνμ²΄κ° κ²½μ°μ λ°λΌ λ€λ₯Έ ꡬνμ²΄κ° νμν μν©μ΄λΌλ κ²μ΄λ€.
λ¬Όλ‘ μ΄κ² μΈμλ ν¨μ¨μ± μΈ‘λ©΄μμ λ κ° μ΄μμ κ°μ²΄κ° νμ μκΈ°μ μ±κΈν€ μΌλ‘ νλλ§ λ§λ€μ΄λκ³ κ³μ μ¬μ¬μ©μ νλ€λ κ²½μ μ± μΈ‘λ©΄λ μκΈ΄ νμ§λ§, μ€νλ§μμμ μμ‘΄μ± μ£Όμ μ μ μ΄μ μμ μ ν΅ν΄μ ν μ€νΈλ μ©μ΄νκ³ μ€κ³μ λ μ μ°ν μ€κ³(κ°μ²΄κ°μ μ μ°ν νλ ₯ ꡬ쑰)λ₯Ό λ¬μ±νκΈ° μν¨μΈ μΈ‘λ©΄μ΄ λ ν° κ² κ°λ€.
κ·Έλ°λ° μ±μμμ μμ‘΄μ± μ£Όμ μ μ¬μ€ μ±κΈν€μΌλ‘ λ§λ€μ΄μ§ νλμ κ°μ²΄μ λν μμν μ¬μ©μ΄ κ°μ₯ ν° μ¬μ© λͺ©μ μΈ κ² κ°μμ μ¬μ€ μ΄κ±Έ μμ‘΄μ± μ£Όμ μ΄λΌκ³ νννκΈ° 보λ€λ 'λꡬλ μ κ·ΌνκΈ° μ¬μ΄ μ΅μλ¨ μμ ―' μ΄λΌλ ννμ΄ μ μ νμ§ μμκΉ μΆλ€.
μμ‘΄μ± μ£Όμ μ μ½κ² ν μ μλλ‘ λμμ£Όλ ν¨ν€μ§μ΄λ€.
This is a simple Service Locator for Dart and Flutter projects with some additional goodies highly inspired by Splat. It can be used instead of
InheritedWidget
orProvider
to access objects e.g. from your UI.Typical usage:
Accessing service objects like REST API clients or databases so that they easily can be mocked.
Accessing View/AppModels/Managers/BLoCs from Flutter Views
InheritedWidget κ³Ό Provider λͺ¨λ ν΅μ¬ μ리λ context μ°Έμ‘°λ‘ μμ ―νΈλ¦¬λ₯Ό νμνμ¬ μ μ¬λΌκ°μ μνλ μμ ―μ access νλ κ²μ΄λ©° get_it μμ λΉμ·ν μλ¦¬λ‘ λ§λ€μ΄μ Έ μλ€. 곡μ λ¬Έμλ₯Ό 보면 μλμ κ°μ΄ μλνκ³ μλ€.
Extremely fast (O(1))
Easy to learn/use
Doesn't clutter your UI tree with special Widgets to access your data like, Provider or Redux does.
νμ€ν Provider 보λ€λ μ°Έμ‘°νλ μ½λκ° μ‘°κΈ λ κ°κ²°νλ€κ³ λκΌλ€.
get_it μ μ΄μ©ν μμ‘΄μ± μ£Όμ
ꡬν
ν¨ν€μ§λ₯Ό μΌλ¨ μ€μΉνλ€.
flutter pub add get_it
κΈλ‘λ²νκ² μ°Έμ‘°λμ΄ μ¬μ©λλ―λ‘ dependency_injectionμ application layer μ μμΉνλ€.

import 'package:device_info_plus/device_info_plus.dart';
import 'package:get_it/get_it.dart';
class Injector {
Injector._();
static DeviceInfoPlugin get deviceInfoPlugin =>
GetIt.instance.get<DeviceInfoPlugin>();
static Future registerDependencies() async {
_registerUtils();
_registerNetworks();
_registerRepositories();
}
static _registerUtils() async {
GetIt.instance
.registerLazySingleton<DeviceInfoPlugin>(() => DeviceInfoPlugin());
}
static _registerNetworks() async {}
static _registerRepositories() async {}
}
κΈ°λ³Έ μμ±μλ₯Ό private μ²λ¦¬ν΄μ μΈλΆμμ Injector λ₯Ό μμ±νμ§ λͺ»νκ² νλ€.
registerDependencies() ν¨μ λ΄μμ μ©λλ³λ‘ ꡬλΆλ λ©μλλ₯Ό λ€μ νΈμΆνκ² ν΄μ μ½λ κ°λ μ±μ λνλ€. λ¨μν λμ΄μμΌλ‘ νμνκ±Έ μ λ±λ‘νλκ² μλλΌ μ΄λ€ λνλμκ° λ¬΄μ¨ μ©λμΈμ§λ₯Ό μ½κ² νμ νκ² νλ€λ μλ―Έμ΄λ€.
getter λ₯Ό μ μΈν΄μ€μΌ λ€λ₯Έ μμ ―μμ μ°Έμ‘° ν μ μλ€.
μ μ½λλ λ μ΄μ§λ‘λ© λ°©μμΌλ‘ ꡬν ν κ²μΈλ° λ μ΄μ§νμ§ μκ² λ°λ‘ μ¦μ λ±λ‘μν€λ λ°©λ²λ μλ€. 곡μ λ¬Έμμ μμΈν λμμκ³ μ 리ν κ°μΉκ° μμ λ§νΌ 볡μ‘νμ§ μμμ μλ΅νλ€.
μΌλ¨ μν ꡬνμ μν΄μ μ΄λ€ νλ‘μ νΈλ λ΄κ° μΈ κ²μΌλ‘ μμλλ device_info_plusλ₯Ό λ±λ‘νλ€.
run() async {
await Injector.registerDependencies();
runApp(const AppName());
}
environment μμ ꡬννλ run() ν¨μμμ runApp() μ νκΈ° μ μ μμ κ°μ΄ μ²λ¦¬ν΄μ€λ€. μ μμ μ νμ μ κΈ°λ°μΌλ‘ κ°μ²΄λ₯Ό μμ± λ° μμ± μ€λΉ(μλ§λ νλ‘μ ν¨ν΄μΌλ‘ μΌλ¨ λ±λ‘μ ν΄λκ³ λ΄λΆ μ°Έμ‘° λ³μλ₯Ό λΉμλλ€κ° μ΅μ΄ μ°Έμ‘°μ λ©λͺ¨λ¦¬μ ν λΉν΄μ£Όλ λ°©μμΌ κ² κ°μλ° λ λ€μ¬λ€ λ³΄μ§ μμλ€)μ ν΄λμ κ²μΌλ‘ μκ° λλ€.(get_it μ λ΄λΆ ꡬλ μλ¦¬κ° μ΄κ²μΌλ‘ μΆμΈ‘λλλ° νμ΅μ κ°μ±λΉλ₯Ό μν΄ μ¬κΈ°μ μΌλ¨ νΈκΈ°μ¬μ λλλ€.)
Future<void> _showPlatformInfo() async {
final DeviceInfoPlugin deviceInfoPlugin = Injector.deviceInfoPlugin;
if (Platform.isAndroid) {
final AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;
print(androidInfo.toString());
}
if (Platform.isIOS) {
final IosDeviceInfo iosDeviceInfo = await deviceInfoPlugin.iosInfo;
print(iosDeviceInfo.toString());
}
}
μ¬μ©μ μμ κ°μ΄ μ μΈν΄λ getter λ₯Ό ν΅ν΄μ access ν΄μ μ¬μ©νλ€.
Last updated