mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
implemented expiremental di with new api
This commit is contained in:
82
example/bin/main_experiment.dart
Normal file
82
example/bin/main_experiment.dart
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:dart_di/experimental/scope.dart';
|
||||||
|
import 'package:dart_di/experimental/module.dart';
|
||||||
|
|
||||||
|
class AppModule extends Module {
|
||||||
|
@override
|
||||||
|
void builder(Scope currentScope) {
|
||||||
|
bind<ApiClient>().withName("apiClient").toInstance(ApiClientMock());
|
||||||
|
bind<DataRepository>().withName("networkRepo").toProvide(
|
||||||
|
() => NetworkDataRepository(
|
||||||
|
currentScope.resolve<ApiClient>(named: "apiClient"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
// .singeltone();
|
||||||
|
bind<DataBloc>().toProvide(
|
||||||
|
() => DataBloc(
|
||||||
|
currentScope.resolve<DataRepository>(named: "networkRepo"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void main() async {
|
||||||
|
final scope = openRootScope().installModules([
|
||||||
|
AppModule(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
final dataBloc = scope.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 'hello world';
|
||||||
|
}
|
||||||
|
}
|
||||||
53
lib/experimental/binding.dart
Normal file
53
lib/experimental/binding.dart
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE }
|
||||||
|
|
||||||
|
class Binding<T> {
|
||||||
|
late Mode _mode;
|
||||||
|
late Type _key;
|
||||||
|
late String _name;
|
||||||
|
late T _instance;
|
||||||
|
late T Function() _provider;
|
||||||
|
late bool _isSingeltone = false;
|
||||||
|
late bool _isNamed = false;
|
||||||
|
|
||||||
|
Binding() {
|
||||||
|
_mode = Mode.SIMPLE;
|
||||||
|
_key = T;
|
||||||
|
}
|
||||||
|
|
||||||
|
Mode get mode => _mode;
|
||||||
|
Type get key => _key;
|
||||||
|
String get name => _name;
|
||||||
|
bool get isSingeltone => _isSingeltone;
|
||||||
|
bool get isNamed => _isNamed;
|
||||||
|
|
||||||
|
Binding<T> withName(String name) {
|
||||||
|
_name = name;
|
||||||
|
_isNamed = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Binding<T> toInstance(T value) {
|
||||||
|
_mode = Mode.INSTANCE;
|
||||||
|
_instance = value;
|
||||||
|
_isSingeltone = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Binding<T> toProvide(T Function() value) {
|
||||||
|
_mode = Mode.PROVIDER_INSTANCE;
|
||||||
|
_provider = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Binding<T> singeltone() {
|
||||||
|
if (_mode == Mode.PROVIDER_INSTANCE) {
|
||||||
|
_instance = _provider.call();
|
||||||
|
}
|
||||||
|
_isSingeltone = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
T? get instance => _instance;
|
||||||
|
|
||||||
|
T? get provider => _provider.call();
|
||||||
|
}
|
||||||
1
lib/experimental/di.dart
Normal file
1
lib/experimental/di.dart
Normal file
@@ -0,0 +1 @@
|
|||||||
|
class DartDi {}
|
||||||
5
lib/experimental/factory.dart
Normal file
5
lib/experimental/factory.dart
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import 'package:dart_di/experimental/scope.dart';
|
||||||
|
|
||||||
|
abstract class Factory<T> {
|
||||||
|
T createInstance(Scope scope);
|
||||||
|
}
|
||||||
18
lib/experimental/module.dart
Normal file
18
lib/experimental/module.dart
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import 'package:dart_di/experimental/binding.dart';
|
||||||
|
import 'package:dart_di/experimental/scope.dart';
|
||||||
|
|
||||||
|
abstract class Module {
|
||||||
|
final Set<Binding> _bindingSet = HashSet();
|
||||||
|
|
||||||
|
Binding<T> bind<T>() {
|
||||||
|
final binding = Binding<T>();
|
||||||
|
_bindingSet.add(binding);
|
||||||
|
return binding;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Binding> get bindingSet => _bindingSet;
|
||||||
|
|
||||||
|
void builder(Scope currentScope);
|
||||||
|
}
|
||||||
88
lib/experimental/scope.dart
Normal file
88
lib/experimental/scope.dart
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
import 'package:dart_di/experimental/binding.dart';
|
||||||
|
import 'package:dart_di/experimental/module.dart';
|
||||||
|
|
||||||
|
Scope openRootScope() => Scope(null);
|
||||||
|
|
||||||
|
class Scope {
|
||||||
|
final Scope? _parentScope;
|
||||||
|
|
||||||
|
Scope? get parentScope => _parentScope;
|
||||||
|
|
||||||
|
final Map<String, Scope> _scopeMap = HashMap();
|
||||||
|
|
||||||
|
Scope(this._parentScope);
|
||||||
|
|
||||||
|
final Set<Module> _modulesList = HashSet();
|
||||||
|
|
||||||
|
Scope openSubScope(String name) {
|
||||||
|
final subScope = Scope(this);
|
||||||
|
if (!_scopeMap.containsKey(name)) {
|
||||||
|
_scopeMap[name] = subScope;
|
||||||
|
}
|
||||||
|
return _scopeMap[name]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
void closeSubScope(String name) {
|
||||||
|
_scopeMap.remove(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
Scope installModules(List<Module> modules) {
|
||||||
|
_modulesList.addAll(modules);
|
||||||
|
modules.forEach((module) => module.builder(this));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Scope dropModules() {
|
||||||
|
_modulesList.removeAll(_modulesList);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает найденную зависимость, определенную параметром типа [T].
|
||||||
|
* Выдает [StateError], если зависимость не может быть разрешена.
|
||||||
|
* Если вы хотите получить [null], если зависимость не может быть найдена,
|
||||||
|
* то используйте вместо этого [tryResolve]
|
||||||
|
* @return - возвращает объект типа [T] или [StateError]
|
||||||
|
*/
|
||||||
|
T resolve<T>({String? named}) {
|
||||||
|
var resolved = tryResolve<T>(named: named);
|
||||||
|
if (resolved != null) {
|
||||||
|
return resolved;
|
||||||
|
} else {
|
||||||
|
throw StateError(
|
||||||
|
'Can\'t resolve dependency `$T`. Maybe you forget register it?');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает найденную зависимость типа [T] или null, если она не может быть найдена.
|
||||||
|
*/
|
||||||
|
T? tryResolve<T>({String? named}) {
|
||||||
|
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||||
|
if (_modulesList.isNotEmpty) {
|
||||||
|
for (Module module in _modulesList) {
|
||||||
|
for (Binding binding in module.bindingSet) {
|
||||||
|
if (binding.key == T &&
|
||||||
|
((!binding.isNamed && named == null) ||
|
||||||
|
(binding.isNamed && named == binding.name))) {
|
||||||
|
switch (binding.mode) {
|
||||||
|
case Mode.INSTANCE:
|
||||||
|
return binding.instance;
|
||||||
|
case Mode.PROVIDER_INSTANCE:
|
||||||
|
return binding.isSingeltone
|
||||||
|
? binding.instance
|
||||||
|
: binding.provider;
|
||||||
|
default:
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2 Поиск зависимостей в родительском скоупе
|
||||||
|
return _parentScope != null ? _parentScope!.tryResolve() : null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user