mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
refactor: unify DIAdapter with generics, ensure type-safety & scalability in benchmark_di
- Refactor DIAdapter to generic abstract class; align interfaces for Cherrypick, GetIt, Riverpod. - Remove all dynamic/object usage from dependency registration and bench scenarios. - Universal getUniversalRegistration function now fully type-safe for all DIs. - Fix teardown and lifecycle for RiverpodAdapter to prevent disposed Container errors. - Update CLI and benchmark entry points; validated all scenarios and stress modes for each DI adapter.
This commit is contained in:
@@ -1,74 +1,59 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'di_adapter.dart';
|
||||
|
||||
class GetItAdapter implements DIAdapter {
|
||||
/// Универсальный DIAdapter для GetIt c поддержкой scopes и строгой типизацией.
|
||||
class GetItAdapter extends DIAdapter<GetIt> {
|
||||
late GetIt _getIt;
|
||||
|
||||
@override
|
||||
void setupDependencies(void Function(dynamic container) registration) {
|
||||
_getIt = GetIt.asNewInstance();
|
||||
registration(_getIt);
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T extends Object>({String? named}) => _getIt<T>(instanceName: named);
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T extends Object>({String? named}) async => _getIt<T>(instanceName: named);
|
||||
|
||||
@override
|
||||
void teardown() => _getIt.reset();
|
||||
|
||||
@override
|
||||
DIAdapter openSubScope(String name) {
|
||||
// Открываем новый scope и возвращаем адаптер, который в setupDependencies будет использовать init.
|
||||
return _GetItScopeAdapter(_getIt, name);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> waitForAsyncReady() async {
|
||||
await _getIt.allReady();
|
||||
}
|
||||
}
|
||||
|
||||
class _GetItScopeAdapter implements DIAdapter {
|
||||
final GetIt _getIt;
|
||||
final String _scopeName;
|
||||
final String? _scopeName;
|
||||
final bool _isSubScope;
|
||||
bool _scopePushed = false;
|
||||
void Function(dynamic container)? _pendingRegistration;
|
||||
|
||||
_GetItScopeAdapter(this._getIt, this._scopeName);
|
||||
|
||||
@override
|
||||
void setupDependencies(void Function(dynamic container) registration) {
|
||||
_pendingRegistration = registration;
|
||||
// Создаём scope через pushNewScope с init для правильной регистрации
|
||||
_getIt.pushNewScope(
|
||||
scopeName: _scopeName,
|
||||
init: (getIt) => _pendingRegistration?.call(getIt),
|
||||
);
|
||||
_scopePushed = true;
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T extends Object>({String? named}) => _getIt<T>(instanceName: named);
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T extends Object>({String? named}) async => _getIt<T>(instanceName: named);
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
if (_scopePushed) {
|
||||
_getIt.popScope();
|
||||
_scopePushed = false;
|
||||
/// Основной (root) и subScope-конструкторы.
|
||||
GetItAdapter({GetIt? instance, String? scopeName, bool isSubScope = false})
|
||||
: _scopeName = scopeName,
|
||||
_isSubScope = isSubScope {
|
||||
if (instance != null) {
|
||||
_getIt = instance;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
DIAdapter openSubScope(String name) {
|
||||
return _GetItScopeAdapter(_getIt, name);
|
||||
void setupDependencies(void Function(GetIt container) registration) {
|
||||
if (_isSubScope) {
|
||||
// Создаём scope через pushNewScope с init
|
||||
_getIt.pushNewScope(
|
||||
scopeName: _scopeName,
|
||||
init: (getIt) => registration(getIt),
|
||||
);
|
||||
_scopePushed = true;
|
||||
} else {
|
||||
_getIt = GetIt.asNewInstance();
|
||||
registration(_getIt);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T extends Object>({String? named}) =>
|
||||
_getIt<T>(instanceName: named);
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T extends Object>({String? named}) async =>
|
||||
_getIt<T>(instanceName: named);
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
if (_isSubScope && _scopePushed) {
|
||||
_getIt.popScope();
|
||||
_scopePushed = false;
|
||||
} else {
|
||||
_getIt.reset();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
GetItAdapter openSubScope(String name) =>
|
||||
GetItAdapter(instance: _getIt, scopeName: name, isSubScope: true);
|
||||
|
||||
@override
|
||||
Future<void> waitForAsyncReady() async {
|
||||
await _getIt.allReady();
|
||||
|
||||
Reference in New Issue
Block a user