mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
Compare commits
32 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f86662c9b | ||
|
|
dcdfce41db | ||
|
|
c2f2577cc6 | ||
|
|
93f431ce93 | ||
|
|
5462f9da07 | ||
|
|
ac30908f2d | ||
|
|
4cfca7c063 | ||
|
|
5afb8bda35 | ||
|
|
86c58191e5 | ||
|
|
f3b1ee84b2 | ||
|
|
b6a4b86b19 | ||
|
|
a20d153c1a | ||
|
|
b621865e82 | ||
|
|
4f127751d8 | ||
|
|
1fb6db6dec | ||
|
|
565fb3e682 | ||
|
|
de404d4ee1 | ||
|
|
35879380d0 | ||
|
|
bab560a856 | ||
|
|
98f12c5eb7 | ||
|
|
2568414a1b | ||
|
|
1ddbb74e3f | ||
|
|
e102b15022 | ||
|
|
ec75ad9172 | ||
|
|
ed0c2fae53 | ||
|
|
4302d733ba | ||
|
|
c9ddc2ffa8 | ||
|
|
b2b66bdcfd | ||
|
|
6a2d86c83c | ||
|
|
2d6fdbe04c | ||
|
|
35a9478446 | ||
|
|
0e3c5037fb |
@@ -1,6 +1,14 @@
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
0.1.1+1 Updated pubspec and readme.md
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
0.1.1 Updated pubspec
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
0.1.0 Initial release
|
0.1.0 Initial release
|
||||||
|
|
||||||
---
|
---
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
# dart_di
|
# cherrypick
|
||||||
|
|
||||||
Experimental development of DI in the Dart language
|
Experimental development of DI in the Dart language
|
||||||
|
|
||||||
@@ -10,4 +10,4 @@ Experimental development of DI in the Dart language
|
|||||||
|
|
||||||
- [x] Scope
|
- [x] Scope
|
||||||
- [x] Sub scope
|
- [x] Sub scope
|
||||||
- [x] Initialization instance with name
|
- [x] Initialization instance with name
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ Example:
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
// open main scope
|
// open main scope
|
||||||
final rootScope = DartDi.openRootScope();
|
final rootScope = Cherrypick.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
|
||||||
DartDi.closeRootScope();
|
Cherrypick.closeRootScope();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example app
|
## Example app
|
||||||
@@ -85,8 +85,8 @@ Example:
|
|||||||
```dart
|
```dart
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:dart_di/experimental/scope.dart';
|
import 'package:cherrypick/scope.dart';
|
||||||
import 'package:dart_di/experimental/module.dart';
|
import 'package:cherrypick/module.dart';
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ Scope - это контейнер, который хранит все дерев
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
// открыть главный scope
|
// открыть главный scope
|
||||||
final rootScope = DartDi.openRootScope();
|
final rootScope = CherryPick.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
|
||||||
DartDi.closeRootScope();
|
Cherrypick.closeRootScope();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Пример приложения
|
## Пример приложения
|
||||||
@@ -85,8 +85,8 @@ Scope - это контейнер, который хранит все дерев
|
|||||||
```dart
|
```dart
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:dart_di/experimental/scope.dart';
|
import 'package:cherrypick/scope.dart';
|
||||||
import 'package:dart_di/experimental/module.dart';
|
import 'package:cherrypick/module.dart';
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
@@ -192,4 +192,4 @@ class ApiClientImpl implements ApiClient {
|
|||||||
return 'Network data';
|
return 'Network data';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
136
example/README.md
Normal file
136
example/README.md
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
## 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,7 +1,7 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:dart_di/scope.dart';
|
import 'package:cherrypick/scope.dart';
|
||||||
import 'package:dart_di/module.dart';
|
import 'package:cherrypick/module.dart';
|
||||||
|
|
||||||
class AppModule extends Module {
|
class AppModule extends Module {
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ environment:
|
|||||||
|
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
dart_di:
|
cherrypick:
|
||||||
path: ../
|
path: ../
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
|
|||||||
@@ -13,7 +13,7 @@
|
|||||||
|
|
||||||
library dart_di;
|
library dart_di;
|
||||||
|
|
||||||
export 'package:dart_di/scope.dart';
|
export 'package:cherrypick/scope.dart';
|
||||||
export 'package:dart_di/module.dart';
|
export 'package:cherrypick/module.dart';
|
||||||
export 'package:dart_di/binding.dart';
|
export 'package:cherrypick/binding.dart';
|
||||||
export 'package:dart_di/di.dart';
|
export 'package:cherrypick/di.dart';
|
||||||
|
|||||||
@@ -10,11 +10,11 @@
|
|||||||
* 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 'package:dart_di/scope.dart';
|
import 'package:cherrypick/scope.dart';
|
||||||
|
|
||||||
Scope? _rootScope = null;
|
Scope? _rootScope = null;
|
||||||
|
|
||||||
class DartDi {
|
class CherryPick {
|
||||||
/// RU: Метод открывает главный [Scope].
|
/// RU: Метод открывает главный [Scope].
|
||||||
/// ENG: The method opens the main [Scope].
|
/// ENG: The method opens the main [Scope].
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import 'package:dart_di/scope.dart';
|
import 'package:cherrypick/scope.dart';
|
||||||
|
|
||||||
abstract class Factory<T> {
|
abstract class Factory<T> {
|
||||||
T createInstance(Scope scope);
|
T createInstance(Scope scope);
|
||||||
|
|||||||
@@ -13,8 +13,8 @@
|
|||||||
|
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:dart_di/binding.dart';
|
import 'package:cherrypick/binding.dart';
|
||||||
import 'package:dart_di/scope.dart';
|
import 'package:cherrypick/scope.dart';
|
||||||
|
|
||||||
/// RU: Класс Module является основой для пользовательских модулей.
|
/// RU: Класс Module является основой для пользовательских модулей.
|
||||||
/// Этот класс нужен для инициализации [Scope].
|
/// Этот класс нужен для инициализации [Scope].
|
||||||
|
|||||||
@@ -13,8 +13,8 @@
|
|||||||
|
|
||||||
import 'dart:collection';
|
import 'dart:collection';
|
||||||
|
|
||||||
import 'package:dart_di/binding.dart';
|
import 'package:cherrypick/binding.dart';
|
||||||
import 'package:dart_di/module.dart';
|
import 'package:cherrypick/module.dart';
|
||||||
|
|
||||||
Scope openRootScope() => Scope(null);
|
Scope openRootScope() => Scope(null);
|
||||||
|
|
||||||
|
|||||||
12
pubspec.yaml
12
pubspec.yaml
@@ -1,8 +1,10 @@
|
|||||||
name: dart_di
|
name: cherrypick
|
||||||
description: Experimental Dependency Injection library.
|
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
||||||
version: 0.1.0
|
version: 0.1.1+1
|
||||||
author: Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
homepage: https://github.com/pese-git/cherrypick
|
||||||
homepage: locahost
|
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||||
|
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"
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import 'package:dart_di/binding.dart';
|
import 'package:cherrypick/binding.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import 'package:dart_di/module.dart';
|
import 'package:cherrypick/module.dart';
|
||||||
import 'package:dart_di/scope.dart';
|
import 'package:cherrypick/scope.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user