mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +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 |
17
CHANGELOG.md
17
CHANGELOG.md
@@ -1,21 +1,6 @@
|
||||
|
||||
# Changelog
|
||||
|
||||
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
|
||||
|
||||
---
|
||||
---
|
||||
@@ -1,4 +1,4 @@
|
||||
# cherrypick
|
||||
# dart_di
|
||||
|
||||
Experimental development of DI in the Dart language
|
||||
|
||||
@@ -10,4 +10,4 @@ Experimental development of DI in the Dart language
|
||||
|
||||
- [x] Scope
|
||||
- [x] Sub scope
|
||||
- [x] Initialization instance with name
|
||||
- [x] Initialization instance with name
|
||||
@@ -1 +0,0 @@
|
||||
include: package:pedantic/analysis_options.yaml
|
||||
@@ -65,7 +65,7 @@ Example:
|
||||
|
||||
```dart
|
||||
// open main scope
|
||||
final rootScope = Cherrypick.openRootScope();
|
||||
final rootScope = DartDi.openRootScope();
|
||||
|
||||
// initializing scope with a custom module
|
||||
rootScope.installModules([AppModule()]);
|
||||
@@ -76,7 +76,7 @@ Example:
|
||||
final str = rootScope.tryResolve<String>();
|
||||
|
||||
// close main scope
|
||||
Cherrypick.closeRootScope();
|
||||
DartDi.closeRootScope();
|
||||
```
|
||||
|
||||
## Example app
|
||||
@@ -85,8 +85,8 @@ Example:
|
||||
```dart
|
||||
import 'dart:async';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:cherrypick/scope.dart';
|
||||
import 'package:cherrypick/module.dart';
|
||||
import 'package:dart_di/experimental/scope.dart';
|
||||
import 'package:dart_di/experimental/module.dart';
|
||||
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
|
||||
@@ -65,7 +65,7 @@ Scope - это контейнер, который хранит все дерев
|
||||
|
||||
```dart
|
||||
// открыть главный scope
|
||||
final rootScope = CherryPick.openRootScope();
|
||||
final rootScope = DartDi.openRootScope();
|
||||
|
||||
// инициализация scope пользовательским модулем
|
||||
rootScope.installModules([AppModule()]);
|
||||
@@ -76,7 +76,7 @@ Scope - это контейнер, который хранит все дерев
|
||||
final str = rootScope.tryResolve<String>();
|
||||
|
||||
// закрыть главный scope
|
||||
Cherrypick.closeRootScope();
|
||||
DartDi.closeRootScope();
|
||||
```
|
||||
|
||||
## Пример приложения
|
||||
@@ -85,8 +85,8 @@ Scope - это контейнер, который хранит все дерев
|
||||
```dart
|
||||
import 'dart:async';
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:cherrypick/scope.dart';
|
||||
import 'package:cherrypick/module.dart';
|
||||
import 'package:dart_di/experimental/scope.dart';
|
||||
import 'package:dart_di/experimental/module.dart';
|
||||
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
@@ -192,4 +192,4 @@ class ApiClientImpl implements ApiClient {
|
||||
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",
|
||||
),
|
||||
),
|
||||
)
|
||||
.singeltone();
|
||||
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 'package:meta/meta.dart';
|
||||
import 'package:cherrypick/scope.dart';
|
||||
import 'package:cherrypick/module.dart';
|
||||
import 'package:dart_di/scope.dart';
|
||||
import 'package:dart_di/module.dart';
|
||||
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<ApiClient>().withName('apiClientMock').toInstance(ApiClientMock());
|
||||
bind<ApiClient>().withName('apiClientImpl').toInstance(ApiClientImpl());
|
||||
bind<ApiClient>().withName("apiClientMock").toInstance(ApiClientMock());
|
||||
bind<ApiClient>().withName("apiClientImpl").toInstance(ApiClientImpl());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,18 +19,18 @@ class FeatureModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<DataRepository>()
|
||||
.withName('networkRepo')
|
||||
.withName("networkRepo")
|
||||
.toProvide(
|
||||
() => NetworkDataRepository(
|
||||
currentScope.resolve<ApiClient>(
|
||||
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
||||
named: isMock ? "apiClientMock" : "apiClientImpl",
|
||||
),
|
||||
),
|
||||
)
|
||||
.singeltone();
|
||||
bind<DataBloc>().toProvide(
|
||||
() => DataBloc(
|
||||
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -42,7 +42,7 @@ void main() async {
|
||||
]);
|
||||
|
||||
final subScope = scope
|
||||
.openSubScope('featureScope')
|
||||
.openSubScope("featureScope")
|
||||
.installModules([FeatureModule(isMock: true)]);
|
||||
|
||||
final dataBloc = subScope.resolve<DataBloc>();
|
||||
@@ -56,7 +56,7 @@ class DataBloc {
|
||||
final DataRepository _dataRepository;
|
||||
|
||||
Stream<String> get data => _dataController.stream;
|
||||
final StreamController<String> _dataController = StreamController.broadcast();
|
||||
StreamController<String> _dataController = new StreamController.broadcast();
|
||||
|
||||
DataBloc(this._dataRepository);
|
||||
|
||||
|
||||
@@ -2,14 +2,13 @@ name: example
|
||||
version: 1.0.0
|
||||
author: "Sergey Penkovsky <sergey.penkovsky@gmail.com>"
|
||||
homepage: localhost
|
||||
publish_to: none
|
||||
|
||||
environment:
|
||||
sdk: ">=2.12.0 <3.0.0"
|
||||
|
||||
|
||||
dependencies:
|
||||
cherrypick:
|
||||
dart_di:
|
||||
path: ../
|
||||
|
||||
dev_dependencies:
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE }
|
||||
|
||||
@@ -20,8 +20,8 @@ class Binding<T> {
|
||||
late Mode _mode;
|
||||
late Type _key;
|
||||
late String _name;
|
||||
T? _instance;
|
||||
T? Function()? _provider;
|
||||
T? _instance = null;
|
||||
T? Function()? _provider = null;
|
||||
late bool _isSingeltone = false;
|
||||
late bool _isNamed = false;
|
||||
|
||||
|
||||
@@ -1,19 +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.
|
||||
///
|
||||
/**
|
||||
* 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:cherrypick/scope.dart';
|
||||
export 'package:cherrypick/module.dart';
|
||||
export 'package:cherrypick/binding.dart';
|
||||
export 'package:cherrypick/di.dart';
|
||||
export 'package:dart_di/scope.dart';
|
||||
export 'package:dart_di/module.dart';
|
||||
export 'package:dart_di/binding.dart';
|
||||
export 'package:dart_di/di.dart';
|
||||
|
||||
35
lib/di.dart
35
lib/di.dart
@@ -1,27 +1,28 @@
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/**
|
||||
* 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';
|
||||
|
||||
import 'package:cherrypick/scope.dart';
|
||||
Scope? _rootScope = null;
|
||||
|
||||
Scope? _rootScope;
|
||||
|
||||
class CherryPick {
|
||||
class DartDi {
|
||||
/// RU: Метод открывает главный [Scope].
|
||||
/// ENG: The method opens the main [Scope].
|
||||
///
|
||||
/// return
|
||||
static Scope openRootScope() {
|
||||
_rootScope ??= Scope(null);
|
||||
if (_rootScope == null) {
|
||||
_rootScope = Scope(null);
|
||||
}
|
||||
return _rootScope!;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/**
|
||||
* 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/scope.dart';
|
||||
import 'package:dart_di/scope.dart';
|
||||
|
||||
abstract class Factory<T> {
|
||||
T createInstance(Scope scope);
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/**
|
||||
* 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 'dart:collection';
|
||||
|
||||
import 'package:cherrypick/binding.dart';
|
||||
import 'package:cherrypick/scope.dart';
|
||||
import 'package:dart_di/binding.dart';
|
||||
import 'package:dart_di/scope.dart';
|
||||
|
||||
/// RU: Класс Module является основой для пользовательских модулей.
|
||||
/// Этот класс нужен для инициализации [Scope].
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/**
|
||||
* 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 'dart:collection';
|
||||
|
||||
import 'package:cherrypick/binding.dart';
|
||||
import 'package:cherrypick/module.dart';
|
||||
import 'package:dart_di/binding.dart';
|
||||
import 'package:dart_di/module.dart';
|
||||
|
||||
Scope openRootScope() => Scope(null);
|
||||
|
||||
@@ -103,8 +103,8 @@ class Scope {
|
||||
T? tryResolve<T>({String? named}) {
|
||||
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||
if (_modulesList.isNotEmpty) {
|
||||
for (var module in _modulesList) {
|
||||
for (var binding in module.bindingSet) {
|
||||
for (Module module in _modulesList) {
|
||||
for (Binding binding in module.bindingSet) {
|
||||
if (binding.key == T &&
|
||||
((!binding.isNamed && named == null) ||
|
||||
(binding.isNamed && named == binding.name))) {
|
||||
|
||||
18
pubspec.yaml
18
pubspec.yaml
@@ -1,10 +1,8 @@
|
||||
name: cherrypick
|
||||
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
||||
version: 0.1.2
|
||||
homepage: https://github.com/pese-git/cherrypick
|
||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||
repository: https://github.com/pese-git/cherrypick
|
||||
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
||||
name: dart_di
|
||||
description: Experimental Dependency Injection library.
|
||||
version: 0.1.0
|
||||
author: Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||
homepage: locahost
|
||||
|
||||
environment:
|
||||
sdk: ">=2.12.0 <3.0.0"
|
||||
@@ -13,8 +11,6 @@ dependencies:
|
||||
meta: ^1.3.0
|
||||
|
||||
dev_dependencies:
|
||||
pedantic: ^1.11.0
|
||||
test: ^1.16.8
|
||||
|
||||
test: ^1.17.2
|
||||
|
||||
mockito: ^5.0.6
|
||||
mockito: ^5.0.3
|
||||
|
||||
@@ -1,201 +1,201 @@
|
||||
import 'package:cherrypick/binding.dart';
|
||||
import 'package:dart_di/binding.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Check instance.', () {
|
||||
group('Without name.', () {
|
||||
test('Binding resolves null', () {
|
||||
group("Check instance.", () {
|
||||
group("Without name.", () {
|
||||
test("Binding resolves null", () {
|
||||
final binding = Binding<int>();
|
||||
expect(binding.instance, null);
|
||||
});
|
||||
|
||||
test('Binding check mode', () {
|
||||
test("Binding check mode", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toInstance(expectedValue);
|
||||
|
||||
expect(binding.mode, Mode.INSTANCE);
|
||||
});
|
||||
|
||||
test('Binding check singeltone', () {
|
||||
test("Binding check singeltone", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toInstance(expectedValue);
|
||||
|
||||
expect(binding.isSingeltone, true);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toInstance(expectedValue);
|
||||
|
||||
expect(binding.instance, expectedValue);
|
||||
});
|
||||
|
||||
test('Binding resolves value', () {
|
||||
test("Binding resolves value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toInstance(expectedValue);
|
||||
expect(binding.instance, expectedValue);
|
||||
});
|
||||
});
|
||||
|
||||
group('With name.', () {
|
||||
test('Binding resolves null', () {
|
||||
final binding = Binding<int>().withName('expectedValue');
|
||||
group("With name.", () {
|
||||
test("Binding resolves null", () {
|
||||
final binding = Binding<int>().withName("expectedValue");
|
||||
expect(binding.instance, null);
|
||||
});
|
||||
|
||||
test('Binding check mode', () {
|
||||
test("Binding check mode", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
||||
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||
|
||||
expect(binding.mode, Mode.INSTANCE);
|
||||
});
|
||||
|
||||
test('Binding check key', () {
|
||||
test("Binding check key", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
||||
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||
|
||||
expect(binding.key, int);
|
||||
});
|
||||
|
||||
test('Binding check singeltone', () {
|
||||
test("Binding check singeltone", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
||||
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||
|
||||
expect(binding.isSingeltone, true);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
||||
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||
|
||||
expect(binding.instance, expectedValue);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
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 binding =
|
||||
Binding<int>().withName('expectedValue').toInstance(expectedValue);
|
||||
Binding<int>().withName("expectedValue").toInstance(expectedValue);
|
||||
expect(binding.instance, expectedValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('Check provide.', () {
|
||||
group('Without name.', () {
|
||||
test('Binding resolves null', () {
|
||||
group("Check provide.", () {
|
||||
group("Without name.", () {
|
||||
test("Binding resolves null", () {
|
||||
final binding = Binding<int>();
|
||||
expect(binding.provider, null);
|
||||
});
|
||||
|
||||
test('Binding check mode', () {
|
||||
test("Binding check mode", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||
});
|
||||
|
||||
test('Binding check singeltone', () {
|
||||
test("Binding check singeltone", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.isSingeltone, false);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.provider, expectedValue);
|
||||
});
|
||||
|
||||
test('Binding resolves value', () {
|
||||
test("Binding resolves value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>().toProvide(() => expectedValue);
|
||||
expect(binding.provider, expectedValue);
|
||||
});
|
||||
});
|
||||
|
||||
group('With name.', () {
|
||||
test('Binding resolves null', () {
|
||||
final binding = Binding<int>().withName('expectedValue');
|
||||
group("With name.", () {
|
||||
test("Binding resolves null", () {
|
||||
final binding = Binding<int>().withName("expectedValue");
|
||||
expect(binding.provider, null);
|
||||
});
|
||||
|
||||
test('Binding check mode', () {
|
||||
test("Binding check mode", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||
});
|
||||
|
||||
test('Binding check key', () {
|
||||
test("Binding check key", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.key, int);
|
||||
});
|
||||
|
||||
test('Binding check singeltone', () {
|
||||
test("Binding check singeltone", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.isSingeltone, false);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.provider, expectedValue);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue);
|
||||
|
||||
expect(binding.name, 'expectedValue');
|
||||
expect(binding.name, "expectedValue");
|
||||
});
|
||||
|
||||
test('Binding resolves value', () {
|
||||
test("Binding resolves value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue);
|
||||
expect(binding.provider, expectedValue);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('Check singeltone provide.', () {
|
||||
group('Without name.', () {
|
||||
test('Binding resolves null', () {
|
||||
group("Check singeltone provide.", () {
|
||||
group("Without name.", () {
|
||||
test("Binding resolves null", () {
|
||||
final binding = Binding<int>().singeltone();
|
||||
expect(binding.provider, null);
|
||||
});
|
||||
|
||||
test('Binding check mode', () {
|
||||
test("Binding check mode", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||
@@ -203,7 +203,7 @@ void main() {
|
||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||
});
|
||||
|
||||
test('Binding check singeltone', () {
|
||||
test("Binding check singeltone", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||
@@ -211,7 +211,7 @@ void main() {
|
||||
expect(binding.isSingeltone, true);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||
@@ -219,7 +219,7 @@ void main() {
|
||||
expect(binding.provider, expectedValue);
|
||||
});
|
||||
|
||||
test('Binding resolves value', () {
|
||||
test("Binding resolves value", () {
|
||||
final expectedValue = 5;
|
||||
final binding =
|
||||
Binding<int>().toProvide(() => expectedValue).singeltone();
|
||||
@@ -227,66 +227,66 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('With name.', () {
|
||||
test('Binding resolves null', () {
|
||||
final binding = Binding<int>().withName('expectedValue').singeltone();
|
||||
group("With name.", () {
|
||||
test("Binding resolves null", () {
|
||||
final binding = Binding<int>().withName("expectedValue").singeltone();
|
||||
expect(binding.provider, null);
|
||||
});
|
||||
|
||||
test('Binding check mode', () {
|
||||
test("Binding check mode", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue)
|
||||
.singeltone();
|
||||
|
||||
expect(binding.mode, Mode.PROVIDER_INSTANCE);
|
||||
});
|
||||
|
||||
test('Binding check key', () {
|
||||
test("Binding check key", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue)
|
||||
.singeltone();
|
||||
|
||||
expect(binding.key, int);
|
||||
});
|
||||
|
||||
test('Binding check singeltone', () {
|
||||
test("Binding check singeltone", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue)
|
||||
.singeltone();
|
||||
|
||||
expect(binding.isSingeltone, true);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue)
|
||||
.singeltone();
|
||||
|
||||
expect(binding.provider, expectedValue);
|
||||
});
|
||||
|
||||
test('Binding check value', () {
|
||||
test("Binding check value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue)
|
||||
.singeltone();
|
||||
|
||||
expect(binding.name, 'expectedValue');
|
||||
expect(binding.name, "expectedValue");
|
||||
});
|
||||
|
||||
test('Binding resolves value', () {
|
||||
test("Binding resolves value", () {
|
||||
final expectedValue = 5;
|
||||
final binding = Binding<int>()
|
||||
.withName('expectedValue')
|
||||
.withName("expectedValue")
|
||||
.toProvide(() => expectedValue)
|
||||
.singeltone();
|
||||
expect(binding.provider, expectedValue);
|
||||
|
||||
@@ -1,28 +1,28 @@
|
||||
import 'package:cherrypick/module.dart';
|
||||
import 'package:cherrypick/scope.dart';
|
||||
import 'package:dart_di/module.dart';
|
||||
import 'package:dart_di/scope.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('Without parent scope.', () {
|
||||
group("Without parent scope.", () {
|
||||
test('Parent scope is null.', () {
|
||||
final scope = Scope(null);
|
||||
final scope = new Scope(null);
|
||||
expect(scope.parentScope, null);
|
||||
});
|
||||
|
||||
test('Open sub scope.', () {
|
||||
final scope = Scope(null);
|
||||
final subScope = scope.openSubScope('subScope');
|
||||
expect(scope.openSubScope('subScope'), subScope);
|
||||
final scope = new Scope(null);
|
||||
final subScope = scope.openSubScope("subScope");
|
||||
expect(scope.openSubScope("subScope"), subScope);
|
||||
});
|
||||
|
||||
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>()));
|
||||
});
|
||||
|
||||
test('Container resolves value after adding a dependency', () {
|
||||
final expectedValue = 'test string';
|
||||
final scope = Scope(null)
|
||||
final expectedValue = "test string";
|
||||
final scope = new Scope(null)
|
||||
.installModules([TestModule<String>(value: expectedValue)]);
|
||||
expect(scope.resolve<String>(), expectedValue);
|
||||
});
|
||||
@@ -42,7 +42,7 @@ void main() {
|
||||
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 parentScope = Scope(null);
|
||||
final scope = Scope(parentScope);
|
||||
@@ -52,10 +52,10 @@ void main() {
|
||||
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 expectedStringValue = 'Hello world';
|
||||
final expectedStringValue = "Hello world";
|
||||
final parentScope = Scope(null).installModules([
|
||||
TestModule<int>(value: expectedIntValue),
|
||||
TestModule<String>(value: expectedStringValue)
|
||||
|
||||
Reference in New Issue
Block a user