mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 13:47:24 +00:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21c3e83a6a | ||
|
|
a983281727 | ||
|
|
e0f5874621 | ||
|
|
8c3a0df452 | ||
|
|
085ccb55f5 | ||
|
|
c91392c978 | ||
|
|
4205993ea7 | ||
|
|
58245fb665 | ||
|
|
7a53844c7d | ||
|
|
73d199b012 | ||
|
|
75bc73d62f | ||
|
|
bdc8951438 | ||
|
|
3c95bf4947 |
14
CHANGELOG.md
14
CHANGELOG.md
@@ -1,6 +1,20 @@
|
|||||||
|
|
||||||
# Changelog
|
# 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+1 Fixed initializtaion error
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
200
README.md
200
README.md
@@ -1,9 +1,201 @@
|
|||||||
# cherrypick
|
# Quick start
|
||||||
|
|
||||||
Experimental development of DI in the Dart language
|
## Main components DI
|
||||||
|
|
||||||
|
|
||||||
|
### 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
|
||||||
|
|||||||
@@ -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 `singeltone ()`.
|
There are two main methods for initializing a custom instance `toInstance ()` and `toProvide ()` and auxiliary `withName ()` and `singleton ()`.
|
||||||
|
|
||||||
`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
|
||||||
|
|
||||||
`singeltone()` - sets a flag in the Binding that tells the DI container that there is only one dependency.
|
`singleton()` - 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").singeltone();
|
Binding<String>().toProvide(() => "hello world").singleton();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ class FeatureModule extends Module {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singeltone();
|
.singleton();
|
||||||
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()` и `singeltone()`.
|
Есть два основных метода для инициализации пользовательского instance `toInstance()` и `toProvide()` и вспомогательных `withName()` и `singleton()`.
|
||||||
|
|
||||||
`toInstance()` - принимает готовый экземпляр
|
`toInstance()` - принимает готовый экземпляр
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ Binding - по сути это конфигуратор для пользов
|
|||||||
|
|
||||||
`withName()` - принимает строку для именования экземпляра. По этому имени можно будет извлечь instance из DI контейнера
|
`withName()` - принимает строку для именования экземпляра. По этому имени можно будет извлечь instance из DI контейнера
|
||||||
|
|
||||||
`singeltone()` - устанавливает флаг в Binding, который говорит DI контейнеру, что зависимость одна.
|
`singleton()` - устанавливает флаг в Binding, который говорит DI контейнеру, что зависимость одна.
|
||||||
|
|
||||||
Пример:
|
Пример:
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ Binding - по сути это конфигуратор для пользов
|
|||||||
// инициализация экземпляра, как сингелтон
|
// инициализация экземпляра, как сингелтон
|
||||||
Binding<String>().toInstance("hello world");
|
Binding<String>().toInstance("hello world");
|
||||||
// или
|
// или
|
||||||
Binding<String>().toProvide(() => "hello world").singeltone();
|
Binding<String>().toProvide(() => "hello world").singleton();
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -111,7 +111,7 @@ class FeatureModule extends Module {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singeltone();
|
.singleton();
|
||||||
bind<DataBloc>().toProvide(
|
bind<DataBloc>().toProvide(
|
||||||
() => DataBloc(
|
() => DataBloc(
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class FeatureModule extends Module {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singeltone();
|
.singleton();
|
||||||
bind<DataBloc>().toProvide(
|
bind<DataBloc>().toProvide(
|
||||||
() => DataBloc(
|
() => DataBloc(
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
|
|||||||
@@ -27,10 +27,12 @@ class FeatureModule extends Module {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singeltone();
|
.singleton();
|
||||||
bind<DataBloc>().toProvide(
|
|
||||||
() => DataBloc(
|
bind<DataBloc>().toProvideWithParams(
|
||||||
|
(param) => DataBloc(
|
||||||
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
||||||
|
param,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -45,7 +47,7 @@ void main() async {
|
|||||||
.openSubScope('featureScope')
|
.openSubScope('featureScope')
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
.installModules([FeatureModule(isMock: true)]);
|
||||||
|
|
||||||
final dataBloc = subScope.resolve<DataBloc>();
|
final dataBloc = subScope.resolve<DataBloc>(params: 'PARAMETER');
|
||||||
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,11 +60,13 @@ class DataBloc {
|
|||||||
Stream<String> get data => _dataController.stream;
|
Stream<String> get data => _dataController.stream;
|
||||||
final StreamController<String> _dataController = StreamController.broadcast();
|
final StreamController<String> _dataController = StreamController.broadcast();
|
||||||
|
|
||||||
DataBloc(this._dataRepository);
|
final String param;
|
||||||
|
|
||||||
|
DataBloc(this._dataRepository, this.param);
|
||||||
|
|
||||||
Future<void> fetchData() async {
|
Future<void> fetchData() async {
|
||||||
try {
|
try {
|
||||||
_dataController.sink.add(await _dataRepository.getData());
|
_dataController.sink.add(await _dataRepository.getData(param));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_dataController.sink.addError(e);
|
_dataController.sink.addError(e);
|
||||||
}
|
}
|
||||||
@@ -74,7 +78,7 @@ class DataBloc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class DataRepository {
|
abstract class DataRepository {
|
||||||
Future<String> getData();
|
Future<String> getData(String param);
|
||||||
}
|
}
|
||||||
|
|
||||||
class NetworkDataRepository implements DataRepository {
|
class NetworkDataRepository implements DataRepository {
|
||||||
@@ -84,26 +88,42 @@ class NetworkDataRepository implements DataRepository {
|
|||||||
NetworkDataRepository(this._apiClient);
|
NetworkDataRepository(this._apiClient);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> getData() async => await _apiClient.sendRequest(
|
Future<String> getData(String param) async => await _apiClient.sendRequest(
|
||||||
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
|
url: 'www.google.com',
|
||||||
|
token: _token,
|
||||||
|
requestBody: {'type': 'data'},
|
||||||
|
param: param);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class ApiClient {
|
abstract class ApiClient {
|
||||||
Future sendRequest({@required String url, String token, Map requestBody});
|
Future sendRequest({
|
||||||
|
@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, String? token, Map? requestBody}) async {
|
@required String? url,
|
||||||
return 'Local Data';
|
String? token,
|
||||||
|
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, String? token, Map? requestBody}) async {
|
@required String? url,
|
||||||
return 'Network data';
|
String? token,
|
||||||
|
Map? requestBody,
|
||||||
|
String? param,
|
||||||
|
}) async {
|
||||||
|
return 'Network data $param';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,9 @@
|
|||||||
/// limitations under the License.
|
/// limitations under the License.
|
||||||
///
|
///
|
||||||
|
|
||||||
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE }
|
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE, PROVIDER_WITH_PARAMS_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,7 +24,8 @@ class Binding<T> {
|
|||||||
late String _name;
|
late String _name;
|
||||||
T? _instance;
|
T? _instance;
|
||||||
T? Function()? _provider;
|
T? Function()? _provider;
|
||||||
late bool _isSingeltone = false;
|
ProviderWithParams<T>? _providerWithParams;
|
||||||
|
late bool _isSingleton = false;
|
||||||
late bool _isNamed = false;
|
late bool _isNamed = false;
|
||||||
|
|
||||||
Binding() {
|
Binding() {
|
||||||
@@ -52,7 +55,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 isSingeltone => _isSingeltone;
|
bool get isSingleton => _isSingleton;
|
||||||
|
|
||||||
/// RU: Метод проверяет именован экземпляр или нет.
|
/// RU: Метод проверяет именован экземпляр или нет.
|
||||||
/// ENG: The method checks whether the instance is named or not.
|
/// ENG: The method checks whether the instance is named or not.
|
||||||
@@ -77,7 +80,7 @@ class Binding<T> {
|
|||||||
Binding<T> toInstance(T value) {
|
Binding<T> toInstance(T value) {
|
||||||
_mode = Mode.INSTANCE;
|
_mode = Mode.INSTANCE;
|
||||||
_instance = value;
|
_instance = value;
|
||||||
_isSingeltone = true;
|
_isSingleton = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,12 +94,22 @@ 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> singeltone() {
|
Binding<T> singleton() {
|
||||||
_isSingeltone = true;
|
_isSingleton = true;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -111,10 +124,19 @@ class Binding<T> {
|
|||||||
///
|
///
|
||||||
/// return [T]
|
/// return [T]
|
||||||
T? get provider {
|
T? get provider {
|
||||||
if (_isSingeltone) {
|
if (_isSingleton) {
|
||||||
_instance ??= _provider?.call();
|
_instance ??= _provider?.call();
|
||||||
return _instance;
|
return _instance;
|
||||||
}
|
}
|
||||||
return _provider?.call();
|
return _provider?.call();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// RU: Поиск экземпляра с параметром.
|
||||||
|
///
|
||||||
|
/// ENG: Resolve instance with [params].
|
||||||
|
///
|
||||||
|
/// return [T]
|
||||||
|
T? providerWithParams(dynamic params) {
|
||||||
|
return _providerWithParams?.call(params);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,8 +86,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}) {
|
T resolve<T>({String? named, dynamic params}) {
|
||||||
var resolved = tryResolve<T>(named: named);
|
var resolved = tryResolve<T>(named: named, params: params);
|
||||||
if (resolved != null) {
|
if (resolved != null) {
|
||||||
return resolved;
|
return resolved;
|
||||||
} else {
|
} else {
|
||||||
@@ -99,7 +99,7 @@ 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}) {
|
T? tryResolve<T>({String? named, dynamic params}) {
|
||||||
// 1 Поиск зависимости по всем модулям текущего скоупа
|
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||||
if (_modulesList.isNotEmpty) {
|
if (_modulesList.isNotEmpty) {
|
||||||
for (var module in _modulesList) {
|
for (var module in _modulesList) {
|
||||||
@@ -112,6 +112,11 @@ class Scope {
|
|||||||
return binding.instance;
|
return binding.instance;
|
||||||
case Mode.PROVIDER_INSTANCE:
|
case Mode.PROVIDER_INSTANCE:
|
||||||
return binding.provider;
|
return binding.provider;
|
||||||
|
case Mode.PROVIDER_WITH_PARAMS_INSTANCE:
|
||||||
|
if (params == null) {
|
||||||
|
throw StateError('Param is null. Maybe you forget pass it');
|
||||||
|
}
|
||||||
|
return binding.providerWithParams(params);
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
name: cherrypick
|
name: cherrypick
|
||||||
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
||||||
version: 1.0.0
|
version: 1.0.3
|
||||||
homepage: https://github.com/pese-git/cherrypick
|
homepage: https://pese-git.github.io/cherrypick-site/
|
||||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||||
repository: https://github.com/pese-git/cherrypick
|
repository: https://github.com/pese-git/cherrypick
|
||||||
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
||||||
|
|||||||
@@ -16,11 +16,11 @@ void main() {
|
|||||||
expect(binding.mode, Mode.INSTANCE);
|
expect(binding.mode, Mode.INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singeltone', () {
|
test('Binding check singleton', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toInstance(expectedValue);
|
final binding = Binding<int>().toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingeltone, true);
|
expect(binding.isSingleton, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test('Binding check value', () {
|
||||||
@@ -59,12 +59,12 @@ void main() {
|
|||||||
expect(binding.key, int);
|
expect(binding.key, int);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singeltone', () {
|
test('Binding check singleton', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingeltone, true);
|
expect(binding.isSingleton, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test('Binding check value', () {
|
||||||
@@ -106,11 +106,11 @@ void main() {
|
|||||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singeltone', () {
|
test('Binding check singleton', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingeltone, false);
|
expect(binding.isSingleton, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test('Binding check value', () {
|
||||||
@@ -151,13 +151,13 @@ void main() {
|
|||||||
expect(binding.key, int);
|
expect(binding.key, int);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singeltone', () {
|
test('Binding check singleton', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName('expectedValue')
|
||||||
.toProvide(() => expectedValue);
|
.toProvide(() => expectedValue);
|
||||||
|
|
||||||
expect(binding.isSingeltone, false);
|
expect(binding.isSingleton, false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test('Binding check value', () {
|
||||||
@@ -188,33 +188,33 @@ void main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('Check singeltone provide.', () {
|
group('Check singleton provide.', () {
|
||||||
group('Without name.', () {
|
group('Without name.', () {
|
||||||
test('Binding resolves null', () {
|
test('Binding resolves null', () {
|
||||||
final binding = Binding<int>().singeltone();
|
final binding = Binding<int>().singleton();
|
||||||
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).singeltone();
|
Binding<int>().toProvide(() => expectedValue).singleton();
|
||||||
|
|
||||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singeltone', () {
|
test('Binding check singleton', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().toProvide(() => expectedValue).singeltone();
|
Binding<int>().toProvide(() => expectedValue).singleton();
|
||||||
|
|
||||||
expect(binding.isSingeltone, true);
|
expect(binding.isSingleton, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test('Binding check value', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().toProvide(() => expectedValue).singeltone();
|
Binding<int>().toProvide(() => expectedValue).singleton();
|
||||||
|
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
@@ -222,14 +222,14 @@ void main() {
|
|||||||
test('Binding resolves value', () {
|
test('Binding resolves value', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding =
|
final binding =
|
||||||
Binding<int>().toProvide(() => expectedValue).singeltone();
|
Binding<int>().toProvide(() => expectedValue).singleton();
|
||||||
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').singeltone();
|
final binding = Binding<int>().withName('expectedValue').singleton();
|
||||||
expect(binding.provider, null);
|
expect(binding.provider, null);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -238,7 +238,7 @@ void main() {
|
|||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName('expectedValue')
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singeltone();
|
.singleton();
|
||||||
|
|
||||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||||
});
|
});
|
||||||
@@ -248,19 +248,19 @@ void main() {
|
|||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName('expectedValue')
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singeltone();
|
.singleton();
|
||||||
|
|
||||||
expect(binding.key, int);
|
expect(binding.key, int);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check singeltone', () {
|
test('Binding check singleton', () {
|
||||||
final expectedValue = 5;
|
final expectedValue = 5;
|
||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName('expectedValue')
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singeltone();
|
.singleton();
|
||||||
|
|
||||||
expect(binding.isSingeltone, true);
|
expect(binding.isSingleton, true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Binding check value', () {
|
test('Binding check value', () {
|
||||||
@@ -268,7 +268,7 @@ void main() {
|
|||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName('expectedValue')
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singeltone();
|
.singleton();
|
||||||
|
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
@@ -278,7 +278,7 @@ void main() {
|
|||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName('expectedValue')
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singeltone();
|
.singleton();
|
||||||
|
|
||||||
expect(binding.name, 'expectedValue');
|
expect(binding.name, 'expectedValue');
|
||||||
});
|
});
|
||||||
@@ -288,7 +288,7 @@ void main() {
|
|||||||
final binding = Binding<int>()
|
final binding = Binding<int>()
|
||||||
.withName('expectedValue')
|
.withName('expectedValue')
|
||||||
.toProvide(() => expectedValue)
|
.toProvide(() => expectedValue)
|
||||||
.singeltone();
|
.singleton();
|
||||||
expect(binding.provider, expectedValue);
|
expect(binding.provider, expectedValue);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user