mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 13:47:24 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fc43167756 | ||
|
|
e726e3bf30 | ||
|
|
9032abe21a | ||
|
|
0708b87753 | ||
|
|
b1973ca418 | ||
|
|
52c7786a49 | ||
|
|
45f81225b0 | ||
|
|
475ca5aedc | ||
|
|
dce2f4f7a9 | ||
|
|
6d7a52f0fa | ||
|
|
b1c4f908fe | ||
|
|
666221199d | ||
|
|
3868bdb0a7 | ||
|
|
b46cfc36a2 | ||
|
|
1aa0ae045e | ||
|
|
12d877333a |
@@ -1 +0,0 @@
|
|||||||
Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
|
||||||
34
CHANGELOG.md
34
CHANGELOG.md
@@ -1,38 +1,6 @@
|
|||||||
|
|
||||||
# Changelog
|
|
||||||
|
|
||||||
1.0.3 Added provider with params
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
1.0.2 Updated doc and fixed syntax error
|
|
||||||
|
|
||||||
---
|
|
||||||
1.0.1 Fixed syntax error
|
|
||||||
|
|
||||||
---
|
|
||||||
1.0.0 Refactored code and added experimental api
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
0.1.2+1 Fixed initializtaion error
|
|
||||||
|
|
||||||
---
|
|
||||||
0.1.2 Fixed warnings in code
|
|
||||||
|
|
||||||
---
|
|
||||||
0.1.1+2 Updated libraries and fixed warnings
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
0.1.1+1 Updated pubspec and readme.md
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
0.1.1 Updated pubspec
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
0.1.0 Initial release
|
0.1.0 Initial release
|
||||||
|
|
||||||
---
|
---
|
||||||
202
README.md
202
README.md
@@ -1,205 +1,13 @@
|
|||||||
# Quick start
|
# dart_di
|
||||||
|
|
||||||
## Main components DI
|
Experimental development of DI in the Dart language
|
||||||
|
|
||||||
|
|
||||||
### Binding
|
|
||||||
|
|
||||||
Binding is a custom instance configurator that contains methods for configuring a dependency.
|
|
||||||
|
|
||||||
There are two main methods for initializing a custom instance `toInstance()` and `toProvide()` and auxiliary `withName()` and `singleton()`.
|
|
||||||
|
|
||||||
`toInstance()` - takes a initialized instance
|
|
||||||
|
|
||||||
`toProvide()` - takes a `provider` function (instance constructor)
|
|
||||||
|
|
||||||
`withName()` - takes a string to name the instance. By this name, it will be possible to extract instance from the DI container
|
|
||||||
|
|
||||||
`singleton()` - sets a flag in the Binding that tells the DI container that there is only one dependency.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
```dart
|
|
||||||
// initializing a text string instance through a method toInstance()
|
|
||||||
Binding<String>().toInstance("hello world");
|
|
||||||
|
|
||||||
// or
|
|
||||||
|
|
||||||
// initializing a text string instance
|
|
||||||
Binding<String>().toProvide(() => "hello world");
|
|
||||||
|
|
||||||
// initializing an instance of a string named
|
|
||||||
Binding<String>().withName("my_string").toInstance("hello world");
|
|
||||||
// or
|
|
||||||
Binding<String>().withName("my_string").toProvide(() => "hello world");
|
|
||||||
|
|
||||||
// instance initialization like singleton
|
|
||||||
Binding<String>().toInstance("hello world");
|
|
||||||
// or
|
|
||||||
Binding<String>().toProvide(() => "hello world").singleton();
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
### Module
|
|
||||||
|
|
||||||
Module is a container of user instances, and on the basis of which the user can create their modules. The user in his module must implement the `void builder (Scope currentScope)` method.
|
|
||||||
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
```dart
|
|
||||||
class AppModule extends Module {
|
|
||||||
@override
|
|
||||||
void builder(Scope currentScope) {
|
|
||||||
bind<ApiClient>().toInstance(ApiClientMock());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Scope
|
|
||||||
|
|
||||||
Scope is a container that stores the entire dependency tree (scope, modules, instances).
|
|
||||||
Through the scope, you can access the custom `instance`, for this you need to call the `resolve<T>()` method and specify the type of the object, and you can also pass additional parameters.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
|
|
||||||
```dart
|
|
||||||
// open main scope
|
|
||||||
final rootScope = Cherrypick.openRootScope();
|
|
||||||
|
|
||||||
// initializing scope with a custom module
|
|
||||||
rootScope.installModules([AppModule()]);
|
|
||||||
|
|
||||||
// takes custom instance
|
|
||||||
final str = rootScope.resolve<String>();
|
|
||||||
// or
|
|
||||||
final str = rootScope.tryResolve<String>();
|
|
||||||
|
|
||||||
// close main scope
|
|
||||||
Cherrypick.closeRootScope();
|
|
||||||
```
|
|
||||||
|
|
||||||
## Example app
|
|
||||||
|
|
||||||
|
|
||||||
```dart
|
|
||||||
import 'dart:async';
|
|
||||||
import 'package:meta/meta.dart';
|
|
||||||
import 'package:cherrypick/cherrypick.dart';
|
|
||||||
|
|
||||||
class AppModule extends Module {
|
|
||||||
@override
|
|
||||||
void builder(Scope currentScope) {
|
|
||||||
bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock());
|
|
||||||
bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FeatureModule extends Module {
|
|
||||||
bool isMock;
|
|
||||||
|
|
||||||
FeatureModule({required this.isMock});
|
|
||||||
|
|
||||||
@override
|
|
||||||
void builder(Scope currentScope) {
|
|
||||||
bind<DataRepository>()
|
|
||||||
.withName("networkRepo")
|
|
||||||
.toProvide(
|
|
||||||
() => NetworkDataRepository(
|
|
||||||
currentScope.resolve<ApiClient>(
|
|
||||||
named: isMock ? "apiClientMock" : "apiClientImpl",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.singleton();
|
|
||||||
bind<DataBloc>().toProvide(
|
|
||||||
() => DataBloc(
|
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() async {
|
|
||||||
final scope = openRootScope().installModules([
|
|
||||||
AppModule(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
final subScope = scope
|
|
||||||
.openSubScope("featureScope")
|
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
|
||||||
|
|
||||||
final dataBloc = subScope.resolve<DataBloc>();
|
|
||||||
dataBloc.data.listen((d) => print('Received data: $d'),
|
|
||||||
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
|
|
||||||
|
|
||||||
await dataBloc.fetchData();
|
|
||||||
}
|
|
||||||
|
|
||||||
class DataBloc {
|
|
||||||
final DataRepository _dataRepository;
|
|
||||||
|
|
||||||
Stream<String> get data => _dataController.stream;
|
|
||||||
StreamController<String> _dataController = new StreamController.broadcast();
|
|
||||||
|
|
||||||
DataBloc(this._dataRepository);
|
|
||||||
|
|
||||||
Future<void> fetchData() async {
|
|
||||||
try {
|
|
||||||
_dataController.sink.add(await _dataRepository.getData());
|
|
||||||
} catch (e) {
|
|
||||||
_dataController.sink.addError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispose() {
|
|
||||||
_dataController.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class DataRepository {
|
|
||||||
Future<String> getData();
|
|
||||||
}
|
|
||||||
|
|
||||||
class NetworkDataRepository implements DataRepository {
|
|
||||||
final ApiClient _apiClient;
|
|
||||||
final _token = 'token';
|
|
||||||
|
|
||||||
NetworkDataRepository(this._apiClient);
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<String> getData() async => await _apiClient.sendRequest(
|
|
||||||
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class ApiClient {
|
|
||||||
Future sendRequest({@required String url, String token, Map requestBody});
|
|
||||||
}
|
|
||||||
|
|
||||||
class ApiClientMock implements ApiClient {
|
|
||||||
@override
|
|
||||||
Future sendRequest(
|
|
||||||
{@required String? url, String? token, Map? requestBody}) async {
|
|
||||||
return 'Local Data';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ApiClientImpl implements ApiClient {
|
|
||||||
@override
|
|
||||||
Future sendRequest(
|
|
||||||
{@required String? url, String? token, Map? requestBody}) async {
|
|
||||||
return 'Network data';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
[GitHub Linl](https://github.com/pese-git/cherrypick)
|
|
||||||
|
|
||||||
|
- [New Api ENG](/doc/quick_start_en.md)
|
||||||
|
- [New Api RU](/doc/quick_start_ru.md)
|
||||||
|
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
- [x] Scope
|
- [x] Scope
|
||||||
- [x] Sub scope
|
- [x] Sub scope
|
||||||
- [x] Initialization instance with name
|
- [x] Initialization instance with name
|
||||||
@@ -1 +0,0 @@
|
|||||||
include: package:pedantic/analysis_options.yaml
|
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
Binding is a custom instance configurator that contains methods for configuring a dependency.
|
Binding is a custom instance configurator that contains methods for configuring a dependency.
|
||||||
|
|
||||||
There are two main methods for initializing a custom instance `toInstance ()` and `toProvide ()` and auxiliary `withName ()` and `singleton ()`.
|
There are two main methods for initializing a custom instance `toInstance ()` and `toProvide ()` and auxiliary `withName ()` and `singeltone ()`.
|
||||||
|
|
||||||
`toInstance()` - takes a initialized instance
|
`toInstance()` - takes a initialized instance
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ There are two main methods for initializing a custom instance `toInstance ()` an
|
|||||||
|
|
||||||
`withName()` - takes a string to name the instance. By this name, it will be possible to extract instance from the DI container
|
`withName()` - takes a string to name the instance. By this name, it will be possible to extract instance from the DI container
|
||||||
|
|
||||||
`singleton()` - sets a flag in the Binding that tells the DI container that there is only one dependency.
|
`singeltone()` - sets a flag in the Binding that tells the DI container that there is only one dependency.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ Example:
|
|||||||
// instance initialization like singleton
|
// instance initialization like singleton
|
||||||
Binding<String>().toInstance("hello world");
|
Binding<String>().toInstance("hello world");
|
||||||
// or
|
// or
|
||||||
Binding<String>().toProvide(() => "hello world").singleton();
|
Binding<String>().toProvide(() => "hello world").singeltone();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ Example:
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
// open main scope
|
// open main scope
|
||||||
final rootScope = Cherrypick.openRootScope();
|
final rootScope = DartDi.openRootScope();
|
||||||
|
|
||||||
// initializing scope with a custom module
|
// initializing scope with a custom module
|
||||||
rootScope.installModules([AppModule()]);
|
rootScope.installModules([AppModule()]);
|
||||||
@@ -76,7 +76,7 @@ Example:
|
|||||||
final str = rootScope.tryResolve<String>();
|
final str = rootScope.tryResolve<String>();
|
||||||
|
|
||||||
// close main scope
|
// close main scope
|
||||||
Cherrypick.closeRootScope();
|
DartDi.closeRootScope();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example app
|
## Example app
|
||||||
@@ -85,7 +85,8 @@ Example:
|
|||||||
```dart
|
```dart
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:cherrypick/cherrypick.dart';
|
import 'package:dart_di/experimental/scope.dart';
|
||||||
|
import 'package:dart_di/experimental/module.dart';
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
@@ -111,7 +112,7 @@ class FeatureModule extends Module {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singleton();
|
.singeltone();
|
||||||
bind<DataBloc>().toProvide(
|
bind<DataBloc>().toProvide(
|
||||||
() => DataBloc(
|
() => DataBloc(
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
|
|||||||
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
Binding - по сути это конфигуратор для пользовательского instance, который соддержит методы для конфигурирования зависимости.
|
Binding - по сути это конфигуратор для пользовательского instance, который соддержит методы для конфигурирования зависимости.
|
||||||
|
|
||||||
Есть два основных метода для инициализации пользовательского instance `toInstance()` и `toProvide()` и вспомогательных `withName()` и `singleton()`.
|
Есть два основных метода для инициализации пользовательского instance `toInstance()` и `toProvide()` и вспомогательных `withName()` и `singeltone()`.
|
||||||
|
|
||||||
`toInstance()` - принимает готовый экземпляр
|
`toInstance()` - принимает готовый экземпляр
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ Binding - по сути это конфигуратор для пользов
|
|||||||
|
|
||||||
`withName()` - принимает строку для именования экземпляра. По этому имени можно будет извлечь instance из DI контейнера
|
`withName()` - принимает строку для именования экземпляра. По этому имени можно будет извлечь instance из DI контейнера
|
||||||
|
|
||||||
`singleton()` - устанавливает флаг в Binding, который говорит DI контейнеру, что зависимость одна.
|
`singeltone()` - устанавливает флаг в Binding, который говорит DI контейнеру, что зависимость одна.
|
||||||
|
|
||||||
Пример:
|
Пример:
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ Binding - по сути это конфигуратор для пользов
|
|||||||
// инициализация экземпляра, как сингелтон
|
// инициализация экземпляра, как сингелтон
|
||||||
Binding<String>().toInstance("hello world");
|
Binding<String>().toInstance("hello world");
|
||||||
// или
|
// или
|
||||||
Binding<String>().toProvide(() => "hello world").singleton();
|
Binding<String>().toProvide(() => "hello world").singeltone();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ Scope - это контейнер, который хранит все дерев
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
// открыть главный scope
|
// открыть главный scope
|
||||||
final rootScope = CherryPick.openRootScope();
|
final rootScope = DartDi.openRootScope();
|
||||||
|
|
||||||
// инициализация scope пользовательским модулем
|
// инициализация scope пользовательским модулем
|
||||||
rootScope.installModules([AppModule()]);
|
rootScope.installModules([AppModule()]);
|
||||||
@@ -76,7 +76,7 @@ Scope - это контейнер, который хранит все дерев
|
|||||||
final str = rootScope.tryResolve<String>();
|
final str = rootScope.tryResolve<String>();
|
||||||
|
|
||||||
// закрыть главный scope
|
// закрыть главный scope
|
||||||
Cherrypick.closeRootScope();
|
DartDi.closeRootScope();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Пример приложения
|
## Пример приложения
|
||||||
@@ -85,7 +85,8 @@ Scope - это контейнер, который хранит все дерев
|
|||||||
```dart
|
```dart
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:cherrypick/cherrypick.dart';
|
import 'package:dart_di/experimental/scope.dart';
|
||||||
|
import 'package:dart_di/experimental/module.dart';
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
@@ -111,7 +112,7 @@ class FeatureModule extends Module {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singleton();
|
.singeltone();
|
||||||
bind<DataBloc>().toProvide(
|
bind<DataBloc>().toProvide(
|
||||||
() => DataBloc(
|
() => DataBloc(
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
@@ -191,4 +192,4 @@ class ApiClientImpl implements ApiClient {
|
|||||||
return 'Network data';
|
return 'Network data';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
# Example
|
|
||||||
|
|
||||||
pubspec.yaml:
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
name: example
|
|
||||||
version: 1.0.0
|
|
||||||
|
|
||||||
environment:
|
|
||||||
sdk: ">=2.12.0 <3.0.0"
|
|
||||||
|
|
||||||
|
|
||||||
dependencies:
|
|
||||||
cherrypick:
|
|
||||||
path: ../
|
|
||||||
|
|
||||||
dev_dependencies:
|
|
||||||
test: ^1.16.8
|
|
||||||
```
|
|
||||||
|
|
||||||
main.dart:
|
|
||||||
|
|
||||||
```dart
|
|
||||||
import 'dart:async';
|
|
||||||
import 'package:meta/meta.dart';
|
|
||||||
import 'package:cherrypick/scope.dart';
|
|
||||||
import 'package:cherrypick/module.dart';
|
|
||||||
|
|
||||||
class AppModule extends Module {
|
|
||||||
@override
|
|
||||||
void builder(Scope currentScope) {
|
|
||||||
bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock());
|
|
||||||
bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class FeatureModule extends Module {
|
|
||||||
bool isMock;
|
|
||||||
|
|
||||||
FeatureModule({required this.isMock});
|
|
||||||
|
|
||||||
@override
|
|
||||||
void builder(Scope currentScope) {
|
|
||||||
bind<DataRepository>()
|
|
||||||
.withName("networkRepo")
|
|
||||||
.toProvide(
|
|
||||||
() => NetworkDataRepository(
|
|
||||||
currentScope.resolve<ApiClient>(
|
|
||||||
named: isMock ? "apiClientMock" : "apiClientImpl",
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.singleton();
|
|
||||||
bind<DataBloc>().toProvide(
|
|
||||||
() => DataBloc(
|
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() async {
|
|
||||||
final scope = openRootScope().installModules([
|
|
||||||
AppModule(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
final subScope = scope
|
|
||||||
.openSubScope("featureScope")
|
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
|
||||||
|
|
||||||
final dataBloc = subScope.resolve<DataBloc>();
|
|
||||||
dataBloc.data.listen((d) => print('Received data: $d'),
|
|
||||||
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
|
|
||||||
|
|
||||||
await dataBloc.fetchData();
|
|
||||||
}
|
|
||||||
|
|
||||||
class DataBloc {
|
|
||||||
final DataRepository _dataRepository;
|
|
||||||
|
|
||||||
Stream<String> get data => _dataController.stream;
|
|
||||||
StreamController<String> _dataController = new StreamController.broadcast();
|
|
||||||
|
|
||||||
DataBloc(this._dataRepository);
|
|
||||||
|
|
||||||
Future<void> fetchData() async {
|
|
||||||
try {
|
|
||||||
_dataController.sink.add(await _dataRepository.getData());
|
|
||||||
} catch (e) {
|
|
||||||
_dataController.sink.addError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void dispose() {
|
|
||||||
_dataController.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class DataRepository {
|
|
||||||
Future<String> getData();
|
|
||||||
}
|
|
||||||
|
|
||||||
class NetworkDataRepository implements DataRepository {
|
|
||||||
final ApiClient _apiClient;
|
|
||||||
final _token = 'token';
|
|
||||||
|
|
||||||
NetworkDataRepository(this._apiClient);
|
|
||||||
|
|
||||||
@override
|
|
||||||
Future<String> getData() async => await _apiClient.sendRequest(
|
|
||||||
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class ApiClient {
|
|
||||||
Future sendRequest({@required String url, String token, Map requestBody});
|
|
||||||
}
|
|
||||||
|
|
||||||
class ApiClientMock implements ApiClient {
|
|
||||||
@override
|
|
||||||
Future sendRequest(
|
|
||||||
{@required String? url, String? token, Map? requestBody}) async {
|
|
||||||
return 'Local Data';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ApiClientImpl implements ApiClient {
|
|
||||||
@override
|
|
||||||
Future sendRequest(
|
|
||||||
{@required String? url, String? token, Map? requestBody}) async {
|
|
||||||
return 'Network data';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:cherrypick/cherrypick.dart';
|
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:dart_di/scope.dart';
|
||||||
|
import 'package:dart_di/module.dart';
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,20 +19,18 @@ class FeatureModule extends Module {
|
|||||||
@override
|
@override
|
||||||
void builder(Scope currentScope) {
|
void builder(Scope currentScope) {
|
||||||
bind<DataRepository>()
|
bind<DataRepository>()
|
||||||
.withName('networkRepo')
|
.withName("networkRepo")
|
||||||
.toProvide(
|
.toProvide(
|
||||||
() => NetworkDataRepository(
|
() => NetworkDataRepository(
|
||||||
currentScope.resolve<ApiClient>(
|
currentScope.resolve<ApiClient>(
|
||||||
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
named: isMock ? "apiClientMock" : "apiClientImpl",
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singleton();
|
.singeltone();
|
||||||
|
bind<DataBloc>().toProvide(
|
||||||
bind<DataBloc>().toProvideWithParams(
|
() => DataBloc(
|
||||||
(param) => DataBloc(
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
|
||||||
param,
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -44,10 +42,10 @@ void main() async {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
final subScope = scope
|
final subScope = scope
|
||||||
.openSubScope('featureScope')
|
.openSubScope("featureScope")
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
.installModules([FeatureModule(isMock: true)]);
|
||||||
|
|
||||||
final dataBloc = subScope.resolve<DataBloc>(params: 'PARAMETER');
|
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'));
|
||||||
|
|
||||||
@@ -58,15 +56,13 @@ class DataBloc {
|
|||||||
final DataRepository _dataRepository;
|
final DataRepository _dataRepository;
|
||||||
|
|
||||||
Stream<String> get data => _dataController.stream;
|
Stream<String> get data => _dataController.stream;
|
||||||
final StreamController<String> _dataController = StreamController.broadcast();
|
StreamController<String> _dataController = new StreamController.broadcast();
|
||||||
|
|
||||||
final String param;
|
DataBloc(this._dataRepository);
|
||||||
|
|
||||||
DataBloc(this._dataRepository, this.param);
|
|
||||||
|
|
||||||
Future<void> fetchData() async {
|
Future<void> fetchData() async {
|
||||||
try {
|
try {
|
||||||
_dataController.sink.add(await _dataRepository.getData(param));
|
_dataController.sink.add(await _dataRepository.getData());
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_dataController.sink.addError(e);
|
_dataController.sink.addError(e);
|
||||||
}
|
}
|
||||||
@@ -78,7 +74,7 @@ class DataBloc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class DataRepository {
|
abstract class DataRepository {
|
||||||
Future<String> getData(String param);
|
Future<String> getData();
|
||||||
}
|
}
|
||||||
|
|
||||||
class NetworkDataRepository implements DataRepository {
|
class NetworkDataRepository implements DataRepository {
|
||||||
@@ -88,42 +84,26 @@ class NetworkDataRepository implements DataRepository {
|
|||||||
NetworkDataRepository(this._apiClient);
|
NetworkDataRepository(this._apiClient);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> getData(String param) async => await _apiClient.sendRequest(
|
Future<String> getData() async => await _apiClient.sendRequest(
|
||||||
url: 'www.google.com',
|
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
|
||||||
token: _token,
|
|
||||||
requestBody: {'type': 'data'},
|
|
||||||
param: param);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class ApiClient {
|
abstract class ApiClient {
|
||||||
Future sendRequest({
|
Future sendRequest({@required String url, String token, Map requestBody});
|
||||||
@required String url,
|
|
||||||
String token,
|
|
||||||
Map requestBody,
|
|
||||||
String param,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiClientMock implements ApiClient {
|
class ApiClientMock implements ApiClient {
|
||||||
@override
|
@override
|
||||||
Future sendRequest({
|
Future sendRequest(
|
||||||
@required String? url,
|
{@required String? url, String? token, Map? requestBody}) async {
|
||||||
String? token,
|
return 'Local Data';
|
||||||
Map? requestBody,
|
|
||||||
String? param,
|
|
||||||
}) async {
|
|
||||||
return 'Local Data $param';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiClientImpl implements ApiClient {
|
class ApiClientImpl implements ApiClient {
|
||||||
@override
|
@override
|
||||||
Future sendRequest({
|
Future sendRequest(
|
||||||
@required String? url,
|
{@required String? url, String? token, Map? requestBody}) async {
|
||||||
String? token,
|
return 'Network data';
|
||||||
Map? requestBody,
|
|
||||||
String? param,
|
|
||||||
}) async {
|
|
||||||
return 'Network data $param';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
name: example
|
name: example
|
||||||
version: 1.0.0
|
version: 1.0.0
|
||||||
|
author: "Sergey Penkovsky <sergey.penkovsky@gmail.com>"
|
||||||
homepage: localhost
|
homepage: localhost
|
||||||
publish_to: none
|
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=2.12.0 <3.0.0"
|
sdk: ">=2.12.0 <3.0.0"
|
||||||
|
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
cherrypick:
|
dart_di:
|
||||||
path: ../
|
path: ../
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
///
|
/**
|
||||||
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
* Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
/// you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
/// You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
/// See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
/// limitations under the License.
|
* limitations under the License.
|
||||||
///
|
*/
|
||||||
|
|
||||||
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE, PROVIDER_WITH_PARAMS_INSTANCE }
|
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE }
|
||||||
|
|
||||||
typedef ProviderWithParams<T> = T Function(dynamic params);
|
|
||||||
|
|
||||||
/// RU: Класс Binding<T> настраивает параметры экземпляра.
|
/// RU: Класс Binding<T> настраивает параметры экземпляра.
|
||||||
/// ENG: The Binding<T> class configures the settings for the instance.
|
/// ENG: The Binding<T> class configures the settings for the instance.
|
||||||
@@ -22,10 +20,9 @@ class Binding<T> {
|
|||||||
late Mode _mode;
|
late Mode _mode;
|
||||||
late Type _key;
|
late Type _key;
|
||||||
late String _name;
|
late String _name;
|
||||||
T? _instance;
|
T? _instance = null;
|
||||||
T? Function()? _provider;
|
T? Function()? _provider = null;
|
||||||
ProviderWithParams<T>? _providerWithParams;
|
late bool _isSingeltone = false;
|
||||||
late bool _isSingleton = false;
|
|
||||||
late bool _isNamed = false;
|
late bool _isNamed = false;
|
||||||
|
|
||||||
Binding() {
|
Binding() {
|
||||||
@@ -55,7 +52,7 @@ class Binding<T> {
|
|||||||
/// ENG: The method checks the singleton instance or not.
|
/// ENG: The method checks the singleton instance or not.
|
||||||
///
|
///
|
||||||
/// return [bool]
|
/// return [bool]
|
||||||
bool get isSingleton => _isSingleton;
|
bool get isSingeltone => _isSingeltone;
|
||||||
|
|
||||||
/// RU: Метод проверяет именован экземпляр или нет.
|
/// RU: Метод проверяет именован экземпляр или нет.
|
||||||
/// ENG: The method checks whether the instance is named or not.
|
/// ENG: The method checks whether the instance is named or not.
|
||||||
@@ -80,7 +77,7 @@ class Binding<T> {
|
|||||||
Binding<T> toInstance(T value) {
|
Binding<T> toInstance(T value) {
|
||||||
_mode = Mode.INSTANCE;
|
_mode = Mode.INSTANCE;
|
||||||
_instance = value;
|
_instance = value;
|
||||||
_isSingleton = true;
|
_isSingeltone = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,22 +91,15 @@ class Binding<T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// RU: Инициализация экземляпяра через провайдер [value] c динамическим параметром.
|
|
||||||
/// ENG: Initialization instance via provider [value] with a dynamic param.
|
|
||||||
///
|
|
||||||
/// return [Binding]
|
|
||||||
Binding<T> toProvideWithParams(ProviderWithParams<T> value) {
|
|
||||||
_mode = Mode.PROVIDER_WITH_PARAMS_INSTANCE;
|
|
||||||
_providerWithParams = value;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// RU: Инициализация экземляпяра как сингелтон [value].
|
/// RU: Инициализация экземляпяра как сингелтон [value].
|
||||||
/// ENG: Initialization instance as a singelton [value].
|
/// ENG: Initialization instance as a singelton [value].
|
||||||
///
|
///
|
||||||
/// return [Binding]
|
/// return [Binding]
|
||||||
Binding<T> singleton() {
|
Binding<T> singeltone() {
|
||||||
_isSingleton = true;
|
if (_mode == Mode.PROVIDER_INSTANCE) {
|
||||||
|
_instance = _provider?.call();
|
||||||
|
}
|
||||||
|
_isSingeltone = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,20 +113,5 @@ class Binding<T> {
|
|||||||
/// ENG: Resolve instance.
|
/// ENG: Resolve instance.
|
||||||
///
|
///
|
||||||
/// return [T]
|
/// return [T]
|
||||||
T? get provider {
|
T? get provider => _provider?.call();
|
||||||
if (_isSingleton) {
|
|
||||||
_instance ??= _provider?.call();
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
return _provider?.call();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// RU: Поиск экземпляра с параметром.
|
|
||||||
///
|
|
||||||
/// ENG: Resolve instance with [params].
|
|
||||||
///
|
|
||||||
/// return [T]
|
|
||||||
T? providerWithParams(dynamic params) {
|
|
||||||
return _providerWithParams?.call(params);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
///
|
|
||||||
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
/// you may not use this file except in compliance with the License.
|
|
||||||
/// You may obtain a copy of the License at
|
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
/// See the License for the specific language governing permissions and
|
|
||||||
/// limitations under the License.
|
|
||||||
///
|
|
||||||
|
|
||||||
library cherrypick;
|
|
||||||
|
|
||||||
export 'package:cherrypick/src/binding.dart';
|
|
||||||
export 'package:cherrypick/src/helper.dart';
|
|
||||||
export 'package:cherrypick/src/module.dart';
|
|
||||||
export 'package:cherrypick/src/scope.dart';
|
|
||||||
19
lib/dart_di.dart
Normal file
19
lib/dart_di.dart
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
library dart_di;
|
||||||
|
|
||||||
|
export 'package:dart_di/scope.dart';
|
||||||
|
export 'package:dart_di/module.dart';
|
||||||
|
export 'package:dart_di/binding.dart';
|
||||||
|
export 'package:dart_di/di.dart';
|
||||||
38
lib/di.dart
Normal file
38
lib/di.dart
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
import 'package:dart_di/scope.dart';
|
||||||
|
|
||||||
|
Scope? _rootScope = null;
|
||||||
|
|
||||||
|
class DartDi {
|
||||||
|
/// RU: Метод открывает главный [Scope].
|
||||||
|
/// ENG: The method opens the main [Scope].
|
||||||
|
///
|
||||||
|
/// return
|
||||||
|
static Scope openRootScope() {
|
||||||
|
if (_rootScope == null) {
|
||||||
|
_rootScope = Scope(null);
|
||||||
|
}
|
||||||
|
return _rootScope!;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// RU: Метод закрывает главный [Scope].
|
||||||
|
/// ENG: The method close the main [Scope].
|
||||||
|
///
|
||||||
|
///
|
||||||
|
static void closeRootScope() {
|
||||||
|
if (_rootScope != null) {
|
||||||
|
_rootScope = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
18
lib/factory.dart
Normal file
18
lib/factory.dart
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import 'package:dart_di/scope.dart';
|
||||||
|
|
||||||
|
abstract class Factory<T> {
|
||||||
|
T createInstance(Scope scope);
|
||||||
|
}
|
||||||
@@ -1,19 +1,20 @@
|
|||||||
///
|
/**
|
||||||
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
* Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
/// you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
/// You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
/// See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
/// limitations under the License.
|
* limitations under the License.
|
||||||
///
|
*/
|
||||||
|
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:cherrypick/src/binding.dart';
|
import 'package:dart_di/binding.dart';
|
||||||
import 'package:cherrypick/src/scope.dart';
|
import 'package:dart_di/scope.dart';
|
||||||
|
|
||||||
/// RU: Класс Module является основой для пользовательских модулей.
|
/// RU: Класс Module является основой для пользовательских модулей.
|
||||||
/// Этот класс нужен для инициализации [Scope].
|
/// Этот класс нужен для инициализации [Scope].
|
||||||
@@ -1,19 +1,20 @@
|
|||||||
///
|
/**
|
||||||
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
* Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
/// you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
/// You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
/// See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
/// limitations under the License.
|
* limitations under the License.
|
||||||
///
|
*/
|
||||||
|
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:cherrypick/src/binding.dart';
|
import 'package:dart_di/binding.dart';
|
||||||
import 'package:cherrypick/src/module.dart';
|
import 'package:dart_di/module.dart';
|
||||||
|
|
||||||
Scope openRootScope() => Scope(null);
|
Scope openRootScope() => Scope(null);
|
||||||
|
|
||||||
@@ -86,8 +87,8 @@ class Scope {
|
|||||||
/// If you want to get [null] if the dependency cannot be found then use [tryResolve] instead
|
/// If you want to get [null] if the dependency cannot be found then use [tryResolve] instead
|
||||||
/// return - returns an object of type [T] or [StateError]
|
/// return - returns an object of type [T] or [StateError]
|
||||||
///
|
///
|
||||||
T resolve<T>({String? named, dynamic params}) {
|
T resolve<T>({String? named}) {
|
||||||
var resolved = tryResolve<T>(named: named, params: params);
|
var resolved = tryResolve<T>(named: named);
|
||||||
if (resolved != null) {
|
if (resolved != null) {
|
||||||
return resolved;
|
return resolved;
|
||||||
} else {
|
} else {
|
||||||
@@ -99,11 +100,11 @@ class Scope {
|
|||||||
/// RU: Возвращает найденную зависимость типа [T] или null, если она не может быть найдена.
|
/// RU: Возвращает найденную зависимость типа [T] или null, если она не может быть найдена.
|
||||||
/// ENG: Returns found dependency of type [T] or null if it cannot be found.
|
/// ENG: Returns found dependency of type [T] or null if it cannot be found.
|
||||||
///
|
///
|
||||||
T? tryResolve<T>({String? named, dynamic params}) {
|
T? tryResolve<T>({String? named}) {
|
||||||
// 1 Поиск зависимости по всем модулям текущего скоупа
|
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||||
if (_modulesList.isNotEmpty) {
|
if (_modulesList.isNotEmpty) {
|
||||||
for (var module in _modulesList) {
|
for (Module module in _modulesList) {
|
||||||
for (var binding in module.bindingSet) {
|
for (Binding binding in module.bindingSet) {
|
||||||
if (binding.key == T &&
|
if (binding.key == T &&
|
||||||
((!binding.isNamed && named == null) ||
|
((!binding.isNamed && named == null) ||
|
||||||
(binding.isNamed && named == binding.name))) {
|
(binding.isNamed && named == binding.name))) {
|
||||||
@@ -111,12 +112,9 @@ class Scope {
|
|||||||
case Mode.INSTANCE:
|
case Mode.INSTANCE:
|
||||||
return binding.instance;
|
return binding.instance;
|
||||||
case Mode.PROVIDER_INSTANCE:
|
case Mode.PROVIDER_INSTANCE:
|
||||||
return binding.provider;
|
return binding.isSingeltone
|
||||||
case Mode.PROVIDER_WITH_PARAMS_INSTANCE:
|
? binding.instance
|
||||||
if (params == null) {
|
: binding.provider;
|
||||||
throw StateError('Param is null. Maybe you forget pass it');
|
|
||||||
}
|
|
||||||
return binding.providerWithParams(params);
|
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
///
|
|
||||||
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
/// you may not use this file except in compliance with the License.
|
|
||||||
/// You may obtain a copy of the License at
|
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
/// See the License for the specific language governing permissions and
|
|
||||||
/// limitations under the License.
|
|
||||||
///
|
|
||||||
import 'package:cherrypick/src/scope.dart';
|
|
||||||
|
|
||||||
abstract class Factory<T> {
|
|
||||||
T createInstance(Scope scope);
|
|
||||||
}
|
|
||||||
@@ -1,105 +0,0 @@
|
|||||||
///
|
|
||||||
/// Copyright 2021 Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
|
||||||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
/// you may not use this file except in compliance with the License.
|
|
||||||
/// You may obtain a copy of the License at
|
|
||||||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
/// Unless required by applicable law or agreed to in writing, software
|
|
||||||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
/// See the License for the specific language governing permissions and
|
|
||||||
/// limitations under the License.
|
|
||||||
///
|
|
||||||
import 'package:cherrypick/src/scope.dart';
|
|
||||||
import 'package:meta/meta.dart';
|
|
||||||
|
|
||||||
Scope? _rootScope;
|
|
||||||
|
|
||||||
class CherryPick {
|
|
||||||
/// RU: Метод открывает главный [Scope].
|
|
||||||
/// ENG: The method opens the main [Scope].
|
|
||||||
///
|
|
||||||
/// return
|
|
||||||
static Scope openRootScope() {
|
|
||||||
_rootScope ??= Scope(null);
|
|
||||||
return _rootScope!;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// RU: Метод закрывает главный [Scope].
|
|
||||||
/// ENG: The method close the main [Scope].
|
|
||||||
///
|
|
||||||
///
|
|
||||||
static void closeRootScope() {
|
|
||||||
if (_rootScope != null) {
|
|
||||||
_rootScope = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// RU: Метод открывает дочерний [Scope].
|
|
||||||
/// ENG: The method open the child [Scope].
|
|
||||||
///
|
|
||||||
/// Дочерний [Scope] открывается с [scopeName]
|
|
||||||
/// Child [Scope] open with [scopeName]
|
|
||||||
///
|
|
||||||
/// Example:
|
|
||||||
/// ```
|
|
||||||
/// final String scopeName = 'firstScope.secondScope';
|
|
||||||
/// final subScope = CherryPick.openScope(scopeName);
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
///
|
|
||||||
@experimental
|
|
||||||
static Scope openScope({String scopeName = '', String separator = '.'}) {
|
|
||||||
if (scopeName.isEmpty) {
|
|
||||||
return openRootScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
final nameParts = scopeName.split(separator);
|
|
||||||
if (nameParts.isEmpty) {
|
|
||||||
throw Exception('Can not open sub scope because scopeName can not split');
|
|
||||||
}
|
|
||||||
|
|
||||||
return nameParts.fold(
|
|
||||||
openRootScope(),
|
|
||||||
(Scope previousValue, String element) =>
|
|
||||||
previousValue.openSubScope(element));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// RU: Метод открывает дочерний [Scope].
|
|
||||||
/// ENG: The method open the child [Scope].
|
|
||||||
///
|
|
||||||
/// Дочерний [Scope] открывается с [scopeName]
|
|
||||||
/// Child [Scope] open with [scopeName]
|
|
||||||
///
|
|
||||||
/// Example:
|
|
||||||
/// ```
|
|
||||||
/// final String scopeName = 'firstScope.secondScope';
|
|
||||||
/// final subScope = CherryPick.closeScope(scopeName);
|
|
||||||
/// ```
|
|
||||||
///
|
|
||||||
///
|
|
||||||
@experimental
|
|
||||||
static void closeScope({String scopeName = '', String separator = '.'}) {
|
|
||||||
if (scopeName.isEmpty) {
|
|
||||||
closeRootScope();
|
|
||||||
}
|
|
||||||
|
|
||||||
final nameParts = scopeName.split(separator);
|
|
||||||
if (nameParts.isEmpty) {
|
|
||||||
throw Exception(
|
|
||||||
'Can not close sub scope because scopeName can not split');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nameParts.length > 1) {
|
|
||||||
final lastPart = nameParts.removeLast();
|
|
||||||
|
|
||||||
final scope = nameParts.fold(
|
|
||||||
openRootScope(),
|
|
||||||
(Scope previousValue, String element) =>
|
|
||||||
previousValue.openSubScope(element));
|
|
||||||
scope.closeSubScope(lastPart);
|
|
||||||
} else {
|
|
||||||
openRootScope().closeSubScope(nameParts[0]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
18
pubspec.yaml
18
pubspec.yaml
@@ -1,10 +1,8 @@
|
|||||||
name: cherrypick
|
name: dart_di
|
||||||
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
description: Experimental Dependency Injection library.
|
||||||
version: 1.0.3
|
version: 0.1.0
|
||||||
homepage: https://pese-git.github.io/cherrypick-site/
|
author: Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
homepage: locahost
|
||||||
repository: https://github.com/pese-git/cherrypick
|
|
||||||
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=2.12.0 <3.0.0"
|
sdk: ">=2.12.0 <3.0.0"
|
||||||
@@ -13,8 +11,6 @@ dependencies:
|
|||||||
meta: ^1.3.0
|
meta: ^1.3.0
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
pedantic: ^1.11.0
|
test: ^1.16.8
|
||||||
|
|
||||||
test: ^1.17.2
|
mockito: ^5.0.3
|
||||||
|
|
||||||
mockito: ^5.0.6
|
|
||||||
|
|||||||
@@ -1,294 +1,294 @@
|
|||||||
import 'package:cherrypick/src/binding.dart';
|
import 'package:dart_di/binding.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('Check instance.', () {
|
group("Check instance.", () {
|
||||||
group('Without name.', () {
|
group("Without name.", () {
|
||||||
test('Binding resolves null', () {
|
test("Binding resolves null", () {
|
||||||
final binding = Binding<int>();
|
final binding = Binding<int>();
|
||||||
expect(binding.instance, null);
|
expect(binding.instance, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check mode', () {
|
test("Binding check mode", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toInstance(expectedValue);
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.mode, Mode.INSTANCE);
|
expect(binding.mode, Mode.INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singleton', () {
|
test("Binding check singeltone", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toInstance(expectedValue);
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingleton, true);
|
expect(binding.isSingeltone, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toInstance(expectedValue);
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.instance, expectedValue);
|
expect(binding.instance, expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding resolves value', () {
|
test("Binding resolves value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toInstance(expectedValue);
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
expect(binding.instance, expectedValue);
|
expect(binding.instance, expectedValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('With name.', () {
|
group("With name.", () {
|
||||||
test('Binding resolves null', () {
|
test("Binding resolves null", () {
|
||||||
final binding = Binding<int>().withName('expectedValue');
|
final binding = Binding<int>().withName("expectedValue");
|
||||||
expect(binding.instance, null);
|
expect(binding.instance, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check mode', () {
|
test("Binding check mode", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.mode, Mode.INSTANCE);
|
expect(binding.mode, Mode.INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check key', () {
|
test("Binding check key", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.key, int);
|
expect(binding.key, int);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singleton', () {
|
test("Binding check singeltone", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingleton, true);
|
expect(binding.isSingeltone, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.instance, expectedValue);
|
expect(binding.instance, expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.name, 'expectedValue');
|
expect(binding.name, "expectedValue");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding resolves value', () {
|
test("Binding resolves value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||||
expect(binding.instance, expectedValue);
|
expect(binding.instance, expectedValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('Check provide.', () {
|
group("Check provide.", () {
|
||||||
group('Without name.', () {
|
group("Without name.", () {
|
||||||
test('Binding resolves null', () {
|
test("Binding resolves null", () {
|
||||||
final binding = Binding<int>();
|
final binding = Binding<int>();
|
||||||
expect(binding.provider, null);
|
expect(binding.provider, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check mode', () {
|
test("Binding check mode", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singleton', () {
|
test("Binding check singeltone", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingleton, false);
|
expect(binding.isSingeltone, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding resolves value', () {
|
test("Binding resolves value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('With name.', () {
|
group("With name.", () {
|
||||||
test('Binding resolves null', () {
|
test("Binding resolves null", () {
|
||||||
final binding = Binding<int>().withName('expectedValue');
|
final binding = Binding<int>().withName("expectedValue");
|
||||||
expect(binding.provider, null);
|
expect(binding.provider, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check mode', () {
|
test("Binding check mode", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue);
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check key', () {
|
test("Binding check key", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue);
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.key, int);
|
expect(binding.key, int);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singleton', () {
|
test("Binding check singeltone", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue);
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingleton, false);
|
expect(binding.isSingeltone, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue);
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue);
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.name, 'expectedValue');
|
expect(binding.name, "expectedValue");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding resolves value', () {
|
test("Binding resolves value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue);
|
.toProvide(() => expectedValue);
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('Check singleton provide.', () {
|
group("Check singeltone provide.", () {
|
||||||
group('Without name.', () {
|
group("Without name.", () {
|
||||||
test('Binding resolves null', () {
|
test("Binding resolves null", () {
|
||||||
final binding = Binding<int>().singleton();
|
final binding = Binding<int>().singeltone();
|
||||||
expect(binding.provider, null);
|
expect(binding.provider, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check mode', () {
|
test("Binding check mode", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().toProvide(() => expectedValue).singleton();
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
|
|
||||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singleton', () {
|
test("Binding check singeltone", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().toProvide(() => expectedValue).singleton();
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
|
|
||||||
expect(binding.isSingleton, true);
|
expect(binding.isSingeltone, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().toProvide(() => expectedValue).singleton();
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
|
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding resolves value', () {
|
test("Binding resolves value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().toProvide(() => expectedValue).singleton();
|
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('With name.', () {
|
group("With name.", () {
|
||||||
test('Binding resolves null', () {
|
test("Binding resolves null", () {
|
||||||
final binding = Binding<int>().withName('expectedValue').singleton();
|
final binding = Binding<int>().withName("expectedValue").singeltone();
|
||||||
expect(binding.provider, null);
|
expect(binding.provider, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check mode', () {
|
test("Binding check mode", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singleton();
|
.singeltone();
|
||||||
|
|
||||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check key', () {
|
test("Binding check key", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singleton();
|
.singeltone();
|
||||||
|
|
||||||
expect(binding.key, int);
|
expect(binding.key, int);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singleton', () {
|
test("Binding check singeltone", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singleton();
|
.singeltone();
|
||||||
|
|
||||||
expect(binding.isSingleton, true);
|
expect(binding.isSingeltone, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singleton();
|
.singeltone();
|
||||||
|
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test("Binding check value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singleton();
|
.singeltone();
|
||||||
|
|
||||||
expect(binding.name, 'expectedValue');
|
expect(binding.name, "expectedValue");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding resolves value', () {
|
test("Binding resolves value", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName("expectedValue")
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singleton();
|
.singeltone();
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
import 'package:cherrypick/src/module.dart';
|
import 'package:dart_di/module.dart';
|
||||||
import 'package:cherrypick/src/scope.dart';
|
import 'package:dart_di/scope.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('Without parent scope.', () {
|
group("Without parent scope.", () {
|
||||||
test('Parent scope is null.', () {
|
test('Parent scope is null.', () {
|
||||||
final scope = Scope(null);
|
final scope = new Scope(null);
|
||||||
expect(scope.parentScope, null);
|
expect(scope.parentScope, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Open sub scope.', () {
|
test('Open sub scope.', () {
|
||||||
final scope = Scope(null);
|
final scope = new Scope(null);
|
||||||
final subScope = scope.openSubScope('subScope');
|
final subScope = scope.openSubScope("subScope");
|
||||||
expect(scope.openSubScope('subScope'), subScope);
|
expect(scope.openSubScope("subScope"), subScope);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Container throws state error if the value can't be resolved", () {
|
test("Container throws state error if the value can't be resolved", () {
|
||||||
final scope = Scope(null);
|
final scope = new Scope(null);
|
||||||
expect(() => scope.resolve<String>(), throwsA(isA<StateError>()));
|
expect(() => scope.resolve<String>(), throwsA(isA<StateError>()));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Container resolves value after adding a dependency', () {
|
test('Container resolves value after adding a dependency', () {
|
||||||
final expectedValue = 'test string';
|
final expectedValue = "test string";
|
||||||
final scope = Scope(null)
|
final scope = new Scope(null)
|
||||||
.installModules([TestModule<String>(value: expectedValue)]);
|
.installModules([TestModule<String>(value: expectedValue)]);
|
||||||
expect(scope.resolve<String>(), expectedValue);
|
expect(scope.resolve<String>(), expectedValue);
|
||||||
});
|
});
|
||||||
@@ -42,7 +42,7 @@ void main() {
|
|||||||
throwsA(isA<StateError>()));
|
throwsA(isA<StateError>()));
|
||||||
});
|
});
|
||||||
*/
|
*/
|
||||||
test('Container resolve() returns a value from parent container.', () {
|
test("Container resolve() returns a value from parent container.", () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final parentScope = Scope(null);
|
final parentScope = Scope(null);
|
||||||
final scope = Scope(parentScope);
|
final scope = Scope(parentScope);
|
||||||
@@ -52,10 +52,10 @@ void main() {
|
|||||||
expect(scope.resolve<int>(), expectedValue);
|
expect(scope.resolve<int>(), expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Container resolve() returns a several value from parent container.',
|
test("Container resolve() returns a several value from parent container.",
|
||||||
() {
|
() {
|
||||||
final expectedIntValue = 5;
|
final expectedIntValue = 5;
|
||||||
final expectedStringValue = 'Hello world';
|
final expectedStringValue = "Hello world";
|
||||||
final parentScope = Scope(null).installModules([
|
final parentScope = Scope(null).installModules([
|
||||||
TestModule<int>(value: expectedIntValue),
|
TestModule<int>(value: expectedIntValue),
|
||||||
TestModule<String>(value: expectedStringValue)
|
TestModule<String>(value: expectedStringValue)
|
||||||
Reference in New Issue
Block a user