mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
refactor: simplify DIAdapter interface with a single registration callback; update benchmarks and cherrypick adapter accordingly
This commit is contained in:
@@ -18,14 +18,16 @@ class UniversalChainAsyncBenchmark extends AsyncBenchmarkBase {
|
||||
|
||||
@override
|
||||
Future<void> setup() async {
|
||||
di.setupModules([
|
||||
di.setupDependencies((scope) {
|
||||
scope.installModules([
|
||||
UniversalChainModule(
|
||||
chainCount: chainCount,
|
||||
nestingDepth: nestingDepth,
|
||||
bindingMode: mode,
|
||||
scenario: UniversalScenario.asyncChain,
|
||||
)
|
||||
),
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -23,33 +23,39 @@ class UniversalChainBenchmark extends BenchmarkBase {
|
||||
void setup() {
|
||||
switch (scenario) {
|
||||
case UniversalScenario.override:
|
||||
_di.setupModules([
|
||||
_di.setupDependencies((scope) {
|
||||
scope.installModules([
|
||||
UniversalChainModule(
|
||||
chainCount: chainCount,
|
||||
nestingDepth: nestingDepth,
|
||||
bindingMode: UniversalBindingMode.singletonStrategy,
|
||||
scenario: UniversalScenario.register,
|
||||
)
|
||||
),
|
||||
]);
|
||||
});
|
||||
_childDi = _di.openSubScope('child');
|
||||
_childDi!.setupModules([
|
||||
_childDi!.setupDependencies((scope) {
|
||||
scope.installModules([
|
||||
UniversalChainModule(
|
||||
chainCount: chainCount,
|
||||
nestingDepth: nestingDepth,
|
||||
bindingMode: UniversalBindingMode.singletonStrategy,
|
||||
scenario: UniversalScenario.register,
|
||||
)
|
||||
),
|
||||
]);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
_di.setupModules([
|
||||
_di.setupDependencies((scope) {
|
||||
scope.installModules([
|
||||
UniversalChainModule(
|
||||
chainCount: chainCount,
|
||||
nestingDepth: nestingDepth,
|
||||
bindingMode: mode,
|
||||
scenario: scenario,
|
||||
)
|
||||
),
|
||||
]);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,23 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'di_adapter.dart';
|
||||
|
||||
/// DIAdapter implementation for the CherryPick DI library.
|
||||
///
|
||||
/// Wraps a CherryPick [Scope] and provides methods
|
||||
/// to setup modules, resolve dependencies, teardown,
|
||||
/// and open nested sub-scopes for benchmarking.
|
||||
/// DIAdapter implementation for the CherryPick DI library using registration callbacks.
|
||||
class CherrypickDIAdapter implements DIAdapter {
|
||||
Scope? _scope;
|
||||
|
||||
@override
|
||||
void setupModules(List<Module> modules) {
|
||||
void setupDependencies(void Function(dynamic container) registration) {
|
||||
_scope = CherryPick.openRootScope();
|
||||
_scope!.installModules(modules);
|
||||
registration(_scope!);
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T>({String? named}) {
|
||||
return named == null
|
||||
? _scope!.resolve<T>()
|
||||
: _scope!.resolve<T>(named: named);
|
||||
}
|
||||
T resolve<T>({String? named}) =>
|
||||
named == null ? _scope!.resolve<T>() : _scope!.resolve<T>(named: named);
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T>({String? named}) async {
|
||||
return named == null
|
||||
? await _scope!.resolveAsync<T>()
|
||||
: await _scope!.resolveAsync<T>(named: named);
|
||||
}
|
||||
Future<T> resolveAsync<T>({String? named}) async =>
|
||||
named == null ? await _scope!.resolveAsync<T>() : await _scope!.resolveAsync<T>(named: named);
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
@@ -42,33 +32,27 @@ class CherrypickDIAdapter implements DIAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
/// Internal adapter for a CherryPick sub-scope.
|
||||
/// Used for simulating child/override DI scopes in benchmarks.
|
||||
/// Internal adapter for a CherryPick sub-scope (callbacks based).
|
||||
class _CherrypickSubScopeAdapter extends CherrypickDIAdapter {
|
||||
final Scope _subScope;
|
||||
_CherrypickSubScopeAdapter(this._subScope);
|
||||
|
||||
@override
|
||||
void setupModules(List<Module> modules) {
|
||||
_subScope.installModules(modules);
|
||||
void setupDependencies(void Function(dynamic container) registration) {
|
||||
registration(_subScope);
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T>({String? named}) {
|
||||
return named == null
|
||||
? _subScope.resolve<T>()
|
||||
: _subScope.resolve<T>(named: named);
|
||||
}
|
||||
T resolve<T>({String? named}) =>
|
||||
named == null ? _subScope.resolve<T>() : _subScope.resolve<T>(named: named);
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T>({String? named}) async {
|
||||
return named == null
|
||||
? await _subScope.resolveAsync<T>()
|
||||
: await _subScope.resolveAsync<T>(named: named);
|
||||
}
|
||||
Future<T> resolveAsync<T>({String? named}) async =>
|
||||
named == null ? await _subScope.resolveAsync<T>() : await _subScope.resolveAsync<T>(named: named);
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
// subScope teardown убирать отдельно не требуется
|
||||
// subScope teardown не требуется
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
/// Abstraction for Dependency Injection (DI) Adapter.
|
||||
/// Абстракция для DI-адаптера с использованием функций регистрации.
|
||||
///
|
||||
/// Provides a uniform interface to setup, resolve, and teardown DI containers/modules
|
||||
/// and open sub-scopes to benchmark them under different libraries.
|
||||
/// Позволяет использовать любые DI-контейнеры: и модульные, и безмодульные.
|
||||
abstract class DIAdapter {
|
||||
/// Installs the provided modules into the DI container.
|
||||
void setupModules(List<Module> modules);
|
||||
/// Устанавливает зависимости с помощью одной функции регистрации.
|
||||
///
|
||||
/// Функция принимает выбранный DI-контейнер, задаваемый реализацией.
|
||||
void setupDependencies(void Function(dynamic container) registration);
|
||||
|
||||
/// Resolves an instance of type [T] by optional [named] tag.
|
||||
/// Резолвит (возвращает) экземпляр типа [T] (по имени, если требуется).
|
||||
T resolve<T>({String? named});
|
||||
|
||||
/// Asynchronously resolves an instance of type [T] by optional [named] tag.
|
||||
/// Асинхронно резолвит экземпляр типа [T].
|
||||
Future<T> resolveAsync<T>({String? named});
|
||||
|
||||
/// Tears down or disposes of the DI container.
|
||||
/// Уничтожает/отчищает DI-контейнер.
|
||||
void teardown();
|
||||
|
||||
/// Opens a child DI sub-scope, useful for override/child-scope benchmarks.
|
||||
/// Открывает дочерний под-scope (если применимо).
|
||||
DIAdapter openSubScope(String name);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user