mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
doc: update README and example
This commit is contained in:
@@ -1,31 +1,43 @@
|
|||||||
# CherryPick Flutter
|
# CherryPick Flutter
|
||||||
|
|
||||||
`cherrypick_flutter` is a powerful Flutter library for managing and accessing dependencies within your application through a root scope context using `CherryPickProvider`. It offers simplified dependency injection, making your application more modular and test-friendly.
|
`cherrypick_flutter` is a robust Flutter library designed for managing and accessing dependencies using a scope context provided by `CherryPickProvider`. It enhances your application's modularity and testability by simplifying dependency injection.
|
||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
### Main Components in Dependency Injection (DI)
|
### Core Components of Dependency Injection (DI)
|
||||||
|
|
||||||
#### Binding
|
#### Binding
|
||||||
|
|
||||||
A Binding is a custom instance configurator, essential for setting up dependencies. It offers the following key methods:
|
A Binding is a custom instance configurator crucial for setting up dependencies. It offers the following key methods:
|
||||||
|
|
||||||
- `toInstance()`: Directly provides an initialized instance.
|
- `toInstance()`: Directly provides an initialized instance.
|
||||||
- `toProvide()`: Accepts a provider function or constructor for lazy initialization.
|
- `toProvide()`: Accepts a provider function for lazy initialization.
|
||||||
- `withName()`: Assigns a name to an instance, allowing for retrieval by name.
|
- `toProvideAsync()`: Accepts an asynchronous provider for lazy initialization.
|
||||||
- `singleton()`: Marks the instance as a singleton, ensuring only one instance exists in the scope.
|
- `toProvideWithParams()`: Accepts a provider function requiring dynamic parameters.
|
||||||
|
- `toProvideAsyncWithParams()`: Accepts an asynchronous provider requiring dynamic parameters.
|
||||||
|
- `withName()`: Assigns a name for instance retrieval by name.
|
||||||
|
- `singleton()`: Marks the instance as a singleton, ensuring only one instance exists within the scope.
|
||||||
|
|
||||||
##### Example:
|
##### Example:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
// Direct instance initialization with toInstance()
|
// Direct instance initialization using toInstance()
|
||||||
Binding<String>().toInstance("hello world");
|
Binding<String>().toInstance("hello world");
|
||||||
|
|
||||||
// Or use a provider for lazy initialization
|
// Lazy initialization via provider
|
||||||
Binding<String>().toProvide(() => "hello world");
|
Binding<String>().toProvide(() => "hello world");
|
||||||
|
|
||||||
// Named instance
|
// Asynchronous lazy initialization
|
||||||
Binding<String>().withName("my_string").toInstance("hello world");
|
Binding<String>().toProvideAsync(() async => "hello async world");
|
||||||
|
|
||||||
|
/ Asynchronous lazy initialization with dynamic parameters
|
||||||
|
Binding<String>().toProvideAsyncWithParams((params) async => "hello $params");
|
||||||
|
|
||||||
|
// Initialization with dynamic parameters
|
||||||
|
Binding<String>().toProvideWithParams((params) => "hello $params");
|
||||||
|
|
||||||
|
// Named instance for resolution
|
||||||
|
Binding<String>().toProvide(() => "hello world").withName("my_string").toInstance("hello world");
|
||||||
|
|
||||||
// Singleton instance
|
// Singleton instance
|
||||||
Binding<String>().toProvide(() => "hello world").singleton();
|
Binding<String>().toProvide(() => "hello world").singleton();
|
||||||
@@ -33,7 +45,7 @@ Binding<String>().toProvide(() => "hello world").singleton();
|
|||||||
|
|
||||||
#### Module
|
#### Module
|
||||||
|
|
||||||
A Module encapsulates bindings, allowing you to organize dependencies logically. To create a custom module, implement the `void builder(Scope currentScope)` method.
|
A Module encapsulates bindings, logically organizing dependencies. Implement the `void builder(Scope currentScope)` method to create a custom module.
|
||||||
|
|
||||||
##### Example:
|
##### Example:
|
||||||
|
|
||||||
@@ -48,13 +60,13 @@ class AppModule extends Module {
|
|||||||
|
|
||||||
#### Scope
|
#### Scope
|
||||||
|
|
||||||
A Scope is the container that manages your dependency tree, holding modules and instances. Use the scope to access dependencies with the `resolve<T>()` method.
|
A Scope manages your dependency tree, holding modules and instances. Use the scope to access dependencies with `resolve<T>()` or `resolveAsync<T>()` for asynchronous operations.
|
||||||
|
|
||||||
##### Example:
|
##### Example:
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
// Open the main scope
|
// Open the main scope
|
||||||
final rootScope = Cherrypick.openRootScope();
|
final rootScope = CherryPick.openRootScope();
|
||||||
|
|
||||||
// Install custom modules
|
// Install custom modules
|
||||||
rootScope.installModules([AppModule()]);
|
rootScope.installModules([AppModule()]);
|
||||||
@@ -62,13 +74,16 @@ rootScope.installModules([AppModule()]);
|
|||||||
// Resolve an instance
|
// Resolve an instance
|
||||||
final str = rootScope.resolve<String>();
|
final str = rootScope.resolve<String>();
|
||||||
|
|
||||||
|
// Asynchronously resolve an instance
|
||||||
|
final asyncStr = await rootScope.resolveAsync<String>();
|
||||||
|
|
||||||
// Close the main scope
|
// Close the main scope
|
||||||
Cherrypick.closeRootScope();
|
CherryPick.closeRootScope();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example Application
|
## Example Application
|
||||||
|
|
||||||
The following example demonstrates module setup, scope management, and dependency resolution.
|
The following example demonstrates module setup, scope management, and dependency resolution (both synchronous and asynchronous).
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
@@ -84,26 +99,30 @@ class AppModule extends Module {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class FeatureModule extends Module {
|
class FeatureModule extends Module {
|
||||||
bool isMock;
|
final bool isMock;
|
||||||
|
|
||||||
FeatureModule({required this.isMock});
|
FeatureModule({required this.isMock});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void builder(Scope currentScope) {
|
void builder(Scope currentScope) {
|
||||||
|
// Using toProvideAsync for async initialization
|
||||||
bind<DataRepository>()
|
bind<DataRepository>()
|
||||||
.withName("networkRepo")
|
.withName("networkRepo")
|
||||||
.toProvide(
|
.toProvideAsync(() async {
|
||||||
() => NetworkDataRepository(
|
final client = await Future.delayed(
|
||||||
currentScope.resolve<ApiClient>(
|
Duration(milliseconds: 100),
|
||||||
named: isMock ? "apiClientMock" : "apiClientImpl",
|
() => currentScope.resolve<ApiClient>(
|
||||||
),
|
named: isMock ? "apiClientMock" : "apiClientImpl"));
|
||||||
),
|
return NetworkDataRepository(client);
|
||||||
)
|
})
|
||||||
.singleton();
|
.singleton();
|
||||||
bind<DataBloc>().toProvide(
|
|
||||||
() => DataBloc(
|
// Asynchronous initialization of DataBloc
|
||||||
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
bind<DataBloc>().toProvideAsync(
|
||||||
),
|
() async {
|
||||||
|
final repo = await currentScope.resolveAsync<DataRepository>(named: "networkRepo");
|
||||||
|
return DataBloc(repo);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,7 +136,8 @@ void main() async {
|
|||||||
.openSubScope("featureScope")
|
.openSubScope("featureScope")
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
.installModules([FeatureModule(isMock: true)]);
|
||||||
|
|
||||||
final dataBloc = subScope.resolve<DataBloc>();
|
// Asynchronous instance resolution
|
||||||
|
final dataBloc = await subScope.resolveAsync<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'));
|
||||||
|
|
||||||
@@ -128,7 +148,7 @@ class DataBloc {
|
|||||||
final DataRepository _dataRepository;
|
final DataRepository _dataRepository;
|
||||||
|
|
||||||
Stream<String> get data => _dataController.stream;
|
Stream<String> get data => _dataController.stream;
|
||||||
StreamController<String> _dataController = new StreamController.broadcast();
|
StreamController<String> _dataController = StreamController.broadcast();
|
||||||
|
|
||||||
DataBloc(this._dataRepository);
|
DataBloc(this._dataRepository);
|
||||||
|
|
||||||
@@ -185,6 +205,8 @@ class ApiClientImpl implements ApiClient {
|
|||||||
|
|
||||||
- [x] Main Scope and Sub Scopes
|
- [x] Main Scope and Sub Scopes
|
||||||
- [x] Named Instance Initialization
|
- [x] Named Instance Initialization
|
||||||
|
- [x] Asynchronous Dependency Resolution
|
||||||
|
- [x] Dynamic Parameter Support for Providers
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:cherrypick/cherrypick.dart';
|
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:cherrypick/cherrypick.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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -17,60 +17,54 @@ class FeatureModule extends Module {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
void builder(Scope currentScope) {
|
void builder(Scope currentScope) {
|
||||||
// DataRepository remains async for demonstration
|
// Using toProvideAsync for async initialization
|
||||||
bind<DataRepository>().withName('networkRepo').toProvideAsync(
|
bind<DataRepository>().withName("networkRepo").toProvideAsync(() async {
|
||||||
|
final client = await Future.delayed(
|
||||||
|
Duration(milliseconds: 100),
|
||||||
|
() => currentScope.resolve<ApiClient>(
|
||||||
|
named: isMock ? "apiClientMock" : "apiClientImpl"));
|
||||||
|
return NetworkDataRepository(client);
|
||||||
|
}).singleton();
|
||||||
|
|
||||||
|
// Asynchronous initialization of DataBloc
|
||||||
|
bind<DataBloc>().toProvideAsync(
|
||||||
() async {
|
() async {
|
||||||
// Using synchronous resolve for ApiClient
|
final repo = await currentScope.resolveAsync<DataRepository>(
|
||||||
final apiClient = currentScope.resolve<ApiClient>(
|
named: "networkRepo");
|
||||||
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
return DataBloc(repo);
|
||||||
);
|
|
||||||
return NetworkDataRepository(apiClient);
|
|
||||||
},
|
|
||||||
).singleton();
|
|
||||||
|
|
||||||
bind<DataBloc>().toProvideAsyncWithParams(
|
|
||||||
(param) async {
|
|
||||||
final dataRepository = await currentScope.resolveAsync<DataRepository>(
|
|
||||||
named: 'networkRepo');
|
|
||||||
return DataBloc(dataRepository, param);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> main() async {
|
void main() async {
|
||||||
final scope = openRootScope().installModules([
|
final scope = openRootScope().installModules([
|
||||||
AppModule(),
|
AppModule(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
final subScope = scope
|
final subScope = scope
|
||||||
.openSubScope('featureScope')
|
.openSubScope("featureScope")
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
.installModules([FeatureModule(isMock: true)]);
|
||||||
|
|
||||||
try {
|
// Asynchronous instance resolution
|
||||||
final dataBloc = await subScope.resolveAsync<DataBloc>(params: 'PARAMETER');
|
final dataBloc = await subScope.resolveAsync<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'));
|
||||||
|
|
||||||
await dataBloc.fetchData();
|
await dataBloc.fetchData();
|
||||||
} catch (e) {
|
|
||||||
print('Error resolving dependency: $e');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DataBloc {
|
class DataBloc {
|
||||||
final DataRepository _dataRepository;
|
final DataRepository _dataRepository;
|
||||||
|
|
||||||
final StreamController<String> _dataController = StreamController.broadcast();
|
|
||||||
Stream<String> get data => _dataController.stream;
|
Stream<String> get data => _dataController.stream;
|
||||||
|
final StreamController<String> _dataController = 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);
|
||||||
}
|
}
|
||||||
@@ -82,7 +76,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 {
|
||||||
@@ -92,42 +86,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';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user