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