fixed resolve method

This commit is contained in:
Sergey Penkovsky
2021-04-21 08:25:55 +03:00
parent b2b66bdcfd
commit c9ddc2ffa8
2 changed files with 16 additions and 7 deletions

View File

@@ -4,15 +4,20 @@ import 'package:dart_di/experimental/scope.dart';
import 'package:dart_di/experimental/module.dart'; import 'package:dart_di/experimental/module.dart';
class AppModule extends Module { class AppModule extends Module {
bool isMock;
AppModule({required this.isMock});
@override @override
void builder(Scope currentScope) { void builder(Scope currentScope) {
bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock()); bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock());
bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl()); bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl());
}
}
class FeatureModule extends Module {
bool isMock;
FeatureModule({required this.isMock});
@override
void builder(Scope currentScope) {
bind<DataRepository>() bind<DataRepository>()
.withName("networkRepo") .withName("networkRepo")
.toProvide( .toProvide(
@@ -33,10 +38,14 @@ class AppModule extends Module {
void main() async { void main() async {
final scope = openRootScope().installModules([ final scope = openRootScope().installModules([
AppModule(isMock: false), AppModule(),
]); ]);
final dataBloc = scope.resolve<DataBloc>(); final subScope = scope
.openSubScope("featureScope")
.installModules([FeatureModule(isMock: true)]);
final dataBloc = subScope.resolve<DataBloc>();
dataBloc.data.listen((d) => print('Received data: $d'), dataBloc.data.listen((d) => print('Received data: $d'),
onError: (e) => print('Error: $e'), onDone: () => print('DONE')); onError: (e) => print('Error: $e'), onDone: () => print('DONE'));

View File

@@ -83,6 +83,6 @@ class Scope {
} }
// 2 Поиск зависимостей в родительском скоупе // 2 Поиск зависимостей в родительском скоупе
return _parentScope != null ? _parentScope!.tryResolve() : null; return _parentScope != null ? _parentScope!.tryResolve(named: named) : null;
} }
} }