mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
Compare commits
80 Commits
cherrypick
...
4953e917c9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4953e917c9 | ||
|
|
1997110d92 | ||
|
|
0e600ca3a2 | ||
|
|
25ae208ea1 | ||
|
|
685c0ae49c | ||
|
|
98d81b13a8 | ||
|
|
cccf460f01 | ||
|
|
0c1ef70b73 | ||
|
|
eb6d786600 | ||
|
|
c483d8c9e2 | ||
|
|
a74cec645e | ||
|
|
082b5a6fb6 | ||
|
|
6c1ba523c6 | ||
|
|
651b2a26d6 | ||
|
|
ec6e9aefd3 | ||
|
|
751cb08064 | ||
|
|
b2fbce74b3 | ||
|
|
81f14f5231 | ||
|
|
a9101513e1 | ||
|
|
f1cf1d054f | ||
|
|
f1ad1c42b5 | ||
|
|
be7f3e0392 | ||
|
|
1b0615810d | ||
|
|
ef04f464da | ||
|
|
6826f0f62c | ||
|
|
9e517d047f | ||
|
|
68a16aaa0c | ||
|
|
679b2b87b7 | ||
|
|
dbdae94673 | ||
|
|
4220967447 | ||
|
|
dfe16fb10f | ||
|
|
ce2e770cbe | ||
|
|
7f5f5c4064 | ||
|
|
04ecb6d3a6 | ||
|
|
484061148d | ||
|
|
b5b672765e | ||
|
|
482b7b0f5f | ||
|
|
722a4d7980 | ||
|
|
16cd7199aa | ||
|
|
1cbcce5b38 | ||
|
|
264c4bbb88 | ||
|
|
cbb5dcc3a0 | ||
|
|
d281c18a75 | ||
|
|
8ef12e990f | ||
|
|
5c57370755 | ||
|
|
8711dc83d0 | ||
|
|
043737e2c9 | ||
|
|
ed65e3c23d | ||
|
|
a897c1b31b | ||
|
|
dd9c3faa62 | ||
|
|
846d55b124 | ||
|
|
4f91d442af | ||
|
|
d0c3870af6 | ||
|
|
c8292035b6 | ||
|
|
63ee3a9966 | ||
|
|
a4c5fd922e | ||
|
|
8870b8ce54 | ||
|
|
1f7e1d120d | ||
|
|
bcc5278c83 | ||
|
|
8863b10cbe | ||
|
|
e0a5ae66f6 | ||
|
|
9fee26c524 | ||
|
|
248bf4c8c5 | ||
|
|
f4c4fe49a0 | ||
|
|
298cb65ac8 | ||
|
|
1b9db31c13 | ||
|
|
ca3cd2c8fd | ||
|
|
c91e15319b | ||
|
|
99e662124f | ||
|
|
03f54981f3 | ||
|
|
349efe6ba6 | ||
|
|
c2f0e027b6 | ||
|
|
f85036d20f | ||
|
|
db4d128d04 | ||
|
|
2c4e2ed251 | ||
|
|
7b4642f407 | ||
|
|
7d45d00d6a | ||
|
|
884df50a34 | ||
|
|
5710af2f9b | ||
|
|
9312ef46ea |
491
CHANGELOG.md
491
CHANGELOG.md
@@ -3,6 +3,497 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
||||
|
||||
## 2025-10-20
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.2`](#cherrypick---v302)
|
||||
- [`cherrypick_flutter` - `v3.0.2`](#cherrypick_flutter---v302)
|
||||
- [`talker_cherrypick_logger` - `v3.0.2`](#talker_cherrypick_logger---v302)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_flutter` - `v3.0.2`
|
||||
- `talker_cherrypick_logger` - `v3.0.2`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.2`
|
||||
|
||||
- **FIX**(test): fix warning.
|
||||
- **FIX**(scope): properly clear binding and module references on dispose.
|
||||
|
||||
|
||||
## 2025-09-09
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_annotations` - `v3.0.2-dev.0`](#cherrypick_annotations---v302-dev0)
|
||||
- [`cherrypick_generator` - `v3.0.2-dev.0`](#cherrypick_generator---v302-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_annotations` - `v3.0.2-dev.0`
|
||||
|
||||
- **REFACTOR**(generator): migrate cherrypick_generator to analyzer element2 API.
|
||||
|
||||
#### `cherrypick_generator` - `v3.0.2-dev.0`
|
||||
|
||||
- **REFACTOR**(generator): migrate cherrypick_generator to analyzer element2 API.
|
||||
|
||||
|
||||
## 2025-09-09
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.1`](#cherrypick---v301)
|
||||
- [`cherrypick_annotations` - `v3.0.1`](#cherrypick_annotations---v301)
|
||||
- [`cherrypick_flutter` - `v3.0.1`](#cherrypick_flutter---v301)
|
||||
- [`cherrypick_generator` - `v3.0.1`](#cherrypick_generator---v301)
|
||||
- [`talker_cherrypick_logger` - `v3.0.1`](#talker_cherrypick_logger---v301)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.1`
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
#### `cherrypick_annotations` - `v3.0.1`
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
#### `cherrypick_flutter` - `v3.0.1`
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
#### `cherrypick_generator` - `v3.0.1`
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
#### `talker_cherrypick_logger` - `v3.0.1`
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0`](#cherrypick---v300)
|
||||
- [`cherrypick_annotations` - `v3.0.0`](#cherrypick_annotations---v300)
|
||||
- [`cherrypick_flutter` - `v3.0.0`](#cherrypick_flutter---v300)
|
||||
- [`cherrypick_generator` - `v3.0.0`](#cherrypick_generator---v300)
|
||||
- [`talker_cherrypick_logger` - `v3.0.0`](#talker_cherrypick_logger---v300)
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- There are no other changes in this release.
|
||||
|
||||
Packages graduated to a stable release (see pre-releases prior to the stable version for changelog entries):
|
||||
|
||||
- `cherrypick` - `v3.0.0`
|
||||
- `cherrypick_annotations` - `v3.0.0`
|
||||
- `cherrypick_flutter` - `v3.0.0`
|
||||
- `cherrypick_generator` - `v3.0.0`
|
||||
- `talker_cherrypick_logger` - `v3.0.0`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0`
|
||||
|
||||
#### `cherrypick_annotations` - `v3.0.0`
|
||||
|
||||
#### `cherrypick_flutter` - `v3.0.0`
|
||||
|
||||
#### `cherrypick_generator` - `v3.0.0`
|
||||
|
||||
#### `talker_cherrypick_logger` - `v3.0.0`
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.13`](#cherrypick---v300-dev13)
|
||||
- [`cherrypick_flutter` - `v3.0.0-dev.1`](#cherrypick_flutter---v300-dev1)
|
||||
- [`talker_cherrypick_logger` - `v3.0.0-dev.1`](#talker_cherrypick_logger---v300-dev1)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_flutter` - `v3.0.0-dev.1`
|
||||
- `talker_cherrypick_logger` - `v3.0.0-dev.1`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.13`
|
||||
|
||||
- **FIX**: fix examples.
|
||||
- **DOCS**: update contributors list with GitHub links and add new contributor.
|
||||
- **DOCS**(binding,docs): clarify `.singleton()` with `.toInstance()` behavior in docs and API.
|
||||
- **DOCS**(binding,docs): explain .singleton() + parametric provider behavior.
|
||||
- **DOCS**(binding): clarify registration limitation in API doc.
|
||||
- **DOCS**(di): clarify 'toInstance' binding limitations in builder.
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v3.0.0-dev.0`](#talker_cherrypick_logger---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(talker_cherrypick_logger): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_flutter` - `v3.0.0-dev.0`](#cherrypick_flutter---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_flutter` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(cherrypick_flutter): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_generator` - `v3.0.0-dev.0`](#cherrypick_generator---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_generator` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(cherrypick_generator): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
|
||||
## 2025-09-08
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_annotations` - `v3.0.0-dev.0`](#cherrypick_annotations---v300-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_annotations` - `v3.0.0-dev.0`
|
||||
|
||||
- chore(cherrypick_annotations): sync version with cherrypick 3.0.0-dev.0
|
||||
|
||||
|
||||
## 2025-08-22
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick_annotations` - `v1.1.2-dev.2`](#cherrypick_annotations---v112-dev2)
|
||||
- [`cherrypick_generator` - `v2.0.0-dev.2`](#cherrypick_generator---v200-dev2)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_generator` - `v2.0.0-dev.2`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_annotations` - `v1.1.2-dev.2`
|
||||
|
||||
- **DOCS**(annotations): improve API documentation and usage example.
|
||||
|
||||
|
||||
## 2025-08-19
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.12`](#cherrypick---v300-dev12)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.12`](#cherrypick_flutter---v113-dev12)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.7`](#talker_cherrypick_logger---v110-dev7)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_flutter` - `v1.1.3-dev.12`
|
||||
- `talker_cherrypick_logger` - `v1.1.0-dev.7`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.12`
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
|
||||
## 2025-08-19
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.11`](#cherrypick---v300-dev11)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.11`](#cherrypick_flutter---v113-dev11)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.6`](#talker_cherrypick_logger---v110-dev6)
|
||||
|
||||
Packages with dependency updates only:
|
||||
|
||||
> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.
|
||||
|
||||
- `cherrypick_flutter` - `v1.1.3-dev.11`
|
||||
- `talker_cherrypick_logger` - `v1.1.0-dev.6`
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.11`
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
|
||||
## 2025-08-15
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.10`](#cherrypick---v300-dev10)
|
||||
- [`cherrypick_annotations` - `v1.1.2-dev.1`](#cherrypick_annotations---v112-dev1)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.10`](#cherrypick_flutter---v113-dev10)
|
||||
- [`cherrypick_generator` - `v2.0.0-dev.1`](#cherrypick_generator---v200-dev1)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.5`](#talker_cherrypick_logger---v110-dev5)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.10`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `cherrypick_annotations` - `v1.1.2-dev.1`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `cherrypick_flutter` - `v1.1.3-dev.10`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `cherrypick_generator` - `v2.0.0-dev.1`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.5`
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.4`](#talker_cherrypick_logger---v110-dev4)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.4`
|
||||
|
||||
- **DOCS**(readme): update install instructions to use pub.dev as default method and remove obsolete git example.
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.3`](#talker_cherrypick_logger---v110-dev3)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.3`
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- There are no breaking changes in this release.
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.2`](#talker_cherrypick_logger---v110-dev2)
|
||||
|
||||
---
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.2`
|
||||
|
||||
- Bump "talker_cherrypick_logger" to `1.1.0-dev.2`.
|
||||
|
||||
|
||||
## 2025-08-13
|
||||
|
||||
### Changes
|
||||
|
||||
---
|
||||
|
||||
Packages with breaking changes:
|
||||
|
||||
- [`cherrypick_generator` - `v2.0.0-dev.0`](#cherrypick_generator---v200-dev0)
|
||||
|
||||
Packages with other changes:
|
||||
|
||||
- [`cherrypick` - `v3.0.0-dev.9`](#cherrypick---v300-dev9)
|
||||
- [`cherrypick_annotations` - `v1.1.2-dev.0`](#cherrypick_annotations---v112-dev0)
|
||||
- [`cherrypick_flutter` - `v1.1.3-dev.9`](#cherrypick_flutter---v113-dev9)
|
||||
- [`talker_cherrypick_logger` - `v1.1.0-dev.0`](#talker_cherrypick_logger---v110-dev0)
|
||||
|
||||
---
|
||||
|
||||
#### `cherrypick_generator` - `v2.0.0-dev.0`
|
||||
|
||||
- **BREAKING** **DOCS**(generator): improve and unify English documentation and examples for all DI source files.
|
||||
|
||||
#### `cherrypick` - `v3.0.0-dev.9`
|
||||
|
||||
- **DOCS**(readme): add talker_cherrypick_logger to Additional Modules section.
|
||||
- **DOCS**(api): improve all DI core code documentation with English dartdoc and examples.
|
||||
|
||||
#### `cherrypick_annotations` - `v1.1.2-dev.0`
|
||||
|
||||
- **DOCS**(annotations): unify and improve English DartDoc for all DI annotations.
|
||||
|
||||
#### `cherrypick_flutter` - `v1.1.3-dev.9`
|
||||
|
||||
- **DOCS**(provider): add detailed English API documentation for CherryPickProvider Flutter integration.
|
||||
|
||||
#### `talker_cherrypick_logger` - `v1.1.0-dev.0`
|
||||
|
||||
- **FEAT**(logging): add talker_dio_logger and talker_bloc_logger integration, improve cherrypick logger structure, add UI log screen for DI and network/bloc debug.
|
||||
- **DOCS**: add full English documentation and usage guide to README.md.
|
||||
- **DOCS**: add detailed English documentation and usage examples for TalkerCherryPickObserver.
|
||||
|
||||
|
||||
## 2025-08-12
|
||||
|
||||
### Changes
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
# Contributors
|
||||
|
||||
- Sergey Penkovsky <sergey.penkovsky@gmail.com>
|
||||
- Klim Yaroshevich <yarashevich_kv@st.by>
|
||||
- [Sergey Penkovsky](https://github.com/pese-git)
|
||||
- [Klim Yaroshevich](https://github.com/KlimYarosh)
|
||||
- [Alexey Popkov](https://github.com/AlexeyYuPopkov)
|
||||
@@ -1,3 +1,8 @@
|
||||
[](https://github.com/pese-git/cherrypick/actions/workflows/pipeline.yml)
|
||||
[](https://app.netlify.com/projects/cherrypick-di/deploys)
|
||||
|
||||
---
|
||||
|
||||
# CherryPick Workspace
|
||||
|
||||
CherryPick Workspace is a modular, open-source dependency injection ecosystem for Dart and Flutter, designed to offer lightweight, flexible, and scalable DI suitable for both backend and frontend (Flutter) development. This monorepo contains the main DI runtime library, annotation helpers, code generation for modular bindings, and seamless Flutter integration.
|
||||
@@ -142,4 +147,4 @@ Please see:
|
||||
|
||||
---
|
||||
|
||||
**Happy Cherry Picking! 🍒**
|
||||
**Happy Cherry Picking! 🍒**
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
# Comparative DI Benchmark Report: cherrypick vs get_it vs riverpod
|
||||
# Comparative DI Benchmark Report: cherrypick vs get_it vs riverpod vs kiwi
|
||||
|
||||
## Benchmark Parameters
|
||||
|
||||
- chainCount = 100
|
||||
- nestingDepth = 100
|
||||
- repeat = 5
|
||||
- warmup = 2
|
||||
|
||||
## Benchmark Scenarios
|
||||
|
||||
@@ -11,41 +18,49 @@
|
||||
|
||||
---
|
||||
|
||||
## Comparative Table: chainCount=10, nestingDepth=10 (Mean, PeakRSS)
|
||||
## Comparative Table: chainCount=100, nestingDepth=100, repeat=5, warmup=2 (Mean time, µs)
|
||||
|
||||
| Scenario | cherrypick Mean (us) | cherrypick PeakRSS | get_it Mean (us) | get_it PeakRSS | riverpod Mean (us) | riverpod PeakRSS |
|
||||
|--------------------|---------------------:|-------------------:|-----------------:|---------------:|-------------------:|-----------------:|
|
||||
| RegisterSingleton | 13.00 | 273104 | 8.40 | 261872 | 9.80 | 268512 |
|
||||
| ChainSingleton | 13.80 | 271072 | 2.00 | 262000 | 33.60 | 268784 |
|
||||
| ChainFactory | 5.00 | 299216 | 4.00 | 297136 | 22.80 | 271296 |
|
||||
| AsyncChain | 28.60 | 290640 | 24.60 | 342976 | 78.20 | 285920 |
|
||||
| Named | 2.20 | 297008 | 0.20 | 449824 | 6.20 | 281136 |
|
||||
| Override | 7.00 | 297024 | 0.00 | 449824 | 30.20 | 281152 |
|
||||
| Scenario | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|-------|----------|
|
||||
| chainSingleton | 20.6 | 14.8 | 275.2 | 47.0 | 82.8 |
|
||||
| chainFactory | 90.6 | 71.6 | 357.0 | 46.2 | 79.6 |
|
||||
| register | 82.6 | 10.2 | 252.6 | 43.6 | 224.0 |
|
||||
| named | 18.4 | 9.4 | 12.2 | 10.2 | 10.8 |
|
||||
| override | 170.6 | 11.2 | 301.4 | 51.4 | 146.4 |
|
||||
| chainAsync | 493.8 | 34.0 | 5,039.0 | – | 87.2 |
|
||||
|
||||
## Maximum Load: chainCount=100, nestingDepth=100 (Mean, PeakRSS)
|
||||
|
||||
| Scenario | cherrypick Mean (us) | cherrypick PeakRSS | get_it Mean (us) | get_it PeakRSS | riverpod Mean (us) | riverpod PeakRSS |
|
||||
|--------------------|---------------------:|-------------------:|-----------------:|---------------:|-------------------:|-----------------:|
|
||||
| RegisterSingleton | 4.00 | 271072 | 1.00 | 262000 | 2.00 | 268688 |
|
||||
| ChainSingleton | 76.60 | 303312 | 2.00 | 297136 | 221.80 | 270784 |
|
||||
| ChainFactory | 80.00 | 293952 | 39.20 | 342720 | 195.80 | 308640 |
|
||||
| AsyncChain | 251.40 | 297008 | 18.20 | 450640 | 748.80 | 285968 |
|
||||
| Named | 2.20 | 297008 | 0.00 | 449824 | 1.00 | 281136 |
|
||||
| Override | 104.80 | 301632 | 2.20 | 477344 | 120.80 | 294752 |
|
||||
## Peak Memory Usage (Peak RSS, Kb)
|
||||
|
||||
| Scenario | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|--------|----------|
|
||||
| chainSingleton | 338,224 | 326,752| 301,856 | 195,520| 320,928 |
|
||||
| chainFactory | 339,040 | 335,712| 304,832 | 319,952| 318,688 |
|
||||
| register | 333,760 | 334,208| 300,368 | 327,968| 326,736 |
|
||||
| named | 241,040 | 229,632| 280,144 | 271,872| 266,352 |
|
||||
| override | 356,912 | 331,456| 329,808 | 369,104| 304,416 |
|
||||
| chainAsync | 311,616 | 434,592| 301,168 | – | 328,912 |
|
||||
|
||||
---
|
||||
|
||||
## Analysis
|
||||
|
||||
- **get_it** is the absolute leader in all scenarios, especially under deep/nested chains and async.
|
||||
- **cherrypick** is highly competitive and much faster than riverpod on any complex graph.
|
||||
- **riverpod** is only suitable for small/simple DI graphs due to major slowdowns with depth, async, or override.
|
||||
- **get_it** remains the clear leader for both speed and memory usage (lowest latency across most scenarios; excellent memory efficiency even on deep chains).
|
||||
- **kiwi** shows the lowest memory footprint in chainSingleton, but is unavailable for async chains.
|
||||
- **yx_scope** demonstrates highly stable performance for both sync and async chains, often at the cost of higher memory usage, especially in the register/override scenarios.
|
||||
- **cherrypick** comfortably beats riverpod, but is outperformed by get_it/kiwi/yx_scope, especially on async and heavy nested chains. It uses a bit less memory than yx_scope and kiwi, but can spike in memory/latency for override.
|
||||
- **riverpod** is unsuitable for deep or async chains—latency and memory usage grow rapidly.
|
||||
- **Peak memory (RSS):** usually around 320–340 MB for all DI; riverpod/kiwi occasionally drops below 300MB. named/factory scenarios use much less.
|
||||
- **Stability:** yx_scope and get_it have the lowest latency spikes; cherrypick can show peaks on override/async; riverpod is least stable on async (stddev/mean much worse).
|
||||
|
||||
### Recommendations
|
||||
- Use **get_it** for performance-critical and deeply nested graphs.
|
||||
- Use **cherrypick** for scalable/testable apps if a small speed loss is acceptable.
|
||||
- Use **riverpod** only if you rely on Flutter integration and your DI chains are simple.
|
||||
|
||||
- **get_it** (and often **kiwi**, if you don't need async): best for ultra-fast deep graphs and minimum peak memory.
|
||||
- **yx_scope**: best blend of performance and async stability; perfect for production mixed DI.
|
||||
- **cherrypick**: great for modular/testable architectures, unless absolute peak is needed; lower memory than yx_scope in some scenarios.
|
||||
- **riverpod**: only for shallow DI or UI wiring in Flutter.
|
||||
|
||||
---
|
||||
|
||||
_Last updated: August 8, 2025._
|
||||
_Last updated: August 20, 2025._
|
||||
_Please see scenario source for details._
|
||||
|
||||
@@ -1,51 +1,63 @@
|
||||
# Сравнительный отчет DI-бенчмарка: cherrypick vs get_it vs riverpod
|
||||
# Сравнительный отчет DI-бенчмарка: cherrypick vs get_it vs riverpod vs kiwi
|
||||
|
||||
## Параметры запуска:
|
||||
- chainCount = 100
|
||||
- nestingDepth = 100
|
||||
- repeat = 5
|
||||
- warmup = 2
|
||||
|
||||
## Описание сценариев
|
||||
|
||||
1. **RegisterSingleton** — регистрация и получение объекта-синглтона (базовая скорость DI).
|
||||
1. **RegisterSingleton** — регистрация и получение singleton (базовая скорость DI).
|
||||
2. **ChainSingleton** — цепочка зависимостей A → B → ... → N (singleton). Глубокий singleton-резолвинг.
|
||||
3. **ChainFactory** — все элементы цепочки — фабрики. Stateless построение графа.
|
||||
4. **AsyncChain** — асинхронная цепочка (async factory). Тестирует async/await граф.
|
||||
3. **ChainFactory** — все элементы цепочки — factory. Stateless построение графа.
|
||||
4. **AsyncChain** — асинхронная цепочка (async factory). Тест async/await графа.
|
||||
5. **Named** — регистрация двух биндингов с именами, разрешение по имени.
|
||||
6. **Override** — регистрация биндинга/цепочки в дочернем scope. Проверка override/scoping.
|
||||
6. **Override** — регистрация биндинга/цепочки в дочернем scope.
|
||||
|
||||
---
|
||||
|
||||
## Сводная таблица: chainCount=10, nestingDepth=10 (Mean, PeakRSS)
|
||||
## Сравнительная таблица: chainCount=100, nestingDepth=100, repeat=5, warmup=2 (среднее время, мкс)
|
||||
|
||||
| Сценарий | cherrypick Mean (мкс) | cherrypick PeakRSS | get_it Mean (мкс) | get_it PeakRSS | riverpod Mean (мкс) | riverpod PeakRSS |
|
||||
|--------------------|----------------------:|-------------------:|------------------:|---------------:|--------------------:|-----------------:|
|
||||
| RegisterSingleton | 13.00 | 273104 | 8.40 | 261872 | 9.80 | 268512 |
|
||||
| ChainSingleton | 13.80 | 271072 | 2.00 | 262000 | 33.60 | 268784 |
|
||||
| ChainFactory | 5.00 | 299216 | 4.00 | 297136 | 22.80 | 271296 |
|
||||
| AsyncChain | 28.60 | 290640 | 24.60 | 342976 | 78.20 | 285920 |
|
||||
| Named | 2.20 | 297008 | 0.20 | 449824 | 6.20 | 281136 |
|
||||
| Override | 7.00 | 297024 | 0.00 | 449824 | 30.20 | 281152 |
|
||||
| Сценарий | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|-------|----------|
|
||||
| chainSingleton | 20.6 | 14.8 | 275.2 | 47.0 | 82.8 |
|
||||
| chainFactory | 90.6 | 71.6 | 357.0 | 46.2 | 79.6 |
|
||||
| register | 82.6 | 10.2 | 252.6 | 43.6 | 224.0 |
|
||||
| named | 18.4 | 9.4 | 12.2 | 10.2 | 10.8 |
|
||||
| override | 170.6 | 11.2 | 301.4 | 51.4 | 146.4 |
|
||||
| chainAsync | 493.8 | 34.0 | 5,039.0 | – | 87.2 |
|
||||
|
||||
## Максимальная нагрузка: chainCount=100, nestingDepth=100 (Mean, PeakRSS)
|
||||
|
||||
| Сценарий | cherrypick Mean (мкс) | cherrypick PeakRSS | get_it Mean (мкс) | get_it PeakRSS | riverpod Mean (мкс) | riverpod PeakRSS |
|
||||
|--------------------|----------------------:|-------------------:|------------------:|---------------:|--------------------:|-----------------:|
|
||||
| RegisterSingleton | 4.00 | 271072 | 1.00 | 262000 | 2.00 | 268688 |
|
||||
| ChainSingleton | 76.60 | 303312 | 2.00 | 297136 | 221.80 | 270784 |
|
||||
| ChainFactory | 80.00 | 293952 | 39.20 | 342720 | 195.80 | 308640 |
|
||||
| AsyncChain | 251.40 | 297008 | 18.20 | 450640 | 748.80 | 285968 |
|
||||
| Named | 2.20 | 297008 | 0.00 | 449824 | 1.00 | 281136 |
|
||||
| Override | 104.80 | 301632 | 2.20 | 477344 | 120.80 | 294752 |
|
||||
## Пиковое потребление памяти (Peak RSS, Кб)
|
||||
|
||||
| Сценарий | cherrypick | get_it | riverpod | kiwi | yx_scope |
|
||||
|------------------|------------|--------|----------|--------|----------|
|
||||
| chainSingleton | 338,224 | 326,752| 301,856 | 195,520| 320,928 |
|
||||
| chainFactory | 339,040 | 335,712| 304,832 | 319,952| 318,688 |
|
||||
| register | 333,760 | 334,208| 300,368 | 327,968| 326,736 |
|
||||
| named | 241,040 | 229,632| 280,144 | 271,872| 266,352 |
|
||||
| override | 356,912 | 331,456| 329,808 | 369,104| 304,416 |
|
||||
| chainAsync | 311,616 | 434,592| 301,168 | – | 328,912 |
|
||||
|
||||
---
|
||||
|
||||
## Краткий анализ и рекомендации
|
||||
|
||||
- **get_it** всегда лидер, особенно на глубине/асинхронных графах.
|
||||
- **cherrypick** заметно быстрее riverpod на сложных сценариях, опережая его в разы.
|
||||
- **riverpod** подходит только для простых/небольших графов — при росте глубины или async/override резко проигрывает по скорости.
|
||||
- **get_it** — абсолютный лидер по скорости и памяти на всех графах (минимальная задержка, небольшой peak RSS в любых цепочках).
|
||||
- **kiwi** — минимальное потребление памяти в chainSingleton/Factory, но не для асинхронности.
|
||||
- **yx_scope** — очень ровная производительность даже на сложных async/sync-цепях, иногда с пиком в памяти на override/register, но задержки всегда минимальны.
|
||||
- **cherrypick** — стабильнее riverpod, но ощутимо уступает top-3 по латентности на длинных/async-графах; по памяти лучше yx_scope для override/named.
|
||||
- **riverpod** — непригоден для глубоких/async-графов: память и время растут очень сильно.
|
||||
- **Пиковое потребление памяти**: большинство DI держится в районе 320–340 Мб (большие цепи), на мелких named/factory — крайне мало.
|
||||
- **Стабильность**: yx_scope и get_it показывают наименьшие скачки времени; у cherrypick иногда всплески на override/async, у riverpod — на async графе stddev почти равен mean!
|
||||
|
||||
### Рекомендации
|
||||
- Используйте **get_it** для критичных к скорости приложений/сложных графов зависимостей.
|
||||
- Выбирайте **cherrypick** для масштабируемых, тестируемых архитектур, если микросекундная разница не критична.
|
||||
- **riverpod** уместен только для реактивного UI или простых графов DI.
|
||||
- Используйте **get_it** (или **kiwi**, если не нужен async) для максимальной производительности и минимального пикового использования памяти.
|
||||
- **yx_scope** — идеально для production-графов с миксом sync/async.
|
||||
- **cherrypick** — хорошо для модульных и тестируемых приложений, если не требуется абсолютная “микросекундная” производительность.
|
||||
- **riverpod** — только если граф плоский или нужно DI только для UI во Flutter.
|
||||
|
||||
---
|
||||
|
||||
_Обновлено: 8 августа 2025_
|
||||
_Обновлено: 20 августа 2025._
|
||||
|
||||
@@ -2,4 +2,4 @@ import 'package:benchmark_di/cli/benchmark_cli.dart';
|
||||
|
||||
Future<void> main(List<String> args) async {
|
||||
await BenchmarkCliRunner().run(args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,8 @@ class UniversalChainBenchmark<TContainer> extends BenchmarkBase {
|
||||
_childDi!.resolve<UniversalService>();
|
||||
break;
|
||||
case UniversalScenario.asyncChain:
|
||||
throw UnsupportedError('asyncChain supported only in UniversalChainAsyncBenchmark');
|
||||
throw UnsupportedError(
|
||||
'asyncChain supported only in UniversalChainAsyncBenchmark');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:benchmark_di/cli/report/markdown_report.dart';
|
||||
import 'package:benchmark_di/di_adapters/yx_scope_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/yx_scope_universal_container.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
@@ -16,6 +18,8 @@ import 'package:benchmark_di/benchmarks/universal_chain_async_benchmark.dart';
|
||||
import 'package:benchmark_di/di_adapters/cherrypick_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/get_it_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/riverpod_adapter.dart';
|
||||
import 'package:benchmark_di/di_adapters/kiwi_adapter.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
|
||||
/// Command-line interface (CLI) runner for benchmarks.
|
||||
///
|
||||
@@ -36,8 +40,11 @@ class BenchmarkCliRunner {
|
||||
if (config.di == 'getit') {
|
||||
final di = GetItAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync = UniversalChainAsyncBenchmark<GetIt>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode,
|
||||
final benchAsync = UniversalChainAsyncBenchmark<GetIt>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
@@ -45,8 +52,41 @@ class BenchmarkCliRunner {
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<GetIt>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode, scenario: scenario,
|
||||
final benchSync = UniversalChainBenchmark<GetIt>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
}
|
||||
} else if (config.di == 'kiwi') {
|
||||
final di = KiwiAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
// UnsupportedError будет выброшен адаптером, но если дойдёт — вызывать async benchmark
|
||||
final benchAsync = UniversalChainAsyncBenchmark<KiwiContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<KiwiContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
@@ -57,8 +97,12 @@ class BenchmarkCliRunner {
|
||||
} else if (config.di == 'riverpod') {
|
||||
final di = RiverpodAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync = UniversalChainAsyncBenchmark<Map<String, rp.ProviderBase<Object?>>>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode,
|
||||
final benchAsync = UniversalChainAsyncBenchmark<
|
||||
Map<String, rp.ProviderBase<Object?>>>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
@@ -66,8 +110,43 @@ class BenchmarkCliRunner {
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<Map<String, rp.ProviderBase<Object?>>>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode, scenario: scenario,
|
||||
final benchSync = UniversalChainBenchmark<
|
||||
Map<String, rp.ProviderBase<Object?>>>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
}
|
||||
} else if (config.di == 'yx_scope') {
|
||||
final di = YxScopeAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync =
|
||||
UniversalChainAsyncBenchmark<UniversalYxScopeContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
warmups: config.warmups,
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync =
|
||||
UniversalChainBenchmark<UniversalYxScopeContainer>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
@@ -78,8 +157,11 @@ class BenchmarkCliRunner {
|
||||
} else {
|
||||
final di = CherrypickDIAdapter();
|
||||
if (scenario == UniversalScenario.asyncChain) {
|
||||
final benchAsync = UniversalChainAsyncBenchmark<Scope>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode,
|
||||
final benchAsync = UniversalChainAsyncBenchmark<Scope>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runAsync(
|
||||
benchmark: benchAsync,
|
||||
@@ -87,8 +169,12 @@ class BenchmarkCliRunner {
|
||||
repeats: config.repeats,
|
||||
);
|
||||
} else {
|
||||
final benchSync = UniversalChainBenchmark<Scope>(di,
|
||||
chainCount: c, nestingDepth: d, mode: mode, scenario: scenario,
|
||||
final benchSync = UniversalChainBenchmark<Scope>(
|
||||
di,
|
||||
chainCount: c,
|
||||
nestingDepth: d,
|
||||
mode: mode,
|
||||
scenario: scenario,
|
||||
);
|
||||
benchResult = await BenchmarkRunner.runSync(
|
||||
benchmark: benchSync,
|
||||
@@ -103,7 +189,11 @@ class BenchmarkCliRunner {
|
||||
var median = timings[timings.length ~/ 2];
|
||||
var minVal = timings.first;
|
||||
var maxVal = timings.last;
|
||||
var stddev = timings.isEmpty ? 0 : sqrt(timings.map((x) => pow(x - mean, 2)).reduce((a, b) => a + b) / timings.length);
|
||||
var stddev = timings.isEmpty
|
||||
? 0
|
||||
: sqrt(
|
||||
timings.map((x) => pow(x - mean, 2)).reduce((a, b) => a + b) /
|
||||
timings.length);
|
||||
results.add({
|
||||
'benchmark': 'Universal_$bench',
|
||||
'chainCount': c,
|
||||
@@ -128,6 +218,7 @@ class BenchmarkCliRunner {
|
||||
'json': JsonReport(),
|
||||
'markdown': MarkdownReport(),
|
||||
};
|
||||
print(reportGenerators[config.format]?.render(results) ?? PrettyReport().render(results));
|
||||
print(reportGenerators[config.format]?.render(results) ??
|
||||
PrettyReport().render(results));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,14 +8,19 @@ import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
enum UniversalBenchmark {
|
||||
/// Simple singleton registration benchmark
|
||||
registerSingleton,
|
||||
|
||||
/// Chain of singleton dependencies
|
||||
chainSingleton,
|
||||
|
||||
/// Chain using factories
|
||||
chainFactory,
|
||||
|
||||
/// Async chain resolution
|
||||
chainAsync,
|
||||
|
||||
/// Named registration benchmark
|
||||
named,
|
||||
|
||||
/// Override/child-scope benchmark
|
||||
override,
|
||||
}
|
||||
@@ -65,23 +70,32 @@ T parseEnum<T>(String value, List<T> values, T defaultValue) {
|
||||
}
|
||||
|
||||
/// Parses comma-separated integer list from [s].
|
||||
List<int> parseIntList(String s) =>
|
||||
s.split(',').map((e) => int.tryParse(e.trim()) ?? 0).where((x) => x > 0).toList();
|
||||
List<int> parseIntList(String s) => s
|
||||
.split(',')
|
||||
.map((e) => int.tryParse(e.trim()) ?? 0)
|
||||
.where((x) => x > 0)
|
||||
.toList();
|
||||
|
||||
/// CLI config describing what and how to benchmark.
|
||||
class BenchmarkCliConfig {
|
||||
/// Benchmarks enabled to run (scenarios).
|
||||
final List<UniversalBenchmark> benchesToRun;
|
||||
|
||||
/// List of chain counts (parallel, per test).
|
||||
final List<int> chainCounts;
|
||||
|
||||
/// List of nesting depths (max chain length, per test).
|
||||
final List<int> nestDepths;
|
||||
|
||||
/// How many times to repeat each trial.
|
||||
final int repeats;
|
||||
|
||||
/// How many times to warm-up before measuring.
|
||||
final int warmups;
|
||||
|
||||
/// Output report format.
|
||||
final String format;
|
||||
|
||||
/// Name of DI implementation ("cherrypick" or "getit")
|
||||
final String di;
|
||||
BenchmarkCliConfig({
|
||||
@@ -105,7 +119,9 @@ BenchmarkCliConfig parseBenchmarkCli(List<String> args) {
|
||||
..addOption('repeat', abbr: 'r', defaultsTo: '2')
|
||||
..addOption('warmup', abbr: 'w', defaultsTo: '1')
|
||||
..addOption('format', abbr: 'f', defaultsTo: 'pretty')
|
||||
..addOption('di', defaultsTo: 'cherrypick', help: 'DI implementation: cherrypick, getit or riverpod')
|
||||
..addOption('di',
|
||||
defaultsTo: 'cherrypick',
|
||||
help: 'DI implementation: cherrypick, getit or riverpod')
|
||||
..addFlag('help', abbr: 'h', negatable: false, help: 'Show help');
|
||||
final result = parser.parse(args);
|
||||
if (result['help'] == true) {
|
||||
@@ -127,4 +143,4 @@ BenchmarkCliConfig parseBenchmarkCli(List<String> args) {
|
||||
format: result['format'] as String,
|
||||
di: result['di'] as String? ?? 'cherrypick',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,20 +5,32 @@ class CsvReport extends ReportGenerator {
|
||||
/// List of all keys/columns to include in the CSV output.
|
||||
@override
|
||||
final List<String> keys = [
|
||||
'benchmark','chainCount','nestingDepth','mean_us','median_us','stddev_us',
|
||||
'min_us','max_us','trials','timings_us','memory_diff_kb','delta_peak_kb','peak_rss_kb'
|
||||
'benchmark',
|
||||
'chainCount',
|
||||
'nestingDepth',
|
||||
'mean_us',
|
||||
'median_us',
|
||||
'stddev_us',
|
||||
'min_us',
|
||||
'max_us',
|
||||
'trials',
|
||||
'timings_us',
|
||||
'memory_diff_kb',
|
||||
'delta_peak_kb',
|
||||
'peak_rss_kb'
|
||||
];
|
||||
|
||||
/// Renders rows as a CSV table string.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
final header = keys.join(',');
|
||||
final lines = rows.map((r) =>
|
||||
keys.map((k) {
|
||||
final v = r[k];
|
||||
if (v is List) return '"${v.join(';')}"';
|
||||
return (v ?? '').toString();
|
||||
}).join(',')
|
||||
).toList();
|
||||
final lines = rows
|
||||
.map((r) => keys.map((k) {
|
||||
final v = r[k];
|
||||
if (v is List) return '"${v.join(';')}"';
|
||||
return (v ?? '').toString();
|
||||
}).join(','))
|
||||
.toList();
|
||||
return ([header] + lines).join('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,9 +5,10 @@ class JsonReport extends ReportGenerator {
|
||||
/// No specific keys; outputs all fields in raw map.
|
||||
@override
|
||||
List<String> get keys => [];
|
||||
|
||||
/// Renders all result rows as a pretty-printed JSON array.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
return '[\n${rows.map((r) => ' $r').join(',\n')}\n]';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,25 +7,46 @@ class MarkdownReport extends ReportGenerator {
|
||||
/// List of columns (keys) to show in the Markdown table.
|
||||
@override
|
||||
final List<String> keys = [
|
||||
'benchmark','chainCount','nestingDepth','mean_us','median_us','stddev_us',
|
||||
'min_us','max_us','trials','memory_diff_kb','delta_peak_kb','peak_rss_kb'
|
||||
'benchmark',
|
||||
'chainCount',
|
||||
'nestingDepth',
|
||||
'mean_us',
|
||||
'median_us',
|
||||
'stddev_us',
|
||||
'min_us',
|
||||
'max_us',
|
||||
'trials',
|
||||
'memory_diff_kb',
|
||||
'delta_peak_kb',
|
||||
'peak_rss_kb'
|
||||
];
|
||||
|
||||
/// Friendly display names for each benchmark type.
|
||||
static const nameMap = {
|
||||
'Universal_UniversalBenchmark.registerSingleton':'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton':'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory':'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync':'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named':'Named',
|
||||
'Universal_UniversalBenchmark.override':'Override',
|
||||
'Universal_UniversalBenchmark.registerSingleton': 'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton': 'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory': 'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync': 'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named': 'Named',
|
||||
'Universal_UniversalBenchmark.override': 'Override',
|
||||
};
|
||||
|
||||
/// Renders all results as a formatted Markdown table with aligned columns and a legend.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
final headers = [
|
||||
'Benchmark', 'Chain Count', 'Depth', 'Mean (us)', 'Median', 'Stddev', 'Min', 'Max', 'N', 'ΔRSS(KB)', 'ΔPeak(KB)', 'PeakRSS(KB)'
|
||||
'Benchmark',
|
||||
'Chain Count',
|
||||
'Depth',
|
||||
'Mean (us)',
|
||||
'Median',
|
||||
'Stddev',
|
||||
'Min',
|
||||
'Max',
|
||||
'N',
|
||||
'ΔRSS(KB)',
|
||||
'ΔPeak(KB)',
|
||||
'PeakRSS(KB)'
|
||||
];
|
||||
final dataRows = rows.map((r) {
|
||||
final readableName = nameMap[r['benchmark']] ?? r['benchmark'];
|
||||
@@ -73,6 +94,6 @@ class MarkdownReport extends ReportGenerator {
|
||||
> `PeakRSS(KB)` – Max observed RSS memory (KB)
|
||||
''';
|
||||
|
||||
return '$legend\n\n${([headerLine, divider] + lines).join('\n')}' ;
|
||||
return '$legend\n\n${([headerLine, divider] + lines).join('\n')}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,25 +7,46 @@ class PrettyReport extends ReportGenerator {
|
||||
/// List of columns to output in the pretty report.
|
||||
@override
|
||||
final List<String> keys = [
|
||||
'benchmark','chainCount','nestingDepth','mean_us','median_us','stddev_us',
|
||||
'min_us','max_us','trials','memory_diff_kb','delta_peak_kb','peak_rss_kb'
|
||||
'benchmark',
|
||||
'chainCount',
|
||||
'nestingDepth',
|
||||
'mean_us',
|
||||
'median_us',
|
||||
'stddev_us',
|
||||
'min_us',
|
||||
'max_us',
|
||||
'trials',
|
||||
'memory_diff_kb',
|
||||
'delta_peak_kb',
|
||||
'peak_rss_kb'
|
||||
];
|
||||
|
||||
/// Mappings from internal benchmark IDs to display names.
|
||||
static const nameMap = {
|
||||
'Universal_UniversalBenchmark.registerSingleton': 'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton': 'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory': 'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync': 'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named': 'Named',
|
||||
'Universal_UniversalBenchmark.override': 'Override',
|
||||
'Universal_UniversalBenchmark.registerSingleton': 'RegisterSingleton',
|
||||
'Universal_UniversalBenchmark.chainSingleton': 'ChainSingleton',
|
||||
'Universal_UniversalBenchmark.chainFactory': 'ChainFactory',
|
||||
'Universal_UniversalBenchmark.chainAsync': 'AsyncChain',
|
||||
'Universal_UniversalBenchmark.named': 'Named',
|
||||
'Universal_UniversalBenchmark.override': 'Override',
|
||||
};
|
||||
|
||||
/// Renders the results as a header + tab-separated value table.
|
||||
@override
|
||||
String render(List<Map<String, dynamic>> rows) {
|
||||
final headers = [
|
||||
'Benchmark', 'Chain Count', 'Depth', 'Mean (us)', 'Median', 'Stddev', 'Min', 'Max', 'N', 'ΔRSS(KB)', 'ΔPeak(KB)', 'PeakRSS(KB)'
|
||||
'Benchmark',
|
||||
'Chain Count',
|
||||
'Depth',
|
||||
'Mean (us)',
|
||||
'Median',
|
||||
'Stddev',
|
||||
'Min',
|
||||
'Max',
|
||||
'N',
|
||||
'ΔRSS(KB)',
|
||||
'ΔPeak(KB)',
|
||||
'PeakRSS(KB)'
|
||||
];
|
||||
final header = headers.join('\t');
|
||||
final lines = rows.map((r) {
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
abstract class ReportGenerator {
|
||||
/// Renders the given [results] as a formatted string (table, markdown, csv, etc).
|
||||
String render(List<Map<String, dynamic>> results);
|
||||
|
||||
/// List of output columns/keys included in the export (or [] for auto/all).
|
||||
List<String> get keys;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,10 +7,13 @@ import 'package:benchmark_di/benchmarks/universal_chain_async_benchmark.dart';
|
||||
class BenchmarkResult {
|
||||
/// List of timings for each run (in microseconds).
|
||||
final List<num> timings;
|
||||
|
||||
/// Difference in memory (RSS, in KB) after running.
|
||||
final int memoryDiffKb;
|
||||
|
||||
/// Difference between peak RSS and initial RSS (in KB).
|
||||
final int deltaPeakKb;
|
||||
|
||||
/// Peak RSS memory observed (in KB).
|
||||
final int peakRssKb;
|
||||
BenchmarkResult({
|
||||
@@ -19,6 +22,7 @@ class BenchmarkResult {
|
||||
required this.deltaPeakKb,
|
||||
required this.peakRssKb,
|
||||
});
|
||||
|
||||
/// Computes a BenchmarkResult instance from run timings and memory data.
|
||||
factory BenchmarkResult.collect({
|
||||
required List<num> timings,
|
||||
@@ -64,7 +68,8 @@ class BenchmarkRunner {
|
||||
rssValues.add(ProcessInfo.currentRss);
|
||||
benchmark.teardown();
|
||||
}
|
||||
return BenchmarkResult.collect(timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
return BenchmarkResult.collect(
|
||||
timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
}
|
||||
|
||||
/// Runs an asynchronous benchmark ([UniversalChainAsyncBenchmark]) for a given number of [warmups] and [repeats].
|
||||
@@ -91,6 +96,7 @@ class BenchmarkRunner {
|
||||
rssValues.add(ProcessInfo.currentRss);
|
||||
await benchmark.teardown();
|
||||
}
|
||||
return BenchmarkResult.collect(timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
return BenchmarkResult.collect(
|
||||
timings: timings, rssValues: rssValues, memBefore: memBefore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import 'package:benchmark_di/scenarios/universal_service.dart';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'di_adapter.dart';
|
||||
|
||||
|
||||
/// Test module that generates a chain of service bindings for benchmarking.
|
||||
///
|
||||
/// Configurable by chain count, nesting depth, binding mode, and scenario
|
||||
@@ -12,10 +11,13 @@ import 'di_adapter.dart';
|
||||
class UniversalChainModule extends Module {
|
||||
/// Number of chains to create.
|
||||
final int chainCount;
|
||||
|
||||
/// Depth of each chain.
|
||||
final int nestingDepth;
|
||||
|
||||
/// How modules are registered (factory/singleton/async).
|
||||
final UniversalBindingMode bindingMode;
|
||||
|
||||
/// Which di scenario to generate (chained, named, etc).
|
||||
final UniversalScenario scenario;
|
||||
|
||||
@@ -38,17 +40,18 @@ class UniversalChainModule extends Module {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
bind<UniversalService>()
|
||||
.toProvideAsync(() async {
|
||||
final prev = level > 1
|
||||
? await currentScope.resolveAsync<UniversalService>(named: prevDepName)
|
||||
: null;
|
||||
return UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: prev,
|
||||
);
|
||||
})
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
.toProvideAsync(() async {
|
||||
final prev = level > 1
|
||||
? await currentScope.resolveAsync<UniversalService>(
|
||||
named: prevDepName)
|
||||
: null;
|
||||
return UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: prev,
|
||||
);
|
||||
})
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
}
|
||||
}
|
||||
return;
|
||||
@@ -58,13 +61,18 @@ class UniversalChainModule extends Module {
|
||||
case UniversalScenario.register:
|
||||
// Simple singleton registration.
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(value: 'reg', dependency: null))
|
||||
.toProvide(
|
||||
() => UniversalServiceImpl(value: 'reg', dependency: null))
|
||||
.singleton();
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
// Named factory registration for two distinct objects.
|
||||
bind<UniversalService>().toProvide(() => UniversalServiceImpl(value: 'impl1')).withName('impl1');
|
||||
bind<UniversalService>().toProvide(() => UniversalServiceImpl(value: 'impl2')).withName('impl2');
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(value: 'impl1'))
|
||||
.withName('impl1');
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(value: 'impl2'))
|
||||
.withName('impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
// Chain of nested services, with dependency on previous level by name.
|
||||
@@ -79,7 +87,8 @@ class UniversalChainModule extends Module {
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: currentScope.tryResolve<UniversalService>(named: prevDepName),
|
||||
dependency: currentScope.tryResolve<UniversalService>(
|
||||
named: prevDepName),
|
||||
))
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
@@ -88,7 +97,8 @@ class UniversalChainModule extends Module {
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: currentScope.tryResolve<UniversalService>(named: prevDepName),
|
||||
dependency: currentScope.tryResolve<UniversalService>(
|
||||
named: prevDepName),
|
||||
))
|
||||
.withName(depName);
|
||||
break;
|
||||
@@ -96,7 +106,9 @@ class UniversalChainModule extends Module {
|
||||
bind<UniversalService>()
|
||||
.toProvideAsync(() async => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: await currentScope.resolveAsync<UniversalService>(named: prevDepName),
|
||||
dependency:
|
||||
await currentScope.resolveAsync<UniversalService>(
|
||||
named: prevDepName),
|
||||
))
|
||||
.withName(depName)
|
||||
.singleton();
|
||||
@@ -107,14 +119,16 @@ class UniversalChainModule extends Module {
|
||||
// Регистрация алиаса без имени (на последний элемент цепочки)
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.toProvide(
|
||||
() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.singleton();
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
// handled at benchmark level, но алиас нужен прямо в этом scope!
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
bind<UniversalService>()
|
||||
.toProvide(() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.toProvide(
|
||||
() => currentScope.resolve<UniversalService>(named: depName))
|
||||
.singleton();
|
||||
break;
|
||||
case UniversalScenario.asyncChain:
|
||||
@@ -124,7 +138,6 @@ class UniversalChainModule extends Module {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class CherrypickDIAdapter extends DIAdapter<Scope> {
|
||||
Scope? _scope;
|
||||
final bool _isSubScope;
|
||||
@@ -158,7 +171,8 @@ class CherrypickDIAdapter extends DIAdapter<Scope> {
|
||||
]);
|
||||
};
|
||||
}
|
||||
throw UnsupportedError('Scenario $scenario not supported by CherrypickDIAdapter');
|
||||
throw UnsupportedError(
|
||||
'Scenario $scenario not supported by CherrypickDIAdapter');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -170,9 +184,9 @@ class CherrypickDIAdapter extends DIAdapter<Scope> {
|
||||
_scope!.resolveAsync<T>(named: named);
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
Future<void> teardown() async {
|
||||
if (!_isSubScope) {
|
||||
CherryPick.closeRootScope();
|
||||
await CherryPick.closeRootScope();
|
||||
_scope = null;
|
||||
}
|
||||
// SubScope teardown не требуется
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:benchmark_di/scenarios/universal_binding_mode.dart';
|
||||
|
||||
/// Универсальная абстракция для DI-адаптера с унифицированной функцией регистрации.
|
||||
/// Теперь для каждого адаптера задаём строгий generic тип контейнера.
|
||||
typedef Registration<TContainer> = void Function(TContainer);
|
||||
|
||||
@@ -80,9 +80,11 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
getIt.registerSingletonAsync<UniversalService>(
|
||||
() async {
|
||||
final prev = level > 1
|
||||
? await getIt.getAsync<UniversalService>(instanceName: prevDepName)
|
||||
? await getIt.getAsync<UniversalService>(
|
||||
instanceName: prevDepName)
|
||||
: null;
|
||||
return UniversalServiceImpl(value: depName, dependency: prev);
|
||||
return UniversalServiceImpl(
|
||||
value: depName, dependency: prev);
|
||||
},
|
||||
instanceName: depName,
|
||||
);
|
||||
@@ -90,11 +92,16 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
}
|
||||
break;
|
||||
case UniversalScenario.register:
|
||||
getIt.registerSingleton<UniversalService>(UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
getIt.registerSingleton<UniversalService>(
|
||||
UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
getIt.registerFactory<UniversalService>(() => UniversalServiceImpl(value: 'impl1'), instanceName: 'impl1');
|
||||
getIt.registerFactory<UniversalService>(() => UniversalServiceImpl(value: 'impl2'), instanceName: 'impl2');
|
||||
getIt.registerFactory<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl1'),
|
||||
instanceName: 'impl1');
|
||||
getIt.registerFactory<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl2'),
|
||||
instanceName: 'impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
@@ -107,8 +114,8 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? getIt<UniversalService>(instanceName: prevDepName)
|
||||
: null,
|
||||
? getIt<UniversalService>(instanceName: prevDepName)
|
||||
: null,
|
||||
),
|
||||
instanceName: depName,
|
||||
);
|
||||
@@ -129,8 +136,9 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
() async => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? await getIt.getAsync<UniversalService>(instanceName: prevDepName)
|
||||
: null,
|
||||
? await getIt.getAsync<UniversalService>(
|
||||
instanceName: prevDepName)
|
||||
: null,
|
||||
),
|
||||
instanceName: depName,
|
||||
);
|
||||
@@ -143,7 +151,8 @@ class GetItAdapter extends DIAdapter<GetIt> {
|
||||
// handled at benchmark level
|
||||
break;
|
||||
}
|
||||
if (scenario == UniversalScenario.chain || scenario == UniversalScenario.override) {
|
||||
if (scenario == UniversalScenario.chain ||
|
||||
scenario == UniversalScenario.override) {
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
getIt.registerSingleton<UniversalService>(
|
||||
getIt<UniversalService>(instanceName: depName),
|
||||
|
||||
132
benchmark_di/lib/di_adapters/kiwi_adapter.dart
Normal file
132
benchmark_di/lib/di_adapters/kiwi_adapter.dart
Normal file
@@ -0,0 +1,132 @@
|
||||
import 'package:benchmark_di/scenarios/universal_binding_mode.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_service.dart';
|
||||
import 'package:kiwi/kiwi.dart';
|
||||
import 'di_adapter.dart';
|
||||
|
||||
/// DIAdapter-для KiwiContainer с поддержкой universal benchmark сценариев.
|
||||
class KiwiAdapter extends DIAdapter<KiwiContainer> {
|
||||
late KiwiContainer _container;
|
||||
// ignore: unused_field
|
||||
final bool _isSubScope;
|
||||
|
||||
KiwiAdapter({KiwiContainer? container, bool isSubScope = false})
|
||||
: _isSubScope = isSubScope {
|
||||
_container = container ?? KiwiContainer();
|
||||
}
|
||||
|
||||
@override
|
||||
void setupDependencies(void Function(KiwiContainer container) registration) {
|
||||
registration(_container);
|
||||
}
|
||||
|
||||
@override
|
||||
Registration<KiwiContainer> universalRegistration<S extends Enum>({
|
||||
required S scenario,
|
||||
required int chainCount,
|
||||
required int nestingDepth,
|
||||
required UniversalBindingMode bindingMode,
|
||||
}) {
|
||||
if (scenario is UniversalScenario) {
|
||||
if (scenario == UniversalScenario.asyncChain ||
|
||||
bindingMode == UniversalBindingMode.asyncStrategy) {
|
||||
throw UnsupportedError(
|
||||
'Kiwi does not support async dependencies or async binding scenarios.');
|
||||
}
|
||||
return (container) {
|
||||
switch (scenario) {
|
||||
case UniversalScenario.asyncChain:
|
||||
break;
|
||||
case UniversalScenario.register:
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => UniversalServiceImpl(value: 'reg', dependency: null),
|
||||
);
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
container.registerFactory<UniversalService>(
|
||||
(c) => UniversalServiceImpl(value: 'impl1'),
|
||||
name: 'impl1');
|
||||
container.registerFactory<UniversalService>(
|
||||
(c) => UniversalServiceImpl(value: 'impl2'),
|
||||
name: 'impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
switch (bindingMode) {
|
||||
case UniversalBindingMode.singletonStrategy:
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? c.resolve<UniversalService>(prevDepName)
|
||||
: null),
|
||||
name: depName);
|
||||
break;
|
||||
case UniversalBindingMode.factoryStrategy:
|
||||
container.registerFactory<UniversalService>(
|
||||
(c) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? c.resolve<UniversalService>(prevDepName)
|
||||
: null),
|
||||
name: depName);
|
||||
break;
|
||||
case UniversalBindingMode.asyncStrategy:
|
||||
// Не поддерживается
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => c.resolve<UniversalService>(depName));
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
container.registerSingleton<UniversalService>(
|
||||
(c) => c.resolve<UniversalService>(depName));
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
throw UnsupportedError('Scenario $scenario not supported by KiwiAdapter');
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T extends Object>({String? named}) {
|
||||
// Для asyncChain нужен resolve<Future<T>>
|
||||
if (T.toString().startsWith('Future<')) {
|
||||
return _container.resolve<T>(named);
|
||||
} else {
|
||||
return _container.resolve<T>(named);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T extends Object>({String? named}) async {
|
||||
if (T.toString().startsWith('Future<')) {
|
||||
// resolve<Future<T>>, unwrap result
|
||||
return Future.value(_container.resolve<T>(named));
|
||||
} else {
|
||||
// Для совместимости с chain/override
|
||||
return Future.value(_container.resolve<T>(named));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
_container.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
KiwiAdapter openSubScope(String name) {
|
||||
// Возвращаем новый scoped контейнер (отдельный). Наследование не реализовано.
|
||||
return KiwiAdapter(container: KiwiContainer.scoped(), isSubScope: true);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> waitForAsyncReady() async {}
|
||||
}
|
||||
@@ -20,7 +20,9 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
_parent = parent;
|
||||
|
||||
@override
|
||||
void setupDependencies(void Function(Map<String, rp.ProviderBase<Object?>> container) registration) {
|
||||
void setupDependencies(
|
||||
void Function(Map<String, rp.ProviderBase<Object?>> container)
|
||||
registration) {
|
||||
_container ??= _parent == null
|
||||
? rp.ProviderContainer()
|
||||
: rp.ProviderContainer(parent: _parent);
|
||||
@@ -76,7 +78,8 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
}
|
||||
|
||||
@override
|
||||
Registration<Map<String, rp.ProviderBase<Object?>>> universalRegistration<S extends Enum>({
|
||||
Registration<Map<String, rp.ProviderBase<Object?>>>
|
||||
universalRegistration<S extends Enum>({
|
||||
required S scenario,
|
||||
required int chainCount,
|
||||
required int nestingDepth,
|
||||
@@ -86,25 +89,34 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
return (providers) {
|
||||
switch (scenario) {
|
||||
case UniversalScenario.register:
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>(
|
||||
(ref) => UniversalServiceImpl(value: 'reg', dependency: null));
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
providers['impl1'] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(value: 'impl1'));
|
||||
providers['impl2'] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(value: 'impl2'));
|
||||
providers['impl1'] = rp.Provider<UniversalService>(
|
||||
(ref) => UniversalServiceImpl(value: 'impl1'));
|
||||
providers['impl2'] = rp.Provider<UniversalService>(
|
||||
(ref) => UniversalServiceImpl(value: 'impl2'));
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
providers[depName] = rp.Provider<UniversalService>((ref) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1 ? ref.watch(providers[prevDepName] as rp.ProviderBase<UniversalService>) : null,
|
||||
));
|
||||
providers[depName] =
|
||||
rp.Provider<UniversalService>((ref) => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? ref.watch(providers[prevDepName]
|
||||
as rp.ProviderBase<UniversalService>)
|
||||
: null,
|
||||
));
|
||||
}
|
||||
}
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>((ref) => ref.watch(providers[depName] as rp.ProviderBase<UniversalService>));
|
||||
providers['UniversalService'] = rp.Provider<UniversalService>(
|
||||
(ref) => ref.watch(
|
||||
providers[depName] as rp.ProviderBase<UniversalService>));
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
// handled at benchmark level
|
||||
@@ -114,24 +126,31 @@ class RiverpodAdapter extends DIAdapter<Map<String, rp.ProviderBase<Object?>>> {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
providers[depName] = rp.FutureProvider<UniversalService>((ref) async {
|
||||
providers[depName] =
|
||||
rp.FutureProvider<UniversalService>((ref) async {
|
||||
return UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? await ref.watch((providers[prevDepName] as rp.FutureProvider<UniversalService>).future) as UniversalService?
|
||||
? await ref.watch((providers[prevDepName]
|
||||
as rp.FutureProvider<UniversalService>)
|
||||
.future) as UniversalService?
|
||||
: null,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
providers['UniversalService'] = rp.FutureProvider<UniversalService>((ref) async {
|
||||
return await ref.watch((providers[depName] as rp.FutureProvider<UniversalService>).future);
|
||||
providers['UniversalService'] =
|
||||
rp.FutureProvider<UniversalService>((ref) async {
|
||||
return await ref.watch(
|
||||
(providers[depName] as rp.FutureProvider<UniversalService>)
|
||||
.future);
|
||||
});
|
||||
break;
|
||||
}
|
||||
};
|
||||
}
|
||||
throw UnsupportedError('Scenario $scenario not supported by RiverpodAdapter');
|
||||
throw UnsupportedError(
|
||||
'Scenario $scenario not supported by RiverpodAdapter');
|
||||
}
|
||||
}
|
||||
|
||||
130
benchmark_di/lib/di_adapters/yx_scope_adapter.dart
Normal file
130
benchmark_di/lib/di_adapters/yx_scope_adapter.dart
Normal file
@@ -0,0 +1,130 @@
|
||||
// ignore_for_file: invalid_use_of_protected_member
|
||||
|
||||
import 'package:benchmark_di/di_adapters/di_adapter.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_binding_mode.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_scenario.dart';
|
||||
import 'package:benchmark_di/scenarios/universal_service.dart';
|
||||
import 'package:benchmark_di/di_adapters/yx_scope_universal_container.dart';
|
||||
|
||||
/// DIAdapter для yx_scope UniversalYxScopeContainer
|
||||
class YxScopeAdapter extends DIAdapter<UniversalYxScopeContainer> {
|
||||
late UniversalYxScopeContainer _scope;
|
||||
|
||||
@override
|
||||
void setupDependencies(
|
||||
void Function(UniversalYxScopeContainer container) registration) {
|
||||
_scope = UniversalYxScopeContainer();
|
||||
registration(_scope);
|
||||
}
|
||||
|
||||
@override
|
||||
T resolve<T extends Object>({String? named}) {
|
||||
return _scope.depFor<T>(name: named).get;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<T> resolveAsync<T extends Object>({String? named}) async {
|
||||
return resolve<T>(named: named);
|
||||
}
|
||||
|
||||
@override
|
||||
void teardown() {
|
||||
// У yx_scope нет явного dispose на ScopeContainer, но можно добавить очистку Map/Deps если потребуется
|
||||
// Ничего не делаем
|
||||
}
|
||||
|
||||
@override
|
||||
YxScopeAdapter openSubScope(String name) {
|
||||
// Для простоты всегда возвращаем новый контейнер, сабскоупы не реализованы явно
|
||||
return YxScopeAdapter();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> waitForAsyncReady() async {
|
||||
// Все зависимости синхронны
|
||||
return;
|
||||
}
|
||||
|
||||
@override
|
||||
Registration<UniversalYxScopeContainer>
|
||||
universalRegistration<S extends Enum>({
|
||||
required S scenario,
|
||||
required int chainCount,
|
||||
required int nestingDepth,
|
||||
required UniversalBindingMode bindingMode,
|
||||
}) {
|
||||
if (scenario is UniversalScenario) {
|
||||
return (scope) {
|
||||
switch (scenario) {
|
||||
case UniversalScenario.asyncChain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
final dep = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? scope.depFor<UniversalService>(name: prevDepName).get
|
||||
: null,
|
||||
),
|
||||
name: depName,
|
||||
);
|
||||
scope.register<UniversalService>(dep, name: depName);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case UniversalScenario.register:
|
||||
final dep = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'reg', dependency: null),
|
||||
);
|
||||
scope.register<UniversalService>(dep);
|
||||
break;
|
||||
case UniversalScenario.named:
|
||||
final dep1 = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl1'),
|
||||
name: 'impl1',
|
||||
);
|
||||
final dep2 = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(value: 'impl2'),
|
||||
name: 'impl2',
|
||||
);
|
||||
scope.register<UniversalService>(dep1, name: 'impl1');
|
||||
scope.register<UniversalService>(dep2, name: 'impl2');
|
||||
break;
|
||||
case UniversalScenario.chain:
|
||||
for (int chain = 1; chain <= chainCount; chain++) {
|
||||
for (int level = 1; level <= nestingDepth; level++) {
|
||||
final prevDepName = '${chain}_${level - 1}';
|
||||
final depName = '${chain}_$level';
|
||||
final dep = scope.dep<UniversalService>(
|
||||
() => UniversalServiceImpl(
|
||||
value: depName,
|
||||
dependency: level > 1
|
||||
? scope.depFor<UniversalService>(name: prevDepName).get
|
||||
: null,
|
||||
),
|
||||
name: depName,
|
||||
);
|
||||
scope.register<UniversalService>(dep, name: depName);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case UniversalScenario.override:
|
||||
// handled at benchmark level
|
||||
break;
|
||||
}
|
||||
if (scenario == UniversalScenario.chain ||
|
||||
scenario == UniversalScenario.override) {
|
||||
final depName = '${chainCount}_$nestingDepth';
|
||||
final lastDep = scope.dep<UniversalService>(
|
||||
() => scope.depFor<UniversalService>(name: depName).get,
|
||||
);
|
||||
scope.register<UniversalService>(lastDep);
|
||||
}
|
||||
};
|
||||
}
|
||||
throw UnsupportedError(
|
||||
'Scenario $scenario not supported by YxScopeAdapter');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import 'package:yx_scope/yx_scope.dart';
|
||||
|
||||
/// Universal container for dynamic DI registration in yx_scope (for benchmarks).
|
||||
/// Allows to register and resolve deps by name/type at runtime.
|
||||
class UniversalYxScopeContainer extends ScopeContainer {
|
||||
final Map<String, Dep<dynamic>> _namedDeps = {};
|
||||
final Map<Type, Dep<dynamic>> _typedDeps = {};
|
||||
|
||||
void register<T>(Dep<T> dep, {String? name}) {
|
||||
if (name != null) {
|
||||
_namedDeps[_depKey<T>(name)] = dep;
|
||||
} else {
|
||||
_typedDeps[T] = dep;
|
||||
}
|
||||
}
|
||||
|
||||
Dep<T> depFor<T>({String? name}) {
|
||||
if (name != null) {
|
||||
final dep = _namedDeps[_depKey<T>(name)];
|
||||
if (dep is Dep<T>) return dep;
|
||||
throw Exception('No dep for type $T/$name');
|
||||
} else {
|
||||
final dep = _typedDeps[T];
|
||||
if (dep is Dep<T>) return dep;
|
||||
throw Exception('No dep for type $T');
|
||||
}
|
||||
}
|
||||
|
||||
static String _depKey<T>(String name) => '$T@$name';
|
||||
}
|
||||
@@ -2,12 +2,16 @@
|
||||
enum UniversalScenario {
|
||||
/// Single registration.
|
||||
register,
|
||||
|
||||
/// Chain of dependencies.
|
||||
chain,
|
||||
|
||||
/// Named registrations.
|
||||
named,
|
||||
|
||||
/// Child-scope override scenario.
|
||||
override,
|
||||
|
||||
/// Asynchronous chain scenario.
|
||||
asyncChain,
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
/// Base interface for any universal service in the benchmarks.
|
||||
///
|
||||
/// Represents an object in the dependency chain with an identifiable value
|
||||
@@ -6,6 +5,7 @@
|
||||
abstract class UniversalService {
|
||||
/// String ID for this service instance (e.g. chain/level info).
|
||||
final String value;
|
||||
|
||||
/// Optional reference to dependency service in the chain.
|
||||
final UniversalService? dependency;
|
||||
UniversalService({required this.value, this.dependency});
|
||||
@@ -14,4 +14,4 @@ abstract class UniversalService {
|
||||
/// Default implementation for [UniversalService] used in service chains.
|
||||
class UniversalServiceImpl extends UniversalService {
|
||||
UniversalServiceImpl({required super.value, super.dependency});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ packages:
|
||||
path: "../cherrypick"
|
||||
relative: true
|
||||
source: path
|
||||
version: "3.0.0-dev.7"
|
||||
version: "3.0.1"
|
||||
collection:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -72,6 +72,14 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "8.2.0"
|
||||
kiwi:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: kiwi
|
||||
sha256: d078364a90fb1b93852bb74468efdf4aaae35c036c538c1cf4f9c74a19df9a61
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.0.1"
|
||||
lazy_memo:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -128,5 +136,13 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.0"
|
||||
yx_scope:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
name: yx_scope
|
||||
sha256: "9ba98b442261596311363bf7361622e5ccc67189705b8d042ca23c9de366f8bf"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.1.2"
|
||||
sdks:
|
||||
dart: ">=3.6.0 <4.0.0"
|
||||
|
||||
@@ -4,7 +4,7 @@ publish_to: none
|
||||
description: Universal benchmark for any DI library (cherrypick, get_it, and others)
|
||||
|
||||
environment:
|
||||
sdk: '>=3.0.0 <4.0.0'
|
||||
sdk: '>=3.2.0 <4.0.0'
|
||||
|
||||
dependencies:
|
||||
cherrypick:
|
||||
@@ -12,6 +12,8 @@ dependencies:
|
||||
args: ^2.7.0
|
||||
get_it: ^8.2.0
|
||||
riverpod: ^2.6.1
|
||||
kiwi: ^5.0.1
|
||||
yx_scope: ^1.1.2
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^5.0.0
|
||||
|
||||
@@ -1,3 +1,44 @@
|
||||
## 3.0.2
|
||||
|
||||
- **FIX**(test): fix warning.
|
||||
- **FIX**(scope): properly clear binding and module references on dispose.
|
||||
|
||||
## 3.0.1
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
## 3.0.0
|
||||
|
||||
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
|
||||
|
||||
## 3.0.0-dev.13
|
||||
|
||||
- **FIX**: fix examples.
|
||||
- **DOCS**: update contributors list with GitHub links and add new contributor.
|
||||
- **DOCS**(binding,docs): clarify `.singleton()` with `.toInstance()` behavior in docs and API.
|
||||
- **DOCS**(binding,docs): explain .singleton() + parametric provider behavior.
|
||||
- **DOCS**(binding): clarify registration limitation in API doc.
|
||||
- **DOCS**(di): clarify 'toInstance' binding limitations in builder.
|
||||
|
||||
## 3.0.0-dev.12
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
## 3.0.0-dev.11
|
||||
|
||||
- **FIX**(scope): prevent concurrent modification in dispose().
|
||||
- **FIX**(binding): fix unterminated string literal and syntax issues in binding.dart.
|
||||
|
||||
## 3.0.0-dev.10
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
## 3.0.0-dev.9
|
||||
|
||||
- **DOCS**(readme): add talker_cherrypick_logger to Additional Modules section.
|
||||
- **DOCS**(api): improve all DI core code documentation with English dartdoc and examples.
|
||||
|
||||
## 3.0.0-dev.8
|
||||
|
||||
- **REFACTOR**(tests): replace MockLogger with MockObserver in scope tests to align with updated observer API.
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
[](https://github.com/pese-git/cherrypick/actions/workflows/pipeline.yml)
|
||||
[](https://app.netlify.com/projects/cherrypick-di/deploys)
|
||||
|
||||
---
|
||||
|
||||
# CherryPick
|
||||
|
||||
`cherrypick` is a flexible and lightweight dependency injection library for Dart and Flutter.
|
||||
@@ -102,31 +107,98 @@ A **Binding** acts as a configuration for how to create or provide a particular
|
||||
#### Example
|
||||
|
||||
```dart
|
||||
// Provide a direct instance
|
||||
Binding<String>().toInstance("Hello world");
|
||||
void builder(Scope scope) {
|
||||
// Provide a direct instance
|
||||
bind<String>().toInstance("Hello world");
|
||||
|
||||
// Provide an async direct instance
|
||||
Binding<String>().toInstanceAsync(Future.value("Hello world"));
|
||||
// Provide an async direct instance
|
||||
bind<String>().toInstanceAsync(Future.value("Hello world"));
|
||||
|
||||
// Provide a lazy sync instance using a factory
|
||||
Binding<String>().toProvide(() => "Hello world");
|
||||
// Provide a lazy sync instance using a factory
|
||||
bind<String>().toProvide(() => "Hello world");
|
||||
|
||||
// Provide a lazy async instance using a factory
|
||||
Binding<String>().toProvideAsync(() async => "Hello async world");
|
||||
// Provide a lazy async instance using a factory
|
||||
bind<String>().toProvideAsync(() async => "Hello async world");
|
||||
|
||||
// Provide an instance with dynamic parameters (sync)
|
||||
Binding<String>().toProvideWithParams((params) => "Hello $params");
|
||||
// Provide an instance with dynamic parameters (sync)
|
||||
bind<String>().toProvideWithParams((params) => "Hello $params");
|
||||
|
||||
// Provide an instance with dynamic parameters (async)
|
||||
Binding<String>().toProvideAsyncWithParams((params) async => "Hello $params");
|
||||
// Provide an instance with dynamic parameters (async)
|
||||
bind<String>().toProvideAsyncWithParams((params) async => "Hello $params");
|
||||
|
||||
// Named instance for retrieval by name
|
||||
Binding<String>().toProvide(() => "Hello world").withName("my_string");
|
||||
// Named instance for retrieval by name
|
||||
bind<String>().toProvide(() => "Hello world").withName("my_string");
|
||||
|
||||
// Mark as singleton (only one instance within the scope)
|
||||
Binding<String>().toProvide(() => "Hello world").singleton();
|
||||
// Mark as singleton (only one instance within the scope)
|
||||
bind<String>().toProvide(() => "Hello world").singleton();
|
||||
}
|
||||
```
|
||||
|
||||
> ⚠️ **Important note about using `toInstance` in Module `builder`:**
|
||||
>
|
||||
> If you register a chain of dependencies via `toInstance` inside a Module's `builder`, **do not** call `scope.resolve<T>()` for types that are also being registered in the same builder — at the moment they are registered.
|
||||
>
|
||||
> CherryPick initializes all bindings in the builder sequentially. Dependencies registered earlier are not yet available to `resolve` within the same builder execution. Trying to resolve just-registered types will result in an error (`Can't resolve dependency ...`).
|
||||
>
|
||||
> **How to do it right:**
|
||||
> Manually construct the full dependency chain before calling `toInstance`:
|
||||
>
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> final a = A();
|
||||
> final b = B(a);
|
||||
> final c = C(b);
|
||||
> bind<A>().toInstance(a);
|
||||
> bind<B>().toInstance(b);
|
||||
> bind<C>().toInstance(c);
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Wrong:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toInstance(A());
|
||||
> // Error! At this point, A is not registered yet.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Wrong:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toProvide(() => A());
|
||||
> // Error! At this point, A is not registered yet.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Note:** This limitation applies **only** to `toInstance`. With `toProvide`/`toProvideAsync` and similar providers, you can safely use `scope.resolve<T>()` inside the builder.
|
||||
|
||||
|
||||
> ⚠️ **Special note regarding `.singleton()` with `toProvideWithParams()` / `toProvideAsyncWithParams()`:**
|
||||
>
|
||||
> If you declare a binding using `.toProvideWithParams(...)` (or its async variant) and then chain `.singleton()`, only the **very first** `resolve<T>(params: ...)` will use its parameters; every subsequent call (regardless of params) will return the same (cached) instance.
|
||||
>
|
||||
> **Example:**
|
||||
> ```dart
|
||||
> bind<Service>().toProvideWithParams((params) => Service(params)).singleton();
|
||||
> final a = scope.resolve<Service>(params: 1); // creates Service(1)
|
||||
> final b = scope.resolve<Service>(params: 2); // returns Service(1)
|
||||
> print(identical(a, b)); // true
|
||||
> ```
|
||||
>
|
||||
> Use this pattern only when you want a “master” singleton. If you expect a new instance per params, **do not** use `.singleton()` on parameterized providers.
|
||||
|
||||
|
||||
> ℹ️ **Note about `.singleton()` and `.toInstance()`:**
|
||||
>
|
||||
> Calling `.singleton()` after `.toInstance()` does **not** change the binding’s behavior: the object passed with `toInstance()` is already a single, constant instance that will be always returned for every resolve.
|
||||
>
|
||||
> It is not necessary to use `.singleton()` with an existing object—this call has no effect.
|
||||
>
|
||||
> `.singleton()` is only meaningful with providers (such as `toProvide`/`toProvideAsync`), to ensure only one instance is created by the factory.
|
||||
|
||||
|
||||
### Module
|
||||
|
||||
A **Module** is a logical collection point for bindings, designed for grouping and initializing related dependencies. Implement the `builder` method to define how dependencies should be bound within the scope.
|
||||
@@ -707,11 +779,20 @@ Yes! Even if none of your services currently implement `Disposable`, always use
|
||||
|
||||
CherryPick provides a set of official add-on modules for advanced use cases and specific platforms:
|
||||
|
||||
| Module name | Description | Documentation |
|
||||
|-------------|-------------|---------------|
|
||||
| [**cherrypick_annotations**](https://pub.dev/packages/cherrypick_annotations) | Dart annotations for concise DI definitions and code generation. | [README](../cherrypick_annotations/README.md) |
|
||||
| [**cherrypick_generator**](https://pub.dev/packages/cherrypick_generator) | Code generator to produce DI bindings based on annotations. | [README](../cherrypick_generator/README.md) |
|
||||
| [**cherrypick_flutter**](https://pub.dev/packages/cherrypick_flutter) | Flutter integration: DI provider widgets and helpers for Flutter. | [README](../cherrypick_flutter/README.md) |
|
||||
| Module name | Description |
|
||||
|-------------|-------------|
|
||||
| [**cherrypick_annotations**](https://pub.dev/packages/cherrypick_annotations) | Dart annotations for concise DI definitions and code generation. |
|
||||
| [**cherrypick_generator**](https://pub.dev/packages/cherrypick_generator) | Code generator to produce DI bindings based on annotations. |
|
||||
| [**cherrypick_flutter**](https://pub.dev/packages/cherrypick_flutter) | Flutter integration: DI provider widgets and helpers for Flutter. |
|
||||
| [**talker_cherrypick_logger**](https://pub.dev/packages/talker_cherrypick_logger) | Advanced logger for CherryPick DI events and state. Provides seamless integration with [Talker](https://pub.dev/packages/talker) logger, enabling central and visual tracking of DI events, errors, and diagnostics in both UI and console. |
|
||||
|
||||
---
|
||||
|
||||
## Contributors
|
||||
|
||||
- [Sergey Penkovsky](https://github.com/pese-git)
|
||||
- [Klim Yaroshevich](https://github.com/KlimYarosh)
|
||||
- [Alexey Popkov](https://github.com/AlexeyYuPopkov)
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -47,19 +47,19 @@ class FeatureModule extends Module {
|
||||
|
||||
Future<void> main() async {
|
||||
try {
|
||||
final scope = CherryPick.openRootScope().installModules([AppModule()]);
|
||||
final scope = CherryPick.openRootScope().installModules([AppModule()]);
|
||||
|
||||
final subScope = scope
|
||||
.openSubScope("featureScope")
|
||||
.installModules([FeatureModule(isMock: true)]);
|
||||
|
||||
// Asynchronous instance resolution
|
||||
final dataBloc = await subScope.resolveAsync<DataBloc>();
|
||||
dataBloc.data.listen(
|
||||
(d) => print('Received data: $d'),
|
||||
onError: (e) => print('Error: $e'),
|
||||
onDone: () => print('DONE'),
|
||||
);
|
||||
// Asynchronous instance resolution
|
||||
final dataBloc = await subScope.resolveAsync<DataBloc>();
|
||||
dataBloc.data.listen(
|
||||
(d) => print('Received data: $d'),
|
||||
onError: (e) => print('Error: $e'),
|
||||
onDone: () => print('DONE'),
|
||||
);
|
||||
|
||||
await dataBloc.fetchData();
|
||||
} catch (e) {
|
||||
|
||||
@@ -8,7 +8,7 @@ class DatabaseService {
|
||||
class ApiService {
|
||||
final DatabaseService database;
|
||||
ApiService(this.database);
|
||||
|
||||
|
||||
void fetchData() {
|
||||
database.connect();
|
||||
print('📡 Fetching data via API');
|
||||
@@ -18,7 +18,7 @@ class ApiService {
|
||||
class UserService {
|
||||
final ApiService apiService;
|
||||
UserService(this.apiService);
|
||||
|
||||
|
||||
void getUser(String id) {
|
||||
apiService.fetchData();
|
||||
print('👤 Fetching user: $id');
|
||||
@@ -36,18 +36,16 @@ class DatabaseModule extends Module {
|
||||
class ApiModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<ApiService>().toProvide(() => ApiService(
|
||||
currentScope.resolve<DatabaseService>()
|
||||
));
|
||||
bind<ApiService>()
|
||||
.toProvide(() => ApiService(currentScope.resolve<DatabaseService>()));
|
||||
}
|
||||
}
|
||||
|
||||
class UserModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserService>().toProvide(() => UserService(
|
||||
currentScope.resolve<ApiService>()
|
||||
));
|
||||
bind<UserService>()
|
||||
.toProvide(() => UserService(currentScope.resolve<ApiService>()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,74 +63,75 @@ class CircularServiceB {
|
||||
class CircularModuleA extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CircularServiceA>().toProvide(() => CircularServiceA(
|
||||
currentScope.resolve<CircularServiceB>()
|
||||
));
|
||||
bind<CircularServiceA>().toProvide(
|
||||
() => CircularServiceA(currentScope.resolve<CircularServiceB>()));
|
||||
}
|
||||
}
|
||||
|
||||
class CircularModuleB extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CircularServiceB>().toProvide(() => CircularServiceB(
|
||||
currentScope.resolve<CircularServiceA>()
|
||||
));
|
||||
bind<CircularServiceB>().toProvide(
|
||||
() => CircularServiceB(currentScope.resolve<CircularServiceA>()));
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
print('=== Improved CherryPick Helper Demonstration ===\n');
|
||||
|
||||
|
||||
// Example 1: Global enabling of cycle detection
|
||||
print('1. Globally enable cycle detection:');
|
||||
|
||||
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
print('✅ Global cycle detection enabled: ${CherryPick.isGlobalCycleDetectionEnabled}');
|
||||
|
||||
print(
|
||||
'✅ Global cycle detection enabled: ${CherryPick.isGlobalCycleDetectionEnabled}');
|
||||
|
||||
// All new scopes will automatically have cycle detection enabled
|
||||
final globalScope = CherryPick.openRootScope();
|
||||
print('✅ Root scope has cycle detection enabled: ${globalScope.isCycleDetectionEnabled}');
|
||||
|
||||
print(
|
||||
'✅ Root scope has cycle detection enabled: ${globalScope.isCycleDetectionEnabled}');
|
||||
|
||||
// Install modules without circular dependencies
|
||||
globalScope.installModules([
|
||||
DatabaseModule(),
|
||||
ApiModule(),
|
||||
UserModule(),
|
||||
]);
|
||||
|
||||
|
||||
final userService = globalScope.resolve<UserService>();
|
||||
userService.getUser('user123');
|
||||
print('');
|
||||
|
||||
|
||||
// Example 2: Safe scope creation
|
||||
print('2. Creating safe scopes:');
|
||||
|
||||
|
||||
CherryPick.closeRootScope(); // Закрываем предыдущий скоуп
|
||||
CherryPick.disableGlobalCycleDetection(); // Отключаем глобальную настройку
|
||||
|
||||
|
||||
// Создаем безопасный скоуп (с автоматически включенным обнаружением)
|
||||
final safeScope = CherryPick.openSafeRootScope();
|
||||
print('✅ Safe scope created with cycle detection: ${safeScope.isCycleDetectionEnabled}');
|
||||
|
||||
print(
|
||||
'✅ Safe scope created with cycle detection: ${safeScope.isCycleDetectionEnabled}');
|
||||
|
||||
safeScope.installModules([
|
||||
DatabaseModule(),
|
||||
ApiModule(),
|
||||
UserModule(),
|
||||
]);
|
||||
|
||||
|
||||
final safeUserService = safeScope.resolve<UserService>();
|
||||
safeUserService.getUser('safe_user456');
|
||||
print('');
|
||||
|
||||
|
||||
// Example 3: Detecting cycles
|
||||
print('3. Detecting circular dependencies:');
|
||||
|
||||
|
||||
final cyclicScope = CherryPick.openSafeRootScope();
|
||||
cyclicScope.installModules([
|
||||
CircularModuleA(),
|
||||
CircularModuleB(),
|
||||
]);
|
||||
|
||||
|
||||
try {
|
||||
cyclicScope.resolve<CircularServiceA>();
|
||||
print('❌ This should not be executed');
|
||||
@@ -144,87 +143,96 @@ void main() {
|
||||
}
|
||||
}
|
||||
print('');
|
||||
|
||||
|
||||
// Example 4: Managing detection for specific scopes
|
||||
print('4. Managing detection for specific scopes:');
|
||||
|
||||
|
||||
CherryPick.closeRootScope();
|
||||
|
||||
|
||||
// Создаем скоуп без обнаружения
|
||||
// ignore: unused_local_variable
|
||||
final specificScope = CherryPick.openRootScope();
|
||||
print(' Detection in root scope: ${CherryPick.isCycleDetectionEnabledForScope()}');
|
||||
|
||||
print(
|
||||
' Detection in root scope: ${CherryPick.isCycleDetectionEnabledForScope()}');
|
||||
|
||||
// Включаем обнаружение для конкретного скоупа
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
print('✅ Detection enabled for root scope: ${CherryPick.isCycleDetectionEnabledForScope()}');
|
||||
|
||||
print(
|
||||
'✅ Detection enabled for root scope: ${CherryPick.isCycleDetectionEnabledForScope()}');
|
||||
|
||||
// Создаем дочерний скоуп
|
||||
// ignore: unused_local_variable
|
||||
final featureScope = CherryPick.openScope(scopeName: 'feature.auth');
|
||||
print(' Detection in feature.auth scope: ${CherryPick.isCycleDetectionEnabledForScope(scopeName: 'feature.auth')}');
|
||||
|
||||
print(
|
||||
' Detection in feature.auth scope: ${CherryPick.isCycleDetectionEnabledForScope(scopeName: 'feature.auth')}');
|
||||
|
||||
// Включаем обнаружение для дочернего скоупа
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: 'feature.auth');
|
||||
print('✅ Detection enabled for feature.auth scope: ${CherryPick.isCycleDetectionEnabledForScope(scopeName: 'feature.auth')}');
|
||||
print(
|
||||
'✅ Detection enabled for feature.auth scope: ${CherryPick.isCycleDetectionEnabledForScope(scopeName: 'feature.auth')}');
|
||||
print('');
|
||||
|
||||
|
||||
// Example 5: Creating safe child scopes
|
||||
print('5. Creating safe child scopes:');
|
||||
|
||||
final safeFeatureScope = CherryPick.openSafeScope(scopeName: 'feature.payments');
|
||||
print('✅ Safe feature scope created: ${safeFeatureScope.isCycleDetectionEnabled}');
|
||||
|
||||
|
||||
final safeFeatureScope =
|
||||
CherryPick.openSafeScope(scopeName: 'feature.payments');
|
||||
print(
|
||||
'✅ Safe feature scope created: ${safeFeatureScope.isCycleDetectionEnabled}');
|
||||
|
||||
// You can create a complex hierarchy of scopes
|
||||
final complexScope = CherryPick.openSafeScope(scopeName: 'app.feature.auth.login');
|
||||
final complexScope =
|
||||
CherryPick.openSafeScope(scopeName: 'app.feature.auth.login');
|
||||
print('✅ Complex scope created: ${complexScope.isCycleDetectionEnabled}');
|
||||
print('');
|
||||
|
||||
|
||||
// Example 6: Tracking resolution chains
|
||||
print('6. Tracking dependency resolution chains:');
|
||||
|
||||
|
||||
final trackingScope = CherryPick.openSafeRootScope();
|
||||
trackingScope.installModules([
|
||||
DatabaseModule(),
|
||||
ApiModule(),
|
||||
UserModule(),
|
||||
]);
|
||||
|
||||
|
||||
print(' Chain before resolve: ${CherryPick.getCurrentResolutionChain()}');
|
||||
|
||||
|
||||
// The chain is populated during resolution, but cleared after completion
|
||||
// ignore: unused_local_variable
|
||||
final trackedUserService = trackingScope.resolve<UserService>();
|
||||
print(' Chain after resolve: ${CherryPick.getCurrentResolutionChain()}');
|
||||
print('');
|
||||
|
||||
|
||||
// Example 7: Usage recommendations
|
||||
print('7. Recommended usage:');
|
||||
print('');
|
||||
|
||||
|
||||
print('🔧 Development mode:');
|
||||
print(' CherryPick.enableGlobalCycleDetection(); // Enable globally');
|
||||
print(' or');
|
||||
print(' final scope = CherryPick.openSafeRootScope(); // Safe scope');
|
||||
print('');
|
||||
|
||||
|
||||
print('🚀 Production mode:');
|
||||
print(' CherryPick.disableGlobalCycleDetection(); // Disable for performance');
|
||||
print(
|
||||
' CherryPick.disableGlobalCycleDetection(); // Disable for performance');
|
||||
print(' final scope = CherryPick.openRootScope(); // Regular scope');
|
||||
print('');
|
||||
|
||||
|
||||
print('🧪 Testing:');
|
||||
print(' setUp(() => CherryPick.enableGlobalCycleDetection());');
|
||||
print(' tearDown(() => CherryPick.closeRootScope());');
|
||||
print('');
|
||||
|
||||
|
||||
print('🎯 Feature-specific:');
|
||||
print(' CherryPick.enableCycleDetectionForScope(scopeName: "feature.critical");');
|
||||
print(
|
||||
' CherryPick.enableCycleDetectionForScope(scopeName: "feature.critical");');
|
||||
print(' // Enable only for critical features');
|
||||
|
||||
|
||||
// Cleanup
|
||||
CherryPick.closeRootScope();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
|
||||
print('\n=== Demonstration complete ===');
|
||||
}
|
||||
|
||||
@@ -3,9 +3,9 @@ import 'package:cherrypick/cherrypick.dart';
|
||||
// Пример сервисов с циклической зависимостью
|
||||
class UserService {
|
||||
final OrderService orderService;
|
||||
|
||||
|
||||
UserService(this.orderService);
|
||||
|
||||
|
||||
void createUser(String name) {
|
||||
print('Creating user: $name');
|
||||
// Пытаемся получить заказы пользователя, что создает циклическую зависимость
|
||||
@@ -15,9 +15,9 @@ class UserService {
|
||||
|
||||
class OrderService {
|
||||
final UserService userService;
|
||||
|
||||
|
||||
OrderService(this.userService);
|
||||
|
||||
|
||||
void getOrdersForUser(String userName) {
|
||||
print('Getting orders for user: $userName');
|
||||
// Пытаемся получить информацию о пользователе, что создает циклическую зависимость
|
||||
@@ -29,18 +29,16 @@ class OrderService {
|
||||
class UserModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserService>().toProvide(() => UserService(
|
||||
currentScope.resolve<OrderService>()
|
||||
));
|
||||
bind<UserService>()
|
||||
.toProvide(() => UserService(currentScope.resolve<OrderService>()));
|
||||
}
|
||||
}
|
||||
|
||||
class OrderModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<OrderService>().toProvide(() => OrderService(
|
||||
currentScope.resolve<UserService>()
|
||||
));
|
||||
bind<OrderService>()
|
||||
.toProvide(() => OrderService(currentScope.resolve<UserService>()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +47,7 @@ class UserRepository {
|
||||
void createUser(String name) {
|
||||
print('Creating user in repository: $name');
|
||||
}
|
||||
|
||||
|
||||
String getUserInfo(String name) {
|
||||
return 'User info for: $name';
|
||||
}
|
||||
@@ -59,7 +57,7 @@ class OrderRepository {
|
||||
void createOrder(String orderId, String userName) {
|
||||
print('Creating order $orderId for user: $userName');
|
||||
}
|
||||
|
||||
|
||||
List<String> getOrdersForUser(String userName) {
|
||||
return ['order1', 'order2', 'order3'];
|
||||
}
|
||||
@@ -67,13 +65,13 @@ class OrderRepository {
|
||||
|
||||
class ImprovedUserService {
|
||||
final UserRepository userRepository;
|
||||
|
||||
|
||||
ImprovedUserService(this.userRepository);
|
||||
|
||||
|
||||
void createUser(String name) {
|
||||
userRepository.createUser(name);
|
||||
}
|
||||
|
||||
|
||||
String getUserInfo(String name) {
|
||||
return userRepository.getUserInfo(name);
|
||||
}
|
||||
@@ -82,17 +80,17 @@ class ImprovedUserService {
|
||||
class ImprovedOrderService {
|
||||
final OrderRepository orderRepository;
|
||||
final ImprovedUserService userService;
|
||||
|
||||
|
||||
ImprovedOrderService(this.orderRepository, this.userService);
|
||||
|
||||
|
||||
void createOrder(String orderId, String userName) {
|
||||
// Проверяем, что пользователь существует
|
||||
final userInfo = userService.getUserInfo(userName);
|
||||
print('User exists: $userInfo');
|
||||
|
||||
|
||||
orderRepository.createOrder(orderId, userName);
|
||||
}
|
||||
|
||||
|
||||
List<String> getOrdersForUser(String userName) {
|
||||
return orderRepository.getOrdersForUser(userName);
|
||||
}
|
||||
@@ -103,9 +101,8 @@ class ImprovedUserModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserRepository>().singleton().toProvide(() => UserRepository());
|
||||
bind<ImprovedUserService>().toProvide(() => ImprovedUserService(
|
||||
currentScope.resolve<UserRepository>()
|
||||
));
|
||||
bind<ImprovedUserService>().toProvide(
|
||||
() => ImprovedUserService(currentScope.resolve<UserRepository>()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,81 +111,82 @@ class ImprovedOrderModule extends Module {
|
||||
void builder(Scope currentScope) {
|
||||
bind<OrderRepository>().singleton().toProvide(() => OrderRepository());
|
||||
bind<ImprovedOrderService>().toProvide(() => ImprovedOrderService(
|
||||
currentScope.resolve<OrderRepository>(),
|
||||
currentScope.resolve<ImprovedUserService>()
|
||||
));
|
||||
currentScope.resolve<OrderRepository>(),
|
||||
currentScope.resolve<ImprovedUserService>()));
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
print('=== Circular Dependency Detection Example ===\n');
|
||||
|
||||
|
||||
// Example 1: Demonstrate circular dependency
|
||||
print('1. Attempt to create a scope with circular dependencies:');
|
||||
try {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection(); // Включаем обнаружение циклических зависимостей
|
||||
|
||||
scope
|
||||
.enableCycleDetection(); // Включаем обнаружение циклических зависимостей
|
||||
|
||||
scope.installModules([
|
||||
UserModule(),
|
||||
OrderModule(),
|
||||
]);
|
||||
|
||||
|
||||
// Это должно выбросить CircularDependencyException
|
||||
final userService = scope.resolve<UserService>();
|
||||
print('UserService created: $userService');
|
||||
} catch (e) {
|
||||
print('❌ Circular dependency detected: $e\n');
|
||||
}
|
||||
|
||||
|
||||
// Example 2: Without circular dependency detection (dangerous!)
|
||||
print('2. Same code without circular dependency detection:');
|
||||
try {
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
final scope = CherryPick.openRootScope();
|
||||
// НЕ включаем обнаружение циклических зависимостей
|
||||
|
||||
|
||||
scope.installModules([
|
||||
UserModule(),
|
||||
OrderModule(),
|
||||
]);
|
||||
|
||||
|
||||
// Это приведет к StackOverflowError при попытке использования
|
||||
final userService = scope.resolve<UserService>();
|
||||
print('UserService создан: $userService');
|
||||
|
||||
|
||||
// Попытка использовать сервис приведет к бесконечной рекурсии
|
||||
// userService.createUser('John'); // Раскомментируйте для демонстрации StackOverflow
|
||||
print('⚠️ UserService created, but using it will cause StackOverflow\n');
|
||||
} catch (e) {
|
||||
print('❌ Error: $e\n');
|
||||
}
|
||||
|
||||
|
||||
// Example 3: Correct architecture without circular dependencies
|
||||
print('3. Correct architecture without circular dependencies:');
|
||||
try {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection(); // Включаем для безопасности
|
||||
|
||||
|
||||
scope.installModules([
|
||||
ImprovedUserModule(),
|
||||
ImprovedOrderModule(),
|
||||
]);
|
||||
|
||||
|
||||
final userService = scope.resolve<ImprovedUserService>();
|
||||
final orderService = scope.resolve<ImprovedOrderService>();
|
||||
|
||||
|
||||
print('✅ Services created successfully');
|
||||
|
||||
|
||||
// Демонстрация работы
|
||||
userService.createUser('John');
|
||||
orderService.createOrder('ORD-001', 'John');
|
||||
final orders = orderService.getOrdersForUser('John');
|
||||
print('✅ Orders for user John: $orders');
|
||||
|
||||
} catch (e) {
|
||||
print('❌ Error: $e');
|
||||
}
|
||||
|
||||
|
||||
print('\n=== Recommendations ===');
|
||||
print('1. Always enable circular dependency detection in development mode.');
|
||||
print('2. Use repositories and services to separate concerns.');
|
||||
|
||||
@@ -14,6 +14,14 @@ class MyService implements Disposable {
|
||||
void doSomething() => print('Doing something...');
|
||||
}
|
||||
|
||||
/// Пример модуля CherryPick
|
||||
class ModuleImpl extends Module {
|
||||
@override
|
||||
void builder(Scope scope) {
|
||||
bind<MyService>().toProvide(() => MyService()).singleton();
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
final scope = CherryPick.openRootScope();
|
||||
|
||||
@@ -30,11 +38,3 @@ void main() {
|
||||
scope.dispose();
|
||||
print('Service wasDisposed = ${service.wasDisposed}'); // true
|
||||
}
|
||||
|
||||
/// Пример модуля CherryPick
|
||||
class ModuleImpl extends Module {
|
||||
@override
|
||||
void builder(Scope scope) {
|
||||
bind<MyService>().toProvide(() => MyService()).singleton();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,9 +13,52 @@
|
||||
|
||||
import 'package:cherrypick/src/binding_resolver.dart';
|
||||
|
||||
/// RU: Класс Binding<T> настраивает параметры экземпляра.
|
||||
/// ENG: The Binding<T> class configures the settings for the instance.
|
||||
/// {@template binding_docs}
|
||||
/// [Binding] configures how a dependency of type [T] is created, provided, or managed in CherryPick DI.
|
||||
///
|
||||
/// A [Binding] can:
|
||||
/// - Register a direct instance
|
||||
/// - Register a provider (sync/async)
|
||||
/// - Register a provider supporting dynamic params
|
||||
/// - Be named (for multi-implementation/keyed injection)
|
||||
/// - Be marked as [singleton] (single instance within scope)
|
||||
///
|
||||
/// ### Examples
|
||||
///
|
||||
/// Register a direct instance:
|
||||
/// ```dart
|
||||
/// bind<String>().toInstance("Hello, world!");
|
||||
/// ```
|
||||
///
|
||||
/// Register via sync provider:
|
||||
/// ```dart
|
||||
/// bind<MyService>().toProvide(() => MyService());
|
||||
/// ```
|
||||
///
|
||||
/// Register via async provider (returns Future):
|
||||
/// ```dart
|
||||
/// bind<MyApi>().toProvide(() async => await MyApi.connect());
|
||||
/// ```
|
||||
///
|
||||
/// Register provider with dynamic params:
|
||||
/// ```dart
|
||||
/// bind<User>().toProvideWithParams((params) => User(name: params["name"]));
|
||||
/// ```
|
||||
///
|
||||
/// Register with name/key:
|
||||
/// ```dart
|
||||
/// bind<Client>().withName("mock").toInstance(MockClient());
|
||||
/// bind<Client>().withName("prod").toInstance(RealClient());
|
||||
/// final c = scope.resolve<Client>(named: "mock");
|
||||
/// ```
|
||||
///
|
||||
/// Singleton (same instance reused):
|
||||
/// ```dart
|
||||
/// bind<Database>().toProvide(() => Database()).singleton();
|
||||
/// ```
|
||||
///
|
||||
/// {@endtemplate}
|
||||
|
||||
import 'package:cherrypick/src/observer.dart';
|
||||
|
||||
class Binding<T> {
|
||||
@@ -82,64 +125,97 @@ class Binding<T> {
|
||||
markSingleton();
|
||||
}
|
||||
|
||||
/// RU: Метод возвращает тип экземпляра.
|
||||
/// ENG: The method returns the type of the instance.
|
||||
/// Returns the type key used by this binding.
|
||||
///
|
||||
/// return [Type]
|
||||
/// Usually you don't need to access it directly.
|
||||
Type get key => _key;
|
||||
|
||||
/// RU: Метод возвращает имя экземпляра.
|
||||
/// ENG: The method returns the name of the instance.
|
||||
///
|
||||
/// return [String]
|
||||
/// Returns the name (if any) for this binding.
|
||||
/// Useful for named/multi-implementation resolution.
|
||||
String? get name => _name;
|
||||
|
||||
/// RU: Метод проверяет именован экземпляр или нет.
|
||||
/// ENG: The method checks whether the instance is named or not.
|
||||
///
|
||||
/// return [bool]
|
||||
/// Returns true if this binding is named (named/keyed binding).
|
||||
bool get isNamed => _name != null;
|
||||
|
||||
/// RU: Метод проверяет сингелтон экземпляр или нет.
|
||||
/// ENG: The method checks the singleton instance or not.
|
||||
///
|
||||
/// return [bool]
|
||||
/// Returns true if this binding is marked as a singleton.
|
||||
/// Singleton bindings will only create one instance within the scope.
|
||||
bool get isSingleton => _resolver?.isSingleton ?? false;
|
||||
|
||||
BindingResolver<T>? get resolver => _resolver;
|
||||
|
||||
/// RU: Добавляет имя для экземляпя [value].
|
||||
/// ENG: Added name for instance [value].
|
||||
/// Adds a name/key to this binding (for multi-implementation or keyed injection).
|
||||
///
|
||||
/// return [Binding]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// bind<Client>().withName("mock").toInstance(MockClient());
|
||||
/// ```
|
||||
Binding<T> withName(String name) {
|
||||
_name = name;
|
||||
// Не логируем здесь, deferred log via markNamed()
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляпяра [value].
|
||||
/// ENG: Initialization instance [value].
|
||||
/// Binds a direct instance (static object) for this type.
|
||||
///
|
||||
/// return [Binding]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// bind<Api>().toInstance(ApiMock());
|
||||
/// ```
|
||||
///
|
||||
/// **Important limitation:**
|
||||
/// If you register several dependencies via [toInstance] inside a [`Module.builder`],
|
||||
/// do _not_ use `scope.resolve<T>()` to get objects that are also being registered during the _same_ builder execution.
|
||||
/// All [toInstance] bindings are applied sequentially, and at the point of registration,
|
||||
/// earlier objects are not yet available for resolve.
|
||||
///
|
||||
/// **Correct:**
|
||||
/// ```dart
|
||||
/// void builder(Scope scope) {
|
||||
/// final a = A();
|
||||
/// final b = B(a);
|
||||
/// bind<A>().toInstance(a);
|
||||
/// bind<B>().toInstance(b);
|
||||
/// }
|
||||
/// ```
|
||||
/// **Wrong:**
|
||||
/// ```dart
|
||||
/// void builder(Scope scope) {
|
||||
/// bind<A>().toInstance(A());
|
||||
/// bind<B>().toInstance(B(scope.resolve<A>())); // Error! A is not available yet.
|
||||
/// }
|
||||
/// ```
|
||||
/// **Wrong:**
|
||||
/// ```dart
|
||||
/// void builder(Scope scope) {
|
||||
/// bind<A>().toProvide(() => A());
|
||||
/// bind<B>().toInstance(B(scope.resolve<A>())); // Error! A is not available yet.
|
||||
/// }
|
||||
/// ```
|
||||
/// This restriction only applies to [toInstance] bindings.
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
/// With [toProvide]/[toProvideAsync] you may freely use `scope.resolve<T>()` in the builder or provider function.
|
||||
Binding<T> toInstance(Instance<T> value) {
|
||||
_resolver = InstanceResolver<T>(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляпяра через провайдер [value].
|
||||
/// ENG: Initialization instance via provider [value].
|
||||
/// Binds a provider function (sync or async) that creates the instance when resolved.
|
||||
///
|
||||
/// return [Binding]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// bind<Api>().toProvide(() => ApiService());
|
||||
/// bind<Db>().toProvide(() async => await openDb());
|
||||
/// ```
|
||||
Binding<T> toProvide(Provider<T> value) {
|
||||
_resolver = ProviderResolver<T>((_) => value.call(), withParams: false);
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляпяра через провайдер [value] c динамическим параметром.
|
||||
/// ENG: Initialization instance via provider [value] with a dynamic param.
|
||||
/// Binds a provider function that takes dynamic params at resolve-time (e.g. for factories).
|
||||
///
|
||||
/// return [Binding]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// bind<User>().toProvideWithParams((params) => User(name: params["name"]));
|
||||
/// ```
|
||||
Binding<T> toProvideWithParams(ProviderWithParams<T> value) {
|
||||
_resolver = ProviderResolver<T>(value, withParams: true);
|
||||
return this;
|
||||
@@ -160,16 +236,49 @@ class Binding<T> {
|
||||
return this.toProvideWithParams(value);
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляпяра как сингелтон [value].
|
||||
/// ENG: Initialization instance as a singelton [value].
|
||||
/// Marks this binding as singleton (will only create and cache one instance per scope).
|
||||
///
|
||||
/// return [Binding]
|
||||
/// Call this after toProvide/toInstance etc:
|
||||
/// ```dart
|
||||
/// bind<Api>().toProvide(() => MyApi()).singleton();
|
||||
/// ```
|
||||
///
|
||||
/// ---
|
||||
///
|
||||
/// ⚠️ **Special note: Behavior with parametric providers (`toProvideWithParams`/`toProvideAsyncWithParams`):**
|
||||
///
|
||||
/// If you declare a binding using `.toProvideWithParams(...)` (or its async variant) and then chain `.singleton()`, only the **very first** `resolve<T>(params: ...)` will use its parameters;
|
||||
/// every subsequent call (regardless of params) will return the same (cached) instance.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// bind<Service>().toProvideWithParams((params) => Service(params)).singleton();
|
||||
/// final a = scope.resolve<Service>(params: 1); // creates Service(1)
|
||||
/// final b = scope.resolve<Service>(params: 2); // returns Service(1)
|
||||
/// print(identical(a, b)); // true
|
||||
/// ```
|
||||
///
|
||||
/// Use this pattern only if you want a master singleton. If you expect a new instance per params, **do not** use `.singleton()` on parameterized providers.
|
||||
///
|
||||
/// ℹ️ **Note about `.singleton()` and `.toInstance()`:**
|
||||
///
|
||||
/// Calling `.singleton()` after `.toInstance()` does **not** change the binding’s behavior:
|
||||
/// the object passed with `toInstance()` is already a single, constant instance that will always be returned for every resolve.
|
||||
/// There is no need to use `.singleton()` with `toInstance()`. This call has no effect.
|
||||
/// `.singleton()` is only meaningful with providers (`toProvide`, `toProvideAsync`, etc), to ensure only one instance is created by the factory.
|
||||
Binding<T> singleton() {
|
||||
_resolver?.toSingleton();
|
||||
// Не логируем здесь, deferred log via markSingleton()
|
||||
return this;
|
||||
}
|
||||
|
||||
/// Resolves the instance synchronously (if binding supports sync access).
|
||||
///
|
||||
/// Returns the created/found instance or null.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final s = scope.resolveSync<String>();
|
||||
/// ```
|
||||
T? resolveSync([dynamic params]) {
|
||||
final res = resolver?.resolveSync(params);
|
||||
if (res != null) {
|
||||
@@ -194,6 +303,14 @@ class Binding<T> {
|
||||
return res;
|
||||
}
|
||||
|
||||
/// Resolves the instance asynchronously (if binding supports async/future access).
|
||||
///
|
||||
/// Returns a [Future] with the instance, or null if unavailable.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final user = await scope.resolveAsync<User>();
|
||||
/// ```
|
||||
Future<T>? resolveAsync([dynamic params]) {
|
||||
final future = resolver?.resolveAsync(params);
|
||||
if (future != null) {
|
||||
|
||||
@@ -13,41 +13,70 @@
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
/// Represents a direct instance or an async instance ([T] or [Future<T>]).
|
||||
/// Used for both direct and async bindings.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// Instance<String> sync = "hello";
|
||||
/// Instance<MyApi> async = Future.value(MyApi());
|
||||
/// ```
|
||||
typedef Instance<T> = FutureOr<T>;
|
||||
|
||||
/// RU: Синхронный или асинхронный провайдер без параметров, возвращающий [T] или [Future<T>].
|
||||
/// ENG: Synchronous or asynchronous provider without parameters, returning [T] or [Future<T>].
|
||||
/// Provider function type for synchronous or asynchronous, parameterless creation of [T].
|
||||
/// Can return [T] or [Future<T>].
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// Provider<MyService> provider = () => MyService();
|
||||
/// Provider<Api> asyncProvider = () async => await Api.connect();
|
||||
/// ```
|
||||
typedef Provider<T> = FutureOr<T> Function();
|
||||
|
||||
/// RU: Провайдер с динамическим параметром, возвращающий [T] или [Future<T>] в зависимости от реализации.
|
||||
/// ENG: Provider with dynamic parameter, returning [T] or [Future<T>] depending on implementation.
|
||||
/// Provider function type that accepts a dynamic parameter, for factory/parametrized injection.
|
||||
/// Returns [T] or [Future<T>].
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// ProviderWithParams<User> provider = (params) => User(params["name"]);
|
||||
/// ```
|
||||
typedef ProviderWithParams<T> = FutureOr<T> Function(dynamic);
|
||||
|
||||
/// RU: Абстрактный интерфейс для классов, которые разрешают зависимости типа [T].
|
||||
/// ENG: Abstract interface for classes that resolve dependencies of type [T].
|
||||
/// Abstract interface for dependency resolvers used by [Binding].
|
||||
/// Defines how to resolve instances of type [T].
|
||||
///
|
||||
/// You usually don't use this directly; it's used internally for advanced/low-level DI.
|
||||
abstract class BindingResolver<T> {
|
||||
/// RU: Синхронное разрешение зависимости с параметром [params].
|
||||
/// ENG: Synchronous resolution of the dependency with [params].
|
||||
/// Synchronously resolves the dependency, optionally taking parameters (for factory cases).
|
||||
/// Throws if implementation does not support sync resolution.
|
||||
T? resolveSync([dynamic params]);
|
||||
|
||||
/// RU: Асинхронное разрешение зависимости с параметром [params].
|
||||
/// ENG: Asynchronous resolution of the dependency with [params].
|
||||
/// Asynchronously resolves the dependency, optionally taking parameters (for factory cases).
|
||||
/// If instance is already a [Future], returns it directly.
|
||||
Future<T>? resolveAsync([dynamic params]);
|
||||
|
||||
/// RU: Помечает текущий резолвер как синглтон — результат будет закеширован.
|
||||
/// ENG: Marks this resolver as singleton — result will be cached.
|
||||
/// Marks this resolver as singleton: instance(s) will be cached and reused inside the scope.
|
||||
void toSingleton();
|
||||
|
||||
/// Returns true if this resolver is marked as singleton.
|
||||
bool get isSingleton;
|
||||
}
|
||||
|
||||
/// RU: Резолвер, оборачивающий конкретный экземпляр [T] (или Future<T>), без вызова провайдера.
|
||||
/// ENG: Resolver that wraps a concrete instance of [T] (or Future<T>), without provider invocation.
|
||||
/// Concrete resolver for direct instance ([T] or [Future<T>]). No provider is called.
|
||||
///
|
||||
/// Used for [Binding.toInstance].
|
||||
/// Supports both sync and async resolution; sync will throw if underlying instance is [Future].
|
||||
/// Examples:
|
||||
/// ```dart
|
||||
/// var resolver = InstanceResolver("hello");
|
||||
/// resolver.resolveSync(); // == "hello"
|
||||
/// var asyncResolver = InstanceResolver(Future.value(7));
|
||||
/// asyncResolver.resolveAsync(); // Future<int>
|
||||
/// ```
|
||||
class InstanceResolver<T> implements BindingResolver<T> {
|
||||
final Instance<T> _instance;
|
||||
|
||||
/// RU: Создаёт резолвер, оборачивающий значение [instance].
|
||||
/// ENG: Creates a resolver that wraps the given [instance].
|
||||
/// Wraps the given instance (sync or async) in a resolver.
|
||||
InstanceResolver(this._instance);
|
||||
|
||||
@override
|
||||
@@ -62,7 +91,6 @@ class InstanceResolver<T> implements BindingResolver<T> {
|
||||
@override
|
||||
Future<T> resolveAsync([_]) {
|
||||
if (_instance is Future<T>) return _instance;
|
||||
|
||||
return Future.value(_instance);
|
||||
}
|
||||
|
||||
@@ -73,8 +101,23 @@ class InstanceResolver<T> implements BindingResolver<T> {
|
||||
bool get isSingleton => true;
|
||||
}
|
||||
|
||||
/// RU: Резолвер, оборачивающий провайдер, с возможностью синглтон-кеширования.
|
||||
/// ENG: Resolver that wraps a provider, with optional singleton caching.
|
||||
/// Resolver for provider functions (sync/async/factory), with optional singleton caching.
|
||||
/// Used for [Binding.toProvide], [Binding.toProvideWithParams], [Binding.singleton].
|
||||
///
|
||||
/// Examples:
|
||||
/// ```dart
|
||||
/// // No param, sync:
|
||||
/// var r = ProviderResolver((_) => 5, withParams: false);
|
||||
/// r.resolveSync(); // == 5
|
||||
/// // With param:
|
||||
/// var rp = ProviderResolver((p) => p * 2, withParams: true);
|
||||
/// rp.resolveSync(2); // == 4
|
||||
/// // Singleton:
|
||||
/// r.toSingleton();
|
||||
/// // Async:
|
||||
/// var ra = ProviderResolver((_) async => await Future.value(10), withParams: false);
|
||||
/// await ra.resolveAsync(); // == 10
|
||||
/// ```
|
||||
class ProviderResolver<T> implements BindingResolver<T> {
|
||||
final ProviderWithParams<T> _provider;
|
||||
final bool _withParams;
|
||||
@@ -82,8 +125,7 @@ class ProviderResolver<T> implements BindingResolver<T> {
|
||||
FutureOr<T>? _cache;
|
||||
bool _singleton = false;
|
||||
|
||||
/// RU: Создаёт резолвер из произвольной функции [raw], поддерживающей ноль или один параметр.
|
||||
/// ENG: Creates a resolver from arbitrary function [raw], supporting zero or one parameter.
|
||||
/// Creates a resolver from [provider], optionally accepting dynamic params.
|
||||
ProviderResolver(
|
||||
ProviderWithParams<T> provider, {
|
||||
required bool withParams,
|
||||
@@ -93,16 +135,13 @@ class ProviderResolver<T> implements BindingResolver<T> {
|
||||
@override
|
||||
T resolveSync([dynamic params]) {
|
||||
_checkParams(params);
|
||||
|
||||
final result = _cache ?? _provider(params);
|
||||
|
||||
if (result is T) {
|
||||
if (_singleton) {
|
||||
_cache ??= result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
throw StateError(
|
||||
'Provider [$_provider] return Future<$T>. Use resolveAsync() instead.',
|
||||
);
|
||||
@@ -111,14 +150,11 @@ class ProviderResolver<T> implements BindingResolver<T> {
|
||||
@override
|
||||
Future<T> resolveAsync([dynamic params]) {
|
||||
_checkParams(params);
|
||||
|
||||
final result = _cache ?? _provider(params);
|
||||
final target = result is Future<T> ? result : Future<T>.value(result);
|
||||
|
||||
if (_singleton) {
|
||||
_cache ??= target;
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
@@ -130,8 +166,7 @@ class ProviderResolver<T> implements BindingResolver<T> {
|
||||
@override
|
||||
bool get isSingleton => _singleton;
|
||||
|
||||
/// RU: Проверяет, был ли передан параметр, если провайдер требует его.
|
||||
/// ENG: Checks if parameter is passed when the provider expects it.
|
||||
/// Throws if params required but not supplied.
|
||||
void _checkParams(dynamic params) {
|
||||
if (_withParams && params == null) {
|
||||
throw StateError(
|
||||
|
||||
@@ -14,16 +14,20 @@
|
||||
import 'dart:collection';
|
||||
import 'package:cherrypick/src/observer.dart';
|
||||
|
||||
/// RU: Исключение, выбрасываемое при обнаружении циклической зависимости.
|
||||
/// ENG: Exception thrown when a circular dependency is detected.
|
||||
/// Exception thrown when a circular dependency is detected during dependency resolution.
|
||||
///
|
||||
/// Contains a [message] and the [dependencyChain] showing the resolution cycle.
|
||||
///
|
||||
/// Example diagnostic:
|
||||
/// ```
|
||||
/// CircularDependencyException: Circular dependency detected for A
|
||||
/// Dependency chain: A -> B -> C -> A
|
||||
/// ```
|
||||
class CircularDependencyException implements Exception {
|
||||
final String message;
|
||||
final List<String> dependencyChain;
|
||||
|
||||
CircularDependencyException(this.message, this.dependencyChain) {
|
||||
// DEBUG
|
||||
|
||||
}
|
||||
CircularDependencyException(this.message, this.dependencyChain);
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
@@ -32,8 +36,26 @@ class CircularDependencyException implements Exception {
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Детектор циклических зависимостей для CherryPick DI контейнера.
|
||||
/// ENG: Circular dependency detector for CherryPick DI container.
|
||||
/// Circular dependency detector for CherryPick DI containers.
|
||||
///
|
||||
/// Tracks dependency resolution chains to detect and prevent infinite recursion caused by cycles.
|
||||
/// Whenever a resolve chain re-enters a started dependency, a [CircularDependencyException] is thrown with the full chain.
|
||||
///
|
||||
/// This class is used internally, but you can interact with it through [CycleDetectionMixin].
|
||||
///
|
||||
/// Example usage (pseudocode):
|
||||
/// ```dart
|
||||
/// final detector = CycleDetector(observer: myObserver);
|
||||
/// try {
|
||||
/// detector.startResolving<A>();
|
||||
/// // ... resolving A which depends on B, etc
|
||||
/// detector.startResolving<B>();
|
||||
/// detector.startResolving<A>(); // BOOM: throws exception
|
||||
/// } finally {
|
||||
/// detector.finishResolving<B>();
|
||||
/// detector.finishResolving<A>();
|
||||
/// }
|
||||
/// ```
|
||||
class CycleDetector {
|
||||
final CherryPickObserver _observer;
|
||||
final Set<String> _resolutionStack = HashSet<String>();
|
||||
@@ -41,10 +63,9 @@ class CycleDetector {
|
||||
|
||||
CycleDetector({required CherryPickObserver observer}) : _observer = observer;
|
||||
|
||||
/// RU: Начинает отслеживание разрешения зависимости.
|
||||
/// ENG: Starts tracking dependency resolution.
|
||||
///
|
||||
/// Throws [CircularDependencyException] if circular dependency is detected.
|
||||
/// Starts tracking dependency resolution for type [T] and optional [named] qualifier.
|
||||
///
|
||||
/// Throws [CircularDependencyException] if a cycle is found.
|
||||
void startResolving<T>({String? named}) {
|
||||
final dependencyKey = _createDependencyKey<T>(named);
|
||||
_observer.onDiagnostic(
|
||||
@@ -56,27 +77,21 @@ class CycleDetector {
|
||||
);
|
||||
if (_resolutionStack.contains(dependencyKey)) {
|
||||
final cycleStartIndex = _resolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _resolutionHistory.sublist(cycleStartIndex)..add(dependencyKey);
|
||||
_observer.onCycleDetected(
|
||||
cycle,
|
||||
);
|
||||
_observer.onError(
|
||||
'Cycle detected for $dependencyKey',
|
||||
null,
|
||||
null,
|
||||
);
|
||||
final cycle = _resolutionHistory.sublist(cycleStartIndex)
|
||||
..add(dependencyKey);
|
||||
_observer.onCycleDetected(cycle);
|
||||
_observer.onError('Cycle detected for $dependencyKey', null, null);
|
||||
throw CircularDependencyException(
|
||||
'Circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
);
|
||||
}
|
||||
|
||||
_resolutionStack.add(dependencyKey);
|
||||
_resolutionHistory.add(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Завершает отслеживание разрешения зависимости.
|
||||
/// ENG: Finishes tracking dependency resolution.
|
||||
/// Stops tracking dependency resolution for type [T] and optional [named] qualifier.
|
||||
/// Should always be called after [startResolving], including for errors.
|
||||
void finishResolving<T>({String? named}) {
|
||||
final dependencyKey = _createDependencyKey<T>(named);
|
||||
_observer.onDiagnostic(
|
||||
@@ -84,15 +99,14 @@ class CycleDetector {
|
||||
details: {'event': 'finishResolving'},
|
||||
);
|
||||
_resolutionStack.remove(dependencyKey);
|
||||
// Удаляем из истории только если это последний элемент
|
||||
if (_resolutionHistory.isNotEmpty &&
|
||||
// Only remove from history if it's the last one
|
||||
if (_resolutionHistory.isNotEmpty &&
|
||||
_resolutionHistory.last == dependencyKey) {
|
||||
_resolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Очищает все состояние детектора.
|
||||
/// ENG: Clears all detector state.
|
||||
/// Clears all resolution state and resets the cycle detector.
|
||||
void clear() {
|
||||
_observer.onDiagnostic(
|
||||
'CycleDetector clear',
|
||||
@@ -105,33 +119,46 @@ class CycleDetector {
|
||||
_resolutionHistory.clear();
|
||||
}
|
||||
|
||||
/// RU: Проверяет, находится ли зависимость в процессе разрешения.
|
||||
/// ENG: Checks if dependency is currently being resolved.
|
||||
/// Returns true if dependency [T] (and [named], if specified) is being resolved right now.
|
||||
bool isResolving<T>({String? named}) {
|
||||
final dependencyKey = _createDependencyKey<T>(named);
|
||||
return _resolutionStack.contains(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Возвращает текущую цепочку разрешения зависимостей.
|
||||
/// ENG: Returns current dependency resolution chain.
|
||||
List<String> get currentResolutionChain => List.unmodifiable(_resolutionHistory);
|
||||
/// Gets the current dependency resolution chain (for diagnostics or debugging).
|
||||
List<String> get currentResolutionChain =>
|
||||
List.unmodifiable(_resolutionHistory);
|
||||
|
||||
/// RU: Создает уникальный ключ для зависимости.
|
||||
/// ENG: Creates unique key for dependency.
|
||||
/// Returns a unique string key for type [T] (+name).
|
||||
String _createDependencyKey<T>(String? named) {
|
||||
final typeName = T.toString();
|
||||
return named != null ? '$typeName@$named' : typeName;
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Миксин для добавления поддержки обнаружения циклических зависимостей.
|
||||
/// ENG: Mixin for adding circular dependency detection support.
|
||||
/// Mixin for adding circular dependency detection support to custom DI containers/classes.
|
||||
///
|
||||
/// Fields:
|
||||
/// - `observer`: must be implemented by your class (used for diagnostics and error reporting)
|
||||
///
|
||||
/// Example usage:
|
||||
/// ```dart
|
||||
/// class MyContainer with CycleDetectionMixin {
|
||||
/// @override
|
||||
/// CherryPickObserver get observer => myObserver;
|
||||
/// }
|
||||
///
|
||||
/// final c = MyContainer();
|
||||
/// c.enableCycleDetection();
|
||||
/// c.withCycleDetection(String, null, () {
|
||||
/// // ... dependency resolution code
|
||||
/// });
|
||||
/// ```
|
||||
mixin CycleDetectionMixin {
|
||||
CycleDetector? _cycleDetector;
|
||||
CherryPickObserver get observer;
|
||||
|
||||
/// RU: Включает обнаружение циклических зависимостей.
|
||||
/// ENG: Enables circular dependency detection.
|
||||
/// Turns on circular dependency detection for this class/container.
|
||||
void enableCycleDetection() {
|
||||
_cycleDetector = CycleDetector(observer: observer);
|
||||
observer.onDiagnostic(
|
||||
@@ -143,8 +170,7 @@ mixin CycleDetectionMixin {
|
||||
);
|
||||
}
|
||||
|
||||
/// RU: Отключает обнаружение циклических зависимостей.
|
||||
/// ENG: Disables circular dependency detection.
|
||||
/// Shuts off detection and clears any cycle history for this container.
|
||||
void disableCycleDetection() {
|
||||
_cycleDetector?.clear();
|
||||
observer.onDiagnostic(
|
||||
@@ -157,12 +183,17 @@ mixin CycleDetectionMixin {
|
||||
_cycleDetector = null;
|
||||
}
|
||||
|
||||
/// RU: Проверяет, включено ли обнаружение циклических зависимостей.
|
||||
/// ENG: Checks if circular dependency detection is enabled.
|
||||
/// Returns true if detection is currently enabled.
|
||||
bool get isCycleDetectionEnabled => _cycleDetector != null;
|
||||
|
||||
/// RU: Выполняет действие с отслеживанием циклических зависимостей.
|
||||
/// ENG: Executes action with circular dependency tracking.
|
||||
/// Executes [action] while tracking for circular DI cycles for [dependencyType] and [named].
|
||||
///
|
||||
/// Throws [CircularDependencyException] if a dependency cycle is detected.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// withCycleDetection(String, 'api', () => resolveApi());
|
||||
/// ```
|
||||
T withCycleDetection<T>(
|
||||
Type dependencyType,
|
||||
String? named,
|
||||
@@ -172,22 +203,17 @@ mixin CycleDetectionMixin {
|
||||
return action();
|
||||
}
|
||||
|
||||
final dependencyKey = named != null
|
||||
? '${dependencyType.toString()}@$named'
|
||||
final dependencyKey = named != null
|
||||
? '${dependencyType.toString()}@$named'
|
||||
: dependencyType.toString();
|
||||
|
||||
if (_cycleDetector!._resolutionStack.contains(dependencyKey)) {
|
||||
final cycleStartIndex = _cycleDetector!._resolutionHistory.indexOf(dependencyKey);
|
||||
final cycleStartIndex =
|
||||
_cycleDetector!._resolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _cycleDetector!._resolutionHistory.sublist(cycleStartIndex)
|
||||
..add(dependencyKey);
|
||||
observer.onCycleDetected(
|
||||
cycle,
|
||||
);
|
||||
observer.onError(
|
||||
'Cycle detected for $dependencyKey',
|
||||
null,
|
||||
null,
|
||||
);
|
||||
observer.onCycleDetected(cycle);
|
||||
observer.onError('Cycle detected for $dependencyKey', null, null);
|
||||
throw CircularDependencyException(
|
||||
'Circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
@@ -201,15 +227,14 @@ mixin CycleDetectionMixin {
|
||||
return action();
|
||||
} finally {
|
||||
_cycleDetector!._resolutionStack.remove(dependencyKey);
|
||||
if (_cycleDetector!._resolutionHistory.isNotEmpty &&
|
||||
if (_cycleDetector!._resolutionHistory.isNotEmpty &&
|
||||
_cycleDetector!._resolutionHistory.last == dependencyKey) {
|
||||
_cycleDetector!._resolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Возвращает текущую цепочку разрешения зависимостей.
|
||||
/// ENG: Returns current dependency resolution chain.
|
||||
List<String> get currentResolutionChain =>
|
||||
/// Gets the current active dependency resolution chain.
|
||||
List<String> get currentResolutionChain =>
|
||||
_cycleDetector?.currentResolutionChain ?? [];
|
||||
}
|
||||
|
||||
@@ -12,6 +12,28 @@
|
||||
//
|
||||
import 'package:cherrypick/src/scope.dart';
|
||||
|
||||
/// Abstract factory interface for creating objects of type [T] using a [Scope].
|
||||
///
|
||||
/// Can be implemented for advanced dependency injection scenarios where
|
||||
/// the resolution requires contextual information from the DI [Scope].
|
||||
///
|
||||
/// Often used to supply complex objects, runtime-bound services,
|
||||
/// or objects depending on dynamic configuration.
|
||||
///
|
||||
/// Example usage:
|
||||
/// ```dart
|
||||
/// class MyServiceFactory implements Factory<MyService> {
|
||||
/// @override
|
||||
/// MyService createInstance(Scope scope) {
|
||||
/// final db = scope.resolve<Database>(named: "main");
|
||||
/// return MyService(db);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // Usage in a module:
|
||||
/// bind<MyService>().toProvide(() => MyServiceFactory().createInstance(scope));
|
||||
/// ```
|
||||
abstract class Factory<T> {
|
||||
/// Implement this to provide an instance of [T], with access to the resolving [scope].
|
||||
T createInstance(Scope scope);
|
||||
}
|
||||
|
||||
@@ -14,34 +14,50 @@
|
||||
import 'dart:collection';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
|
||||
/// RU: Глобальный детектор циклических зависимостей для всей иерархии скоупов.
|
||||
/// ENG: Global circular dependency detector for entire scope hierarchy.
|
||||
/// GlobalCycleDetector detects and prevents circular dependencies across an entire DI scope hierarchy.
|
||||
///
|
||||
/// This is particularly important for modular/feature-based applications
|
||||
/// where subscopes can introduce indirect cycles that span different scopes.
|
||||
///
|
||||
/// The detector tracks resolution chains and throws [CircularDependencyException]
|
||||
/// when a cycle is found, showing the full chain (including scope context).
|
||||
///
|
||||
/// Example usage via [GlobalCycleDetectionMixin]:
|
||||
/// ```dart
|
||||
/// class MyScope with GlobalCycleDetectionMixin { /* ... */ }
|
||||
///
|
||||
/// final scope = MyScope();
|
||||
/// scope.setScopeId('feature');
|
||||
/// scope.enableGlobalCycleDetection();
|
||||
///
|
||||
/// scope.withGlobalCycleDetection(String, null, () {
|
||||
/// // ... resolve dependencies here, will detect cross-scope cycles
|
||||
/// });
|
||||
/// ```
|
||||
class GlobalCycleDetector {
|
||||
static GlobalCycleDetector? _instance;
|
||||
|
||||
final CherryPickObserver _observer;
|
||||
|
||||
// Глобальный стек разрешения зависимостей
|
||||
|
||||
// Global set and chain history for all resolutions
|
||||
final Set<String> _globalResolutionStack = HashSet<String>();
|
||||
|
||||
// История разрешения для построения цепочки зависимостей
|
||||
final List<String> _globalResolutionHistory = [];
|
||||
|
||||
// Карта активных детекторов по скоупам
|
||||
final Map<String, CycleDetector> _scopeDetectors = HashMap<String, CycleDetector>();
|
||||
|
||||
GlobalCycleDetector._internal({required CherryPickObserver observer}): _observer = observer;
|
||||
// Map of active detectors for subscopes (rarely used directly)
|
||||
final Map<String, CycleDetector> _scopeDetectors =
|
||||
HashMap<String, CycleDetector>();
|
||||
|
||||
/// RU: Получить единственный экземпляр глобального детектора.
|
||||
/// ENG: Get singleton instance of global detector.
|
||||
GlobalCycleDetector._internal({required CherryPickObserver observer})
|
||||
: _observer = observer;
|
||||
|
||||
/// Returns the singleton global detector instance, initializing it if needed.
|
||||
static GlobalCycleDetector get instance {
|
||||
_instance ??= GlobalCycleDetector._internal(observer: CherryPick.globalObserver);
|
||||
_instance ??=
|
||||
GlobalCycleDetector._internal(observer: CherryPick.globalObserver);
|
||||
return _instance!;
|
||||
}
|
||||
|
||||
/// RU: Сбросить глобальный детектор (полезно для тестов).
|
||||
/// ENG: Reset global detector (useful for tests).
|
||||
/// Reset internal state (useful for testing).
|
||||
static void reset() {
|
||||
_instance?._globalResolutionStack.clear();
|
||||
_instance?._globalResolutionHistory.clear();
|
||||
@@ -49,70 +65,57 @@ class GlobalCycleDetector {
|
||||
_instance = null;
|
||||
}
|
||||
|
||||
/// RU: Начать отслеживание разрешения зависимости в глобальном контексте.
|
||||
/// ENG: Start tracking dependency resolution in global context.
|
||||
/// Start tracking resolution of dependency [T] with optional [named] and [scopeId].
|
||||
/// Throws [CircularDependencyException] on cycle.
|
||||
void startGlobalResolving<T>({String? named, String? scopeId}) {
|
||||
final dependencyKey = _createDependencyKeyFromType(T, named, scopeId);
|
||||
|
||||
|
||||
if (_globalResolutionStack.contains(dependencyKey)) {
|
||||
// Найдена глобальная циклическая зависимость
|
||||
final cycleStartIndex = _globalResolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _globalResolutionHistory.sublist(cycleStartIndex)..add(dependencyKey);
|
||||
_observer.onCycleDetected(
|
||||
cycle,
|
||||
scopeName: scopeId,
|
||||
);
|
||||
final cycle = _globalResolutionHistory.sublist(cycleStartIndex)
|
||||
..add(dependencyKey);
|
||||
_observer.onCycleDetected(cycle, scopeName: scopeId);
|
||||
_observer.onError(
|
||||
'Global circular dependency detected for $dependencyKey',
|
||||
null,
|
||||
null,
|
||||
);
|
||||
'Global circular dependency detected for $dependencyKey', null, null);
|
||||
throw CircularDependencyException(
|
||||
'Global circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
_globalResolutionStack.add(dependencyKey);
|
||||
_globalResolutionHistory.add(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Завершить отслеживание разрешения зависимости в глобальном контексте.
|
||||
/// ENG: Finish tracking dependency resolution in global context.
|
||||
/// Finish tracking a dependency. Should always be called after [startGlobalResolving].
|
||||
void finishGlobalResolving<T>({String? named, String? scopeId}) {
|
||||
final dependencyKey = _createDependencyKeyFromType(T, named, scopeId);
|
||||
_globalResolutionStack.remove(dependencyKey);
|
||||
|
||||
// Удаляем из истории только если это последний элемент
|
||||
if (_globalResolutionHistory.isNotEmpty &&
|
||||
|
||||
if (_globalResolutionHistory.isNotEmpty &&
|
||||
_globalResolutionHistory.last == dependencyKey) {
|
||||
_globalResolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Выполнить действие с глобальным отслеживанием циклических зависимостей.
|
||||
/// ENG: Execute action with global circular dependency tracking.
|
||||
/// Internally execute [action] with global cycle detection for [dependencyType], [named], [scopeId].
|
||||
/// Throws [CircularDependencyException] on cycle.
|
||||
T withGlobalCycleDetection<T>(
|
||||
Type dependencyType,
|
||||
String? named,
|
||||
String? scopeId,
|
||||
T Function() action,
|
||||
) {
|
||||
final dependencyKey = _createDependencyKeyFromType(dependencyType, named, scopeId);
|
||||
|
||||
final dependencyKey =
|
||||
_createDependencyKeyFromType(dependencyType, named, scopeId);
|
||||
|
||||
if (_globalResolutionStack.contains(dependencyKey)) {
|
||||
final cycleStartIndex = _globalResolutionHistory.indexOf(dependencyKey);
|
||||
final cycle = _globalResolutionHistory.sublist(cycleStartIndex)
|
||||
..add(dependencyKey);
|
||||
_observer.onCycleDetected(
|
||||
cycle,
|
||||
scopeName: scopeId,
|
||||
);
|
||||
_observer.onCycleDetected(cycle, scopeName: scopeId);
|
||||
_observer.onError(
|
||||
'Global circular dependency detected for $dependencyKey',
|
||||
null,
|
||||
null,
|
||||
);
|
||||
'Global circular dependency detected for $dependencyKey', null, null);
|
||||
throw CircularDependencyException(
|
||||
'Global circular dependency detected for $dependencyKey',
|
||||
cycle,
|
||||
@@ -126,38 +129,35 @@ class GlobalCycleDetector {
|
||||
return action();
|
||||
} finally {
|
||||
_globalResolutionStack.remove(dependencyKey);
|
||||
if (_globalResolutionHistory.isNotEmpty &&
|
||||
if (_globalResolutionHistory.isNotEmpty &&
|
||||
_globalResolutionHistory.last == dependencyKey) {
|
||||
_globalResolutionHistory.removeLast();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Получить детектор для конкретного скоупа.
|
||||
/// ENG: Get detector for specific scope.
|
||||
/// Get per-scope detector (not usually needed by consumers).
|
||||
CycleDetector getScopeDetector(String scopeId) {
|
||||
return _scopeDetectors.putIfAbsent(scopeId, () => CycleDetector(observer: CherryPick.globalObserver));
|
||||
return _scopeDetectors.putIfAbsent(
|
||||
scopeId, () => CycleDetector(observer: CherryPick.globalObserver));
|
||||
}
|
||||
|
||||
/// RU: Удалить детектор для скоупа.
|
||||
/// ENG: Remove detector for scope.
|
||||
/// Remove detector for a given scope.
|
||||
void removeScopeDetector(String scopeId) {
|
||||
_scopeDetectors.remove(scopeId);
|
||||
}
|
||||
|
||||
/// RU: Проверить, находится ли зависимость в процессе глобального разрешения.
|
||||
/// ENG: Check if dependency is currently being resolved globally.
|
||||
/// Returns true if dependency [T] is currently being resolved in the global scope.
|
||||
bool isGloballyResolving<T>({String? named, String? scopeId}) {
|
||||
final dependencyKey = _createDependencyKeyFromType(T, named, scopeId);
|
||||
return _globalResolutionStack.contains(dependencyKey);
|
||||
}
|
||||
|
||||
/// RU: Получить текущую глобальную цепочку разрешения зависимостей.
|
||||
/// ENG: Get current global dependency resolution chain.
|
||||
List<String> get globalResolutionChain => List.unmodifiable(_globalResolutionHistory);
|
||||
/// Get current global dependency resolution chain (for debugging or diagnostics).
|
||||
List<String> get globalResolutionChain =>
|
||||
List.unmodifiable(_globalResolutionHistory);
|
||||
|
||||
/// RU: Очистить все состояние детектора.
|
||||
/// ENG: Clear all detector state.
|
||||
/// Clears all global and per-scope state in this detector.
|
||||
void clear() {
|
||||
_globalResolutionStack.clear();
|
||||
_globalResolutionHistory.clear();
|
||||
@@ -167,15 +167,9 @@ class GlobalCycleDetector {
|
||||
|
||||
void _detectorClear(detector) => detector.clear();
|
||||
|
||||
/// RU: Создать уникальный ключ для зависимости с учетом скоупа.
|
||||
/// ENG: Create unique key for dependency including scope.
|
||||
//String _createDependencyKey<T>(String? named, String? scopeId) {
|
||||
// return _createDependencyKeyFromType(T, named, scopeId);
|
||||
//}
|
||||
|
||||
/// RU: Создать уникальный ключ для зависимости по типу с учетом скоупа.
|
||||
/// ENG: Create unique key for dependency by type including scope.
|
||||
String _createDependencyKeyFromType(Type type, String? named, String? scopeId) {
|
||||
/// Creates a unique dependency key string including scope and name (for diagnostics/cycle checks).
|
||||
String _createDependencyKeyFromType(
|
||||
Type type, String? named, String? scopeId) {
|
||||
final typeName = type.toString();
|
||||
final namePrefix = named != null ? '@$named' : '';
|
||||
final scopePrefix = scopeId != null ? '[$scopeId]' : '';
|
||||
@@ -183,40 +177,53 @@ class GlobalCycleDetector {
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Улучшенный миксин для глобального обнаружения циклических зависимостей.
|
||||
/// ENG: Enhanced mixin for global circular dependency detection.
|
||||
/// Enhanced mixin for global circular dependency detection, to be mixed into
|
||||
/// DI scopes or containers that want cross-scope protection.
|
||||
///
|
||||
/// Typical usage pattern:
|
||||
/// ```dart
|
||||
/// class MySubscope with GlobalCycleDetectionMixin { ... }
|
||||
///
|
||||
/// final scope = MySubscope();
|
||||
/// scope.setScopeId('user_profile');
|
||||
/// scope.enableGlobalCycleDetection();
|
||||
///
|
||||
/// scope.withGlobalCycleDetection(UserService, null, () {
|
||||
/// // ... resolve user service and friends, auto-detects global cycles
|
||||
/// });
|
||||
/// ```
|
||||
mixin GlobalCycleDetectionMixin {
|
||||
String? _scopeId;
|
||||
bool _globalCycleDetectionEnabled = false;
|
||||
|
||||
/// RU: Установить идентификатор скоупа для глобального отслеживания.
|
||||
/// ENG: Set scope identifier for global tracking.
|
||||
/// Set the scope's unique identifier for global tracking (should be called at scope initialization).
|
||||
void setScopeId(String scopeId) {
|
||||
_scopeId = scopeId;
|
||||
}
|
||||
|
||||
/// RU: Получить идентификатор скоупа.
|
||||
/// ENG: Get scope identifier.
|
||||
/// Get the scope's id, if configured.
|
||||
String? get scopeId => _scopeId;
|
||||
|
||||
/// RU: Включить глобальное обнаружение циклических зависимостей.
|
||||
/// ENG: Enable global circular dependency detection.
|
||||
/// Enable global cross-scope circular dependency detection.
|
||||
void enableGlobalCycleDetection() {
|
||||
_globalCycleDetectionEnabled = true;
|
||||
}
|
||||
|
||||
/// RU: Отключить глобальное обнаружение циклических зависимостей.
|
||||
/// ENG: Disable global circular dependency detection.
|
||||
/// Disable global cycle detection (no cycle checks will be performed globally).
|
||||
void disableGlobalCycleDetection() {
|
||||
_globalCycleDetectionEnabled = false;
|
||||
}
|
||||
|
||||
/// RU: Проверить, включено ли глобальное обнаружение циклических зависимостей.
|
||||
/// ENG: Check if global circular dependency detection is enabled.
|
||||
/// Returns true if global cycle detection is currently enabled for this scope/container.
|
||||
bool get isGlobalCycleDetectionEnabled => _globalCycleDetectionEnabled;
|
||||
|
||||
/// RU: Выполнить действие с глобальным отслеживанием циклических зависимостей.
|
||||
/// ENG: Execute action with global circular dependency tracking.
|
||||
/// Executes [action] with global cycle detection for [dependencyType] and [named].
|
||||
/// Throws [CircularDependencyException] if a cycle is detected.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// withGlobalCycleDetection(UserService, null, () => resolveUser());
|
||||
/// ```
|
||||
T withGlobalCycleDetection<T>(
|
||||
Type dependencyType,
|
||||
String? named,
|
||||
@@ -234,8 +241,7 @@ mixin GlobalCycleDetectionMixin {
|
||||
);
|
||||
}
|
||||
|
||||
/// RU: Получить текущую глобальную цепочку разрешения зависимостей.
|
||||
/// ENG: Get current global dependency resolution chain.
|
||||
List<String> get globalResolutionChain =>
|
||||
/// Access the current global dependency resolution chain for diagnostics.
|
||||
List<String> get globalResolutionChain =>
|
||||
GlobalCycleDetector.instance.globalResolutionChain;
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ import 'package:cherrypick/src/global_cycle_detector.dart';
|
||||
import 'package:cherrypick/src/observer.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
|
||||
Scope? _rootScope;
|
||||
|
||||
/// Global logger for all [Scope]s managed by [CherryPick].
|
||||
@@ -80,7 +79,8 @@ class CherryPick {
|
||||
if (_globalCycleDetectionEnabled && !_rootScope!.isCycleDetectionEnabled) {
|
||||
_rootScope!.enableCycleDetection();
|
||||
}
|
||||
if (_globalCrossScopeCycleDetectionEnabled && !_rootScope!.isGlobalCycleDetectionEnabled) {
|
||||
if (_globalCrossScopeCycleDetectionEnabled &&
|
||||
!_rootScope!.isGlobalCycleDetectionEnabled) {
|
||||
_rootScope!.enableGlobalCycleDetection();
|
||||
}
|
||||
return _rootScope!;
|
||||
@@ -96,7 +96,8 @@ class CherryPick {
|
||||
/// ```
|
||||
static Future<void> closeRootScope() async {
|
||||
if (_rootScope != null) {
|
||||
await _rootScope!.dispose(); // Автоматический вызов dispose для rootScope!
|
||||
await _rootScope!
|
||||
.dispose(); // Автоматический вызов dispose для rootScope!
|
||||
_rootScope = null;
|
||||
}
|
||||
}
|
||||
@@ -141,13 +142,15 @@ class CherryPick {
|
||||
/// ```dart
|
||||
/// CherryPick.enableCycleDetectionForScope(scopeName: 'api.feature');
|
||||
/// ```
|
||||
static void enableCycleDetectionForScope({String scopeName = '', String separator = '.'}) {
|
||||
static void enableCycleDetectionForScope(
|
||||
{String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
scope.enableCycleDetection();
|
||||
}
|
||||
|
||||
/// Disables cycle detection for a given scope. See [enableCycleDetectionForScope].
|
||||
static void disableCycleDetectionForScope({String scopeName = '', String separator = '.'}) {
|
||||
static void disableCycleDetectionForScope(
|
||||
{String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
scope.disableCycleDetection();
|
||||
}
|
||||
@@ -158,7 +161,8 @@ class CherryPick {
|
||||
/// ```dart
|
||||
/// CherryPick.isCycleDetectionEnabledForScope(scopeName: 'feature.api');
|
||||
/// ```
|
||||
static bool isCycleDetectionEnabledForScope({String scopeName = '', String separator = '.'}) {
|
||||
static bool isCycleDetectionEnabledForScope(
|
||||
{String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
return scope.isCycleDetectionEnabled;
|
||||
}
|
||||
@@ -171,7 +175,8 @@ class CherryPick {
|
||||
/// ```dart
|
||||
/// print(CherryPick.getCurrentResolutionChain(scopeName: 'feature.api'));
|
||||
/// ```
|
||||
static List<String> getCurrentResolutionChain({String scopeName = '', String separator = '.'}) {
|
||||
static List<String> getCurrentResolutionChain(
|
||||
{String scopeName = '', String separator = '.'}) {
|
||||
final scope = _getScope(scopeName, separator);
|
||||
return scope.currentResolutionChain;
|
||||
}
|
||||
@@ -229,14 +234,13 @@ class CherryPick {
|
||||
if (nameParts.isEmpty) {
|
||||
throw Exception('Can not open sub scope because scopeName can not split');
|
||||
}
|
||||
final scope = nameParts.fold(
|
||||
openRootScope(),
|
||||
(Scope previous, String element) => previous.openSubScope(element)
|
||||
);
|
||||
final scope = nameParts.fold(openRootScope(),
|
||||
(Scope previous, String element) => previous.openSubScope(element));
|
||||
if (_globalCycleDetectionEnabled && !scope.isCycleDetectionEnabled) {
|
||||
scope.enableCycleDetection();
|
||||
}
|
||||
if (_globalCrossScopeCycleDetectionEnabled && !scope.isGlobalCycleDetectionEnabled) {
|
||||
if (_globalCrossScopeCycleDetectionEnabled &&
|
||||
!scope.isGlobalCycleDetectionEnabled) {
|
||||
scope.enableGlobalCycleDetection();
|
||||
}
|
||||
return scope;
|
||||
@@ -252,21 +256,21 @@ class CherryPick {
|
||||
/// CherryPick.closeScope(scopeName: 'network.super.api');
|
||||
/// ```
|
||||
@experimental
|
||||
static Future<void> closeScope({String scopeName = '', String separator = '.'}) async {
|
||||
static Future<void> closeScope(
|
||||
{String scopeName = '', String separator = '.'}) async {
|
||||
if (scopeName.isEmpty) {
|
||||
await closeRootScope();
|
||||
return;
|
||||
}
|
||||
final nameParts = scopeName.split(separator);
|
||||
if (nameParts.isEmpty) {
|
||||
throw Exception('Can not close sub scope because scopeName can not split');
|
||||
throw Exception(
|
||||
'Can not close sub scope because scopeName can not split');
|
||||
}
|
||||
if (nameParts.length > 1) {
|
||||
final lastPart = nameParts.removeLast();
|
||||
final scope = nameParts.fold(
|
||||
openRootScope(),
|
||||
(Scope previous, String element) => previous.openSubScope(element)
|
||||
);
|
||||
final scope = nameParts.fold(openRootScope(),
|
||||
(Scope previous, String element) => previous.openSubScope(element));
|
||||
await scope.closeSubScope(lastPart);
|
||||
} else {
|
||||
await openRootScope().closeSubScope(nameParts.first);
|
||||
@@ -316,7 +320,8 @@ class CherryPick {
|
||||
/// print('Global cross-scope detection is ON');
|
||||
/// }
|
||||
/// ```
|
||||
static bool get isGlobalCrossScopeCycleDetectionEnabled => _globalCrossScopeCycleDetectionEnabled;
|
||||
static bool get isGlobalCrossScopeCycleDetectionEnabled =>
|
||||
_globalCrossScopeCycleDetectionEnabled;
|
||||
|
||||
/// Returns the current global dependency resolution chain (across all scopes).
|
||||
///
|
||||
@@ -367,10 +372,11 @@ class CherryPick {
|
||||
/// ```dart
|
||||
/// final featureScope = CherryPick.openGlobalSafeScope(scopeName: 'featureA.api');
|
||||
/// ```
|
||||
static Scope openGlobalSafeScope({String scopeName = '', String separator = '.'}) {
|
||||
static Scope openGlobalSafeScope(
|
||||
{String scopeName = '', String separator = '.'}) {
|
||||
final scope = openScope(scopeName: scopeName, separator: separator);
|
||||
scope.enableCycleDetection();
|
||||
scope.enableGlobalCycleDetection();
|
||||
return scope;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,39 +15,71 @@ import 'dart:collection';
|
||||
import 'package:cherrypick/src/binding.dart';
|
||||
import 'package:cherrypick/src/scope.dart';
|
||||
|
||||
/// RU: Класс Module является основой для пользовательских модулей.
|
||||
/// Этот класс нужен для инициализации [Scope].
|
||||
/// Represents a DI module—a reusable group of dependency bindings.
|
||||
///
|
||||
/// RU: The Module class is the basis for custom modules.
|
||||
/// This class is needed to initialize [Scope].
|
||||
/// Extend [Module] to declaratively group related [Binding] definitions,
|
||||
/// then install your module(s) into a [Scope] for dependency resolution.
|
||||
///
|
||||
/// Modules make it easier to organize your DI configuration for features, layers,
|
||||
/// infrastructure, or integration, and support modular app architecture.
|
||||
///
|
||||
/// Usage example:
|
||||
/// ```dart
|
||||
/// class AppModule extends Module {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<NetworkService>().toProvide(() => NetworkService());
|
||||
/// bind<AuthService>().toProvide(() => AuthService(currentScope.resolve<NetworkService>()));
|
||||
/// bind<Config>().toInstance(Config.dev());
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // Installing the module into the root DI scope:
|
||||
/// final rootScope = CherryPick.openRootScope();
|
||||
/// rootScope.installModules([AppModule()]);
|
||||
/// ```
|
||||
///
|
||||
/// Combine several modules and submodules to implement scalable architectures.
|
||||
///
|
||||
abstract class Module {
|
||||
final Set<Binding> _bindingSet = HashSet();
|
||||
|
||||
/// RU: Метод добавляет в коллекцию модуля [Binding] экземпляр.
|
||||
/// Begins the declaration of a new binding within this module.
|
||||
///
|
||||
/// ENG: The method adds an instance to the collection of the [Binding] module.
|
||||
/// Typically used within [builder] to register all needed dependency bindings.
|
||||
///
|
||||
/// return [Binding<T>]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// bind<Api>().toProvide(() => MockApi());
|
||||
/// bind<Config>().toInstance(Config.dev());
|
||||
/// ```
|
||||
Binding<T> bind<T>() {
|
||||
final binding = Binding<T>();
|
||||
_bindingSet.add(binding);
|
||||
return binding;
|
||||
}
|
||||
|
||||
/// RU: Метод возвращает коллекцию [Binding] экземпляров.
|
||||
/// Returns the set of all [Binding] instances registered in this module.
|
||||
///
|
||||
/// ENG: The method returns a collection of [Binding] instances.
|
||||
///
|
||||
/// return [Set<Binding>]
|
||||
/// This is typically used internally by [Scope] during module installation,
|
||||
/// but can also be used for diagnostics or introspection.
|
||||
Set<Binding> get bindingSet => _bindingSet;
|
||||
|
||||
/// RU: Абстрактный метод для инициализации пользовательских экземпляров.
|
||||
/// В этом методе осуществляется конфигурация зависимостей.
|
||||
/// Abstract method where all dependency bindings are registered.
|
||||
///
|
||||
/// ENG: Abstract method for initializing custom instances.
|
||||
/// This method configures dependencies.
|
||||
/// Override this method in your custom module subclass to declare
|
||||
/// all dependency bindings to be provided by this module.
|
||||
///
|
||||
/// return [void]
|
||||
/// The provided [currentScope] can be used for resolving other dependencies,
|
||||
/// accessing configuration, or controlling binding behavior dynamically.
|
||||
///
|
||||
/// Example (with dependency chaining):
|
||||
/// ```dart
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<ApiClient>().toProvide(() => RestApi());
|
||||
/// bind<UserRepo>().toProvide(() => UserRepo(currentScope.resolve<ApiClient>()));
|
||||
/// }
|
||||
/// ```
|
||||
void builder(Scope currentScope);
|
||||
}
|
||||
|
||||
@@ -49,7 +49,8 @@ abstract class CherryPickObserver {
|
||||
/// ```dart
|
||||
/// observer.onInstanceCreated('MyService', MyService, instance, scopeName: 'root');
|
||||
/// ```
|
||||
void onInstanceCreated(String name, Type type, Object instance, {String? scopeName});
|
||||
void onInstanceCreated(String name, Type type, Object instance,
|
||||
{String? scopeName});
|
||||
|
||||
/// Called when an instance is disposed (removed from cache and/or finalized).
|
||||
///
|
||||
@@ -57,7 +58,8 @@ abstract class CherryPickObserver {
|
||||
/// ```dart
|
||||
/// observer.onInstanceDisposed('MyService', MyService, instance, scopeName: 'root');
|
||||
/// ```
|
||||
void onInstanceDisposed(String name, Type type, Object instance, {String? scopeName});
|
||||
void onInstanceDisposed(String name, Type type, Object instance,
|
||||
{String? scopeName});
|
||||
|
||||
// === Module events ===
|
||||
/// Called when modules are installed into the container.
|
||||
@@ -157,19 +159,23 @@ class PrintCherryPickObserver implements CherryPickObserver {
|
||||
print('[request][CherryPick] $name — $type (scope: $scopeName)');
|
||||
|
||||
@override
|
||||
void onInstanceCreated(String name, Type type, Object instance, {String? scopeName}) =>
|
||||
print('[create][CherryPick] $name — $type => $instance (scope: $scopeName)');
|
||||
void onInstanceCreated(String name, Type type, Object instance,
|
||||
{String? scopeName}) =>
|
||||
print(
|
||||
'[create][CherryPick] $name — $type => $instance (scope: $scopeName)');
|
||||
|
||||
@override
|
||||
void onInstanceDisposed(String name, Type type, Object instance, {String? scopeName}) =>
|
||||
print('[dispose][CherryPick] $name — $type => $instance (scope: $scopeName)');
|
||||
void onInstanceDisposed(String name, Type type, Object instance,
|
||||
{String? scopeName}) =>
|
||||
print(
|
||||
'[dispose][CherryPick] $name — $type => $instance (scope: $scopeName)');
|
||||
|
||||
@override
|
||||
void onModulesInstalled(List<String> modules, {String? scopeName}) =>
|
||||
print('[modules installed][CherryPick] ${modules.join(', ')} (scope: $scopeName)');
|
||||
void onModulesInstalled(List<String> modules, {String? scopeName}) => print(
|
||||
'[modules installed][CherryPick] ${modules.join(', ')} (scope: $scopeName)');
|
||||
@override
|
||||
void onModulesRemoved(List<String> modules, {String? scopeName}) =>
|
||||
print('[modules removed][CherryPick] ${modules.join(', ')} (scope: $scopeName)');
|
||||
void onModulesRemoved(List<String> modules, {String? scopeName}) => print(
|
||||
'[modules removed][CherryPick] ${modules.join(', ')} (scope: $scopeName)');
|
||||
|
||||
@override
|
||||
void onScopeOpened(String name) => print('[scope opened][CherryPick] $name');
|
||||
@@ -178,8 +184,8 @@ class PrintCherryPickObserver implements CherryPickObserver {
|
||||
void onScopeClosed(String name) => print('[scope closed][CherryPick] $name');
|
||||
|
||||
@override
|
||||
void onCycleDetected(List<String> chain, {String? scopeName}) =>
|
||||
print('[cycle][CherryPick] Detected: ${chain.join(' -> ')}${scopeName != null ? ' (scope: $scopeName)' : ''}');
|
||||
void onCycleDetected(List<String> chain, {String? scopeName}) => print(
|
||||
'[cycle][CherryPick] Detected: ${chain.join(' -> ')}${scopeName != null ? ' (scope: $scopeName)' : ''}');
|
||||
|
||||
@override
|
||||
void onCacheHit(String name, Type type, {String? scopeName}) =>
|
||||
@@ -210,9 +216,11 @@ class SilentCherryPickObserver implements CherryPickObserver {
|
||||
@override
|
||||
void onInstanceRequested(String name, Type type, {String? scopeName}) {}
|
||||
@override
|
||||
void onInstanceCreated(String name, Type type, Object instance, {String? scopeName}) {}
|
||||
void onInstanceCreated(String name, Type type, Object instance,
|
||||
{String? scopeName}) {}
|
||||
@override
|
||||
void onInstanceDisposed(String name, Type type, Object instance, {String? scopeName}) {}
|
||||
void onInstanceDisposed(String name, Type type, Object instance,
|
||||
{String? scopeName}) {}
|
||||
@override
|
||||
void onModulesInstalled(List<String> modules, {String? scopeName}) {}
|
||||
@override
|
||||
|
||||
@@ -21,6 +21,37 @@ import 'package:cherrypick/src/module.dart';
|
||||
import 'package:cherrypick/src/observer.dart';
|
||||
// import 'package:cherrypick/src/log_format.dart';
|
||||
|
||||
/// Represents a DI scope (container) for modules, subscopes,
|
||||
/// and dependency resolution (sync/async) in CherryPick.
|
||||
///
|
||||
/// Scopes provide hierarchical DI: you can resolve dependencies from parents,
|
||||
/// override or isolate modules, and manage scope-specific singletons.
|
||||
///
|
||||
/// Each scope:
|
||||
/// - Can install modules ([installModules]) that define [Binding]s
|
||||
/// - Supports parent-child scope tree (see [openSubScope])
|
||||
/// - Can resolve dependencies synchronously ([resolve]) or asynchronously ([resolveAsync])
|
||||
/// - Cleans up resources for [Disposable] objects (see [dispose])
|
||||
/// - Detects dependency cycles (local and global, if enabled)
|
||||
///
|
||||
/// Example usage:
|
||||
/// ```dart
|
||||
/// final rootScope = CherryPick.openRootScope();
|
||||
/// rootScope.installModules([AppModule()]);
|
||||
///
|
||||
/// // Synchronous resolution:
|
||||
/// final auth = rootScope.resolve<AuthService>();
|
||||
///
|
||||
/// // Asynchronous resolution:
|
||||
/// final db = await rootScope.resolveAsync<Database>();
|
||||
///
|
||||
/// // Open a child scope (for a feature, page, or test):
|
||||
/// final userScope = rootScope.openSubScope('user');
|
||||
/// userScope.installModules([UserModule()]);
|
||||
///
|
||||
/// // Proper resource cleanup (calls dispose() on tracked objects)
|
||||
/// await CherryPick.closeRootScope();
|
||||
/// ```
|
||||
class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
final Scope? _parentScope;
|
||||
|
||||
@@ -32,16 +63,13 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
/// COLLECTS all resolved instances that implement [Disposable].
|
||||
final Set<Disposable> _disposables = HashSet();
|
||||
|
||||
/// RU: Метод возвращает родительский [Scope].
|
||||
///
|
||||
/// ENG: The method returns the parent [Scope].
|
||||
///
|
||||
/// return [Scope]
|
||||
/// Returns the parent [Scope] if present, or null if this is the root scope.
|
||||
Scope? get parentScope => _parentScope;
|
||||
|
||||
final Map<String, Scope> _scopeMap = HashMap();
|
||||
|
||||
Scope(this._parentScope, {required CherryPickObserver observer}) : _observer = observer {
|
||||
Scope(this._parentScope, {required CherryPickObserver observer})
|
||||
: _observer = observer {
|
||||
setScopeId(_generateScopeId());
|
||||
observer.onScopeOpened(scopeId ?? 'NO_ID');
|
||||
observer.onDiagnostic(
|
||||
@@ -60,9 +88,9 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
// индекс для мгновенного поиска binding’ов
|
||||
final Map<Object, Map<String?, BindingResolver>> _bindingResolvers = {};
|
||||
|
||||
|
||||
/// RU: Генерирует уникальный идентификатор для скоупа.
|
||||
/// ENG: Generates unique identifier for scope.
|
||||
/// Generates a unique identifier string for this scope instance.
|
||||
///
|
||||
/// Used internally for diagnostics, logging and global scope tracking.
|
||||
String _generateScopeId() {
|
||||
final random = Random();
|
||||
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
||||
@@ -70,16 +98,20 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
return 'scope_${timestamp}_$randomPart';
|
||||
}
|
||||
|
||||
/// RU: Метод открывает дочерний (дополнительный) [Scope].
|
||||
/// Opens a named child [Scope] (subscope) as a descendant of the current scope.
|
||||
///
|
||||
/// ENG: The method opens child (additional) [Scope].
|
||||
/// Subscopes inherit modules and DI context from their parent, but can override or extend bindings.
|
||||
/// Useful for feature-isolation, screens, request/transaction lifetimes, and test separation.
|
||||
///
|
||||
/// return [Scope]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final featureScope = rootScope.openSubScope('feature');
|
||||
/// featureScope.installModules([FeatureModule()]);
|
||||
/// final dep = featureScope.resolve<MyDep>();
|
||||
/// ```
|
||||
Scope openSubScope(String name) {
|
||||
if (!_scopeMap.containsKey(name)) {
|
||||
final childScope = Scope(this, observer: observer); // Наследуем observer вниз по иерархии
|
||||
// print removed (trace)
|
||||
// Наследуем настройки обнаружения циклических зависимостей
|
||||
final childScope = Scope(this, observer: observer);
|
||||
if (isCycleDetectionEnabled) {
|
||||
childScope.enableCycleDetection();
|
||||
}
|
||||
@@ -101,16 +133,19 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
return _scopeMap[name]!;
|
||||
}
|
||||
|
||||
/// RU: Метод закрывает дочерний (дополнительный) [Scope] асинхронно.
|
||||
/// Asynchronously closes and disposes a named child [Scope] (subscope).
|
||||
///
|
||||
/// ENG: The method closes child (additional) [Scope] asynchronously.
|
||||
/// Ensures all [Disposable] objects and internal modules
|
||||
/// in the subscope are properly cleaned up. Also removes any global cycle detectors associated with the subscope.
|
||||
///
|
||||
/// return [Future<void>]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// await rootScope.closeSubScope('feature');
|
||||
/// ```
|
||||
Future<void> closeSubScope(String name) async {
|
||||
final childScope = _scopeMap[name];
|
||||
if (childScope != null) {
|
||||
await childScope.dispose(); // асинхронный вызов
|
||||
// Очищаем детектор для дочернего скоупа
|
||||
await childScope.dispose();
|
||||
if (childScope.scopeId != null) {
|
||||
GlobalCycleDetector.instance.removeScopeDetector(childScope.scopeId!);
|
||||
}
|
||||
@@ -129,11 +164,15 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
_scopeMap.remove(name);
|
||||
}
|
||||
|
||||
/// RU: Метод инициализирует пользовательские модули в [Scope].
|
||||
/// Installs a list of custom [Module]s into the [Scope].
|
||||
///
|
||||
/// ENG: The method initializes custom modules in [Scope].
|
||||
/// Each module registers bindings and configuration for dependencies.
|
||||
/// After calling this, bindings are immediately available for resolve/tryResolve.
|
||||
///
|
||||
/// return [Scope]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// rootScope.installModules([AppModule(), NetworkModule()]);
|
||||
/// ```
|
||||
Scope installModules(List<Module> modules) {
|
||||
_modulesList.addAll(modules);
|
||||
if (modules.isNotEmpty) {
|
||||
@@ -153,7 +192,7 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
},
|
||||
);
|
||||
module.builder(this);
|
||||
// После builder: для всех новых биндингов
|
||||
// Associate bindings with this scope's observer
|
||||
for (final binding in module.bindingSet) {
|
||||
binding.observer = observer;
|
||||
binding.logAllDeferred();
|
||||
@@ -163,11 +202,15 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Метод удаляет пользовательские модули из [Scope].
|
||||
/// Removes all installed [Module]s and their bindings from this [Scope].
|
||||
///
|
||||
/// ENG: This method removes custom modules from [Scope].
|
||||
/// Typically used in tests or when resetting app configuration/runtime environment.
|
||||
/// Note: this does not dispose resolved [Disposable]s (call [dispose] for that).
|
||||
///
|
||||
/// return [Scope]
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// testScope.dropModules();
|
||||
/// ```
|
||||
Scope dropModules() {
|
||||
if (_modulesList.isNotEmpty) {
|
||||
observer.onModulesRemoved(
|
||||
@@ -188,24 +231,22 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Возвращает найденную зависимость, определенную параметром типа [T].
|
||||
/// Выдает [StateError], если зависимость не может быть разрешена.
|
||||
/// Если вы хотите получить [null], если зависимость не может быть найдена,
|
||||
/// то используйте вместо этого [tryResolve]
|
||||
/// return - возвращает объект типа [T] или [StateError]
|
||||
/// Resolves a dependency of type [T], optionally by name and with params.
|
||||
///
|
||||
/// ENG: Returns the found dependency specified by the type parameter [T].
|
||||
/// Throws [StateError] if the dependency cannot be resolved.
|
||||
/// If you want to get [null] if the dependency cannot be found then use [tryResolve] instead
|
||||
/// return - returns an object of type [T] or [StateError]
|
||||
/// Throws [StateError] if the dependency cannot be resolved. (Use [tryResolve] for fallible lookup).
|
||||
/// Resolves from installed modules or recurses up the parent scope chain.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final logger = scope.resolve<Logger>();
|
||||
/// final special = scope.resolve<Service>(named: 'special');
|
||||
/// ```
|
||||
T resolve<T>({String? named, dynamic params}) {
|
||||
observer.onInstanceRequested(T.toString(), T, scopeName: scopeId);
|
||||
// Используем глобальное отслеживание, если включено
|
||||
T result;
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
try {
|
||||
result = withGlobalCycleDetection<T>(T, named, () {
|
||||
result = withGlobalCycleDetection<T>(T, named, () {
|
||||
return _resolveWithLocalDetection<T>(named: named, params: params);
|
||||
});
|
||||
} catch (e, s) {
|
||||
@@ -232,13 +273,15 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
return result;
|
||||
}
|
||||
|
||||
/// RU: Разрешение с локальным детектором циклических зависимостей.
|
||||
/// ENG: Resolution with local circular dependency detector.
|
||||
/// Resolves [T] using the local cycle detector for this scope.
|
||||
/// Throws [StateError] if not found or cycle is detected.
|
||||
/// Used internally by [resolve].
|
||||
T _resolveWithLocalDetection<T>({String? named, dynamic params}) {
|
||||
return withCycleDetection<T>(T, named, () {
|
||||
var resolved = _tryResolveInternal<T>(named: named, params: params);
|
||||
if (resolved != null) {
|
||||
observer.onInstanceCreated(T.toString(), T, resolved, scopeName: scopeId);
|
||||
observer.onInstanceCreated(T.toString(), T, resolved,
|
||||
scopeName: scopeId);
|
||||
observer.onDiagnostic(
|
||||
'Successfully resolved: $T',
|
||||
details: {
|
||||
@@ -262,11 +305,16 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
});
|
||||
}
|
||||
|
||||
/// RU: Возвращает найденную зависимость типа [T] или null, если она не может быть найдена.
|
||||
/// ENG: Returns found dependency of type [T] or null if it cannot be found.
|
||||
/// Attempts to resolve a dependency of type [T], optionally by name and with params.
|
||||
///
|
||||
/// Returns the resolved dependency, or `null` if not found.
|
||||
/// Does not throw if missing (unlike [resolve]).
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final maybeDb = scope.tryResolve<Database>();
|
||||
/// ```
|
||||
T? tryResolve<T>({String? named, dynamic params}) {
|
||||
// Используем глобальное отслеживание, если включено
|
||||
T? result;
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
result = withGlobalCycleDetection<T?>(T, named, () {
|
||||
@@ -279,8 +327,8 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
return result;
|
||||
}
|
||||
|
||||
/// RU: Попытка разрешения с локальным детектором циклических зависимостей.
|
||||
/// ENG: Try resolution with local circular dependency detector.
|
||||
/// Attempts to resolve [T] using the local cycle detector. Returns null if not found or cycle.
|
||||
/// Used internally by [tryResolve].
|
||||
T? _tryResolveWithLocalDetection<T>({String? named, dynamic params}) {
|
||||
if (isCycleDetectionEnabled) {
|
||||
return withCycleDetection<T?>(T, named, () {
|
||||
@@ -291,48 +339,49 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Внутренний метод для разрешения зависимостей без проверки циклических зависимостей.
|
||||
/// ENG: Internal method for dependency resolution without circular dependency checking.
|
||||
/// Locates and resolves [T] without cycle detection (direct lookup).
|
||||
/// Returns null if not found. Used internally.
|
||||
T? _tryResolveInternal<T>({String? named, dynamic params}) {
|
||||
final resolver = _findBindingResolver<T>(named);
|
||||
|
||||
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||
// 1 - Try from own modules; 2 - Fallback to parent
|
||||
return resolver?.resolveSync(params) ??
|
||||
// 2 Поиск зависимостей в родительском скоупе
|
||||
_parentScope?.tryResolve(named: named, params: params);
|
||||
}
|
||||
|
||||
/// RU: Асинхронно возвращает найденную зависимость, определенную параметром типа [T].
|
||||
/// Выдает [StateError], если зависимость не может быть разрешена.
|
||||
/// Если хотите получить [null], если зависимость не может быть найдена, используйте [tryResolveAsync].
|
||||
/// return - возвращает объект типа [T] or [StateError]
|
||||
/// Asynchronously resolves a dependency of type [T].
|
||||
///
|
||||
/// ENG: Asynchronously returns the found dependency specified by the type parameter [T].
|
||||
/// Throws [StateError] if the dependency cannot be resolved.
|
||||
/// If you want to get [null] if the dependency cannot be found, use [tryResolveAsync] instead.
|
||||
/// return - returns an object of type [T] or [StateError]
|
||||
/// Throws [StateError] if not found. (Use [tryResolveAsync] for a fallible async resolve.)
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final db = await scope.resolveAsync<Database>();
|
||||
/// final special = await scope.resolveAsync<Service>(named: "special");
|
||||
/// ```
|
||||
Future<T> resolveAsync<T>({String? named, dynamic params}) async {
|
||||
// Используем глобальное отслеживание, если включено
|
||||
T result;
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
result = await withGlobalCycleDetection<Future<T>>(T, named, () async {
|
||||
return await _resolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
return await _resolveAsyncWithLocalDetection<T>(
|
||||
named: named, params: params);
|
||||
});
|
||||
} else {
|
||||
result = await _resolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
result = await _resolveAsyncWithLocalDetection<T>(
|
||||
named: named, params: params);
|
||||
}
|
||||
_trackDisposable(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// RU: Асинхронное разрешение с локальным детектором циклических зависимостей.
|
||||
/// ENG: Async resolution with local circular dependency detector.
|
||||
Future<T> _resolveAsyncWithLocalDetection<T>({String? named, dynamic params}) async {
|
||||
/// Resolves [T] asynchronously using local cycle detector. Throws if not found.
|
||||
/// Internal implementation for async [resolveAsync].
|
||||
Future<T> _resolveAsyncWithLocalDetection<T>(
|
||||
{String? named, dynamic params}) async {
|
||||
return withCycleDetection<Future<T>>(T, named, () async {
|
||||
var resolved = await _tryResolveAsyncInternal<T>(named: named, params: params);
|
||||
var resolved =
|
||||
await _tryResolveAsyncInternal<T>(named: named, params: params);
|
||||
if (resolved != null) {
|
||||
observer.onInstanceCreated(T.toString(), T, resolved, scopeName: scopeId);
|
||||
observer.onInstanceCreated(T.toString(), T, resolved,
|
||||
scopeName: scopeId);
|
||||
observer.onDiagnostic(
|
||||
'Successfully async resolved: $T',
|
||||
details: {
|
||||
@@ -356,23 +405,32 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
});
|
||||
}
|
||||
|
||||
/// Attempts to asynchronously resolve a dependency of type [T].
|
||||
/// Returns the dependency or null if not present (never throws).
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final user = await scope.tryResolveAsync<User>();
|
||||
/// ```
|
||||
Future<T?> tryResolveAsync<T>({String? named, dynamic params}) async {
|
||||
// Используем глобальное отслеживание, если включено
|
||||
T? result;
|
||||
if (isGlobalCycleDetectionEnabled) {
|
||||
result = await withGlobalCycleDetection<Future<T?>>(T, named, () async {
|
||||
return await _tryResolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
return await _tryResolveAsyncWithLocalDetection<T>(
|
||||
named: named, params: params);
|
||||
});
|
||||
} else {
|
||||
result = await _tryResolveAsyncWithLocalDetection<T>(named: named, params: params);
|
||||
result = await _tryResolveAsyncWithLocalDetection<T>(
|
||||
named: named, params: params);
|
||||
}
|
||||
if (result != null) _trackDisposable(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// RU: Асинхронная попытка разрешения с локальным детектором циклических зависимостей.
|
||||
/// ENG: Async try resolution with local circular dependency detector.
|
||||
Future<T?> _tryResolveAsyncWithLocalDetection<T>({String? named, dynamic params}) async {
|
||||
/// Attempts to resolve [T] asynchronously using local cycle detector. Returns null if missing.
|
||||
/// Internal implementation for async [tryResolveAsync].
|
||||
Future<T?> _tryResolveAsyncWithLocalDetection<T>(
|
||||
{String? named, dynamic params}) async {
|
||||
if (isCycleDetectionEnabled) {
|
||||
return withCycleDetection<Future<T?>>(T, named, () async {
|
||||
return await _tryResolveAsyncInternal<T>(named: named, params: params);
|
||||
@@ -382,21 +440,22 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
}
|
||||
}
|
||||
|
||||
/// RU: Внутренний метод для асинхронного разрешения зависимостей без проверки циклических зависимостей.
|
||||
/// ENG: Internal method for async dependency resolution without circular dependency checking.
|
||||
Future<T?> _tryResolveAsyncInternal<T>({String? named, dynamic params}) async {
|
||||
/// Direct async resolution for [T] without cycle check. Returns null if missing. Internal use only.
|
||||
Future<T?> _tryResolveAsyncInternal<T>(
|
||||
{String? named, dynamic params}) async {
|
||||
final resolver = _findBindingResolver<T>(named);
|
||||
|
||||
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||
// 1 - Try from own modules; 2 - Fallback to parent
|
||||
return resolver?.resolveAsync(params) ??
|
||||
// 2 Поиск зависимостей в родительском скоупе
|
||||
_parentScope?.tryResolveAsync(named: named, params: params);
|
||||
}
|
||||
|
||||
/// Looks up the [BindingResolver] for [T] and [named] within this scope.
|
||||
/// Returns null if none found. Internal use only.
|
||||
BindingResolver<T>? _findBindingResolver<T>(String? named) =>
|
||||
_bindingResolvers[T]?[named] as BindingResolver<T>?;
|
||||
|
||||
// Индексируем все binding’и после каждого installModules/dropModules
|
||||
/// Rebuilds the internal index of all [BindingResolver]s from installed modules.
|
||||
/// Called after [installModules] and [dropModules]. Internal use only.
|
||||
void _rebuildResolversIndex() {
|
||||
_bindingResolvers.clear();
|
||||
for (var module in _modulesList) {
|
||||
@@ -408,26 +467,41 @@ class Scope with CycleDetectionMixin, GlobalCycleDetectionMixin {
|
||||
}
|
||||
}
|
||||
|
||||
/// INTERNAL: Tracks Disposable objects
|
||||
/// Tracks resolved [Disposable] instances, to ensure dispose is called automatically.
|
||||
/// Internal use only.
|
||||
void _trackDisposable(Object? obj) {
|
||||
if (obj is Disposable && !_disposables.contains(obj)) {
|
||||
_disposables.add(obj);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls dispose on all tracked disposables and child scopes recursively (async).
|
||||
/// Asynchronously disposes this [Scope], all tracked [Disposable] objects, and recursively
|
||||
/// all its child subscopes.
|
||||
///
|
||||
/// This method should always be called when a scope is no longer needed
|
||||
/// to guarantee timely resource cleanup (files, sockets, streams, handles, etc).
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// await myScope.dispose();
|
||||
/// ```
|
||||
Future<void> dispose() async {
|
||||
// First dispose children scopes
|
||||
for (final subScope in _scopeMap.values) {
|
||||
// Create copies to avoid concurrent modification
|
||||
final scopesCopy = Map<String, Scope>.from(_scopeMap);
|
||||
for (final subScope in scopesCopy.values) {
|
||||
await subScope.dispose();
|
||||
}
|
||||
_scopeMap.clear();
|
||||
// Then dispose own disposables
|
||||
for (final d in _disposables) {
|
||||
try {
|
||||
await d.dispose();
|
||||
} catch (_) {}
|
||||
|
||||
final disposablesCopy = Set<Disposable>.from(_disposables);
|
||||
for (final d in disposablesCopy) {
|
||||
await d.dispose();
|
||||
}
|
||||
_disposables.clear();
|
||||
|
||||
// Clear modules
|
||||
_modulesList.clear();
|
||||
// Clear binding-index
|
||||
_bindingResolvers.clear();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
name: cherrypick
|
||||
description: Cherrypick is a small dependency injection (DI) library for dart/flutter projects.
|
||||
version: 3.0.0-dev.8
|
||||
homepage: https://pese-git.github.io/cherrypick-site/
|
||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||
version: 3.0.2
|
||||
homepage: https://cherrypick-di.netlify.app
|
||||
documentation: https://cherrypick-di.netlify.app/docs/intro
|
||||
repository: https://github.com/pese-git/cherrypick
|
||||
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
||||
topics:
|
||||
@@ -13,14 +13,14 @@ topics:
|
||||
- inversion-of-control
|
||||
|
||||
environment:
|
||||
sdk: ">=3.5.2 <4.0.0"
|
||||
sdk: '>=3.2.0 <4.0.0'
|
||||
|
||||
dependencies:
|
||||
meta: ^1.3.0
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^5.0.0
|
||||
test: ^1.25.15
|
||||
lints: ^4.0.0
|
||||
test: ^1.25.6
|
||||
|
||||
mockito: ^5.0.6
|
||||
mockito: ^5.4.4
|
||||
melos: ^6.3.2
|
||||
|
||||
65
cherrypick/test/binding_memory_leak_test.dart
Normal file
65
cherrypick/test/binding_memory_leak_test.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'dart:async';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
class HeavyService implements Disposable {
|
||||
static int instanceCount = 0;
|
||||
HeavyService() {
|
||||
instanceCount++;
|
||||
print('HeavyService created. Instance count: '
|
||||
'\u001b[32m$instanceCount\u001b[0m');
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
instanceCount--;
|
||||
print('HeavyService disposed. Instance count: '
|
||||
'\u001b[31m$instanceCount\u001b[0m');
|
||||
}
|
||||
|
||||
static final Finalizer<String> _finalizer = Finalizer((msg) {
|
||||
print('GC FINALIZED HeavyService: $msg');
|
||||
});
|
||||
void registerFinalizer() => _finalizer.attach(this, toString(), detach: this);
|
||||
}
|
||||
|
||||
class HeavyModule extends Module {
|
||||
@override
|
||||
void builder(Scope scope) {
|
||||
bind<HeavyService>().toProvide(() => HeavyService());
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('Binding memory is cleared after closing and reopening scope', () async {
|
||||
final root = CherryPick.openRootScope();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
print('\nIteration $i -------------------------------');
|
||||
final subScope = root.openSubScope('leak-test-scope');
|
||||
subScope.installModules([HeavyModule()]);
|
||||
final service = subScope.resolve<HeavyService>();
|
||||
expect(service, isNotNull);
|
||||
await root.closeSubScope('leak-test-scope');
|
||||
// Dart GC не сразу удаляет освобождённые объекты, добавляем паузу и вызываем GC.
|
||||
await Future.delayed(const Duration(milliseconds: 200));
|
||||
}
|
||||
|
||||
// Если dispose не вызвался, instanceCount > 0 => утечка.
|
||||
expect(HeavyService.instanceCount, equals(0));
|
||||
});
|
||||
|
||||
test('Service is finalized after scope is closed/cleaned', () async {
|
||||
final root = CherryPick.openRootScope();
|
||||
HeavyService? ref;
|
||||
{
|
||||
final sub = root.openSubScope('s');
|
||||
sub.installModules([HeavyModule()]);
|
||||
ref = sub.resolve<HeavyService>();
|
||||
ref.registerFinalizer();
|
||||
expect(HeavyService.instanceCount, 1);
|
||||
await root.closeSubScope('s');
|
||||
}
|
||||
await Future.delayed(const Duration(seconds: 2));
|
||||
expect(HeavyService.instanceCount, 0);
|
||||
});
|
||||
}
|
||||
@@ -12,6 +12,7 @@ class DummyModule extends Module {
|
||||
}
|
||||
|
||||
class A {}
|
||||
|
||||
class B {}
|
||||
|
||||
class CyclicModule extends Module {
|
||||
@@ -52,10 +53,13 @@ void main() {
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
// Проверяем, что цикл зафиксирован либо в errors, либо в diagnostics либо cycles
|
||||
final foundInErrors = observer.errors.any((m) => m.contains('cycle detected'));
|
||||
final foundInDiagnostics = observer.diagnostics.any((m) => m.contains('cycle detected'));
|
||||
final foundInErrors =
|
||||
observer.errors.any((m) => m.contains('cycle detected'));
|
||||
final foundInDiagnostics =
|
||||
observer.diagnostics.any((m) => m.contains('cycle detected'));
|
||||
final foundCycleNotified = observer.cycles.isNotEmpty;
|
||||
expect(foundInErrors || foundInDiagnostics || foundCycleNotified, isTrue,
|
||||
reason: 'Ожидаем хотя бы один лог о цикле! errors: ${observer.errors}\ndiag: ${observer.diagnostics}\ncycles: ${observer.cycles}');
|
||||
reason:
|
||||
'Ожидаем хотя бы один лог о цикле! errors: ${observer.errors}\ndiag: ${observer.diagnostics}\ncycles: ${observer.cycles}');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,9 +15,8 @@ class MockObserver implements CherryPickObserver {
|
||||
void onWarning(String message, {Object? details}) => warnings.add(message);
|
||||
|
||||
@override
|
||||
void onError(String message, Object? error, StackTrace? stackTrace) =>
|
||||
errors.add(
|
||||
'$message${error != null ? ' $error' : ''}${stackTrace != null ? '\n$stackTrace' : ''}');
|
||||
void onError(String message, Object? error, StackTrace? stackTrace) => errors.add(
|
||||
'$message${error != null ? ' $error' : ''}${stackTrace != null ? '\n$stackTrace' : ''}');
|
||||
|
||||
@override
|
||||
void onCycleDetected(List<String> chain, {String? scopeName}) =>
|
||||
@@ -30,9 +29,11 @@ class MockObserver implements CherryPickObserver {
|
||||
@override
|
||||
void onInstanceRequested(String name, Type type, {String? scopeName}) {}
|
||||
@override
|
||||
void onInstanceCreated(String name, Type type, Object instance, {String? scopeName}) {}
|
||||
void onInstanceCreated(String name, Type type, Object instance,
|
||||
{String? scopeName}) {}
|
||||
@override
|
||||
void onInstanceDisposed(String name, Type type, Object instance, {String? scopeName}) {}
|
||||
void onInstanceDisposed(String name, Type type, Object instance,
|
||||
{String? scopeName}) {}
|
||||
@override
|
||||
void onModulesInstalled(List<String> moduleNames, {String? scopeName}) {}
|
||||
@override
|
||||
|
||||
@@ -30,7 +30,7 @@ void main() {
|
||||
final rootScope = CherryPick.openSafeRootScope();
|
||||
final level1Scope = rootScope.openSubScope('level1');
|
||||
final level2Scope = level1Scope.openSubScope('level2');
|
||||
|
||||
|
||||
level1Scope.enableCycleDetection();
|
||||
level2Scope.enableCycleDetection();
|
||||
|
||||
@@ -46,14 +46,16 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('current implementation limitation - may not detect cross-scope cycles', () {
|
||||
test(
|
||||
'current implementation limitation - may not detect cross-scope cycles',
|
||||
() {
|
||||
// Этот тест демонстрирует ограничение текущей реализации
|
||||
final parentScope = CherryPick.openRootScope();
|
||||
parentScope.enableCycleDetection();
|
||||
|
||||
|
||||
final childScope = parentScope.openSubScope('child');
|
||||
// НЕ включаем cycle detection для дочернего скоупа
|
||||
|
||||
|
||||
parentScope.installModules([ParentScopeModule()]);
|
||||
childScope.installModules([ChildScopeModule()]);
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ void main() {
|
||||
|
||||
test('should detect simple circular dependency', () {
|
||||
detector.startResolving<String>();
|
||||
|
||||
|
||||
expect(
|
||||
() => detector.startResolving<String>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
@@ -27,7 +27,7 @@ void main() {
|
||||
|
||||
test('should detect circular dependency with named bindings', () {
|
||||
detector.startResolving<String>(named: 'test');
|
||||
|
||||
|
||||
expect(
|
||||
() => detector.startResolving<String>(named: 'test'),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
@@ -37,7 +37,7 @@ void main() {
|
||||
test('should allow different types to be resolved simultaneously', () {
|
||||
detector.startResolving<String>();
|
||||
detector.startResolving<int>();
|
||||
|
||||
|
||||
expect(() => detector.finishResolving<int>(), returnsNormally);
|
||||
expect(() => detector.finishResolving<String>(), returnsNormally);
|
||||
});
|
||||
@@ -46,32 +46,31 @@ void main() {
|
||||
detector.startResolving<String>();
|
||||
detector.startResolving<int>();
|
||||
detector.startResolving<bool>();
|
||||
|
||||
|
||||
expect(
|
||||
() => detector.startResolving<String>(),
|
||||
throwsA(predicate((e) =>
|
||||
e is CircularDependencyException &&
|
||||
e.dependencyChain.contains('String') &&
|
||||
e.dependencyChain.length > 1
|
||||
)),
|
||||
throwsA(predicate((e) =>
|
||||
e is CircularDependencyException &&
|
||||
e.dependencyChain.contains('String') &&
|
||||
e.dependencyChain.length > 1)),
|
||||
);
|
||||
});
|
||||
|
||||
test('should clear state properly', () {
|
||||
detector.startResolving<String>();
|
||||
detector.clear();
|
||||
|
||||
|
||||
expect(() => detector.startResolving<String>(), returnsNormally);
|
||||
});
|
||||
|
||||
test('should track resolution history correctly', () {
|
||||
detector.startResolving<String>();
|
||||
detector.startResolving<int>();
|
||||
|
||||
|
||||
expect(detector.currentResolutionChain, contains('String'));
|
||||
expect(detector.currentResolutionChain, contains('int'));
|
||||
expect(detector.currentResolutionChain.length, equals(2));
|
||||
|
||||
|
||||
detector.finishResolving<int>();
|
||||
expect(detector.currentResolutionChain.length, equals(1));
|
||||
expect(detector.currentResolutionChain, contains('String'));
|
||||
@@ -82,7 +81,7 @@ void main() {
|
||||
test('should detect circular dependency in real scenario', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
|
||||
|
||||
// Создаем циклическую зависимость: A зависит от B, B зависит от A
|
||||
scope.installModules([
|
||||
CircularModuleA(),
|
||||
@@ -98,7 +97,7 @@ void main() {
|
||||
test('should work normally without cycle detection enabled', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
// Не включаем обнаружение циклических зависимостей
|
||||
|
||||
|
||||
scope.installModules([
|
||||
SimpleModule(),
|
||||
]);
|
||||
@@ -111,7 +110,7 @@ void main() {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
|
||||
|
||||
scope.disableCycleDetection();
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
@@ -119,7 +118,7 @@ void main() {
|
||||
test('should handle named dependencies in cycle detection', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
|
||||
|
||||
scope.installModules([
|
||||
NamedCircularModule(),
|
||||
]);
|
||||
@@ -133,7 +132,7 @@ void main() {
|
||||
test('should detect cycles in async resolution', () async {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
|
||||
|
||||
scope.installModules([
|
||||
AsyncCircularModule(),
|
||||
]);
|
||||
@@ -161,14 +160,16 @@ class ServiceB {
|
||||
class CircularModuleA extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<ServiceA>().toProvide(() => ServiceA(currentScope.resolve<ServiceB>()));
|
||||
bind<ServiceA>()
|
||||
.toProvide(() => ServiceA(currentScope.resolve<ServiceB>()));
|
||||
}
|
||||
}
|
||||
|
||||
class CircularModuleB extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<ServiceB>().toProvide(() => ServiceB(currentScope.resolve<ServiceA>()));
|
||||
bind<ServiceB>()
|
||||
.toProvide(() => ServiceB(currentScope.resolve<ServiceA>()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -210,7 +211,7 @@ class AsyncCircularModule extends Module {
|
||||
final serviceB = await currentScope.resolveAsync<AsyncServiceB>();
|
||||
return AsyncServiceA(serviceB);
|
||||
});
|
||||
|
||||
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
bind<AsyncServiceB>().toProvideAsync(() async {
|
||||
final serviceA = await currentScope.resolveAsync<AsyncServiceA>();
|
||||
|
||||
@@ -22,50 +22,57 @@ void main() {
|
||||
group('Global Cross-Scope Cycle Detection', () {
|
||||
test('should enable global cross-scope cycle detection', () {
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isFalse);
|
||||
|
||||
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should disable global cross-scope cycle detection', () {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isTrue);
|
||||
|
||||
|
||||
CherryPick.disableGlobalCrossScopeCycleDetection();
|
||||
|
||||
|
||||
expect(CherryPick.isGlobalCrossScopeCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
|
||||
test('should automatically enable global cycle detection for new root scope', () {
|
||||
test(
|
||||
'should automatically enable global cycle detection for new root scope',
|
||||
() {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
|
||||
final scope = CherryPick.openRootScope();
|
||||
|
||||
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should automatically enable global cycle detection for existing root scope', () {
|
||||
test(
|
||||
'should automatically enable global cycle detection for existing root scope',
|
||||
() {
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isFalse);
|
||||
|
||||
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Global Safe Scope Creation', () {
|
||||
test('should create global safe root scope with both detections enabled', () {
|
||||
test('should create global safe root scope with both detections enabled',
|
||||
() {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should create global safe sub-scope with both detections enabled', () {
|
||||
final scope = CherryPick.openGlobalSafeScope(scopeName: 'feature.global');
|
||||
|
||||
test('should create global safe sub-scope with both detections enabled',
|
||||
() {
|
||||
final scope =
|
||||
CherryPick.openGlobalSafeScope(scopeName: 'feature.global');
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
@@ -104,7 +111,7 @@ void main() {
|
||||
test('should provide detailed global resolution chain in exception', () {
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
scope.installModules([GlobalParentModule()]);
|
||||
|
||||
|
||||
final childScope = scope.openSubScope('child');
|
||||
childScope.installModules([GlobalChildModule()]);
|
||||
|
||||
@@ -114,11 +121,11 @@ void main() {
|
||||
} catch (e) {
|
||||
expect(e, isA<CircularDependencyException>());
|
||||
final circularError = e as CircularDependencyException;
|
||||
|
||||
|
||||
// Проверяем, что цепочка содержит информацию о скоупах
|
||||
expect(circularError.dependencyChain, isNotEmpty);
|
||||
expect(circularError.dependencyChain.length, greaterThan(1));
|
||||
|
||||
|
||||
// Цепочка должна содержать оба сервиса
|
||||
final chainString = circularError.dependencyChain.join(' -> ');
|
||||
expect(chainString, contains('GlobalServiceA'));
|
||||
@@ -144,11 +151,11 @@ void main() {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
// ignore: unused_local_variable
|
||||
final scope = CherryPick.openGlobalSafeRootScope();
|
||||
|
||||
|
||||
expect(CherryPick.getGlobalResolutionChain(), isEmpty);
|
||||
|
||||
|
||||
CherryPick.clearGlobalCycleDetector();
|
||||
|
||||
|
||||
// После очистки детектор должен быть сброшен
|
||||
expect(CherryPick.getGlobalResolutionChain(), isEmpty);
|
||||
});
|
||||
@@ -157,10 +164,10 @@ void main() {
|
||||
group('Inheritance of Global Settings', () {
|
||||
test('should inherit global cycle detection in child scopes', () {
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
|
||||
final parentScope = CherryPick.openRootScope();
|
||||
final childScope = parentScope.openSubScope('child');
|
||||
|
||||
|
||||
expect(parentScope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
expect(childScope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
@@ -168,9 +175,9 @@ void main() {
|
||||
test('should inherit both local and global cycle detection', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
|
||||
|
||||
final scope = CherryPick.openScope(scopeName: 'feature.test');
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
expect(scope.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
@@ -24,53 +24,59 @@ void main() {
|
||||
group('Global Cycle Detection', () {
|
||||
test('should enable global cycle detection', () {
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isFalse);
|
||||
|
||||
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should disable global cycle detection', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isTrue);
|
||||
|
||||
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
|
||||
test('should automatically enable cycle detection for new root scope when global is enabled', () {
|
||||
test(
|
||||
'should automatically enable cycle detection for new root scope when global is enabled',
|
||||
() {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
|
||||
final scope = CherryPick.openRootScope();
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should automatically enable cycle detection for existing root scope when global is enabled', () {
|
||||
test(
|
||||
'should automatically enable cycle detection for existing root scope when global is enabled',
|
||||
() {
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
|
||||
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should automatically disable cycle detection for existing root scope when global is disabled', () {
|
||||
test(
|
||||
'should automatically disable cycle detection for existing root scope when global is disabled',
|
||||
() {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
|
||||
|
||||
CherryPick.disableGlobalCycleDetection();
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
});
|
||||
|
||||
test('should apply global setting to sub-scopes', () {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
|
||||
final scope = CherryPick.openScope(scopeName: 'test.subscope');
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
@@ -79,9 +85,9 @@ void main() {
|
||||
test('should enable cycle detection for root scope', () {
|
||||
final scope = CherryPick.openRootScope();
|
||||
expect(scope.isCycleDetectionEnabled, isFalse);
|
||||
|
||||
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isTrue);
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
@@ -89,91 +95,103 @@ void main() {
|
||||
test('should disable cycle detection for root scope', () {
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isTrue);
|
||||
|
||||
|
||||
CherryPick.disableCycleDetectionForScope();
|
||||
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isFalse);
|
||||
});
|
||||
|
||||
test('should enable cycle detection for specific scope', () {
|
||||
final scopeName = 'feature.auth';
|
||||
CherryPick.openScope(scopeName: scopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isFalse);
|
||||
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName),
|
||||
isFalse);
|
||||
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isTrue);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName),
|
||||
isTrue);
|
||||
});
|
||||
|
||||
test('should disable cycle detection for specific scope', () {
|
||||
final scopeName = 'feature.auth';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName);
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isTrue);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName),
|
||||
isTrue);
|
||||
|
||||
CherryPick.disableCycleDetectionForScope(scopeName: scopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName), isFalse);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName),
|
||||
isFalse);
|
||||
});
|
||||
});
|
||||
|
||||
group('Safe Scope Creation', () {
|
||||
test('should create safe root scope with cycle detection enabled', () {
|
||||
final scope = CherryPick.openSafeRootScope();
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('should create safe sub-scope with cycle detection enabled', () {
|
||||
final scope = CherryPick.openSafeScope(scopeName: 'feature.safe');
|
||||
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
|
||||
test('safe scope should work independently of global setting', () {
|
||||
// Глобальная настройка отключена
|
||||
expect(CherryPick.isGlobalCycleDetectionEnabled, isFalse);
|
||||
|
||||
final scope = CherryPick.openSafeScope(scopeName: 'feature.independent');
|
||||
|
||||
|
||||
final scope =
|
||||
CherryPick.openSafeScope(scopeName: 'feature.independent');
|
||||
|
||||
expect(scope.isCycleDetectionEnabled, isTrue);
|
||||
});
|
||||
});
|
||||
|
||||
group('Resolution Chain Tracking', () {
|
||||
test('should return empty resolution chain for scope without cycle detection', () {
|
||||
test(
|
||||
'should return empty resolution chain for scope without cycle detection',
|
||||
() {
|
||||
CherryPick.openRootScope();
|
||||
|
||||
|
||||
final chain = CherryPick.getCurrentResolutionChain();
|
||||
|
||||
|
||||
expect(chain, isEmpty);
|
||||
});
|
||||
|
||||
test('should return empty resolution chain for scope with cycle detection but no active resolution', () {
|
||||
test(
|
||||
'should return empty resolution chain for scope with cycle detection but no active resolution',
|
||||
() {
|
||||
CherryPick.enableCycleDetectionForScope();
|
||||
|
||||
|
||||
final chain = CherryPick.getCurrentResolutionChain();
|
||||
|
||||
|
||||
expect(chain, isEmpty);
|
||||
});
|
||||
|
||||
test('should track resolution chain for specific scope', () {
|
||||
final scopeName = 'feature.tracking';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName);
|
||||
|
||||
final chain = CherryPick.getCurrentResolutionChain(scopeName: scopeName);
|
||||
|
||||
|
||||
final chain =
|
||||
CherryPick.getCurrentResolutionChain(scopeName: scopeName);
|
||||
|
||||
expect(chain, isEmpty); // Пустая, так как нет активного разрешения
|
||||
});
|
||||
});
|
||||
|
||||
group('Integration with Circular Dependencies', () {
|
||||
test('should detect circular dependency with global cycle detection enabled', () {
|
||||
test(
|
||||
'should detect circular dependency with global cycle detection enabled',
|
||||
() {
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
|
||||
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.installModules([CircularTestModule()]);
|
||||
|
||||
|
||||
expect(
|
||||
() => scope.resolve<CircularServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
@@ -183,44 +201,54 @@ void main() {
|
||||
test('should detect circular dependency with safe scope', () {
|
||||
final scope = CherryPick.openSafeRootScope();
|
||||
scope.installModules([CircularTestModule()]);
|
||||
|
||||
|
||||
expect(
|
||||
() => scope.resolve<CircularServiceA>(),
|
||||
throwsA(isA<CircularDependencyException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('should not detect circular dependency when cycle detection is disabled', () {
|
||||
test(
|
||||
'should not detect circular dependency when cycle detection is disabled',
|
||||
() {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.installModules([CircularTestModule()]);
|
||||
|
||||
|
||||
// Без обнаружения циклических зависимостей не будет выброшено CircularDependencyException,
|
||||
// но может произойти StackOverflowError при попытке создания объекта
|
||||
expect(() => scope.resolve<CircularServiceA>(),
|
||||
throwsA(isA<StackOverflowError>()));
|
||||
expect(() => scope.resolve<CircularServiceA>(),
|
||||
throwsA(isA<StackOverflowError>()));
|
||||
});
|
||||
});
|
||||
|
||||
group('Scope Name Handling', () {
|
||||
test('should handle empty scope name as root scope', () {
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: '');
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: ''), isTrue);
|
||||
|
||||
expect(
|
||||
CherryPick.isCycleDetectionEnabledForScope(scopeName: ''), isTrue);
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(), isTrue);
|
||||
});
|
||||
|
||||
test('should handle complex scope names', () {
|
||||
final complexScopeName = 'app.feature.auth.login';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: complexScopeName);
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: complexScopeName), isTrue);
|
||||
|
||||
expect(
|
||||
CherryPick.isCycleDetectionEnabledForScope(
|
||||
scopeName: complexScopeName),
|
||||
isTrue);
|
||||
});
|
||||
|
||||
test('should handle custom separator', () {
|
||||
final scopeName = 'app/feature/auth';
|
||||
CherryPick.enableCycleDetectionForScope(scopeName: scopeName, separator: '/');
|
||||
|
||||
expect(CherryPick.isCycleDetectionEnabledForScope(scopeName: scopeName, separator: '/'), isTrue);
|
||||
CherryPick.enableCycleDetectionForScope(
|
||||
scopeName: scopeName, separator: '/');
|
||||
|
||||
expect(
|
||||
CherryPick.isCycleDetectionEnabledForScope(
|
||||
scopeName: scopeName, separator: '/'),
|
||||
isTrue);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -240,7 +268,9 @@ class CircularServiceB {
|
||||
class CircularTestModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<CircularServiceA>().toProvide(() => CircularServiceA(currentScope.resolve<CircularServiceB>()));
|
||||
bind<CircularServiceB>().toProvide(() => CircularServiceB(currentScope.resolve<CircularServiceA>()));
|
||||
bind<CircularServiceA>().toProvide(
|
||||
() => CircularServiceA(currentScope.resolve<CircularServiceB>()));
|
||||
bind<CircularServiceB>().toProvide(
|
||||
() => CircularServiceB(currentScope.resolve<CircularServiceA>()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:cherrypick/cherrypick.dart' show Disposable, Module, Scope, CherryPick;
|
||||
import 'package:cherrypick/cherrypick.dart'
|
||||
show Disposable, Module, Scope, CherryPick;
|
||||
import 'dart:async';
|
||||
import 'package:test/test.dart';
|
||||
import '../mock_logger.dart';
|
||||
@@ -18,7 +19,9 @@ class AsyncExampleDisposable implements Disposable {
|
||||
class AsyncExampleModule extends Module {
|
||||
@override
|
||||
void builder(Scope scope) {
|
||||
bind<AsyncExampleDisposable>().toProvide(() => AsyncExampleDisposable()).singleton();
|
||||
bind<AsyncExampleDisposable>()
|
||||
.toProvide(() => AsyncExampleDisposable())
|
||||
.singleton();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +52,9 @@ class CountingDisposable implements Disposable {
|
||||
class ModuleCountingDisposable extends Module {
|
||||
@override
|
||||
void builder(Scope scope) {
|
||||
bind<CountingDisposable>().toProvide(() => CountingDisposable()).singleton();
|
||||
bind<CountingDisposable>()
|
||||
.toProvide(() => CountingDisposable())
|
||||
.singleton();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,10 +102,9 @@ class AsyncModule extends Module {
|
||||
bind<AsyncCreatedDisposable>()
|
||||
// ignore: deprecated_member_use_from_same_package
|
||||
.toProvideAsync(() async {
|
||||
await Future.delayed(Duration(milliseconds: 10));
|
||||
return AsyncCreatedDisposable();
|
||||
})
|
||||
.singleton();
|
||||
await Future.delayed(Duration(milliseconds: 10));
|
||||
return AsyncCreatedDisposable();
|
||||
}).singleton();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +123,8 @@ void main() {
|
||||
final scope = Scope(null, observer: observer);
|
||||
expect(Scope(scope, observer: observer), isNotNull); // эквивалент
|
||||
});
|
||||
test('closeSubScope removes subscope so next openSubScope returns new', () async {
|
||||
test('closeSubScope removes subscope so next openSubScope returns new',
|
||||
() async {
|
||||
final observer = MockObserver();
|
||||
final scope = Scope(null, observer: observer);
|
||||
final subScope = scope.openSubScope("child");
|
||||
@@ -181,7 +186,8 @@ void main() {
|
||||
});
|
||||
test("After dropModules resolves fail", () {
|
||||
final observer = MockObserver();
|
||||
final scope = Scope(null, observer: observer)..installModules([TestModule<int>(value: 5)]);
|
||||
final scope = Scope(null, observer: observer)
|
||||
..installModules([TestModule<int>(value: 5)]);
|
||||
expect(scope.resolve<int>(), 5);
|
||||
scope.dropModules();
|
||||
expect(() => scope.resolve<int>(), throwsA(isA<StateError>()));
|
||||
@@ -294,7 +300,8 @@ void main() {
|
||||
await scope.dispose();
|
||||
expect(t.disposed, isTrue);
|
||||
});
|
||||
test('scope.disposeAsync calls dispose on all unique disposables', () async {
|
||||
test('scope.disposeAsync calls dispose on all unique disposables',
|
||||
() async {
|
||||
final scope = Scope(null, observer: MockObserver());
|
||||
scope.installModules([ModuleWithDisposable()]);
|
||||
final t1 = scope.resolve<TestDisposable>();
|
||||
@@ -305,7 +312,8 @@ void main() {
|
||||
expect(t1.disposed, isTrue);
|
||||
expect(t2.disposed, isTrue);
|
||||
});
|
||||
test('calling disposeAsync twice does not throw and not call twice', () async {
|
||||
test('calling disposeAsync twice does not throw and not call twice',
|
||||
() async {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.installModules([ModuleWithDisposable()]);
|
||||
final t = scope.resolve<TestDisposable>();
|
||||
@@ -313,7 +321,8 @@ void main() {
|
||||
await scope.dispose();
|
||||
expect(t.disposed, isTrue);
|
||||
});
|
||||
test('Non-disposable dependency is ignored by scope.disposeAsync', () async {
|
||||
test('Non-disposable dependency is ignored by scope.disposeAsync',
|
||||
() async {
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.installModules([ModuleWithDisposable()]);
|
||||
final s = scope.resolve<String>();
|
||||
@@ -327,7 +336,8 @@ void main() {
|
||||
group('Scope/subScope dispose edge cases', () {
|
||||
test('Dispose called in closed subScope only', () async {
|
||||
final root = CherryPick.openRootScope();
|
||||
final sub = root.openSubScope('feature')..installModules([ModuleCountingDisposable()]);
|
||||
final sub = root.openSubScope('feature')
|
||||
..installModules([ModuleCountingDisposable()]);
|
||||
final d = sub.resolve<CountingDisposable>();
|
||||
expect(d.disposeCount, 0);
|
||||
|
||||
@@ -339,7 +349,8 @@ void main() {
|
||||
expect(d.disposeCount, 1);
|
||||
|
||||
// Повторное открытие subScope создает NEW instance (dispose на старый не вызовется снова)
|
||||
final sub2 = root.openSubScope('feature')..installModules([ModuleCountingDisposable()]);
|
||||
final sub2 = root.openSubScope('feature')
|
||||
..installModules([ModuleCountingDisposable()]);
|
||||
final d2 = sub2.resolve<CountingDisposable>();
|
||||
expect(identical(d, d2), isFalse);
|
||||
await root.closeSubScope('feature');
|
||||
@@ -347,8 +358,14 @@ void main() {
|
||||
});
|
||||
test('Dispose for all nested subScopes on root disposeAsync', () async {
|
||||
final root = CherryPick.openRootScope();
|
||||
root.openSubScope('a').openSubScope('b').installModules([ModuleCountingDisposable()]);
|
||||
final d = root.openSubScope('a').openSubScope('b').resolve<CountingDisposable>();
|
||||
root
|
||||
.openSubScope('a')
|
||||
.openSubScope('b')
|
||||
.installModules([ModuleCountingDisposable()]);
|
||||
final d = root
|
||||
.openSubScope('a')
|
||||
.openSubScope('b')
|
||||
.resolve<CountingDisposable>();
|
||||
await root.dispose();
|
||||
expect(d.disposeCount, 1);
|
||||
});
|
||||
@@ -357,11 +374,12 @@ void main() {
|
||||
// --------------------------------------------------------------------------
|
||||
group('Async disposable (Future test)', () {
|
||||
test('Async Disposable is awaited on disposeAsync', () async {
|
||||
final scope = CherryPick.openRootScope()..installModules([AsyncExampleModule()]);
|
||||
final scope = CherryPick.openRootScope()
|
||||
..installModules([AsyncExampleModule()]);
|
||||
final d = scope.resolve<AsyncExampleDisposable>();
|
||||
expect(d.disposed, false);
|
||||
await scope.dispose();
|
||||
expect(d.disposed, true);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,31 @@
|
||||
## 3.0.2-dev.0
|
||||
|
||||
- **REFACTOR**(generator): migrate cherrypick_generator to analyzer element2 API.
|
||||
|
||||
## 3.0.1
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
## 3.0.0
|
||||
|
||||
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
|
||||
|
||||
## 3.0.0-dev.0
|
||||
|
||||
- chore(cherrypick_annotations): sync version with cherrypick 3.0.0-dev.0
|
||||
|
||||
## 1.1.2-dev.2
|
||||
|
||||
- **DOCS**(annotations): improve API documentation and usage example.
|
||||
|
||||
## 1.1.2-dev.1
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
## 1.1.2-dev.0
|
||||
|
||||
- **DOCS**(annotations): unify and improve English DartDoc for all DI annotations.
|
||||
|
||||
## 1.1.1
|
||||
|
||||
- **FIX**(license): correct urls.
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
[](https://github.com/pese-git/cherrypick/actions/workflows/pipeline.yml)
|
||||
[](https://app.netlify.com/projects/cherrypick-di/deploys)
|
||||
|
||||
---
|
||||
|
||||
# cherrypick_annotations
|
||||
|
||||
[](LICENSE)
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
# The core lints are also what is used by pub.dev for scoring packages.
|
||||
|
||||
include: package:lints/recommended.yaml
|
||||
analyzer:
|
||||
errors:
|
||||
camel_case_types: ignore
|
||||
|
||||
# Uncomment the following section to specify additional rules.
|
||||
|
||||
|
||||
111
cherrypick_annotations/example/example.dart
Normal file
111
cherrypick_annotations/example/example.dart
Normal file
@@ -0,0 +1,111 @@
|
||||
// ignore: dangling_library_doc_comments
|
||||
/// Example using cherrypick_annotations together with cherrypick (core) and cherrypick_generator.
|
||||
|
||||
///
|
||||
/// Steps to use this example:
|
||||
/// 1. Make sure your example/pubspec.yaml contains:
|
||||
/// - cherrypick_annotations (this package)
|
||||
/// - cherrypick (core DI engine)
|
||||
/// - cherrypick_generator (as a dev_dependency)
|
||||
/// - build_runner (as a dev_dependency)
|
||||
/// 2. Run code generation to produce DI injectors and mixins:
|
||||
/// ```sh
|
||||
/// dart run build_runner build
|
||||
/// ```
|
||||
/// 3. The `_$ApiScreen` mixin will be generated automatically.
|
||||
/// 4. In your app/bootstrap code, install modules and use the generated features.
|
||||
///
|
||||
/// See documentation and advanced details at:
|
||||
/// https://pub.dev/packages/cherrypick_annotations
|
||||
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
// In a real project, use this import:
|
||||
// import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
// Temporary stub for demonstration purposes only.
|
||||
// In real usage, import 'Module' from `package:cherrypick/cherrypick.dart`.
|
||||
class Module {}
|
||||
|
||||
/// This mixin is a stub for documentation and IDE hints only.
|
||||
/// In a real project, it will be generated by cherrypick_generator after running build_runner.
|
||||
///
|
||||
/// Do not implement or edit this by hand!
|
||||
mixin _$ApiScreen {}
|
||||
|
||||
/// Example UI/service class with dependencies to be injected.
|
||||
///
|
||||
/// The [@injectable] annotation tells the generator to create an injector mixin for this class.
|
||||
/// Fields marked with [@inject] will be automatically filled by the code generator (using DI).
|
||||
@injectable()
|
||||
class ApiScreen with _$ApiScreen {
|
||||
/// The default (main) implementation of the API service.
|
||||
@inject()
|
||||
late final ApiService apiService;
|
||||
|
||||
/// An alternate API (mock) implementation, injected by name using @named.
|
||||
@inject()
|
||||
@named('mock')
|
||||
late final ApiService mockApiService;
|
||||
|
||||
/// Logger injected from another scope (e.g., global singleton).
|
||||
@inject()
|
||||
@scope('global')
|
||||
late final Logger logger;
|
||||
}
|
||||
|
||||
/// Example DI module using CherryPick annotations.
|
||||
///
|
||||
/// The [@module] annotation tells the generator to treat this class as a source of bindings.
|
||||
/// Methods annotated with [@singleton], [@named], [@provide], [@instance] will be registered into the DI container.
|
||||
@module()
|
||||
abstract class AppModule extends Module {
|
||||
/// Global singleton logger available throughout the app.
|
||||
@singleton()
|
||||
Logger provideLogger() => Logger();
|
||||
|
||||
/// Main API implementation, identified with the name 'main'.
|
||||
@named('main')
|
||||
ApiService createApi() => ApiService();
|
||||
|
||||
/// Mock API implementation, identified as 'mock'.
|
||||
@named('mock')
|
||||
ApiService createMockApi() => MockApiService();
|
||||
|
||||
/// UserManager is created with runtime parameters, such as per-user session.
|
||||
@provide()
|
||||
UserManager createManager(@params() Map<String, dynamic> runtimeParams) {
|
||||
return UserManager(runtimeParams['id'] as String);
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Example implementations for demonstration only.
|
||||
// In a real project, these would contain application/service logic.
|
||||
|
||||
/// The main API service.
|
||||
class ApiService {}
|
||||
|
||||
/// A mock API implementation (for development or testing).
|
||||
class MockApiService extends ApiService {}
|
||||
|
||||
/// Manages user operations, created using dynamic (runtime) parameters.
|
||||
class UserManager {
|
||||
final String id;
|
||||
UserManager(this.id);
|
||||
}
|
||||
|
||||
/// Global logger service.
|
||||
class Logger {}
|
||||
|
||||
void main() {
|
||||
// After running code generation, injectors and mixins will be ready to use.
|
||||
// Example integration (pseudo-code):
|
||||
//
|
||||
// import 'package:cherrypick/cherrypick.dart';
|
||||
//
|
||||
// final scope = CherryPick.openRootScope()..installModules([$AppModule()]);
|
||||
// final screen = ApiScreen()..injectFields();
|
||||
// print(screen.apiService); // <-- injected!
|
||||
//
|
||||
// This main() is provided for reference only.
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
library;
|
||||
|
||||
//
|
||||
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -13,6 +11,24 @@ library;
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/// Annotations for use with the CherryPick dependency injection generator.
|
||||
///
|
||||
/// These annotations are used on classes, methods, fields or parameters to
|
||||
/// describe how they should participate in dependency injection.
|
||||
/// See: https://pub.dev/packages/cherrypick
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @injectable()
|
||||
/// class MyService {
|
||||
/// @inject()
|
||||
/// late final Logger logger;
|
||||
/// }
|
||||
/// ```
|
||||
library;
|
||||
|
||||
export 'src/module.dart';
|
||||
export 'src/provide.dart';
|
||||
export 'src/instance.dart';
|
||||
|
||||
@@ -13,22 +13,31 @@
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Annotation for field injection in CherryPick DI framework.
|
||||
/// Apply this to a field, and the code generator will automatically inject
|
||||
/// the appropriate dependency into it.
|
||||
/// Marks a field for dependency injection by CherryPick's code generator.
|
||||
///
|
||||
/// ---
|
||||
/// Use `@inject()` on fields within a class marked with `@injectable()`.
|
||||
/// Such fields will be automatically injected from the DI [Scope]
|
||||
/// when using the generated mixin or calling `.injectFields()`.
|
||||
///
|
||||
/// Аннотация для внедрения зависимости в поле через фреймворк CherryPick DI.
|
||||
/// Поместите её на поле класса — генератор кода автоматически подставит нужную зависимость.
|
||||
///
|
||||
/// Example / Пример:
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// @inject()
|
||||
/// late final SomeService service;
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @injectable()
|
||||
/// class LoginScreen with _\$LoginScreen {
|
||||
/// @inject()
|
||||
/// late final AuthService authService;
|
||||
///
|
||||
/// @inject()
|
||||
/// @named('main')
|
||||
/// late final ApiClient api;
|
||||
/// }
|
||||
///
|
||||
/// // After running build_runner, call:
|
||||
/// // LoginScreen().injectFields();
|
||||
/// ```
|
||||
@experimental
|
||||
// ignore: camel_case_types
|
||||
final class inject {
|
||||
/// Creates an [inject] annotation for field injection.
|
||||
const inject();
|
||||
}
|
||||
|
||||
@@ -13,26 +13,32 @@
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Marks a class as injectable for the CherryPick dependency injection framework.
|
||||
/// If a class is annotated with [@injectable()], the code generator will
|
||||
/// create a mixin to perform automatic injection of fields marked with [@inject].
|
||||
/// Marks a class as injectable, enabling automatic field injection by CherryPick's code generator.
|
||||
///
|
||||
/// ---
|
||||
/// Use `@injectable()` on a class whose fields (marked with `@inject`) you want to be automatically injected from the DI [Scope].
|
||||
/// When used together with code generation (see cherrypick_generator), a mixin will be generated to inject fields.
|
||||
///
|
||||
/// Помечает класс как внедряемый для фреймворка внедрения зависимостей CherryPick.
|
||||
/// Если класс помечен аннотацией [@injectable()], генератор создаст миксин
|
||||
/// для автоматического внедрения полей, отмеченных [@inject].
|
||||
///
|
||||
/// Example / Пример:
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @injectable()
|
||||
/// class MyWidget extends StatelessWidget {
|
||||
/// class ProfileScreen with _\$ProfileScreen {
|
||||
/// @inject()
|
||||
/// late final MyService service;
|
||||
/// late final UserManager manager;
|
||||
///
|
||||
/// @inject()
|
||||
/// @named('main')
|
||||
/// late final ApiClient api;
|
||||
/// }
|
||||
///
|
||||
/// // After running build_runner, call:
|
||||
/// // profileScreen.injectFields();
|
||||
/// ```
|
||||
///
|
||||
/// After running the generator, the mixin (`_\$ProfileScreen`) will be available to help auto-inject all [@inject] fields in your widget/service/controller.
|
||||
@experimental
|
||||
// ignore: camel_case_types
|
||||
final class injectable {
|
||||
/// Creates an [injectable] annotation for classes.
|
||||
const injectable();
|
||||
}
|
||||
|
||||
@@ -11,58 +11,40 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/// ENGLISH:
|
||||
/// Annotation to specify that a new instance should be provided on each request.
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Marks a provider method or class to always create a new instance (factory) in CherryPick DI.
|
||||
///
|
||||
/// Use the `@instance()` annotation for methods or classes in your DI module
|
||||
/// to declare that the DI container must create a new object every time
|
||||
/// the dependency is injected (i.e., no singleton behavior).
|
||||
/// Use `@instance()` to annotate methods or classes in your DI module/class
|
||||
/// when you want a new object to be created on every injection (no singleton caching).
|
||||
/// The default DI lifecycle is instance/factory unless otherwise specified.
|
||||
///
|
||||
/// Example:
|
||||
/// ### Example (in a module method)
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// abstract class FeatureModule {
|
||||
/// @instance()
|
||||
/// Foo foo() => Foo();
|
||||
/// MyService provideService() => MyService();
|
||||
///
|
||||
/// @singleton()
|
||||
/// Logger provideLogger() => Logger();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This will generate:
|
||||
/// ### Example (on a class, with @injectable)
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Foo>().toInstance(() => foo());
|
||||
/// }
|
||||
/// @injectable()
|
||||
/// @instance()
|
||||
/// class MyFactoryClass {
|
||||
/// // ...
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// RUSSIAN (Русский):
|
||||
/// Аннотация для создания нового экземпляра при каждом запросе.
|
||||
///
|
||||
/// Используйте `@instance()` для методов или классов в DI-модуле,
|
||||
/// чтобы указать, что контейнер внедрения зависимостей должен создавать
|
||||
/// новый объект при каждом обращении к зависимости (то есть, не синглтон).
|
||||
///
|
||||
/// Пример:
|
||||
/// ```dart
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// @instance()
|
||||
/// Foo foo() => Foo();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Будет сгенерирован следующий код:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Foo>().toInstance(() => foo());
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
// ignore: camel_case_types
|
||||
/// See also: [@singleton]
|
||||
@experimental
|
||||
final class instance {
|
||||
/// Creates an [instance] annotation for classes or providers.
|
||||
const instance();
|
||||
}
|
||||
|
||||
@@ -11,59 +11,40 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/// ENGLISH:
|
||||
/// Annotation for marking Dart classes or libraries as modules.
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Marks an abstract Dart class as a dependency injection module for CherryPick code generation.
|
||||
///
|
||||
/// Use the `@module()` annotation on abstract classes (or on a library)
|
||||
/// to indicate that the class represents a DI (Dependency Injection) module.
|
||||
/// This is commonly used in code generation tools to automatically register
|
||||
/// and configure dependencies defined within the module.
|
||||
/// Use `@module()` on your abstract class to indicate it provides DI bindings (via provider methods).
|
||||
/// This enables code generation of a concrete module that registers all bindings from your methods.
|
||||
///
|
||||
/// Example:
|
||||
/// Typical usage:
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// // Dependency definitions go here.
|
||||
/// abstract class AppModule {
|
||||
/// @singleton()
|
||||
/// Logger provideLogger() => Logger();
|
||||
///
|
||||
/// @named('mock')
|
||||
/// ApiClient mockApi() => MockApiClient();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Generates code like:
|
||||
/// The generated code will look like:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// // Dependency registration...
|
||||
/// // Dependency registration code...
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// RUSSIAN (Русский):
|
||||
/// Аннотация для пометки классов или библиотек Dart как модуля.
|
||||
///
|
||||
/// Используйте `@module()` для абстрактных классов (или библиотек), чтобы
|
||||
/// показать, что класс реализует DI-модуль (Dependency Injection).
|
||||
/// Обычно используется генераторами кода для автоматической регистрации
|
||||
/// и конфигурирования зависимостей, определённых в модуле.
|
||||
///
|
||||
/// Пример:
|
||||
/// ```dart
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// // Определения зависимостей
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Будет сгенерирован код:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// // Регистрация зависимостей...
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
// ignore: camel_case_types
|
||||
/// See also: [@provide], [@singleton], [@instance], [@named]
|
||||
@experimental
|
||||
final class module {
|
||||
/// Creates a [module] annotation.
|
||||
/// Creates a [module] annotation for use on a DI module class.
|
||||
const module();
|
||||
}
|
||||
|
||||
@@ -11,67 +11,52 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/// ENGLISH:
|
||||
/// Annotation to assign a name or identifier to a class, method, or other element.
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Assigns a name or key identifier to a class, field, factory method or parameter
|
||||
/// for use in multi-registration scenarios (named dependencies) in CherryPick DI.
|
||||
///
|
||||
/// The `@named('value')` annotation allows you to specify a string name
|
||||
/// for a dependency, factory, or injectable. This is useful for distinguishing
|
||||
/// between multiple registrations of the same type in dependency injection,
|
||||
/// code generation, and for providing human-readable metadata.
|
||||
/// Use `@named('key')` to distinguish between multiple bindings/implementations
|
||||
/// of the same type—when registering and when injecting dependencies.
|
||||
///
|
||||
/// Example:
|
||||
/// You can use `@named`:
|
||||
/// - On provider/factory methods in a module
|
||||
/// - On fields with `@inject()` to receive a named instance
|
||||
/// - On function parameters (for method/constructor injection)
|
||||
///
|
||||
/// ### Example: On Provider Method
|
||||
/// ```dart
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// @named('dio')
|
||||
/// Dio dio() => Dio();
|
||||
/// abstract class AppModule {
|
||||
/// @named('main')
|
||||
/// ApiClient apiClient() => ApiClient();
|
||||
///
|
||||
/// @named('mock')
|
||||
/// ApiClient mockApi() => MockApiClient();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This will generate:
|
||||
/// ### Example: On Injectable Field
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Dio>().toProvide(() => dio()).withName('dio').singleton();
|
||||
/// }
|
||||
/// @injectable()
|
||||
/// class WidgetModel with _\$WidgetModel {
|
||||
/// @inject()
|
||||
/// @named('main')
|
||||
/// late final ApiClient api;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// RUSSIAN (Русский):
|
||||
/// Аннотация для задания имени или идентификатора классу, методу или другому элементу.
|
||||
///
|
||||
/// Аннотация `@named('значение')` позволяет указать строковое имя для зависимости,
|
||||
/// фабрики или внедряемого значения. Это удобно для различения нескольких
|
||||
/// регистраций одного типа в DI, генерации кода.
|
||||
///
|
||||
/// Пример:
|
||||
/// ### Example: On Parameter
|
||||
/// ```dart
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// @named('dio')
|
||||
/// Dio dio() => Dio();
|
||||
/// class UserScreen {
|
||||
/// UserScreen(@named('current') User user);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Будет сгенерирован следующий код:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Dio>().toProvide(() => dio()).withName('dio').singleton();
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
// ignore: camel_case_types
|
||||
@experimental
|
||||
final class named {
|
||||
/// EN: The assigned name or identifier for the element.
|
||||
///
|
||||
/// RU: Назначенное имя или идентификатор для элемента.
|
||||
/// The assigned name or identifier for the dependency, provider, or parameter.
|
||||
final String value;
|
||||
|
||||
/// EN: Creates a [named] annotation with the given [value].
|
||||
///
|
||||
/// RU: Создаёт аннотацию [named] с заданным значением [value].
|
||||
/// Creates a [named] annotation with the given [value] key or name.
|
||||
const named(this.value);
|
||||
}
|
||||
|
||||
@@ -11,46 +11,33 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/// ENGLISH:
|
||||
/// Annotation to mark a method parameter for injection with run-time arguments.
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Marks a parameter in a provider method to receive dynamic runtime arguments when resolving a dependency.
|
||||
///
|
||||
/// Use the `@params()` annotation to specify that a particular parameter of a
|
||||
/// provider method should be assigned a value supplied at resolution time,
|
||||
/// rather than during static dependency graph creation. This is useful in DI
|
||||
/// when a dependency must receive dynamic data passed by the consumer
|
||||
/// (via `.withParams(...)` in the generated code).
|
||||
/// Use `@params()` in a DI module/factory method when the value must be supplied by the user/code at injection time,
|
||||
/// not during static wiring (such as user input, navigation arguments, etc).
|
||||
///
|
||||
/// This enables CherryPick and its codegen to generate .withParams or .toProvideWithParams bindings — so your provider can access runtime values.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// @provide()
|
||||
/// String greet(@params() dynamic params) => 'Hello $params';
|
||||
/// ```
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// This will generate:
|
||||
/// @module()
|
||||
/// abstract class FeatureModule {
|
||||
/// @provide
|
||||
/// UserManager createManager(@params Map<String, dynamic> runtimeParams) {
|
||||
/// return UserManager.forUserId(runtimeParams['userId']);
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
/// Usage at injection/resolution:
|
||||
/// ```dart
|
||||
/// bind<String>().toProvideWithParams((args) => greet(args));
|
||||
/// final manager = scope.resolve<UserManager>(params: {'userId': myId});
|
||||
/// ```
|
||||
///
|
||||
/// RUSSIAN (Русский):
|
||||
/// Аннотация для пометки параметра метода, который будет внедряться со значением во время выполнения.
|
||||
///
|
||||
/// Используйте `@params()` чтобы указать, что конкретный параметр метода-провайдера
|
||||
/// должен получать значение, передаваемое в момент обращения к зависимости,
|
||||
/// а не на этапе построения графа зависимостей. Это полезно, если зависимость
|
||||
/// должна получать данные динамически от пользователя или другого процесса
|
||||
/// через `.withParams(...)` в сгенерированном коде.
|
||||
///
|
||||
/// Пример:
|
||||
/// ```dart
|
||||
/// @provide()
|
||||
/// String greet(@params() dynamic params) => 'Hello $params';
|
||||
/// ```
|
||||
///
|
||||
/// Будет сгенерировано:
|
||||
/// ```dart
|
||||
/// bind<String>().toProvideWithParams((args) => greet(args));
|
||||
/// ```
|
||||
// ignore: camel_case_types
|
||||
@experimental
|
||||
final class params {
|
||||
/// Marks a method/constructor parameter as supplied at runtime by the caller.
|
||||
const params();
|
||||
}
|
||||
|
||||
@@ -11,60 +11,34 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/// ENGLISH:
|
||||
/// Annotation to declare a factory/provider method or class as a singleton.
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Marks a method or class as a dependency provider (factory/provider) for CherryPick module code generation.
|
||||
///
|
||||
/// Use the `@singleton()` annotation on methods in your DI module to specify
|
||||
/// that only one instance of the resulting object should be created and shared
|
||||
/// for all consumers. This is especially useful in dependency injection
|
||||
/// frameworks and service locators.
|
||||
/// Use `@provide` on any method inside a `@module()` annotated class when you want that method
|
||||
/// to be used as a DI factory/provider during code generation.
|
||||
///
|
||||
/// This should be used for methods that create dynamic, optional, or complex dependencies, especially
|
||||
/// if you want to control the codegen/injection pipeline explicitly and support parameters.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// abstract class FeatureModule {
|
||||
/// @provide
|
||||
/// Future<Api> provideApi(@params Map<String, dynamic> args) async => ...;
|
||||
///
|
||||
/// @singleton()
|
||||
/// Dio dio() => Dio();
|
||||
/// @provide
|
||||
/// Logger provideLogger() => Logger();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This generates the following code:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Dio>().toProvide(() => dio()).singleton();
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// RUSSIAN (Русский):
|
||||
/// Аннотация для объявления фабричного/провайдерного метода или класса синглтоном.
|
||||
///
|
||||
/// Используйте `@singleton()` для методов внутри DI-модуля, чтобы указать,
|
||||
/// что соответствующий объект (экземпляр класса) должен быть создан только один раз
|
||||
/// и использоваться всеми компонентами приложения (единый общий экземпляр).
|
||||
/// Это характерно для систем внедрения зависимостей и сервис-локаторов.
|
||||
///
|
||||
/// Пример:
|
||||
/// ```dart
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// @singleton()
|
||||
/// Dio dio() => Dio();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Будет сгенерирован следующий код:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Dio>().toProvide(() => dio()).singleton();
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
// ignore: camel_case_types
|
||||
/// See also: [@singleton], [@instance], [@params], [@named]
|
||||
@experimental
|
||||
final class provide {
|
||||
/// Creates a [provide] annotation for marking provider methods/classes in DI modules.
|
||||
const provide();
|
||||
}
|
||||
|
||||
@@ -13,25 +13,43 @@
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Annotation to specify a scope for dependency injection in CherryPick.
|
||||
/// Use this on an injected field to indicate from which scope
|
||||
/// the dependency must be resolved.
|
||||
/// Specifies the DI scope or region from which a dependency should be resolved.
|
||||
///
|
||||
/// ---
|
||||
/// Use `@scope('scopeName')` on an injected field, parameter, or provider method when you want
|
||||
/// to resolve a dependency not from the current scope, but from another named scope/subcontainer.
|
||||
///
|
||||
/// Аннотация для указания области внедрения (scope) в CherryPick.
|
||||
/// Используйте её на инъецируемом поле, чтобы определить из какой области
|
||||
/// должна быть получена зависимость.
|
||||
/// Useful for advanced DI scenarios: multi-feature/state isolation, navigation stacks, explicit subscopes, or testing.
|
||||
///
|
||||
/// Example / Пример:
|
||||
/// Example (injected field):
|
||||
/// ```dart
|
||||
/// @inject()
|
||||
/// @scope('profile')
|
||||
/// late final ProfileManager profileManager;
|
||||
/// @injectable()
|
||||
/// class ProfileScreen with _\$ProfileScreen {
|
||||
/// @inject()
|
||||
/// @scope('profile')
|
||||
/// late final ProfileManager manager;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Example (parameter):
|
||||
/// ```dart
|
||||
/// class TabBarModel {
|
||||
/// TabBarModel(@scope('tabs') TabContext context);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Example (in a module):
|
||||
/// ```dart
|
||||
/// @module()
|
||||
/// abstract class FeatureModule {
|
||||
/// @provide
|
||||
/// Service service(@scope('shared') SharedConfig config);
|
||||
/// }
|
||||
/// ```
|
||||
@experimental
|
||||
// ignore: camel_case_types
|
||||
final class scope {
|
||||
/// The name/key of the DI scope from which to resolve this dependency.
|
||||
final String? name;
|
||||
|
||||
/// Creates a [scope] annotation specifying which DI scope to use for the dependency resolution.
|
||||
const scope(this.name);
|
||||
}
|
||||
|
||||
@@ -11,63 +11,32 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
/// ENGLISH:
|
||||
/// Annotation to declare a dependency as a singleton.
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
/// Marks a provider method or class so its instance is created only once and shared (singleton) for DI in CherryPick.
|
||||
///
|
||||
/// Use the `@singleton()` annotation on provider methods inside a module
|
||||
/// to indicate that only a single instance of this dependency should be
|
||||
/// created and shared throughout the application's lifecycle. This is
|
||||
/// typically used in dependency injection frameworks or service locators
|
||||
/// to guarantee a single shared instance.
|
||||
/// Use `@singleton()` on provider methods or classes in your DI module to ensure only one instance is ever created
|
||||
/// and reused across the application's lifetime (or scope lifetime).
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// abstract class AppModule {
|
||||
/// @singleton()
|
||||
/// Dio dio() => Dio();
|
||||
/// ApiClient createApi() => ApiClient();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// This will generate code like:
|
||||
/// The generated code will ensure:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Dio>().toProvide(() => dio()).singleton();
|
||||
/// }
|
||||
/// }
|
||||
/// bind<ApiClient>().toProvide(() => createApi()).singleton();
|
||||
/// ```
|
||||
///
|
||||
/// RUSSIAN (Русский):
|
||||
/// Аннотация для объявления зависимости как синглтона.
|
||||
///
|
||||
/// Используйте `@singleton()` для методов-провайдеров внутри модуля,
|
||||
/// чтобы указать, что соответствующий объект должен быть создан
|
||||
/// единожды и использоваться во всём приложении (общий синглтон).
|
||||
/// Это характерно для систем внедрения зависимостей и сервис-локаторов,
|
||||
/// чтобы гарантировать один общий экземпляр.
|
||||
///
|
||||
/// Пример:
|
||||
/// ```dart
|
||||
/// @module()
|
||||
/// abstract class AppModule extends Module {
|
||||
/// @singleton()
|
||||
/// Dio dio() => Dio();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Будет сгенерирован следующий код:
|
||||
/// ```dart
|
||||
/// final class $AppModule extends AppModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Dio>().toProvide(() => dio()).singleton();
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
// ignore: camel_case_types
|
||||
/// See also: [@instance], [@provide], [@named]
|
||||
@experimental
|
||||
final class singleton {
|
||||
/// Creates a [singleton] annotation.
|
||||
/// Creates a [singleton] annotation for DI providers/classes.
|
||||
const singleton();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
name: cherrypick_annotations
|
||||
description: |
|
||||
Set of annotations for CherryPick dependency injection library. Enables code generation and declarative DI for Dart & Flutter projects.
|
||||
version: 1.1.1
|
||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||
version: 3.0.2-dev.0
|
||||
homepage: https://cherrypick-di.netlify.app
|
||||
documentation: https://cherrypick-di.netlify.app/docs/intro
|
||||
repository: https://github.com/pese-git/cherrypick/cherrypick_annotations
|
||||
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
||||
topics:
|
||||
@@ -13,7 +14,7 @@ topics:
|
||||
- inversion-of-control
|
||||
|
||||
environment:
|
||||
sdk: ">=3.5.2 <4.0.0"
|
||||
sdk: ">=3.8.0 <4.0.0"
|
||||
|
||||
# Add regular dependencies here.
|
||||
dependencies:
|
||||
@@ -21,5 +22,5 @@ dependencies:
|
||||
# path: ^1.8.0
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^5.0.0
|
||||
lints: ^6.0.0
|
||||
test: ^1.25.8
|
||||
|
||||
@@ -1,3 +1,39 @@
|
||||
## 3.0.2
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
## 3.0.1
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
## 3.0.0
|
||||
|
||||
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
|
||||
|
||||
## 3.0.0-dev.1
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
## 3.0.0-dev.0
|
||||
|
||||
- chore(cherrypick_flutter): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
## 1.1.3-dev.12
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
## 1.1.3-dev.11
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
## 1.1.3-dev.10
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
## 1.1.3-dev.9
|
||||
|
||||
- **DOCS**(provider): add detailed English API documentation for CherryPickProvider Flutter integration.
|
||||
|
||||
## 1.1.3-dev.8
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
[](https://github.com/pese-git/cherrypick/actions/workflows/pipeline.yml)
|
||||
[](https://app.netlify.com/projects/cherrypick-di/deploys)
|
||||
|
||||
---
|
||||
|
||||
# CherryPick Flutter
|
||||
|
||||
`cherrypick_flutter` offers a Flutter integration to access and manage dependency injection scopes using the `CherryPickProvider`. This setup facilitates accessing the root scope directly from the widget tree, providing a straightforward mechanism for dependences management within Flutter applications.
|
||||
|
||||
@@ -14,29 +14,87 @@ import 'package:flutter/widgets.dart';
|
||||
/// limitations under the License.
|
||||
///
|
||||
|
||||
/// {@template cherrypick_flutter_provider}
|
||||
/// An [InheritedWidget] that provides convenient integration of CherryPick
|
||||
/// dependency injection scopes into the Flutter widget tree.
|
||||
///
|
||||
/// Place `CherryPickProvider` at the top of your widget subtree to make a
|
||||
/// [Scope] (or its descendants) available via `CherryPickProvider.of(context)`.
|
||||
///
|
||||
/// This is the recommended entry point for connecting CherryPick DI to your
|
||||
/// Flutter app or feature area, enabling context-based scope management and
|
||||
/// DI resolution in child widgets.
|
||||
///
|
||||
/// ### Example: Root Integration
|
||||
/// ```dart
|
||||
/// void main() {
|
||||
/// final rootScope = CherryPick.openRootScope()
|
||||
/// ..installModules([AppModule()]);
|
||||
/// runApp(
|
||||
/// CherryPickProvider(
|
||||
/// child: MyApp(),
|
||||
/// ),
|
||||
/// );
|
||||
/// }
|
||||
///
|
||||
/// // In any widget:
|
||||
/// final provider = CherryPickProvider.of(context);
|
||||
/// final scope = provider.openRootScope();
|
||||
/// final myService = scope.resolve<MyService>();
|
||||
/// ```
|
||||
///
|
||||
/// ### Example: Subscope for a Feature/Screen
|
||||
/// ```dart
|
||||
/// Widget build(BuildContext context) {
|
||||
/// final provider = CherryPickProvider.of(context);
|
||||
/// final featureScope = provider.openSubScope(scopeName: 'featureA');
|
||||
/// return MyFeatureScreen(scope: featureScope);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// You can use [openRootScope] and [openSubScope] as helpers to get/create scopes.
|
||||
/// {@endtemplate}
|
||||
final class CherryPickProvider extends InheritedWidget {
|
||||
/// Opens (or returns) the application-wide root [Scope].
|
||||
///
|
||||
/// Use to make all dependencies available at the top of your widget tree.
|
||||
Scope openRootScope() => CherryPick.openRootScope();
|
||||
|
||||
/// Opens a subscope (child [Scope]) with the given [scopeName].
|
||||
///
|
||||
/// Useful to create isolated feature/module scopes in widget subtrees.
|
||||
/// If [scopeName] is empty, an unnamed scope is created.
|
||||
Scope openSubScope({String scopeName = '', String separator = '.'}) =>
|
||||
CherryPick.openScope(scopeName: scopeName, separator: separator);
|
||||
|
||||
// Constructor for CherryPickProvider. Initializes with a required rootScope and child widget.
|
||||
/// Creates a [CherryPickProvider] and exposes it to the widget subtree.
|
||||
///
|
||||
/// Place near the root of your widget tree. Use [child] to provide the subtree.
|
||||
const CherryPickProvider({
|
||||
super.key,
|
||||
required super.child,
|
||||
});
|
||||
|
||||
// Method to access the nearest CherryPickProvider instance from the context
|
||||
/// Locates the nearest [CherryPickProvider] up the widget tree from [context].
|
||||
///
|
||||
/// Throws if not found. Use this to access DI [Scope] controls anywhere below the provider.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final provider = CherryPickProvider.of(context);
|
||||
/// final scope = provider.openRootScope();
|
||||
/// ```
|
||||
static CherryPickProvider of(BuildContext context) {
|
||||
// Looks up the widget tree for an instance of CherryPickProvider
|
||||
final CherryPickProvider? result =
|
||||
context.dependOnInheritedWidgetOfExactType<CherryPickProvider>();
|
||||
// Assert to ensure a CherryPickProvider is present in the context
|
||||
assert(result != null, 'No CherryPickProvider found in context');
|
||||
return result!;
|
||||
}
|
||||
|
||||
// Determines whether the widget should notify dependents when it changes
|
||||
/// Controls update notifications for dependent widgets.
|
||||
///
|
||||
/// Always returns false because the provider itself is stateless:
|
||||
/// changes are to the underlying scopes, not the widget.
|
||||
@override
|
||||
bool updateShouldNotify(CherryPickProvider oldWidget) {
|
||||
return false;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
name: cherrypick_flutter
|
||||
description: "Flutter library that allows access to the root scope through the context using `CherryPickProvider`."
|
||||
version: 1.1.3-dev.8
|
||||
homepage: https://pese-git.github.io/cherrypick-site/
|
||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||
version: 3.0.2
|
||||
homepage: https://cherrypick-di.netlify.app
|
||||
documentation: https://cherrypick-di.netlify.app/docs/intro
|
||||
repository: https://github.com/pese-git/cherrypick
|
||||
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
||||
topics:
|
||||
@@ -13,19 +13,19 @@ topics:
|
||||
- inversion-of-control
|
||||
|
||||
environment:
|
||||
sdk: ">=3.5.2 <4.0.0"
|
||||
flutter: ">=3.24.0"
|
||||
sdk: '>=3.2.0 <4.0.0'
|
||||
flutter: ">=3.16.0"
|
||||
|
||||
dependencies:
|
||||
flutter:
|
||||
sdk: flutter
|
||||
cherrypick: ^3.0.0-dev.8
|
||||
cherrypick: ^3.0.2
|
||||
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
flutter_lints: ^5.0.0
|
||||
test: ^1.25.7
|
||||
flutter_lints: ^4.0.0
|
||||
test: ^1.25.6
|
||||
melos: ^6.3.2
|
||||
|
||||
# For information on the generic Dart part of this file, see the
|
||||
|
||||
@@ -1,3 +1,33 @@
|
||||
## 3.0.2-dev.0
|
||||
|
||||
- **REFACTOR**(generator): migrate cherrypick_generator to analyzer element2 API.
|
||||
|
||||
## 3.0.1
|
||||
|
||||
- **DOCS**: add Netlify deployment status badge to README files.
|
||||
|
||||
## 3.0.0
|
||||
|
||||
- Graduate package to a stable release. See pre-releases prior to this version for changelog entries.
|
||||
|
||||
## 3.0.0-dev.0
|
||||
|
||||
- chore(cherrypick_generator): sync version with cherrypick 3.0.0-dev.12
|
||||
|
||||
## 2.0.0-dev.2
|
||||
|
||||
- Update a dependency to the latest release.
|
||||
|
||||
## 2.0.0-dev.1
|
||||
|
||||
- **DOCS**(pub): update homepage and documentation URLs in pubspec.yaml to new official site.
|
||||
|
||||
## 2.0.0-dev.0
|
||||
|
||||
> Note: This release has breaking changes.
|
||||
|
||||
- **BREAKING** **DOCS**(generator): improve and unify English documentation and examples for all DI source files.
|
||||
|
||||
## 1.1.1
|
||||
|
||||
- **FIX**(license): correct urls.
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
[](https://github.com/pese-git/cherrypick/actions/workflows/pipeline.yml)
|
||||
[](https://app.netlify.com/projects/cherrypick-di/deploys)
|
||||
|
||||
---
|
||||
|
||||
# Cherrypick Generator
|
||||
|
||||
**Cherrypick Generator** is a Dart code generation library for automating dependency injection (DI) boilerplate. It processes classes and fields annotated with [cherrypick_annotations](https://pub.dev/packages/cherrypick_annotations) and generates registration code for services, modules, and field injection for classes marked as `@injectable`. It supports advanced DI features such as scopes, named bindings, parameters, and asynchronous dependencies.
|
||||
|
||||
@@ -31,4 +31,5 @@ include: package:lints/recommended.yaml
|
||||
|
||||
analyzer:
|
||||
errors:
|
||||
deprecated_member_use: ignore
|
||||
deprecated_member_use: ignore
|
||||
unintended_html_in_doc_comment: ignore
|
||||
@@ -1,5 +1,3 @@
|
||||
library;
|
||||
|
||||
//
|
||||
// Copyright 2021 Sergey Penkovsky (sergey.penkovsky@gmail.com)
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
@@ -12,6 +10,28 @@ library;
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
library;
|
||||
|
||||
/// CherryPick code generation library: entry point for build_runner DI codegen.
|
||||
///
|
||||
/// This library exports generators for CherryPick dependency injection:
|
||||
/// - [ModuleGenerator]: Generates DI module classes for all `@module()`-annotated classes.
|
||||
/// - [InjectGenerator]: Generates field-injection mixins for classes annotated with `@injectable()`.
|
||||
///
|
||||
/// These generators are hooked into [build_runner] and cherrypick_generator's builder configuration.
|
||||
/// Normally you do not import this directly; instead, add cherrypick_generator
|
||||
/// as a dev_dependency and run `dart run build_runner build`.
|
||||
///
|
||||
/// Example usage in `build.yaml` or your project's workflow:
|
||||
/// ```yaml
|
||||
/// targets:
|
||||
/// $default:
|
||||
/// builders:
|
||||
/// cherrypick_generator|cherrypick_generator:
|
||||
/// generate_for:
|
||||
/// - lib/**.dart
|
||||
/// ```
|
||||
///
|
||||
/// For annotation details, see `package:cherrypick_annotations`.
|
||||
export 'module_generator.dart';
|
||||
export 'inject_generator.dart';
|
||||
|
||||
@@ -11,44 +11,100 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:analyzer/dart/constant/value.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:analyzer/dart/element/nullability_suffix.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
|
||||
|
||||
/// InjectGenerator generates a mixin for a class marked with @injectable()
|
||||
/// and injects all fields annotated with @inject(), using CherryPick DI.
|
||||
/// CherryPick DI field injector generator for codegen.
|
||||
///
|
||||
/// For Future<T> fields it calls .resolveAsync<T>(),
|
||||
/// otherwise .resolve<T>() is used. Scope and named qualifiers are supported.
|
||||
/// Analyzes all Dart classes marked with `@injectable()` and generates a mixin (for example, `_$ProfileScreen`)
|
||||
/// which contains the `_inject` method. This method will assign all fields annotated with `@inject()`
|
||||
/// using the CherryPick DI container. Extra annotation qualifiers such as `@named` and `@scope` are respected
|
||||
/// for each field. Nullable fields and Future/injectable async dependencies are also supported automatically.
|
||||
///
|
||||
/// ---
|
||||
///
|
||||
/// InjectGenerator генерирует миксин для класса с аннотацией @injectable()
|
||||
/// и внедряет все поля, помеченные @inject(), используя DI-фреймворк CherryPick.
|
||||
/// ### Example usage in a project:
|
||||
///
|
||||
/// Для Future<T> полей вызывается .resolveAsync<T>(),
|
||||
/// для остальных — .resolve<T>(). Поддерживаются scope и named qualifier.
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @injectable()
|
||||
/// class MyScreen with _$MyScreen {
|
||||
/// @inject()
|
||||
/// late final Logger logger;
|
||||
///
|
||||
/// @inject()
|
||||
/// @named('test')
|
||||
/// late final HttpClient client;
|
||||
///
|
||||
/// @inject()
|
||||
/// Future<Analytics>? analytics;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// After running build_runner, this mixin will be auto-generated:
|
||||
///
|
||||
/// ```dart
|
||||
/// mixin _$MyScreen {
|
||||
/// void _inject(MyScreen instance) {
|
||||
/// instance.logger = CherryPick.openRootScope().resolve<Logger>();
|
||||
/// instance.client = CherryPick.openRootScope().resolve<HttpClient>(named: 'test');
|
||||
/// instance.analytics = CherryPick.openRootScope().tryResolveAsync<Analytics>(); // nullable async inject
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// You may use the mixin (e.g., `myScreen._inject(myScreen)`) or expose your own public helper for instance field injection.
|
||||
///
|
||||
/// **Supported scenarios:**
|
||||
/// - Ordinary injectable fields: `resolve<T>()`.
|
||||
/// - Named qualifiers: `resolve<T>(named: ...)`.
|
||||
/// - Scoping: `CherryPick.openScope(scopeName: ...).resolve<T>()`.
|
||||
/// - Nullable/incomplete fields: `tryResolve`/`tryResolveAsync`.
|
||||
/// - Async dependencies: `Future<T>`/`resolveAsync<T>()`.
|
||||
///
|
||||
/// See also:
|
||||
/// * @inject
|
||||
/// * @injectable
|
||||
class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
const InjectGenerator();
|
||||
|
||||
/// The main entry point for code generation.
|
||||
/// Main entry point for CherryPick field injection code generation.
|
||||
///
|
||||
/// Checks class validity, collects injectable fields, and produces injection code.
|
||||
/// - Only triggers for classes marked with `@injectable()`.
|
||||
/// - Throws an error if used on non-class elements.
|
||||
/// - Scans all fields marked with `@inject()` and gathers qualifiers (if any).
|
||||
/// - Generates Dart code for a mixin that injects all dependencies into the target class instance.
|
||||
///
|
||||
/// Основная точка входа генератора. Проверяет класс, собирает инъектируемые поля и создает код внедрения зависимостей.
|
||||
/// Returns the Dart code as a String defining the new mixin.
|
||||
///
|
||||
/// Example input (user code):
|
||||
/// ```dart
|
||||
/// @injectable()
|
||||
/// class UserBloc with _$UserBloc {
|
||||
/// @inject() late final AuthRepository authRepository;
|
||||
/// }
|
||||
/// ```
|
||||
/// Example output (generated):
|
||||
/// ```dart
|
||||
/// mixin _$UserBloc {
|
||||
/// void _inject(UserBloc instance) {
|
||||
/// instance.authRepository = CherryPick.openRootScope().resolve<AuthRepository>();
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
@override
|
||||
FutureOr<String> generateForAnnotatedElement(
|
||||
Element element,
|
||||
dynamic generateForAnnotatedElement(
|
||||
Element2 element,
|
||||
ConstantReader annotation,
|
||||
BuildStep buildStep,
|
||||
) {
|
||||
if (element is! ClassElement) {
|
||||
if (element is! ClassElement2) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'@injectable() can only be applied to classes.',
|
||||
element: element,
|
||||
@@ -56,17 +112,17 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
}
|
||||
|
||||
final classElement = element;
|
||||
final className = classElement.name;
|
||||
final className = classElement.firstFragment.name2;
|
||||
final mixinName = '_\$$className';
|
||||
|
||||
final buffer = StringBuffer()
|
||||
..writeln('mixin $mixinName {')
|
||||
..writeln(' void _inject($className instance) {');
|
||||
|
||||
// Collect and process all @inject fields.
|
||||
// Собираем и обрабатываем все поля с @inject.
|
||||
final injectFields =
|
||||
classElement.fields.where(_isInjectField).map(_parseInjectField);
|
||||
// Collect and process all @inject fields
|
||||
final injectFields = classElement.fields2
|
||||
.where((f) => _isInjectField(f))
|
||||
.map((f) => _parseInjectField(f));
|
||||
|
||||
for (final parsedField in injectFields) {
|
||||
buffer.writeln(_generateInjectionLine(parsedField));
|
||||
@@ -79,25 +135,25 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
/// Checks if a field has the @inject annotation.
|
||||
/// Returns true if a field is annotated with `@inject`.
|
||||
///
|
||||
/// Проверяет, отмечено ли поле аннотацией @inject.
|
||||
static bool _isInjectField(FieldElement field) {
|
||||
return field.metadata.any(
|
||||
/// Used to detect which fields should be processed for injection.
|
||||
static bool _isInjectField(FieldElement2 field) {
|
||||
return field.firstFragment.metadata2.annotations.any(
|
||||
(m) => m.computeConstantValue()?.type?.getDisplayString() == 'inject',
|
||||
);
|
||||
}
|
||||
|
||||
/// Parses the field for scope/named qualifiers and determines its type.
|
||||
/// Returns a [_ParsedInjectField] describing injection information.
|
||||
/// Parses `@inject()` field and extracts all injection metadata
|
||||
/// (core type, qualifiers, scope, nullability, etc).
|
||||
///
|
||||
/// Разбирает поле на наличие модификаторов scope/named и выясняет его тип.
|
||||
/// Возвращает [_ParsedInjectField] с информацией о внедрении.
|
||||
static _ParsedInjectField _parseInjectField(FieldElement field) {
|
||||
/// Converts Dart field declaration and all parameterizing injection-related
|
||||
/// annotations into a [_ParsedInjectField] which is used for codegen.
|
||||
static _ParsedInjectField _parseInjectField(FieldElement2 field) {
|
||||
String? scopeName;
|
||||
String? namedValue;
|
||||
|
||||
for (final meta in field.metadata) {
|
||||
for (final meta in field.firstFragment.metadata2.annotations) {
|
||||
final DartObject? obj = meta.computeConstantValue();
|
||||
final type = obj?.type?.getDisplayString();
|
||||
if (type == 'scope') {
|
||||
@@ -120,17 +176,16 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
isFuture = false;
|
||||
}
|
||||
|
||||
// ***
|
||||
// Добавим определение nullable для типа (например PostRepository? или Future<PostRepository?>)
|
||||
bool isNullable = dartType.nullabilitySuffix ==
|
||||
NullabilitySuffix.question ||
|
||||
// Determine nullability for field types like T? or Future<T?>
|
||||
bool isNullable =
|
||||
dartType.nullabilitySuffix == NullabilitySuffix.question ||
|
||||
(dartType is ParameterizedType &&
|
||||
(dartType)
|
||||
.typeArguments
|
||||
.any((t) => t.nullabilitySuffix == NullabilitySuffix.question));
|
||||
(dartType).typeArguments.any(
|
||||
(t) => t.nullabilitySuffix == NullabilitySuffix.question,
|
||||
));
|
||||
|
||||
return _ParsedInjectField(
|
||||
fieldName: field.name,
|
||||
fieldName: field.firstFragment.name2 ?? '',
|
||||
coreType: coreTypeName.replaceAll('?', ''), // удаляем "?" на всякий
|
||||
isFuture: isFuture,
|
||||
isNullable: isNullable,
|
||||
@@ -139,20 +194,24 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
);
|
||||
}
|
||||
|
||||
/// Generates a line of code that performs the dependency injection for a field.
|
||||
/// Handles resolve/resolveAsync, scoping, and named qualifiers.
|
||||
/// Generates Dart code for a single dependency-injected field based on its metadata.
|
||||
///
|
||||
/// Генерирует строку кода, которая внедряет зависимость для поля.
|
||||
/// Учитывает resolve/resolveAsync, scoping и named qualifier.
|
||||
/// This code will resolve the field from the CherryPick DI container and assign it to the class instance.
|
||||
/// Correctly dispatches to resolve, tryResolve, resolveAsync, or tryResolveAsync methods,
|
||||
/// and applies container scoping or named resolution where required.
|
||||
///
|
||||
/// Returns literal Dart code as string (1 line).
|
||||
///
|
||||
/// Example output:
|
||||
/// `instance.logger = CherryPick.openRootScope().resolve<Logger>();`
|
||||
String _generateInjectionLine(_ParsedInjectField field) {
|
||||
// Используем tryResolve для nullable, иначе resolve
|
||||
final resolveMethod = field.isFuture
|
||||
? (field.isNullable
|
||||
? 'tryResolveAsync<${field.coreType}>'
|
||||
: 'resolveAsync<${field.coreType}>')
|
||||
? 'tryResolveAsync<${field.coreType}>'
|
||||
: 'resolveAsync<${field.coreType}>')
|
||||
: (field.isNullable
|
||||
? 'tryResolve<${field.coreType}>'
|
||||
: 'resolve<${field.coreType}>');
|
||||
? 'tryResolve<${field.coreType}>'
|
||||
: 'resolve<${field.coreType}>');
|
||||
|
||||
final openCall = (field.scopeName != null && field.scopeName!.isNotEmpty)
|
||||
? "CherryPick.openScope(scopeName: '${field.scopeName}')"
|
||||
@@ -166,30 +225,29 @@ class InjectGenerator extends GeneratorForAnnotation<ann.injectable> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Data structure representing all information required to generate
|
||||
/// injection code for a field.
|
||||
/// Internal structure: describes all required information for generating the injection
|
||||
/// assignment for a given field.
|
||||
///
|
||||
/// Структура данных, содержащая всю информацию,
|
||||
/// необходимую для генерации кода внедрения для поля.
|
||||
/// Not exported. Used as a DTO in the generator for each field.
|
||||
class _ParsedInjectField {
|
||||
/// The name of the field / Имя поля.
|
||||
/// The name of the field to be injected.
|
||||
final String fieldName;
|
||||
|
||||
/// The base type name (T or Future<T>) / Базовый тип (T или тип из Future<T>).
|
||||
/// The Dart type to resolve (e.g. `Logger` from `Logger` or `Future<Logger>`).
|
||||
final String coreType;
|
||||
|
||||
/// True if the field type is Future<T>; false otherwise
|
||||
/// Истина, если поле — Future<T>, иначе — ложь.
|
||||
/// True if the field is an async dependency (Future<...>), otherwise false.
|
||||
final bool isFuture;
|
||||
|
||||
/// Optional scope annotation argument / Опциональное имя scope.
|
||||
/// True if the field accepts null (T?), otherwise false.
|
||||
final bool isNullable;
|
||||
|
||||
/// The scoping for DI resolution, or null to use root scope.
|
||||
final String? scopeName;
|
||||
|
||||
/// Optional named annotation argument / Опциональное имя named.
|
||||
/// Name qualifier for named resolution, or null if not set.
|
||||
final String? namedValue;
|
||||
|
||||
final bool isNullable;
|
||||
|
||||
_ParsedInjectField({
|
||||
required this.fieldName,
|
||||
required this.coreType,
|
||||
@@ -200,8 +258,8 @@ class _ParsedInjectField {
|
||||
});
|
||||
}
|
||||
|
||||
/// Builder factory. Used by build_runner.
|
||||
/// Factory for creating the build_runner builder for DI field injection.
|
||||
///
|
||||
/// Фабрика билдера. Используется build_runner.
|
||||
/// Add this builder in your build.yaml if you're invoking CherryPick generators manually.
|
||||
Builder injectBuilder(BuilderOptions options) =>
|
||||
PartBuilder([InjectGenerator()], '.inject.cherrypick.g.dart');
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:build/build.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
|
||||
@@ -19,75 +19,89 @@ import 'package:cherrypick_annotations/cherrypick_annotations.dart' as ann;
|
||||
import 'src/generated_class.dart';
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// ModuleGenerator for code generation of dependency-injected modules.
|
||||
/// CherryPick Module Generator — Codegen for DI modules
|
||||
///
|
||||
/// ENGLISH
|
||||
/// This generator scans for Dart classes annotated with `@module()` and
|
||||
/// automatically generates boilerplate code for dependency injection
|
||||
/// (DI) based on the public methods in those classes. Each method can be
|
||||
/// annotated to describe how an object should be provided to the DI container.
|
||||
/// The generated code registers those methods as bindings. This automates the
|
||||
/// creation of factories, singletons, and named instances, reducing repetitive
|
||||
/// manual code.
|
||||
/// This generator scans Dart classes annotated with `@module()` and generates
|
||||
/// boilerplate for dependency injection registration automatically. Each public
|
||||
/// method in such classes can be annotated to describe how an object should be
|
||||
/// bound to the DI container (singleton, factory, named, with parameters, etc).
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Генератор зависимостей для DI-контейнера на основе аннотаций.
|
||||
/// Данный генератор автоматически создаёт код для внедрения зависимостей (DI)
|
||||
/// на основе аннотаций в вашем исходном коде. Когда вы отмечаете класс
|
||||
/// аннотацией `@module()`, этот генератор обработает все его публичные методы
|
||||
/// и автоматически сгенерирует класс с биндингами (регистрациями зависимостей)
|
||||
/// для DI-контейнера. Это избавляет от написания однообразного шаблонного кода.
|
||||
/// The generated code collects all such bind methods and produces a Dart
|
||||
/// companion *module registration class* with a `.bindAll()` method, which you
|
||||
/// can use from your DI root to automatically register those dependencies.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```dart
|
||||
/// import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
///
|
||||
/// @module()
|
||||
/// abstract class AppModule {
|
||||
/// @singleton()
|
||||
/// Logger logger() => Logger();
|
||||
///
|
||||
/// @provide()
|
||||
/// ApiService api(Logger logger) => ApiService(logger);
|
||||
///
|
||||
/// @named('dev')
|
||||
/// FakeService fake() => FakeService();
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// After codegen, you will get (simplified):
|
||||
/// ```dart
|
||||
/// class _\$AppModuleCherrypickModule extend AppModule {
|
||||
/// static void bindAll(CherryPickScope scope, AppModule module) {
|
||||
/// scope.addSingleton<Logger>(() => module.logger());
|
||||
/// scope.addFactory<ApiService>(() => module.api(scope.resolve<Logger>()));
|
||||
/// scope.addFactory<FakeService>(() => module.fake(), named: 'dev');
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// Use it e.g. in your bootstrap:
|
||||
/// ```dart
|
||||
/// final scope = CherryPick.openRootScope()..intallModules([_\$AppModuleCherrypickModule()]);
|
||||
/// ```
|
||||
///
|
||||
/// Features supported:
|
||||
/// - Singleton, factory, named, parametric, and async providers
|
||||
/// - Eliminates all boilerplate for DI registration
|
||||
/// - Works with abstract classes and real classes
|
||||
/// - Error if @module() is applied to a non-class
|
||||
///
|
||||
/// See also: [@singleton], [@provide], [@named], [@module]
|
||||
/// ---------------------------------------------------------------------------
|
||||
|
||||
class ModuleGenerator extends GeneratorForAnnotation<ann.module> {
|
||||
/// -------------------------------------------------------------------------
|
||||
/// ENGLISH
|
||||
/// Generates the Dart source for a class marked with the `@module()` annotation.
|
||||
/// - [element]: the original Dart class element.
|
||||
/// - [annotation]: the annotation parameters (not usually used here).
|
||||
/// - [buildStep]: the current build step info.
|
||||
/// Generates Dart source for a class marked with the `@module()` annotation.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Генерирует исходный код для класса-модуля с аннотацией `@module()`.
|
||||
/// [element] — исходный класс, помеченный аннотацией.
|
||||
/// [annotation] — значения параметров аннотации.
|
||||
/// [buildStep] — информация о текущем шаге генерации.
|
||||
/// -------------------------------------------------------------------------
|
||||
/// Throws [InvalidGenerationSourceError] if used on anything except a class.
|
||||
///
|
||||
/// See file-level docs for usage and generated output example.
|
||||
@override
|
||||
String generateForAnnotatedElement(
|
||||
Element element,
|
||||
dynamic generateForAnnotatedElement(
|
||||
Element2 element,
|
||||
ConstantReader annotation,
|
||||
BuildStep buildStep,
|
||||
) {
|
||||
// Only classes are supported for @module() annotation
|
||||
// Обрабатываются только классы (другие элементы — ошибка)
|
||||
if (element is! ClassElement) {
|
||||
if (element is! ClassElement2) {
|
||||
throw InvalidGenerationSourceError(
|
||||
'@module() can only be applied to classes. / @module() может быть применён только к классам.',
|
||||
'@module() can only be applied to classes.',
|
||||
element: element,
|
||||
);
|
||||
}
|
||||
|
||||
final classElement = element;
|
||||
|
||||
// Build a representation of the generated bindings based on class methods /
|
||||
// Создаёт объект, описывающий, какие биндинги нужно сгенерировать на основании методов класса
|
||||
final generatedClass = GeneratedClass.fromClassElement(classElement);
|
||||
|
||||
// Generate the resulting Dart code / Генерирует итоговый Dart-код
|
||||
return generatedClass.generate();
|
||||
}
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// ENGLISH
|
||||
/// Entry point for build_runner. Returns a Builder used to generate code for
|
||||
/// every file with a @module() annotation.
|
||||
/// Codegen builder entry point: register this builder in build.yaml or your package.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Точка входа для генератора build_runner.
|
||||
/// Возвращает Builder, используемый build_runner для генерации кода для всех
|
||||
/// файлов, где встречается @module().
|
||||
/// Used by build_runner. Generates .module.cherrypick.g.dart files for each
|
||||
/// source file with an annotated @module() class.
|
||||
/// ---------------------------------------------------------------------------
|
||||
Builder moduleBuilder(BuilderOptions options) =>
|
||||
PartBuilder([ModuleGenerator()], '.module.cherrypick.g.dart');
|
||||
|
||||
@@ -12,35 +12,93 @@
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'exceptions.dart';
|
||||
import 'metadata_utils.dart';
|
||||
|
||||
/// Validates annotation combinations and usage patterns
|
||||
/// Provides static utility methods for validating annotation usage in CherryPick
|
||||
/// dependency injection code generation.
|
||||
///
|
||||
/// This validator helps ensure that `@provide`, `@instance`, `@singleton`, `@params`,
|
||||
/// `@inject`, `@named`, `@module`, and `@injectable` annotations are correctly and safely
|
||||
/// combined in your codebase, preventing common configuration and codegen errors before
|
||||
/// code is generated.
|
||||
///
|
||||
/// #### Example Usage
|
||||
/// ```dart
|
||||
/// void processMethod(MethodElement method) {
|
||||
/// AnnotationValidator.validateMethodAnnotations(method);
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// All exceptions are thrown as [AnnotationValidationException] and will include
|
||||
/// a helpful message and context.
|
||||
///
|
||||
/// ---
|
||||
/// Typical checks performed by this utility:
|
||||
/// - Mutual exclusivity (`@instance` vs `@provide`)
|
||||
/// - Required presence for fields and methods
|
||||
/// - Proper parameters for `@named` and `@params`
|
||||
/// - Correct usage of injectable fields, module class methods, etc.
|
||||
///
|
||||
class AnnotationValidator {
|
||||
/// Validates annotations on a method element
|
||||
static void validateMethodAnnotations(MethodElement method) {
|
||||
final annotations = _getAnnotationNames(method.metadata);
|
||||
/// Validates annotations for a given [MethodElement].
|
||||
///
|
||||
/// Checks:
|
||||
/// - Mutual exclusivity of `@instance` and `@provide`.
|
||||
/// - That a method is annotated with exactly one DI-producing annotation.
|
||||
/// - If `@params` is present, that it is used together with `@provide`.
|
||||
/// - Appropriate usage of `@singleton`.
|
||||
/// - [@named] syntax and conventions.
|
||||
/// - Parameter validation for method arguments.
|
||||
///
|
||||
/// Throws [AnnotationValidationException] on any violation.
|
||||
static void validateMethodAnnotations(MethodElement2 method) {
|
||||
final annotations = _getAnnotationNames(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
_validateMutuallyExclusiveAnnotations(method, annotations);
|
||||
_validateAnnotationCombinations(method, annotations);
|
||||
_validateAnnotationParameters(method);
|
||||
}
|
||||
|
||||
/// Validates annotations on a field element
|
||||
static void validateFieldAnnotations(FieldElement field) {
|
||||
final annotations = _getAnnotationNames(field.metadata);
|
||||
/// Validates that a [FieldElement] has correct injection annotations.
|
||||
///
|
||||
/// Specifically, ensures:
|
||||
/// - Injectable fields are of valid type.
|
||||
/// - No `void` injection.
|
||||
/// - Correct scope naming if present.
|
||||
///
|
||||
/// Throws [AnnotationValidationException] if checks fail.
|
||||
static void validateFieldAnnotations(FieldElement2 field) {
|
||||
final annotations = _getAnnotationNames(
|
||||
field.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
_validateInjectFieldAnnotations(field, annotations);
|
||||
}
|
||||
|
||||
/// Validates annotations on a class element
|
||||
static void validateClassAnnotations(ClassElement classElement) {
|
||||
final annotations = _getAnnotationNames(classElement.metadata);
|
||||
/// Validates all class-level DI annotations.
|
||||
///
|
||||
/// Executes checks for:
|
||||
/// - Module class validity (e.g. must have public DI methods if `@module`).
|
||||
/// - Injectable class: at least one @inject field, field finalness, etc.
|
||||
/// - Provides helpful context for error/warning reporting.
|
||||
///
|
||||
/// Throws [AnnotationValidationException] if checks fail.
|
||||
static void validateClassAnnotations(ClassElement2 classElement) {
|
||||
final annotations = _getAnnotationNames(
|
||||
classElement.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
_validateModuleClassAnnotations(classElement, annotations);
|
||||
_validateInjectableClassAnnotations(classElement, annotations);
|
||||
}
|
||||
|
||||
// --- Internal helpers follow (private) ---
|
||||
|
||||
/// Helper: Returns the names of all annotation types on `metadata`.
|
||||
static List<String> _getAnnotationNames(List<ElementAnnotation> metadata) {
|
||||
return metadata
|
||||
.map((m) => m.computeConstantValue()?.type?.getDisplayString())
|
||||
@@ -49,8 +107,11 @@ class AnnotationValidator {
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// Validates that mutually exclusive method annotations are not used together.
|
||||
///
|
||||
/// For example, `@instance` and `@provide` cannot both be present.
|
||||
static void _validateMutuallyExclusiveAnnotations(
|
||||
MethodElement method,
|
||||
MethodElement2 method,
|
||||
List<String> annotations,
|
||||
) {
|
||||
// @instance and @provide are mutually exclusive
|
||||
@@ -68,8 +129,12 @@ class AnnotationValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates correct annotation combinations, e.g.
|
||||
/// - `@params` must be with `@provide`
|
||||
/// - One of `@instance` or `@provide` must be present for a registration method
|
||||
/// - Validates singleton usage
|
||||
static void _validateAnnotationCombinations(
|
||||
MethodElement method,
|
||||
MethodElement2 method,
|
||||
List<String> annotations,
|
||||
) {
|
||||
// @params can only be used with @provide
|
||||
@@ -105,8 +170,9 @@ class AnnotationValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Singleton-specific method annotation checks.
|
||||
static void _validateSingletonUsage(
|
||||
MethodElement method,
|
||||
MethodElement2 method,
|
||||
List<String> annotations,
|
||||
) {
|
||||
// Singleton with params might not make sense in some contexts
|
||||
@@ -122,17 +188,17 @@ class AnnotationValidator {
|
||||
'Singleton methods cannot return void',
|
||||
element: method,
|
||||
suggestion: 'Remove @singleton annotation or change return type',
|
||||
context: {
|
||||
'method_name': method.displayName,
|
||||
'return_type': returnType,
|
||||
},
|
||||
context: {'method_name': method.displayName, 'return_type': returnType},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static void _validateAnnotationParameters(MethodElement method) {
|
||||
/// Validates extra requirements or syntactic rules for annotation arguments, like @named.
|
||||
static void _validateAnnotationParameters(MethodElement2 method) {
|
||||
// Validate @named annotation parameters
|
||||
final namedValue = MetadataUtils.getNamedValue(method.metadata);
|
||||
final namedValue = MetadataUtils.getNamedValue(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
if (namedValue != null) {
|
||||
if (namedValue.isEmpty) {
|
||||
throw AnnotationValidationException(
|
||||
@@ -162,19 +228,23 @@ class AnnotationValidator {
|
||||
}
|
||||
|
||||
// Validate method parameters for @params usage
|
||||
for (final param in method.parameters) {
|
||||
final paramAnnotations = _getAnnotationNames(param.metadata);
|
||||
for (final param in method.formalParameters) {
|
||||
final paramAnnotations = _getAnnotationNames(
|
||||
param.firstFragment.metadata2.annotations,
|
||||
);
|
||||
if (paramAnnotations.contains('params')) {
|
||||
_validateParamsParameter(param, method);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks that @params is used with compatible parameter type.
|
||||
static void _validateParamsParameter(
|
||||
ParameterElement param, MethodElement method) {
|
||||
FormalParameterElement param,
|
||||
MethodElement2 method,
|
||||
) {
|
||||
// @params parameter should typically be dynamic or Map<String, dynamic>
|
||||
final paramType = param.type.getDisplayString();
|
||||
|
||||
if (paramType != 'dynamic' &&
|
||||
paramType != 'Map<String, dynamic>' &&
|
||||
paramType != 'Map<String, dynamic>?') {
|
||||
@@ -194,8 +264,9 @@ class AnnotationValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks field-level annotation for valid injectable fields.
|
||||
static void _validateInjectFieldAnnotations(
|
||||
FieldElement field,
|
||||
FieldElement2 field,
|
||||
List<String> annotations,
|
||||
) {
|
||||
if (!annotations.contains('inject')) {
|
||||
@@ -209,15 +280,12 @@ class AnnotationValidator {
|
||||
'Cannot inject void type',
|
||||
element: field,
|
||||
suggestion: 'Use a concrete type instead of void',
|
||||
context: {
|
||||
'field_name': field.displayName,
|
||||
'field_type': fieldType,
|
||||
},
|
||||
context: {'field_name': field.displayName, 'field_type': fieldType},
|
||||
);
|
||||
}
|
||||
|
||||
// Validate scope annotation if present
|
||||
for (final meta in field.metadata) {
|
||||
for (final meta in field.firstFragment.metadata2.annotations) {
|
||||
final obj = meta.computeConstantValue();
|
||||
final type = obj?.type?.getDisplayString();
|
||||
if (type == 'scope') {
|
||||
@@ -227,8 +295,9 @@ class AnnotationValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks @module usage: must have at least one DI method, each with DI-annotation.
|
||||
static void _validateModuleClassAnnotations(
|
||||
ClassElement classElement,
|
||||
ClassElement2 classElement,
|
||||
List<String> annotations,
|
||||
) {
|
||||
if (!annotations.contains('module')) {
|
||||
@@ -236,8 +305,9 @@ class AnnotationValidator {
|
||||
}
|
||||
|
||||
// Check if class has public methods
|
||||
final publicMethods =
|
||||
classElement.methods.where((m) => m.isPublic).toList();
|
||||
final publicMethods = classElement.methods2
|
||||
.where((m) => m.isPublic)
|
||||
.toList();
|
||||
if (publicMethods.isEmpty) {
|
||||
throw AnnotationValidationException(
|
||||
'Module class must have at least one public method',
|
||||
@@ -252,7 +322,9 @@ class AnnotationValidator {
|
||||
|
||||
// Validate that public methods have appropriate annotations
|
||||
for (final method in publicMethods) {
|
||||
final methodAnnotations = _getAnnotationNames(method.metadata);
|
||||
final methodAnnotations = _getAnnotationNames(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
if (!methodAnnotations.contains('instance') &&
|
||||
!methodAnnotations.contains('provide')) {
|
||||
throw AnnotationValidationException(
|
||||
@@ -268,8 +340,9 @@ class AnnotationValidator {
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks @injectable usage on classes and their fields.
|
||||
static void _validateInjectableClassAnnotations(
|
||||
ClassElement classElement,
|
||||
ClassElement2 classElement,
|
||||
List<String> annotations,
|
||||
) {
|
||||
if (!annotations.contains('injectable')) {
|
||||
@@ -277,8 +350,10 @@ class AnnotationValidator {
|
||||
}
|
||||
|
||||
// Check if class has injectable fields
|
||||
final injectFields = classElement.fields.where((f) {
|
||||
final fieldAnnotations = _getAnnotationNames(f.metadata);
|
||||
final injectFields = classElement.fields2.where((f) {
|
||||
final fieldAnnotations = _getAnnotationNames(
|
||||
f.firstFragment.metadata2.annotations,
|
||||
);
|
||||
return fieldAnnotations.contains('inject');
|
||||
}).toList();
|
||||
|
||||
|
||||
@@ -12,57 +12,59 @@
|
||||
//
|
||||
|
||||
/// ----------------------------------------------------------------------------
|
||||
/// BindParameterSpec - describes a single method parameter and how to resolve it.
|
||||
/// BindParameterSpec
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Describes a single parameter for a provider/binding method in the DI system.
|
||||
/// Stores the parameter type, its optional `@named` key for named resolution,
|
||||
/// and whether it is a runtime "params" argument. Used to generate code that
|
||||
/// resolves dependencies from the DI scope:
|
||||
/// - If the parameter is a dependency type (e.g. SomeDep), emits:
|
||||
/// currentScope.resolve<SomeDep>()
|
||||
/// - If the parameter is named, emits:
|
||||
/// currentScope.resolve<SomeDep>(named: 'yourName')
|
||||
/// - If it's a runtime parameter (e.g. via @params()), emits:
|
||||
/// args
|
||||
/// Describes a single parameter for a DI provider/factory/binding method,
|
||||
/// specifying how that parameter is to be resolved in generated code.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Описывает один параметр метода в DI, и его способ разрешения из контейнера.
|
||||
/// Сохраняет имя типа, дополнительное имя (если параметр аннотирован через @named),
|
||||
/// и признак runtime-параметра (@params).
|
||||
/// Для обычной зависимости типа (например, SomeDep) генерирует строку вида:
|
||||
/// currentScope.resolve<SomeDep>()
|
||||
/// Для зависимости с именем:
|
||||
/// currentScope.resolve<SomeDep>(named: 'имя')
|
||||
/// Для runtime-параметра:
|
||||
/// args
|
||||
/// Stores the parameter's type name, optional `@named` identifier (for named DI resolution),
|
||||
/// and a flag for runtime (@params) arguments. Used in CherryPick generator
|
||||
/// for creating argument lists when invoking factories or provider methods.
|
||||
///
|
||||
/// ## Example usage
|
||||
/// ```dart
|
||||
/// // Binding method: @provide() Logger provideLogger(@named('debug') Config config, @params Map<String, dynamic> args)
|
||||
/// final namedParam = BindParameterSpec('Config', 'debug');
|
||||
/// final runtimeParam = BindParameterSpec('Map<String, dynamic>', null, isParams: true);
|
||||
/// print(namedParam.generateArg()); // prints: currentScope.resolve<Config>(named: 'debug')
|
||||
/// print(runtimeParam.generateArg()); // prints: args
|
||||
/// ```
|
||||
///
|
||||
/// ## Code generation logic
|
||||
/// - Injected: currentScope.resolve<Service>()
|
||||
/// - Named: currentScope.resolve<Service>(named: 'name')
|
||||
/// - @params: args
|
||||
/// ----------------------------------------------------------------------------
|
||||
class BindParameterSpec {
|
||||
/// Type name of the parameter (e.g. SomeService)
|
||||
/// Имя типа параметра (например, SomeService)
|
||||
/// The type name of the parameter (e.g., 'UserRepository')
|
||||
final String typeName;
|
||||
|
||||
/// Optional name for named resolution (from @named)
|
||||
/// Необязательное имя для разрешения по имени (если аннотировано через @named)
|
||||
/// If non-null, this is the named-key for DI resolution (from @named).
|
||||
final String? named;
|
||||
|
||||
/// True if this parameter uses @params and should be provided from runtime args
|
||||
/// Признак, что параметр — runtime (через @params)
|
||||
/// True if this parameter is a runtime param (annotated with @params and
|
||||
/// filled by a runtime argument map).
|
||||
final bool isParams;
|
||||
|
||||
BindParameterSpec(this.typeName, this.named, {this.isParams = false});
|
||||
|
||||
/// --------------------------------------------------------------------------
|
||||
/// generateArg
|
||||
/// Generates Dart code to resolve this parameter in the DI container.
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Generates Dart code for resolving the dependency from the DI scope,
|
||||
/// considering type, named, and param-argument.
|
||||
/// - For normal dependencies: resolves by type
|
||||
/// - For named dependencies: resolves by type and name
|
||||
/// - For @params: uses the supplied params variable (default 'args')
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Генерирует строку для получения зависимости из DI scope (с учётом имени
|
||||
/// и типа параметра или runtime-режима @params).
|
||||
/// --------------------------------------------------------------------------
|
||||
/// ## Example
|
||||
/// ```dart
|
||||
/// final a = BindParameterSpec('Api', null); // normal
|
||||
/// print(a.generateArg()); // currentScope.resolve<Api>()
|
||||
///
|
||||
/// final b = BindParameterSpec('Api', 'prod'); // named
|
||||
/// print(b.generateArg()); // currentScope.resolve<Api>(named: 'prod')
|
||||
///
|
||||
/// final c = BindParameterSpec('Map<String,dynamic>', null, isParams: true); // params
|
||||
/// print(c.generateArg()); // args
|
||||
/// ```
|
||||
String generateArg([String paramsVar = 'args']) {
|
||||
if (isParams) {
|
||||
return paramsVar;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
|
||||
import 'bind_parameters_spec.dart';
|
||||
import 'metadata_utils.dart';
|
||||
@@ -19,62 +19,64 @@ import 'exceptions.dart';
|
||||
import 'type_parser.dart';
|
||||
import 'annotation_validator.dart';
|
||||
|
||||
/// Enum representing the binding annotation applied to a module method.
|
||||
enum BindingType {
|
||||
/// Direct instance returned from the method (@instance).
|
||||
instance,
|
||||
provide;
|
||||
|
||||
/// Provider/factory function (@provide).
|
||||
provide,
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// BindSpec -- describes a binding specification generated for a dependency.
|
||||
/// BindSpec
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Represents all the data necessary to generate a DI binding for a single
|
||||
/// method in a module class. Each BindSpec corresponds to one public method
|
||||
/// and contains information about its type, provider method, lifecycle (singleton),
|
||||
/// parameters (with their annotations), binding strategy (instance/provide),
|
||||
/// asynchronous mode, and named keys. It is responsible for generating the
|
||||
/// correct Dart code to register this binding with the DI container, in both
|
||||
/// sync and async cases, with and without named or runtime arguments.
|
||||
/// Describes a DI container binding as generated from a single public factory,
|
||||
/// instance, or provider method of a module (annotated with @instance or @provide).
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Описывает параметры для создания одного биндинга зависимости (binding spec).
|
||||
/// Каждый биндинг соответствует одному публичному методу класса-модуля и
|
||||
/// содержит всю информацию для генерации кода регистрации этого биндинга в
|
||||
/// DI-контейнере: тип возвращаемой зависимости, имя метода, параметры, аннотации
|
||||
/// (@singleton, @named, @instance, @provide), асинхронность, признак runtime
|
||||
/// аргументов и др. Генерирует правильный Dart-код для регистрации биндера.
|
||||
/// Includes all annotation-driven parameters required to generate valid DI
|
||||
/// registration Dart code in CherryPick:
|
||||
/// - Return type
|
||||
/// - Provider method name
|
||||
/// - Singleton flag
|
||||
/// - Named identifier (from @named)
|
||||
/// - List of resolved or runtime (@params) parameters
|
||||
/// - Binding mode (instance/provide)
|
||||
/// - Async and parametric variants
|
||||
///
|
||||
/// ## Example usage
|
||||
/// ```dart
|
||||
/// // Suppose @provide() Api api(@named('test') Client client)
|
||||
/// final bindSpec = BindSpec.fromMethod(methodElement);
|
||||
/// print(bindSpec.generateBind(2)); // bind<Api>().toProvide(() => api(currentScope.resolve<Client>(named: 'test')));
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class BindSpec {
|
||||
/// The type this binding provides (e.g. SomeService)
|
||||
/// Тип, который предоставляет биндинг (например, SomeService)
|
||||
final String returnType;
|
||||
|
||||
/// Method name that implements the binding
|
||||
/// Имя метода, который реализует биндинг
|
||||
/// Binding provider/factory method name
|
||||
final String methodName;
|
||||
|
||||
/// Optional name for named dependency (from @named)
|
||||
/// Необязательное имя, для именованной зависимости (используется с @named)
|
||||
/// Named identifier for DI resolution (null if unnamed)
|
||||
final String? named;
|
||||
|
||||
/// Whether the dependency is a singleton (@singleton annotation)
|
||||
/// Является ли зависимость синглтоном (имеется ли аннотация @singleton)
|
||||
/// If true, binding is registered as singleton in DI
|
||||
final bool isSingleton;
|
||||
|
||||
/// List of method parameters to inject dependencies with
|
||||
/// Список параметров, которые требуются методу для внедрения зависимостей
|
||||
/// Provider/factory method parameters (in order)
|
||||
final List<BindParameterSpec> parameters;
|
||||
|
||||
/// Binding type: 'instance' or 'provide' (@instance or @provide)
|
||||
final BindingType bindingType; // 'instance' | 'provide'
|
||||
/// Instance vs provider mode, from annotation choice
|
||||
final BindingType bindingType;
|
||||
|
||||
/// True if the method is asynchronous and uses instance binding (Future)
|
||||
/// Async flag for .toInstanceAsync()
|
||||
final bool isAsyncInstance;
|
||||
|
||||
/// True if the method is asynchronous and uses provide binding (Future)
|
||||
/// Async flag for .toProvideAsync()
|
||||
final bool isAsyncProvide;
|
||||
|
||||
/// True if the binding method accepts runtime "params" argument (@params)
|
||||
/// True if a @params runtime parameter is present
|
||||
final bool hasParams;
|
||||
|
||||
BindSpec({
|
||||
@@ -89,20 +91,12 @@ class BindSpec {
|
||||
required this.hasParams,
|
||||
});
|
||||
|
||||
/// -------------------------------------------------------------------------
|
||||
/// generateBind
|
||||
/// Generates a Dart code line for binding registration.
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Generates a line of Dart code registering the binding with the DI framework.
|
||||
/// Produces something like:
|
||||
/// bind<Type>().toProvide(() => method(args)).withName('name').singleton();
|
||||
/// Indent parameter allows formatted multiline output.
|
||||
/// Example (single-line):
|
||||
/// bind<Api>().toProvide(() => provideApi(currentScope.resolve<Client>(named: 'test'))).withName('prod').singleton();
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Формирует dart-код для биндинга, например:
|
||||
/// bind<Type>().toProvide(() => method(args)).withName('name').singleton();
|
||||
/// Параметр [indent] задаёт отступ для красивого форматирования кода.
|
||||
/// -------------------------------------------------------------------------
|
||||
/// The [indent] argument sets the space indentation for pretty-printing.
|
||||
String generateBind(int indent) {
|
||||
final indentStr = ' ' * indent;
|
||||
final provide = _generateProvideClause(indent);
|
||||
@@ -151,7 +145,7 @@ class BindSpec {
|
||||
return _generatePlainProvideClause(indent);
|
||||
}
|
||||
|
||||
/// EN / RU: Supports runtime parameters (@params).
|
||||
/// Generates code when using runtime parameters (@params).
|
||||
String _generateWithParamsProvideClause(int indent) {
|
||||
// Safe variable name for parameters.
|
||||
const paramVar = 'args';
|
||||
@@ -161,7 +155,8 @@ class BindSpec {
|
||||
switch (bindingType) {
|
||||
case BindingType.instance:
|
||||
throw StateError(
|
||||
'Internal error: _generateWithParamsProvideClause called for @instance binding with @params.');
|
||||
'Internal error: _generateWithParamsProvideClause called for @instance binding with @params.',
|
||||
);
|
||||
//return isAsyncInstance
|
||||
// ? '.toInstanceAsync(($fnArgs) => $methodName($fnArgs))'
|
||||
// : '.toInstance(($fnArgs) => $methodName($fnArgs))';
|
||||
@@ -178,7 +173,7 @@ class BindSpec {
|
||||
}
|
||||
}
|
||||
|
||||
/// EN / RU: Supports only injected dependencies, not runtime (@params).
|
||||
/// Generates code when only resolved (not runtime) arguments used.
|
||||
String _generatePlainProvideClause(int indent) {
|
||||
final argsStr = parameters.map((p) => p.generateArg()).join(', ');
|
||||
|
||||
@@ -195,20 +190,24 @@ class BindSpec {
|
||||
case BindingType.provide:
|
||||
if (isAsyncProvide) {
|
||||
if (needsMultiline) {
|
||||
final lambdaIndent =
|
||||
(isSingleton || named != null) ? indent + 6 : indent + 2;
|
||||
final closingIndent =
|
||||
(isSingleton || named != null) ? indent + 4 : indent;
|
||||
final lambdaIndent = (isSingleton || named != null)
|
||||
? indent + 6
|
||||
: indent + 2;
|
||||
final closingIndent = (isSingleton || named != null)
|
||||
? indent + 4
|
||||
: indent;
|
||||
return '.toProvideAsync(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
|
||||
} else {
|
||||
return '.toProvideAsync(() => $methodName($argsStr))';
|
||||
}
|
||||
} else {
|
||||
if (needsMultiline) {
|
||||
final lambdaIndent =
|
||||
(isSingleton || named != null) ? indent + 6 : indent + 2;
|
||||
final closingIndent =
|
||||
(isSingleton || named != null) ? indent + 4 : indent;
|
||||
final lambdaIndent = (isSingleton || named != null)
|
||||
? indent + 6
|
||||
: indent + 2;
|
||||
final closingIndent = (isSingleton || named != null)
|
||||
? indent + 4
|
||||
: indent;
|
||||
return '.toProvide(\n${' ' * lambdaIndent}() => $methodName($argsStr),\n${' ' * closingIndent})';
|
||||
} else {
|
||||
return '.toProvide(() => $methodName($argsStr))';
|
||||
@@ -241,17 +240,18 @@ class BindSpec {
|
||||
/// -------------------------------------------------------------------------
|
||||
/// fromMethod
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Creates a BindSpec from a module class method by analyzing its return type,
|
||||
/// annotations, list of parameters (with their own annotations), and async-ness.
|
||||
/// Throws if a method does not have the required @instance() or @provide().
|
||||
/// Constructs a [BindSpec] from an analyzer [MethodElement].
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Создаёт спецификацию биндинга (BindSpec) из метода класса-модуля, анализируя
|
||||
/// возвращаемый тип, аннотации, параметры (и их аннотации), а также факт
|
||||
/// асинхронности. Если нет @instance или @provide — кидает ошибку.
|
||||
/// -------------------------------------------------------------------------
|
||||
static BindSpec fromMethod(MethodElement method) {
|
||||
/// Validates and parses all type annotations, method/parameter DI hints,
|
||||
/// and derives async and parametric flags as needed.
|
||||
///
|
||||
/// ## Example
|
||||
/// ```dart
|
||||
/// final bindSpec = BindSpec.fromMethod(methodElement);
|
||||
/// print(bindSpec.returnType); // e.g., 'Logger'
|
||||
/// ```
|
||||
/// Throws [AnnotationValidationException] or [CodeGenerationException] if invalid.
|
||||
static BindSpec fromMethod(MethodElement2 method) {
|
||||
try {
|
||||
// Validate method annotations
|
||||
AnnotationValidator.validateMethodAnnotations(method);
|
||||
@@ -259,28 +259,44 @@ class BindSpec {
|
||||
// Parse return type using improved type parser
|
||||
final parsedReturnType = TypeParser.parseType(method.returnType, method);
|
||||
|
||||
final methodName = method.displayName;
|
||||
final methodName = method.firstFragment.name2 ?? '';
|
||||
|
||||
// Check for @singleton annotation.
|
||||
final isSingleton = MetadataUtils.anyMeta(method.metadata, 'singleton');
|
||||
final isSingleton = MetadataUtils.anyMeta(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
'singleton',
|
||||
);
|
||||
|
||||
// Get @named value if present.
|
||||
final named = MetadataUtils.getNamedValue(method.metadata);
|
||||
final named = MetadataUtils.getNamedValue(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
);
|
||||
|
||||
// Parse each method parameter.
|
||||
final params = <BindParameterSpec>[];
|
||||
bool hasParams = false;
|
||||
for (final p in method.parameters) {
|
||||
for (final p in method.formalParameters) {
|
||||
final typeStr = p.type.getDisplayString();
|
||||
final paramNamed = MetadataUtils.getNamedValue(p.metadata);
|
||||
final isParams = MetadataUtils.anyMeta(p.metadata, 'params');
|
||||
final paramNamed = MetadataUtils.getNamedValue(
|
||||
p.firstFragment.metadata2.annotations,
|
||||
);
|
||||
final isParams = MetadataUtils.anyMeta(
|
||||
p.firstFragment.metadata2.annotations,
|
||||
'params',
|
||||
);
|
||||
if (isParams) hasParams = true;
|
||||
params.add(BindParameterSpec(typeStr, paramNamed, isParams: isParams));
|
||||
}
|
||||
|
||||
// Determine bindingType: @instance or @provide.
|
||||
final hasInstance = MetadataUtils.anyMeta(method.metadata, 'instance');
|
||||
final hasProvide = MetadataUtils.anyMeta(method.metadata, 'provide');
|
||||
final hasInstance = MetadataUtils.anyMeta(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
'instance',
|
||||
);
|
||||
final hasProvide = MetadataUtils.anyMeta(
|
||||
method.firstFragment.metadata2.annotations,
|
||||
'provide',
|
||||
);
|
||||
|
||||
if (!hasInstance && !hasProvide) {
|
||||
throw AnnotationValidationException(
|
||||
@@ -295,8 +311,9 @@ class BindSpec {
|
||||
);
|
||||
}
|
||||
|
||||
final bindingType =
|
||||
hasInstance ? BindingType.instance : BindingType.provide;
|
||||
final bindingType = hasInstance
|
||||
? BindingType.instance
|
||||
: BindingType.provide;
|
||||
|
||||
// PROHIBIT @params with @instance bindings!
|
||||
if (bindingType == BindingType.instance && hasParams) {
|
||||
|
||||
@@ -11,32 +11,58 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:source_gen/source_gen.dart';
|
||||
|
||||
/// Enhanced exception class for CherryPick generator with detailed context information
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// CherryPickGeneratorException
|
||||
///
|
||||
/// The base exception for all CherryPick code generation and annotation
|
||||
/// validation errors. This exception provides enhanced diagnostics including
|
||||
/// the error category, helpful suggestions, and additional debugging context.
|
||||
///
|
||||
/// All errors are structured to be as helpful as possible for users
|
||||
/// running build_runner and for CherryPick contributors debugging generators.
|
||||
///
|
||||
/// ## Example usage:
|
||||
/// ```dart
|
||||
/// if (someErrorCondition) {
|
||||
/// throw AnnotationValidationException(
|
||||
/// 'Custom message about what went wrong',
|
||||
/// element: methodElement,
|
||||
/// suggestion: 'Add @provide() or @instance() annotation',
|
||||
/// context: {'found_annotations': annotations},
|
||||
/// );
|
||||
/// }
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
/// A string describing the error category (for grouping).
|
||||
final String category;
|
||||
|
||||
/// An optional suggestion string for resolving the error.
|
||||
final String? suggestion;
|
||||
|
||||
/// Arbitrary key-value map for additional debugging information.
|
||||
final Map<String, dynamic>? context;
|
||||
|
||||
CherryPickGeneratorException(
|
||||
String message, {
|
||||
required Element element,
|
||||
required Element2 element,
|
||||
required this.category,
|
||||
this.suggestion,
|
||||
this.context,
|
||||
}) : super(
|
||||
_formatMessage(message, category, suggestion, context, element),
|
||||
element: element,
|
||||
);
|
||||
_formatMessage(message, category, suggestion, context, element),
|
||||
element: element,
|
||||
);
|
||||
|
||||
static String _formatMessage(
|
||||
String message,
|
||||
String category,
|
||||
String? suggestion,
|
||||
Map<String, dynamic>? context,
|
||||
Element element,
|
||||
Element2 element,
|
||||
) {
|
||||
final buffer = StringBuffer();
|
||||
|
||||
@@ -48,9 +74,11 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
buffer.writeln('Context:');
|
||||
buffer.writeln(' Element: ${element.displayName}');
|
||||
buffer.writeln(' Type: ${element.runtimeType}');
|
||||
buffer.writeln(' Location: ${element.source?.fullName ?? 'unknown'}');
|
||||
buffer.writeln(
|
||||
' Location: ${element.firstFragment.libraryFragment?.source.fullName ?? 'unknown'}',
|
||||
);
|
||||
|
||||
// Note: enclosingElement may not be available in all analyzer versions
|
||||
// Try to show enclosing element info for extra context
|
||||
try {
|
||||
final enclosing = (element as dynamic).enclosingElement;
|
||||
if (enclosing != null) {
|
||||
@@ -60,7 +88,7 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
// Ignore if enclosingElement is not available
|
||||
}
|
||||
|
||||
// Additional context
|
||||
// Arbitrary user context
|
||||
if (context != null && context.isNotEmpty) {
|
||||
buffer.writeln('');
|
||||
buffer.writeln('Additional Context:');
|
||||
@@ -69,7 +97,7 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
});
|
||||
}
|
||||
|
||||
// Suggestion
|
||||
// Hint/suggestion if present
|
||||
if (suggestion != null) {
|
||||
buffer.writeln('');
|
||||
buffer.writeln('💡 Suggestion: $suggestion');
|
||||
@@ -79,7 +107,24 @@ class CherryPickGeneratorException extends InvalidGenerationSourceError {
|
||||
}
|
||||
}
|
||||
|
||||
/// Specific exception types for different error categories
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// AnnotationValidationException
|
||||
///
|
||||
/// Thrown when annotation usage is invalid (e.g., missing required annotation,
|
||||
/// mutually exclusive annotations, or incorrect @named format).
|
||||
///
|
||||
/// Grouped as category "ANNOTATION_VALIDATION".
|
||||
///
|
||||
/// ## Example:
|
||||
/// ```dart
|
||||
/// throw AnnotationValidationException(
|
||||
/// '@instance and @provide cannot be used together',
|
||||
/// element: method,
|
||||
/// suggestion: 'Use only one of @instance or @provide.',
|
||||
/// context: {'method_name': method.displayName},
|
||||
/// );
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class AnnotationValidationException extends CherryPickGeneratorException {
|
||||
AnnotationValidationException(
|
||||
super.message, {
|
||||
@@ -89,6 +134,24 @@ class AnnotationValidationException extends CherryPickGeneratorException {
|
||||
}) : super(category: 'ANNOTATION_VALIDATION');
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// TypeParsingException
|
||||
///
|
||||
/// Thrown when a Dart type cannot be interpreted/parsed for DI,
|
||||
/// or if it's not compatible (void, raw Future, etc).
|
||||
///
|
||||
/// Grouped as category "TYPE_PARSING".
|
||||
///
|
||||
/// ## Example:
|
||||
/// ```dart
|
||||
/// throw TypeParsingException(
|
||||
/// 'Cannot parse injected type',
|
||||
/// element: field,
|
||||
/// suggestion: 'Specify a concrete type. Avoid dynamic and raw Future.',
|
||||
/// context: {'type': field.type.getDisplayString()},
|
||||
/// );
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class TypeParsingException extends CherryPickGeneratorException {
|
||||
TypeParsingException(
|
||||
super.message, {
|
||||
@@ -98,6 +161,23 @@ class TypeParsingException extends CherryPickGeneratorException {
|
||||
}) : super(category: 'TYPE_PARSING');
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// CodeGenerationException
|
||||
///
|
||||
/// Thrown on unexpected code generation or formatting failures
|
||||
/// during generator execution.
|
||||
///
|
||||
/// Grouped as category "CODE_GENERATION".
|
||||
///
|
||||
/// ## Example:
|
||||
/// ```dart
|
||||
/// throw CodeGenerationException(
|
||||
/// 'Could not generate module binding',
|
||||
/// element: classElement,
|
||||
/// suggestion: 'Check module class methods and signatures.',
|
||||
/// );
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class CodeGenerationException extends CherryPickGeneratorException {
|
||||
CodeGenerationException(
|
||||
super.message, {
|
||||
@@ -107,6 +187,23 @@ class CodeGenerationException extends CherryPickGeneratorException {
|
||||
}) : super(category: 'CODE_GENERATION');
|
||||
}
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// DependencyResolutionException
|
||||
///
|
||||
/// Thrown if dependency information (for example, types or names)
|
||||
/// cannot be resolved during code generation analysis.
|
||||
///
|
||||
/// Grouped as category "DEPENDENCY_RESOLUTION".
|
||||
///
|
||||
/// ## Example:
|
||||
/// ```dart
|
||||
/// throw DependencyResolutionException(
|
||||
/// 'Dependency type not found in scope',
|
||||
/// element: someElement,
|
||||
/// suggestion: 'Check CherryPick registration for this type.',
|
||||
/// );
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class DependencyResolutionException extends CherryPickGeneratorException {
|
||||
DependencyResolutionException(
|
||||
super.message, {
|
||||
|
||||
@@ -12,45 +12,49 @@
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'bind_spec.dart';
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// GeneratedClass -- represents the result of processing a single module class.
|
||||
/// GeneratedClass
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Encapsulates all the information produced from analyzing a DI module class:
|
||||
/// - The original class name,
|
||||
/// - Its generated class name (e.g., `$SomeModule`),
|
||||
/// - The collection of bindings (BindSpec) for all implemented provider methods.
|
||||
/// Represents a processed DI module class with all its binding methods analyzed.
|
||||
/// Stores:
|
||||
/// - The original class name,
|
||||
/// - The generated implementation class name (with $ prefix),
|
||||
/// - The list of all BindSpec for the module methods,
|
||||
/// - The source file name for reference or directive generation.
|
||||
///
|
||||
/// Also provides code generation functionality, allowing to generate the source
|
||||
/// code for the derived DI module class, including all binding registrations.
|
||||
/// Provides static and instance methods to construct from a ClassElement
|
||||
/// and generate Dart source code for the resulting DI registration class.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Описывает результат обработки одного класса-модуля DI:
|
||||
/// - Имя оригинального класса,
|
||||
/// - Имя генерируемого класса (например, `$SomeModule`),
|
||||
/// - Список всех бидингов (BindSpec) — по публичным методам модуля.
|
||||
///
|
||||
/// Также содержит функцию генерации исходного кода для этого класса и
|
||||
/// регистрации всех зависимостей через bind(...).
|
||||
/// ## Example usage
|
||||
/// ```dart
|
||||
/// final gen = GeneratedClass.fromClassElement(myModuleClassElement);
|
||||
/// print(gen.generate());
|
||||
/// /*
|
||||
/// Produces:
|
||||
/// final class $MyModule extends MyModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Service>().toProvide(() => provideService(currentScope.resolve<Dep>()));
|
||||
/// ...
|
||||
/// }
|
||||
/// }
|
||||
/// */
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class GeneratedClass {
|
||||
/// The name of the original module class.
|
||||
/// Имя исходного класса-модуля
|
||||
/// Name of the original Dart module class.
|
||||
final String className;
|
||||
|
||||
/// The name of the generated class (e.g., $SomeModule).
|
||||
/// Имя генерируемого класса (например, $SomeModule)
|
||||
/// Name of the generated class, e.g. `$MyModule`
|
||||
final String generatedClassName;
|
||||
|
||||
/// List of all discovered bindings for the class.
|
||||
/// Список всех обнаруженных биндингов
|
||||
/// Binding specs for all provider/factory methods in the class.
|
||||
final List<BindSpec> binds;
|
||||
|
||||
/// Source file name for the part directive
|
||||
/// Имя исходного файла для part директивы
|
||||
/// Source filename of the module class (for code references).
|
||||
final String sourceFile;
|
||||
|
||||
GeneratedClass(
|
||||
@@ -63,24 +67,20 @@ class GeneratedClass {
|
||||
/// -------------------------------------------------------------------------
|
||||
/// fromClassElement
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Static factory: creates a GeneratedClass from a Dart ClassElement (AST representation).
|
||||
/// Discovers all non-abstract methods, builds BindSpec for each, and computes the
|
||||
/// generated class name by prefixing `$`.
|
||||
/// Creates a [GeneratedClass] by analyzing a Dart [ClassElement].
|
||||
/// Collects all public non-abstract methods, creates a [BindSpec] for each,
|
||||
/// and infers the generated class name using a `$` prefix.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Строит объект класса по элементу AST (ClassElement): имя класса,
|
||||
/// сгенерированное имя, список BindSpec по всем не абстрактным методам.
|
||||
/// Имя ген-класса строится с префиксом `$`.
|
||||
/// -------------------------------------------------------------------------
|
||||
static GeneratedClass fromClassElement(ClassElement element) {
|
||||
final className = element.displayName;
|
||||
// Generated class name with '$' prefix (standard for generated Dart code).
|
||||
/// ## Example usage
|
||||
/// ```dart
|
||||
/// final gen = GeneratedClass.fromClassElement(classElement);
|
||||
/// print(gen.generatedClassName); // e.g. $AppModule
|
||||
/// ```
|
||||
static GeneratedClass fromClassElement(ClassElement2 element) {
|
||||
final className = element.firstFragment.name2 ?? '';
|
||||
final generatedClassName = r'$' + className;
|
||||
// Get source file name
|
||||
final sourceFile = element.source.shortName;
|
||||
// Collect bindings for all non-abstract methods.
|
||||
final binds = element.methods
|
||||
final sourceFile = element.firstFragment.libraryFragment.source.shortName;
|
||||
final binds = element.methods2
|
||||
.where((m) => !m.isAbstract)
|
||||
.map(BindSpec.fromMethod)
|
||||
.toList();
|
||||
@@ -91,16 +91,19 @@ class GeneratedClass {
|
||||
/// -------------------------------------------------------------------------
|
||||
/// generate
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Generates Dart source code for the DI module class. The generated class
|
||||
/// inherits from the original, overrides the 'builder' method, and registers
|
||||
/// all bindings in the DI scope.
|
||||
/// Generates the Dart source code for the DI registration class.
|
||||
/// The generated class extends the original module, and the `builder` method
|
||||
/// registers all bindings (dependencies) into the DI scope.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Генерирует исходный Dart-код для класса-модуля DI.
|
||||
/// Новая версия класса наследует оригинальный, переопределяет builder(Scope),
|
||||
/// и регистрирует все зависимости через методы bind<Type>()...
|
||||
/// -------------------------------------------------------------------------
|
||||
/// ## Example output
|
||||
/// ```dart
|
||||
/// final class $UserModule extends UserModule {
|
||||
/// @override
|
||||
/// void builder(Scope currentScope) {
|
||||
/// bind<Service>().toProvide(() => provideService(currentScope.resolve<Dep>()));
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
String generate() {
|
||||
final buffer = StringBuffer()
|
||||
..writeln('final class $generatedClassName extends $className {')
|
||||
@@ -108,7 +111,6 @@ class GeneratedClass {
|
||||
..writeln(' void builder(Scope currentScope) {');
|
||||
|
||||
// For each binding, generate bind<Type>() code string.
|
||||
// Для каждого биндинга — генерируем строку bind<Type>()...
|
||||
for (final bind in binds) {
|
||||
buffer.writeln(bind.generateBind(4));
|
||||
}
|
||||
|
||||
@@ -14,52 +14,54 @@
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
|
||||
/// ---------------------------------------------------------------------------
|
||||
/// MetadataUtils -- utilities for analyzing method and parameter annotations.
|
||||
/// MetadataUtils
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Provides static utility methods to analyze Dart annotations on methods or
|
||||
/// parameters. For instance, helps to find if an element is annotated with
|
||||
/// `@named()`, `@singleton()`, or other meta-annotations used in this DI framework.
|
||||
/// Static utilities for querying and extracting information from
|
||||
/// Dart annotations ([ElementAnnotation]) in the context of code generation,
|
||||
/// such as checking for the presence of specific DI-related annotations.
|
||||
/// Designed to be used internally by code generation and validation routines.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Утилиты для разбора аннотаций методов и параметров.
|
||||
/// Позволяют находить наличие аннотаций, например, @named() и @singleton(),
|
||||
/// у методов и параметров. Используется для анализа исходного кода при генерации.
|
||||
/// # Example usage
|
||||
/// ```dart
|
||||
/// if (MetadataUtils.anyMeta(method.metadata, 'singleton')) {
|
||||
/// // The method is annotated with @singleton
|
||||
/// }
|
||||
/// final name = MetadataUtils.getNamedValue(field.metadata);
|
||||
/// if (name != null) print('@named value: $name');
|
||||
/// ```
|
||||
/// ---------------------------------------------------------------------------
|
||||
class MetadataUtils {
|
||||
/// -------------------------------------------------------------------------
|
||||
/// anyMeta
|
||||
/// Checks whether any annotation in [meta] matches the [typeName]
|
||||
/// (type name is compared in a case-insensitive manner and can be partial).
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Checks if any annotation in the list has a type name that contains
|
||||
/// [typeName] (case insensitive).
|
||||
/// Returns true if an annotation (such as @singleton, @provide, @named) is found.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Проверяет: есть ли среди аннотаций метка, имя которой содержит [typeName]
|
||||
/// (регистр не учитывается).
|
||||
/// -------------------------------------------------------------------------
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// bool isSingleton = MetadataUtils.anyMeta(myMethod.metadata, 'singleton');
|
||||
/// ```
|
||||
static bool anyMeta(List<ElementAnnotation> meta, String typeName) {
|
||||
return meta.any((m) =>
|
||||
m
|
||||
.computeConstantValue()
|
||||
?.type
|
||||
?.getDisplayString()
|
||||
.toLowerCase()
|
||||
.contains(typeName.toLowerCase()) ??
|
||||
false);
|
||||
return meta.any(
|
||||
(m) =>
|
||||
m
|
||||
.computeConstantValue()
|
||||
?.type
|
||||
?.getDisplayString()
|
||||
.toLowerCase()
|
||||
.contains(typeName.toLowerCase()) ??
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
/// -------------------------------------------------------------------------
|
||||
/// getNamedValue
|
||||
/// Extracts the string value from a `@named('value')` annotation if present in [meta].
|
||||
///
|
||||
/// ENGLISH
|
||||
/// Retrieves the value from a `@named('value')` annotation if present.
|
||||
/// Returns the string value or null if not found.
|
||||
/// Returns the named value or `null` if not annotated.
|
||||
///
|
||||
/// RUSSIAN
|
||||
/// Находит значение из аннотации @named('значение').
|
||||
/// Возвращает строку значения, если аннотация присутствует; иначе null.
|
||||
/// -------------------------------------------------------------------------
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// // For: @named('dev') ApiClient provideApi() ...
|
||||
/// final named = MetadataUtils.getNamedValue(method.metadata); // 'dev'
|
||||
/// ```
|
||||
static String? getNamedValue(List<ElementAnnotation> meta) {
|
||||
for (final m in meta) {
|
||||
final cv = m.computeConstantValue();
|
||||
|
||||
@@ -11,15 +11,41 @@
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:analyzer/dart/element/nullability_suffix.dart';
|
||||
import 'package:analyzer/dart/element/type.dart';
|
||||
import 'exceptions.dart';
|
||||
|
||||
/// Enhanced type parser that uses AST analysis instead of regular expressions
|
||||
/// Utility for analyzing and parsing Dart types for CherryPick DI code generation.
|
||||
///
|
||||
/// This type parser leverages the Dart analyzer AST to extract nuanced information
|
||||
/// from Dart types encountered in the source code, in particular for dependency
|
||||
/// injection purposes. It is capable of extracting nullability, generics,
|
||||
/// and Future-related metadata with strong guarantees and handles even nested generics.
|
||||
///
|
||||
/// # Example usage for parsing types:
|
||||
/// ```dart
|
||||
/// final parsed = TypeParser.parseType(method.returnType, method);
|
||||
/// print(parsed);
|
||||
/// print(parsed.resolveMethodName); // e.g. "resolveAsync" or "tryResolve"
|
||||
/// ```
|
||||
///
|
||||
/// # Supported scenarios:
|
||||
/// - Nullable types (e.g., `List<String>?`)
|
||||
/// - Generic types (e.g., `Map<String, User>`)
|
||||
/// - Async types (`Future<T>`, including nested generics)
|
||||
/// - Validation for DI compatibility (throws for `void`, warns on `dynamic`)
|
||||
class TypeParser {
|
||||
/// Parses a DartType and extracts detailed type information
|
||||
static ParsedType parseType(DartType dartType, Element context) {
|
||||
/// Parses a [DartType] and extracts detailed type information for use in code generation.
|
||||
///
|
||||
/// If a type is not suitable or cannot be parsed, a [TypeParsingException] is thrown.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final parsed = TypeParser.parseType(field.type, field);
|
||||
/// if (parsed.isNullable) print('Field is nullable');
|
||||
/// ```
|
||||
static ParsedType parseType(DartType dartType, Element2 context) {
|
||||
try {
|
||||
return _parseTypeInternal(dartType, context);
|
||||
} catch (e) {
|
||||
@@ -35,7 +61,7 @@ class TypeParser {
|
||||
}
|
||||
}
|
||||
|
||||
static ParsedType _parseTypeInternal(DartType dartType, Element context) {
|
||||
static ParsedType _parseTypeInternal(DartType dartType, Element2 context) {
|
||||
final displayString = dartType.getDisplayString();
|
||||
final isNullable = dartType.nullabilitySuffix == NullabilitySuffix.question;
|
||||
|
||||
@@ -49,7 +75,7 @@ class TypeParser {
|
||||
return _parseGenericType(dartType, context, isNullable);
|
||||
}
|
||||
|
||||
// Simple type
|
||||
// Simple type (non-generic, non-Future)
|
||||
return ParsedType(
|
||||
displayString: displayString,
|
||||
coreType: displayString.replaceAll('?', ''),
|
||||
@@ -61,7 +87,10 @@ class TypeParser {
|
||||
}
|
||||
|
||||
static ParsedType _parseFutureType(
|
||||
DartType dartType, Element context, bool isNullable) {
|
||||
DartType dartType,
|
||||
Element2 context,
|
||||
bool isNullable,
|
||||
) {
|
||||
if (dartType is! ParameterizedType || dartType.typeArguments.isEmpty) {
|
||||
throw TypeParsingException(
|
||||
'Future type must have a type argument',
|
||||
@@ -86,7 +115,10 @@ class TypeParser {
|
||||
}
|
||||
|
||||
static ParsedType _parseGenericType(
|
||||
ParameterizedType dartType, Element context, bool isNullable) {
|
||||
ParameterizedType dartType,
|
||||
Element2 context,
|
||||
bool isNullable,
|
||||
) {
|
||||
final typeArguments = dartType.typeArguments
|
||||
.map((arg) => _parseTypeInternal(arg, context))
|
||||
.toList();
|
||||
@@ -103,8 +135,16 @@ class TypeParser {
|
||||
);
|
||||
}
|
||||
|
||||
/// Validates that a type is suitable for dependency injection
|
||||
static void validateInjectableType(ParsedType parsedType, Element context) {
|
||||
/// Validates that a parsed type is suitable for dependency injection.
|
||||
///
|
||||
/// Throws [TypeParsingException] for void and may warn for dynamic.
|
||||
///
|
||||
/// Example:
|
||||
/// ```dart
|
||||
/// final parsed = TypeParser.parseType(field.type, field);
|
||||
/// TypeParser.validateInjectableType(parsed, field);
|
||||
/// ```
|
||||
static void validateInjectableType(ParsedType parsedType, Element2 context) {
|
||||
// Check for void type
|
||||
if (parsedType.coreType == 'void') {
|
||||
throw TypeParsingException(
|
||||
@@ -131,7 +171,7 @@ class TypeParser {
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a parsed type with detailed information
|
||||
/// Represents a parsed type with full metadata for code generation.
|
||||
class ParsedType {
|
||||
/// The full display string of the type (e.g., "Future<List<String>?>")
|
||||
final String displayString;
|
||||
@@ -139,19 +179,19 @@ class ParsedType {
|
||||
/// The core type name without nullability and Future wrapper (e.g., "List<String>")
|
||||
final String coreType;
|
||||
|
||||
/// Whether the type is nullable
|
||||
/// True if nullable (has `?`)
|
||||
final bool isNullable;
|
||||
|
||||
/// Whether the type is wrapped in Future
|
||||
/// True if this type is a `Future<T>`
|
||||
final bool isFuture;
|
||||
|
||||
/// Whether the type has generic parameters
|
||||
/// True if the type is a generic type (`List<T>`)
|
||||
final bool isGeneric;
|
||||
|
||||
/// Parsed type arguments for generic types
|
||||
/// List of parsed type arguments in generics, if any.
|
||||
final List<ParsedType> typeArguments;
|
||||
|
||||
/// For Future types, the inner type
|
||||
/// For `Future<T>`, this is the type inside the `Future`.
|
||||
final ParsedType? innerType;
|
||||
|
||||
const ParsedType({
|
||||
@@ -164,7 +204,11 @@ class ParsedType {
|
||||
this.innerType,
|
||||
});
|
||||
|
||||
/// Returns the type string suitable for code generation
|
||||
/// Generates the type string suitable for code generation.
|
||||
///
|
||||
/// - For futures, the codegen type of the inner type is returned
|
||||
/// - For generics, returns e.g. `List<User>`
|
||||
/// - For plain types, just the name
|
||||
String get codeGenType {
|
||||
if (isFuture && innerType != null) {
|
||||
return innerType!.codeGenType;
|
||||
@@ -179,10 +223,10 @@ class ParsedType {
|
||||
return coreType;
|
||||
}
|
||||
|
||||
/// Returns whether this type should use tryResolve instead of resolve
|
||||
/// True if this type should use `tryResolve` instead of `resolve` for DI.
|
||||
bool get shouldUseTryResolve => isNullable;
|
||||
|
||||
/// Returns the appropriate resolve method name
|
||||
/// Returns the method name for DI, e.g. "resolve", "tryResolveAsync", etc.
|
||||
String get resolveMethodName {
|
||||
if (isFuture) {
|
||||
return shouldUseTryResolve ? 'tryResolveAsync' : 'resolveAsync';
|
||||
|
||||
@@ -2,8 +2,9 @@ name: cherrypick_generator
|
||||
description: |
|
||||
Source code generator for the cherrypick dependency injection system. Processes annotations to generate binding and module code for Dart & Flutter projects.
|
||||
|
||||
version: 1.1.1
|
||||
documentation: https://github.com/pese-git/cherrypick/wiki
|
||||
version: 3.0.2-dev.0
|
||||
homepage: https://cherrypick-di.netlify.app
|
||||
documentation: https://cherrypick-di.netlify.app/docs/intro
|
||||
repository: https://github.com/pese-git/cherrypick/cherrypick_generator
|
||||
issue_tracker: https://github.com/pese-git/cherrypick/issues
|
||||
topics:
|
||||
@@ -14,20 +15,20 @@ topics:
|
||||
- inversion-of-control
|
||||
|
||||
environment:
|
||||
sdk: ">=3.5.2 <4.0.0"
|
||||
sdk: ">=3.8.0 <4.0.0"
|
||||
|
||||
# Add regular dependencies here.
|
||||
dependencies:
|
||||
cherrypick_annotations: ^1.1.1
|
||||
analyzer: ^7.0.0
|
||||
cherrypick_annotations: ^3.0.2-dev.0
|
||||
analyzer: ">=7.5.9 <8.0.0"
|
||||
dart_style: ^3.0.0
|
||||
build: ^2.4.1
|
||||
source_gen: ^2.0.0
|
||||
build: ^3.0.0
|
||||
source_gen: ^3.1.0
|
||||
collection: ^1.18.0
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^4.0.0
|
||||
mockito: ^5.4.4
|
||||
lints: ^6.0.0
|
||||
mockito: ^5.4.5
|
||||
test: ^1.25.8
|
||||
build_test: ^2.1.7
|
||||
build_runner: ^2.4.13
|
||||
build_test: ^3.0.0
|
||||
build_runner: ^2.5.0
|
||||
|
||||
@@ -244,8 +244,7 @@ void main() {
|
||||
final result = bindSpec.generateBind(4);
|
||||
expect(
|
||||
result,
|
||||
equals(
|
||||
" bind<ApiClient>()\n"
|
||||
equals(" bind<ApiClient>()\n"
|
||||
" .toProvideAsync(() => createApiClient())\n"
|
||||
" .withName('mainApi')\n"
|
||||
" .singleton();"));
|
||||
|
||||
@@ -480,9 +480,10 @@ void notAClass() {}
|
||||
);
|
||||
});
|
||||
|
||||
test('should generate empty mixin for class without @inject fields',
|
||||
() async {
|
||||
const input = '''
|
||||
test(
|
||||
'should generate empty mixin for class without @inject fields',
|
||||
() async {
|
||||
const input = '''
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
|
||||
part 'test_widget.inject.cherrypick.g.dart';
|
||||
@@ -494,7 +495,7 @@ class TestWidget {
|
||||
}
|
||||
''';
|
||||
|
||||
const expectedOutput = '''
|
||||
const expectedOutput = '''
|
||||
// dart format width=80
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
@@ -509,8 +510,9 @@ mixin _\$TestWidget {
|
||||
}
|
||||
''';
|
||||
|
||||
await _testGeneration(input, expectedOutput);
|
||||
});
|
||||
await _testGeneration(input, expectedOutput);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('Edge Cases', () {
|
||||
@@ -593,12 +595,8 @@ mixin _\$TestWidget {
|
||||
Future<void> _testGeneration(String input, String expectedOutput) async {
|
||||
await testBuilder(
|
||||
injectBuilder(BuilderOptions.empty),
|
||||
{
|
||||
'a|lib/test_widget.dart': input,
|
||||
},
|
||||
outputs: {
|
||||
'a|lib/test_widget.inject.cherrypick.g.dart': expectedOutput,
|
||||
},
|
||||
reader: await PackageAssetReader.currentIsolate(),
|
||||
{'a|lib/test_widget.dart': input},
|
||||
outputs: {'a|lib/test_widget.inject.cherrypick.g.dart': expectedOutput},
|
||||
readerWriter: TestReaderWriter(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -590,9 +590,10 @@ void notAClass() {}
|
||||
);
|
||||
});
|
||||
|
||||
test('should throw error for method without @instance or @provide',
|
||||
() async {
|
||||
const input = '''
|
||||
test(
|
||||
'should throw error for method without @instance or @provide',
|
||||
() async {
|
||||
const input = '''
|
||||
import 'package:cherrypick_annotations/cherrypick_annotations.dart';
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
|
||||
@@ -604,11 +605,12 @@ abstract class TestModule extends Module {
|
||||
}
|
||||
''';
|
||||
|
||||
await expectLater(
|
||||
() => _testGeneration(input, ''),
|
||||
throwsA(isA<InvalidGenerationSourceError>()),
|
||||
);
|
||||
});
|
||||
await expectLater(
|
||||
() => _testGeneration(input, ''),
|
||||
throwsA(isA<InvalidGenerationSourceError>()),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('should throw error for @params with @instance', () async {
|
||||
const input = '''
|
||||
@@ -637,12 +639,8 @@ abstract class TestModule extends Module {
|
||||
Future<void> _testGeneration(String input, String expectedOutput) async {
|
||||
await testBuilder(
|
||||
moduleBuilder(BuilderOptions.empty),
|
||||
{
|
||||
'a|lib/test_module.dart': input,
|
||||
},
|
||||
outputs: {
|
||||
'a|lib/test_module.module.cherrypick.g.dart': expectedOutput,
|
||||
},
|
||||
reader: await PackageAssetReader.currentIsolate(),
|
||||
{'a|lib/test_module.dart': input},
|
||||
outputs: {'a|lib/test_module.module.cherrypick.g.dart': expectedOutput},
|
||||
readerWriter: TestReaderWriter(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import 'package:analyzer/dart/element/element2.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:analyzer/dart/element/element.dart';
|
||||
import 'package:analyzer/source/source.dart';
|
||||
import 'package:cherrypick_generator/src/type_parser.dart';
|
||||
import 'package:cherrypick_generator/src/exceptions.dart';
|
||||
|
||||
@@ -42,7 +41,9 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => TypeParser.validateInjectableType(
|
||||
parsedType, _createMockElement()),
|
||||
parsedType,
|
||||
_createMockElement(),
|
||||
),
|
||||
throwsA(isA<TypeParsingException>()),
|
||||
);
|
||||
});
|
||||
@@ -59,7 +60,9 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => TypeParser.validateInjectableType(
|
||||
parsedType, _createMockElement()),
|
||||
parsedType,
|
||||
_createMockElement(),
|
||||
),
|
||||
throwsA(isA<TypeParsingException>()),
|
||||
);
|
||||
});
|
||||
@@ -76,7 +79,9 @@ void main() {
|
||||
|
||||
expect(
|
||||
() => TypeParser.validateInjectableType(
|
||||
parsedType, _createMockElement()),
|
||||
parsedType,
|
||||
_createMockElement(),
|
||||
),
|
||||
returnsNormally,
|
||||
);
|
||||
});
|
||||
@@ -159,19 +164,21 @@ void main() {
|
||||
expect(parsedType.resolveMethodName, equals('resolveAsync'));
|
||||
});
|
||||
|
||||
test('should return correct resolveMethodName for nullable async types',
|
||||
() {
|
||||
final parsedType = ParsedType(
|
||||
displayString: 'Future<String?>',
|
||||
coreType: 'String',
|
||||
isNullable: true,
|
||||
isFuture: true,
|
||||
isGeneric: false,
|
||||
typeArguments: [],
|
||||
);
|
||||
test(
|
||||
'should return correct resolveMethodName for nullable async types',
|
||||
() {
|
||||
final parsedType = ParsedType(
|
||||
displayString: 'Future<String?>',
|
||||
coreType: 'String',
|
||||
isNullable: true,
|
||||
isFuture: true,
|
||||
isGeneric: false,
|
||||
typeArguments: [],
|
||||
);
|
||||
|
||||
expect(parsedType.resolveMethodName, equals('tryResolveAsync'));
|
||||
});
|
||||
expect(parsedType.resolveMethodName, equals('tryResolveAsync'));
|
||||
},
|
||||
);
|
||||
|
||||
test('should implement equality correctly', () {
|
||||
final parsedType1 = ParsedType(
|
||||
@@ -216,19 +223,19 @@ void main() {
|
||||
}
|
||||
|
||||
// Mock element for testing
|
||||
Element _createMockElement() {
|
||||
Element2 _createMockElement() {
|
||||
return _MockElement();
|
||||
}
|
||||
|
||||
class _MockElement implements Element {
|
||||
class _MockElement implements Element2 {
|
||||
@override
|
||||
String get displayName => 'MockElement';
|
||||
|
||||
@override
|
||||
String get name => 'MockElement';
|
||||
|
||||
@override
|
||||
Source? get source => null;
|
||||
//@override
|
||||
//String get name => 'MockElement';
|
||||
//
|
||||
//@override
|
||||
//Source? get source => null;
|
||||
|
||||
@override
|
||||
noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
||||
|
||||
BIN
doc/assets/cherrypick-banner.jpg
Normal file
BIN
doc/assets/cherrypick-banner.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 224 KiB |
@@ -44,6 +44,46 @@ final setupFuture = loadEnvironment();
|
||||
bind<Environment>().toInstanceAsync(setupFuture);
|
||||
```
|
||||
|
||||
> ⚠️ **Important note about using toInstance in Module**
|
||||
>
|
||||
> If you register a chain of dependencies via `toInstance` inside the `builder` method of your `Module`, you must NOT call `scope.resolve<T>()` for a type that you have just bound—at this moment.
|
||||
>
|
||||
> CherryPick initializes all bindings inside `builder` sequentially: at the time of a new binding, not all other dependencies are registered yet in the DI container. If you try to use `scope.resolve<T>()` for an object you have just added in the same `builder`, it will result in an error (`Can't resolve dependency ...`).
|
||||
>
|
||||
> **Correct way:**
|
||||
> Manually construct the entire object chain before registering:
|
||||
>
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> final a = A();
|
||||
> final b = B(a);
|
||||
> final c = C(b);
|
||||
> bind<A>().toInstance(a);
|
||||
> bind<B>().toInstance(b);
|
||||
> bind<C>().toInstance(c);
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Incorrect:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toInstance(A());
|
||||
> // Error! At this point, A is not registered yet.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Incorrect:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toProvide(() => A());
|
||||
> // Error! At this point, A is not registered yet.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Note:** This limitation applies only to `toInstance`. For providers (`toProvide`/`toProvideAsync`) and other strategies, you can freely use `scope.resolve<T>()` inside `builder`.
|
||||
|
||||
- **toProvide** — regular sync factory
|
||||
- **toProvideAsync** — async factory (if you need to await a Future)
|
||||
- **toProvideWithParams / toProvideAsyncWithParams** — factories with runtime parameters
|
||||
@@ -67,6 +107,14 @@ final api = scope.resolve<ApiClient>(named: 'mock');
|
||||
- `.singleton()` — single instance per Scope lifetime
|
||||
- By default, every resolve creates a new object
|
||||
|
||||
> ℹ️ **Note about `.singleton()` and `.toInstance()`:**
|
||||
>
|
||||
> Calling `.singleton()` after `.toInstance()` does **not** change the binding’s behavior: the object passed with `toInstance()` is already a single, constant instance that will be always returned for every resolve.
|
||||
>
|
||||
> It is not necessary to use `.singleton()` with an existing object—this call has no effect.
|
||||
>
|
||||
> `.singleton()` is only meaningful with providers (such as `toProvide`/`toProvideAsync`), to ensure only one instance is created by the factory.
|
||||
|
||||
### Parameterized bindings
|
||||
|
||||
Allows you to create dependencies with runtime parameters, e.g., a service for a user with a given ID:
|
||||
@@ -78,6 +126,26 @@ bind<UserService>().toProvideWithParams((userId) => UserService(userId));
|
||||
final userService = scope.resolve<UserService>(params: '123');
|
||||
```
|
||||
|
||||
> ⚠️ **Special note on using `.singleton()` after `toProvideWithParams` or `toProvideAsyncWithParams`:**
|
||||
>
|
||||
> If you declare a binding using `.toProvideWithParams((params) => ...)` (or its async variant) and then call `.singleton()`, the DI container will create and cache **only one instance** on the first `resolve` call—with the initial parameters. All subsequent calls to `resolve<T>(params: ...)` will return that same (cached) instance, **regardless of the new parameters**.
|
||||
>
|
||||
> **Example:**
|
||||
> ```dart
|
||||
> bind<Service>().toProvideWithParams((params) => Service(params)).singleton();
|
||||
>
|
||||
> final a = scope.resolve<Service>(params: 1); // Creates Service(1)
|
||||
> final b = scope.resolve<Service>(params: 2); // Returns Service(1)
|
||||
> print(identical(a, b)); // true
|
||||
> ```
|
||||
>
|
||||
> In other words:
|
||||
> - The provider function receives parameters only on its first call,
|
||||
> - After that, no matter what parameters are passed, the same instance is always returned.
|
||||
>
|
||||
> **Recommendation:**
|
||||
> Use `.singleton()` with parameterized providers only if you are sure all parameters should always be identical, or you intentionally want a “master” instance. Otherwise, omit `.singleton()` to ensure a new object is constructed for every unique `params` value.
|
||||
|
||||
---
|
||||
|
||||
## Scope management: dependency hierarchy
|
||||
|
||||
@@ -45,6 +45,48 @@ bind<Environment>().toInstanceAsync(setupFuture);
|
||||
```
|
||||
|
||||
|
||||
> ⚠️ **Важное примечание по использованию toInstance в Module**
|
||||
>
|
||||
> Если вы регистрируете цепочку зависимостей через `toInstance` внутри метода `builder` вашего `Module`, нельзя в это же время вызывать `scope.resolve<T>()` для только что объявленного типа.
|
||||
>
|
||||
> CherryPick инициализирует биндинги последовательно внутри builder: в этот момент ещё не все зависимости зарегистрированы в DI-контейнере. Попытка воспользоваться `scope.resolve<T>()` для только что добавленного объекта приведёт к ошибке (`Can't resolve dependency ...`).
|
||||
>
|
||||
> **Как правильно:**
|
||||
> Складывайте всю цепочку вручную до регистрации:
|
||||
>
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> final a = A();
|
||||
> final b = B(a);
|
||||
> final c = C(b);
|
||||
> bind<A>().toInstance(a);
|
||||
> bind<B>().toInstance(b);
|
||||
> bind<C>().toInstance(c);
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Неправильно:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toInstance(A());
|
||||
> // Ошибка! В этот момент A ещё не зарегистрирован.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Неправильно:**
|
||||
> ```dart
|
||||
> void builder(Scope scope) {
|
||||
> bind<A>().toProvide(() => A());
|
||||
> // Ошибка! В этот момент A ещё не зарегистрирован.
|
||||
> bind<B>().toInstance(B(scope.resolve<A>()));
|
||||
> }
|
||||
> ```
|
||||
>
|
||||
> **Примечание:** Это ограничение касается только `toInstance`. Для провайдеров (`toProvide`/`toProvideAsync`) и других стратегий вы можете использовать `scope.resolve<T>()` внутри builder без ограничений.
|
||||
|
||||
|
||||
|
||||
- **toProvide** — обычная синхронная фабрика.
|
||||
- **toProvideAsync** — асинхронная фабрика (например, если нужно дожидаться Future).
|
||||
- **toProvideWithParams / toProvideAsyncWithParams** — фабрики с параметрами.
|
||||
@@ -68,6 +110,15 @@ final api = scope.resolve<ApiClient>(named: 'mock');
|
||||
- `.singleton()` — один инстанс на всё время жизни Scope.
|
||||
- По умолчанию каждый resolve создаёт новый объект.
|
||||
|
||||
> ℹ️ **Примечание о `.singleton()` и `.toInstance()`:**
|
||||
>
|
||||
> Вызов `.singleton()` после `.toInstance()` не изменяет поведения биндинга: объект, переданный через `toInstance()`, и так всегда будет "единственным" (single instance), возвращаемым при каждом resolve.
|
||||
>
|
||||
> Применять `.singleton()` к уже существующему объекту нет необходимости — этот вызов ничего не меняет.
|
||||
>
|
||||
> `.singleton()` нужен только для провайдеров (например, `toProvide`/`toProvideAsync`), чтобы зафиксировать единственный экземпляр, создаваемый фабрикой.
|
||||
|
||||
|
||||
### Параметрические биндинги
|
||||
|
||||
Позволяют создавать зависимости с runtime-параметрами — например, сервис для пользователя с ID:
|
||||
@@ -79,6 +130,26 @@ bind<UserService>().toProvideWithParams((userId) => UserService(userId));
|
||||
final userService = scope.resolve<UserService>(params: '123');
|
||||
```
|
||||
|
||||
> ⚠️ **Особенности использования `.singleton()` после `toProvideWithParams` или `toProvideAsyncWithParams`:**
|
||||
>
|
||||
> Если вы объявляете биндинг через `.toProvideWithParams((params) => ...)` (или асинхронный вариант) и затем вызываете `.singleton()`, DI-контейнер создаст и закэширует **только один экземпляр** при первом вызове `resolve` — с первыми переданными параметрами. Все последующие вызовы `resolve<T>(params: ...)` вернут этот же (кэшированный) объект **независимо от новых параметров**.
|
||||
>
|
||||
> **Пример:**
|
||||
> ```dart
|
||||
> bind<Service>().toProvideWithParams((params) => Service(params)).singleton();
|
||||
>
|
||||
> final a = scope.resolve<Service>(params: 1); // Создаётся Service(1)
|
||||
> final b = scope.resolve<Service>(params: 2); // Возвращается уже Service(1)
|
||||
> print(identical(a, b)); // true
|
||||
> ```
|
||||
>
|
||||
> То есть:
|
||||
> - параметры работают только для первого вызова,
|
||||
> - дальше всегда возвращается экземпляр, созданный при первом обращении.
|
||||
>
|
||||
> **Рекомендация:**
|
||||
> Используйте `.singleton()` совместно с провайдерами с параметрами только тогда, когда вы точно уверены, что все параметры всегда должны совпадать, или нужны именно “мастер”-экземпляры. В противном случае не используйте `.singleton()`, чтобы каждый вызов с новыми parameters создавал новый объект.
|
||||
|
||||
---
|
||||
|
||||
## Управление Scope'ами: иерархия зависимостей
|
||||
|
||||
178
doc/news/cherrypick-3.x_en.md
Normal file
178
doc/news/cherrypick-3.x_en.md
Normal file
@@ -0,0 +1,178 @@
|
||||
# Release - CherryPick 3.x
|
||||
|
||||
> **CherryPick** — a lightweight and modular DI framework for Dart and Flutter that solves dependency injection through strong typing, code generation, and dependency control.
|
||||
|
||||
Version **3.x** was recently released with significant improvements.
|
||||
|
||||
## Main Changes in 3.x
|
||||
|
||||
* **O(1) dependency resolution** — thanks to Map indexing of bindings, performance does not depend on the size of the scope in the DI graph. This provides noticeable speedup in large projects.
|
||||
* **Protection against circular dependencies** — checking works both within a single scope and across the entire hierarchy. When a cycle is detected, an informative exception with the dependency chain is thrown.
|
||||
* **Integration with Talker** — all DI events (registration, creation, deletion, errors) are logged and can be displayed in the console or UI.
|
||||
* **Automatic resource cleanup** — objects implementing `Disposable` are properly released when the scope is closed.
|
||||
* **Stabilized declarative approach support** — annotations and code generation now work more reliably and are more convenient for use in projects.
|
||||
|
||||
## Resource Cleanup Example
|
||||
|
||||
```dart
|
||||
class MyServiceWithSocket implements Disposable {
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await socket.close();
|
||||
print('Socket closed!');
|
||||
}
|
||||
}
|
||||
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
// singleton Api
|
||||
bind<MyServiceWithSocket>()
|
||||
.toProvide(() => MyServiceWithSocket())
|
||||
.singleton();
|
||||
}
|
||||
}
|
||||
|
||||
scope.installModules([AppModule()]);
|
||||
|
||||
await CherryPick.closeRootScope(); // will wait for async dispose to complete
|
||||
```
|
||||
|
||||
## Circular Dependency Checking
|
||||
|
||||
One of the new features in CherryPick 3.x is built-in cycle protection.
|
||||
This helps catch situations early where services start depending on each other recursively.
|
||||
|
||||
### How to Enable Checking
|
||||
|
||||
For checking within a single scope:
|
||||
|
||||
```dart
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
```
|
||||
|
||||
For global checking across the entire hierarchy:
|
||||
|
||||
```dart
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
final rootScope = CherryPick.openRootScope();
|
||||
```
|
||||
|
||||
### How a Cycle Can Occur
|
||||
|
||||
Suppose we have two services that depend on each other:
|
||||
|
||||
```dart
|
||||
class UserService {
|
||||
final OrderService orderService;
|
||||
UserService(this.orderService);
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
final UserService userService;
|
||||
OrderService(this.userService);
|
||||
}
|
||||
```
|
||||
|
||||
If we register them in the same scope:
|
||||
|
||||
```dart
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserService>().toProvide(() => UserService(scope.resolve()));
|
||||
bind<OrderService>().toProvide(() => OrderService(scope.resolve()));
|
||||
}
|
||||
}
|
||||
|
||||
final scope = CherryPick.openRootScope()
|
||||
..enableCycleDetection()
|
||||
..installModules([AppModule()]);
|
||||
|
||||
scope.resolve<UserService>();
|
||||
```
|
||||
|
||||
Then when trying to resolve the dependency, an exception will be thrown:
|
||||
|
||||
```bash
|
||||
❌ Circular dependency detected for UserService
|
||||
Dependency chain: UserService -> OrderService -> UserService
|
||||
```
|
||||
|
||||
This way, the error is detected immediately, not "somewhere in runtime".
|
||||
|
||||
## Integration with Talker
|
||||
|
||||
CherryPick 3.x allows logging all DI events through [Talker](https://pub.dev/packages/talker): registration, object creation, deletion, and errors. This is convenient for debugging and diagnosing the dependency graph.
|
||||
|
||||
Connection example:
|
||||
|
||||
```dart
|
||||
final talker = Talker();
|
||||
final observer = TalkerCherryPickObserver(talker);
|
||||
CherryPick.setGlobalObserver(observer);
|
||||
```
|
||||
|
||||
After this, DI events will be displayed in the console or UI:
|
||||
|
||||
```bash
|
||||
┌───────────────────────────────────────────────────────────────
|
||||
│ [info] 9:41:33 | [scope opened][CherryPick] scope_1757054493089_7072
|
||||
└───────────────────────────────────────────────────────────────
|
||||
┌───────────────────────────────────────────────────────────────
|
||||
│ [verbose] 9:41:33 | [diagnostic][CherryPick] Scope created: scope_1757054493089_7072 {type: Scope, name: scope_1757054493089_7072, description: scope created}
|
||||
└───────────────────────────────────────────────────────────────
|
||||
```
|
||||
|
||||
In the log, you can see when scopes are created, which objects are registered and deleted, and catch errors and cycles in real time.
|
||||
|
||||
|
||||
## Declarative Approach with Annotations
|
||||
|
||||
In addition to fully programmatic module descriptions, CherryPick supports **declarative DI style through annotations**.
|
||||
This allows minimizing manual code and automatically generating modules and mixins for automatic dependency injection.
|
||||
|
||||
Example of a declarative module:
|
||||
|
||||
```dart
|
||||
@module()
|
||||
abstract class AppModule extends Module {
|
||||
@provide()
|
||||
@singleton()
|
||||
Api api() => Api();
|
||||
|
||||
@provide()
|
||||
Repo repo(Api api) => Repo(api);
|
||||
}
|
||||
````
|
||||
|
||||
After code generation, you can automatically inject dependencies into widgets or services:
|
||||
|
||||
```dart
|
||||
@injectable()
|
||||
class MyScreen extends StatelessWidget with _$MyScreen {
|
||||
@inject()
|
||||
late final Repo repo;
|
||||
|
||||
MyScreen() {
|
||||
_inject(this);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This way you can choose an approach in development: **programmatic (imperative) with explicit dependency registration** or **declarative through annotations**.
|
||||
|
||||
|
||||
## Who Might Find CherryPick Useful?
|
||||
|
||||
* Projects where it's important to guarantee **no cycles in the dependency graph**;
|
||||
* Teams that want to **minimize manual DI code** and use a declarative style with annotations;
|
||||
* Applications that require **automatic resource cleanup** (sockets, controllers, streams).
|
||||
|
||||
## Useful Links
|
||||
|
||||
* 📦 Package: [pub.dev/packages/cherrypick](https://pub.dev/packages/cherrypick)
|
||||
* 💻 Code: [github.com/pese-git/cherrypick](https://github.com/pese-git/cherrypick)
|
||||
* 📖 Documentation: [cherrypick-di.netlify.app](https://cherrypick-di.netlify.app/)
|
||||
180
doc/news/cherrypick-3.x_ru.md
Normal file
180
doc/news/cherrypick-3.x_ru.md
Normal file
@@ -0,0 +1,180 @@
|
||||
# Release - CherryPick 3.x
|
||||
|
||||
> **CherryPick** — лёгкий и модульный DI-фреймворк для Dart и Flutter, который решает задачу через строгую типизацию, кодогенерацию и контроль за зависимостями.
|
||||
|
||||
Недавно вышла версия **3.x**, где появились заметные улучшения.
|
||||
|
||||
|
||||
## Основные изменения в 3.x
|
||||
|
||||
* **O(1) разрешение зависимостей** — благодаря Map-индексации биндингов производительность не зависит от размера скоупа в DI графе. На больших проектах это даёт ощутимое ускорение.
|
||||
* **Защита от циклических зависимостей** — проверка работает как внутри одного scope, так и во всей иерархии. При обнаружении цикла выбрасывается информативное исключение с цепочкой зависимостей.
|
||||
* **Интеграция с Talker** — все события DI (регистрация, создание, удаление, ошибки) логируются и могут выводиться в консоль или UI.
|
||||
* **Автоматическая очистка ресурсов** — объекты, реализующие `Disposable`, корректно освобождаются при закрытии scope.
|
||||
* **Стабилизирована поддержка декларативного подхода** — аннотации и генерация кода теперь работают надёжнее и удобнее для использования в проектах.
|
||||
|
||||
|
||||
## Пример с очисткой ресурсов
|
||||
|
||||
```dart
|
||||
class MyServiceWithSocket implements Disposable {
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await socket.close();
|
||||
print('Socket закрыт!');
|
||||
}
|
||||
}
|
||||
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
// singleton Api
|
||||
bind<MyServiceWithSocket>()
|
||||
.toProvide(() => MyServiceWithSocket())
|
||||
.singleton();
|
||||
}
|
||||
}
|
||||
|
||||
scope.installModules([AppModule()]);
|
||||
|
||||
await CherryPick.closeRootScope(); // дождётся завершения async dispose
|
||||
```
|
||||
|
||||
## Проверка циклических зависимостей
|
||||
|
||||
Одна из новинок CherryPick 3.x — встроенная защита от циклов.
|
||||
Это помогает на раннем этапе отлавливать ситуации, когда сервисы начинают зависеть друг от друга рекурсивно.
|
||||
|
||||
### Как включить проверку
|
||||
|
||||
Для проверки внутри одного scope:
|
||||
|
||||
```dart
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
```
|
||||
|
||||
Для глобальной проверки во всей иерархии:
|
||||
|
||||
```dart
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
final rootScope = CherryPick.openRootScope();
|
||||
```
|
||||
|
||||
### Как может возникнуть цикл
|
||||
|
||||
Предположим, у нас есть два сервиса, которые зависят друг от друга:
|
||||
|
||||
```dart
|
||||
class UserService {
|
||||
final OrderService orderService;
|
||||
UserService(this.orderService);
|
||||
}
|
||||
|
||||
class OrderService {
|
||||
final UserService userService;
|
||||
OrderService(this.userService);
|
||||
}
|
||||
```
|
||||
|
||||
Если зарегистрировать их в одном scope:
|
||||
|
||||
```dart
|
||||
class AppModule extends Module {
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<UserService>().toProvide(() => UserService(scope.resolve());
|
||||
bind<OrderService>().toProvide(() => OrderService(scope.resolve()));
|
||||
}
|
||||
}
|
||||
|
||||
final scope = CherryPick.openRootScope()
|
||||
..enableCycleDetection()
|
||||
..installModules([AppModule()]);
|
||||
|
||||
scope.resolve<UserService>();
|
||||
```
|
||||
|
||||
То при попытке разрешить зависимость будет выброшено исключение:
|
||||
|
||||
```bash
|
||||
❌ Circular dependency detected for UserService
|
||||
Dependency chain: UserService -> OrderService -> UserService
|
||||
```
|
||||
|
||||
Таким образом, ошибка выявляется сразу, а не «где-то в runtime».
|
||||
|
||||
## Интеграция с Talker
|
||||
|
||||
CherryPick 3.x позволяет логировать все события DI через [Talker](https://pub.dev/packages/talker): регистрацию, создание объектов, удаление и ошибки. Это удобно для отладки и диагностики графа зависимостей.
|
||||
|
||||
Пример подключения:
|
||||
|
||||
```dart
|
||||
final talker = Talker();
|
||||
final observer = TalkerCherryPickObserver(talker);
|
||||
CherryPick.setGlobalObserver(observer);
|
||||
```
|
||||
|
||||
После этого в консоли или UI будут отображаться события DI:
|
||||
|
||||
```bash
|
||||
┌───────────────────────────────────────────────────────────────
|
||||
│ [info] 9:41:33 | [scope opened][CherryPick] scope_1757054493089_7072
|
||||
└───────────────────────────────────────────────────────────────
|
||||
┌───────────────────────────────────────────────────────────────
|
||||
│ [verbose] 9:41:33 | [diagnostic][CherryPick] Scope created: scope_1757054493089_7072 {type: Scope, name: scope_1757054493089_7072, description: scope created}
|
||||
└───────────────────────────────────────────────────────────────
|
||||
```
|
||||
|
||||
В логе можно увидеть, когда scope создаётся, какие объекты регистрируются и удаляются, а также отлавливать ошибки и циклы в реальном времени.
|
||||
|
||||
|
||||
## Декларативный подход с аннотациями
|
||||
|
||||
Помимо полностью программного описания модулей, CherryPick поддерживает **декларативный стиль DI через аннотации**.
|
||||
Это позволяет минимизировать ручной код и автоматически генерировать модули и mixin для автоподстановки зависимостей.
|
||||
|
||||
Пример декларативного модуля:
|
||||
|
||||
```dart
|
||||
@module()
|
||||
abstract class AppModule extends Module {
|
||||
@provide()
|
||||
@singleton()
|
||||
Api api() => Api();
|
||||
|
||||
@provide()
|
||||
Repo repo(Api api) => Repo(api);
|
||||
}
|
||||
````
|
||||
|
||||
После генерации кода можно автоматически подтягивать зависимости в виджеты или сервисы:
|
||||
|
||||
```dart
|
||||
@injectable()
|
||||
class MyScreen extends StatelessWidget with _$MyScreen {
|
||||
@inject()
|
||||
late final Repo repo;
|
||||
|
||||
MyScreen() {
|
||||
_inject(this);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Таким образом можно выбрать подход в разработке: **программный (императивный) с явной регистрацией зависимостей** или **декларативный через аннотации**.
|
||||
|
||||
|
||||
## Кому может быть полезен CherryPick?
|
||||
|
||||
* проектам, где важно гарантировать **отсутствие циклов в графе зависимостей**;
|
||||
* командам, которые хотят **минимизировать ручной DI-код** и использовать декларативный стиль с аннотациями;
|
||||
* приложениям, где требуется **автоматическое освобождение ресурсов** (сокеты, контроллеры, потоки).
|
||||
|
||||
## Полезные ссылки
|
||||
|
||||
* 📦 Пакет: [pub.dev/packages/cherrypick](https://pub.dev/packages/cherrypick)
|
||||
* 💻 Код: [github.com/pese-git/cherrypick](https://github.com/pese-git/cherrypick)
|
||||
* 📖 Документация: [cherrypick-di.netlify.app](https://cherrypick-di.netlify.app/)
|
||||
244
doc/presentation_ru.md
Normal file
244
doc/presentation_ru.md
Normal file
@@ -0,0 +1,244 @@
|
||||
---
|
||||
marp: true
|
||||
---
|
||||
|
||||
<!--
|
||||
#backgroundImage: url('./doc/assets/image.png')
|
||||
backgroundSize: cover
|
||||
-->
|
||||
|
||||
# CherryPick 3.x
|
||||
### Быстро. Безопасно. Просто.
|
||||
|
||||
Современный DI-framework для Dart и Flutter
|
||||
Автор: Сергей Пеньковский
|
||||
|
||||
---
|
||||
<!--
|
||||
backgroundImage: none
|
||||
-->
|
||||
|
||||
## Что такое CherryPick?
|
||||
|
||||
- Лёгкий и модульный framework для внедрения зависимостей (DI)
|
||||
- Фокус: производительность, безопасность и лаконичный код
|
||||
- Применяется во frontend, backend, CLI
|
||||
|
||||
---
|
||||
|
||||
## Эволюция: что нового в 3.x?
|
||||
|
||||
- Оптимизация скорости разрешения зависимостей
|
||||
- Интеграция с Talker для наглядного логирования DI-событий
|
||||
- Защита от циклических зависимостей на уровне ядра
|
||||
- Полностью декларативное описание DI через аннотации и генерацию кода
|
||||
- Автоматическая очистка ресурсов
|
||||
|
||||
---
|
||||
|
||||
## Быстро
|
||||
|
||||
* Мгновенное разрешение зависимостей
|
||||
|
||||
---
|
||||
|
||||
### Мгновенное разрешение зависимостей
|
||||
|
||||
- Операция resolve<T> теперь выполняется за O(1)
|
||||
- Используется Map-индексация всех биндингов в каждом скоупе (в среднем ускорение в 10x+ на крупных графах)
|
||||
- Производительность не зависит от размера приложения
|
||||
|
||||
---
|
||||
|
||||
## Безопасно
|
||||
|
||||
* Циклические зависимости больше не страшны
|
||||
* Интеграция с Talker и расширенное логирование
|
||||
|
||||
---
|
||||
|
||||
### Циклические зависимости больше не страшны
|
||||
|
||||
- CherryPick 3.x автоматически выявляет циклы при разрешении зависимостей.
|
||||
- Возможна проверка как внутри отдельного scope, так и во всём DI-графе (глобально).
|
||||
|
||||
---
|
||||
|
||||
#### Как включить проверку циклов
|
||||
|
||||
|
||||
- Для защиты только внутри одного scope:
|
||||
|
||||
```dart
|
||||
// 1. Для текущего scope (локальная проверка)
|
||||
final scope = CherryPick.openRootScope();
|
||||
scope.enableCycleDetection();
|
||||
```
|
||||
|
||||
- Для защиты всей иерархии скоупов:
|
||||
|
||||
```dart
|
||||
// 2. Для всей иерархии скоупов (глобальная проверка)
|
||||
CherryPick.enableGlobalCycleDetection();
|
||||
CherryPick.enableGlobalCrossScopeCycleDetection();
|
||||
final rootScope = CherryPick.openRootScope();
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
#### Пример обработки ошибки
|
||||
|
||||
При обнаружении цикла будет выброшено исключение с подробной трассировкой:
|
||||
|
||||
```dart
|
||||
try {
|
||||
scope.resolve<A>();
|
||||
} on CircularDependencyException catch(e) {
|
||||
print(e.dependencyChain);
|
||||
}
|
||||
```
|
||||
|
||||
```bash
|
||||
=== Circular Dependency Detection Example ===
|
||||
|
||||
1. Attempt to create a scope with circular dependencies:
|
||||
❌ Circular dependency detected: CircularDependencyException: Circular dependency detected for UserService
|
||||
Dependency chain: UserService -> OrderService -> UserService
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Интеграция с Talker и расширенное логирование
|
||||
|
||||
- Всё, что происходит в DI: регистрация, создание, удаление, ошибки ― теперь логируется!
|
||||
- Достаточно подключить observer:
|
||||
|
||||
```dart
|
||||
final talker = Talker();
|
||||
final talkerLogger = TalkerCherryPickObserver(talker);
|
||||
CherryPick.setGlobalObserver(talkerLogger);
|
||||
```
|
||||
- Логи сразу видны в консоли, UI
|
||||
|
||||
```bash
|
||||
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
||||
│ [info] | 9:41:33 89ms | [scope opened][CherryPick] scope_1757054493089_7072
|
||||
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
||||
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
||||
│ [verbose] | 9:41:33 90ms | [diagnostic][CherryPick] Scope created: scope_1757054493089_7072 {type: Scope, name: scope_1757054493089_7072, description: scope created}
|
||||
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Просто
|
||||
|
||||
* Декларативный DI
|
||||
* Автоматическая очистка ресурсов
|
||||
|
||||
---
|
||||
|
||||
### Декларативный DI: аннотации и генерация кода
|
||||
|
||||
- Описывайте зависимости с помощью аннотаций
|
||||
- Автоматически генерируется модуль DI и mixin для автоподстановки зависимостей
|
||||
|
||||
```dart
|
||||
@module()
|
||||
abstract class AppModule extends Module {
|
||||
@provide()
|
||||
@singleton()
|
||||
Api api() => Api();
|
||||
@provide()
|
||||
Repo repo(Api api) => Repo(api);
|
||||
}
|
||||
```
|
||||
|
||||
Регистрация модуля
|
||||
|
||||
```dart
|
||||
final scope = openRootScope()
|
||||
..installModules([$AppModule()]);
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### Field injection: минимум кода — максимум удобства
|
||||
|
||||
```dart
|
||||
@injectable()
|
||||
class MyScreen extends StatelessWidget with _$MyScreen {
|
||||
@inject()
|
||||
late final Repo repo;
|
||||
|
||||
MyScreen() {
|
||||
_inject(this);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- После генерации mixin и вызова `screen._inject()` — зависимости готовы
|
||||
- Сильная типизация, никаких ручных вызовов resolve
|
||||
|
||||
---
|
||||
|
||||
## Автоматическая очистка ресурсов
|
||||
|
||||
Автоматическая очистка ресурсов (контроллеры, потоки, сокеты, файлы и др.).
|
||||
|
||||
Если вы регистрируете объект, реализующий Disposable, через DI-контейнер, CherryPick вызовет его метод dispose() при закрытии скоупа.
|
||||
|
||||
```dart
|
||||
class MyServiceWithSocket implements Disposable {
|
||||
@override
|
||||
Future<void> dispose() async {
|
||||
await socket.close();
|
||||
print('Socket закрыт!');
|
||||
}
|
||||
}
|
||||
|
||||
scope.installModules([
|
||||
Module((bind) => bind<MyServiceWithSocket>().toProvide(() => MyServiceWithSocket()).singleton()),
|
||||
]);
|
||||
|
||||
await CherryPick.closeRootScope(); // дождётся завершения async очистки
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Почему это удобно?
|
||||
### Сравнение с ручным DI
|
||||
|
||||
|| Аннотации | Ручной DI |
|
||||
|:---|:-----------|:------------|
|
||||
|Гибко|✅|✅|
|
||||
|Кратко|✅|❌|
|
||||
|Безопасно|✅|❌ (легко ошибиться)|
|
||||
|
||||
---
|
||||
|
||||
## CherryPick 3.x: ваш DI-фреймворк
|
||||
|
||||
- Быстрое разрешение зависимостей
|
||||
- Гарантия безопасности и тестируемости
|
||||
- Интеграция с логированием
|
||||
- Максимально простой и декларативный код
|
||||
|
||||
---
|
||||
|
||||
<!--
|
||||
#backgroundImage: url('./doc/assets/image.png')
|
||||
backgroundSize: cover
|
||||
-->
|
||||
|
||||
## Спасибо за внимание
|
||||
|
||||
---
|
||||
|
||||
## Вопросы?
|
||||
|
||||
- Try CherryPick - [https://pub.dev/packages/cherrypick](https://pub.dev/packages/cherrypick)
|
||||
- Contributing — [https://github.com/pese-git/cherrypick](https://github.com/pese-git/cherrypick)
|
||||
- Документация и примеры — [https://cherrypick-di.netlify.app](https://cherrypick-di.netlify.app/)
|
||||
- Готов помочь — пишите, пробуйте, внедряйте!
|
||||
|
||||
@@ -9,11 +9,7 @@ void main() {
|
||||
// Создаем модуль, который будет предоставлять UseCase
|
||||
]);
|
||||
|
||||
runApp(
|
||||
const CherryPickProvider(
|
||||
child: MyApp(),
|
||||
),
|
||||
);
|
||||
runApp(const CherryPickProvider(child: MyApp()));
|
||||
}
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
@@ -21,10 +17,6 @@ class MyApp extends StatelessWidget {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return CherryPickProvider(
|
||||
child: MaterialApp(
|
||||
home: MyHomePage(),
|
||||
),
|
||||
);
|
||||
return CherryPickProvider(child: MaterialApp(home: MyHomePage()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,8 @@ class MyHomePage extends StatelessWidget {
|
||||
Widget build(BuildContext context) {
|
||||
//_inject(context); // Make sure this function is called in context
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Example App'),
|
||||
),
|
||||
body: Center(
|
||||
child: Text(useCase.fetchData()),
|
||||
),
|
||||
appBar: AppBar(title: const Text('Example App')),
|
||||
body: Center(child: Text(useCase.fetchData())),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,18 +5,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: _fe_analyzer_shared
|
||||
sha256: e55636ed79578b9abca5fecf9437947798f5ef7456308b5cb85720b793eac92f
|
||||
sha256: da0d9209ca76bde579f2da330aeb9df62b6319c834fa7baae052021b0462401f
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "82.0.0"
|
||||
version: "85.0.0"
|
||||
analyzer:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: analyzer
|
||||
sha256: "904ae5bb474d32c38fb9482e2d925d5454cda04ddd0e55d2e6826bc72f6ba8c0"
|
||||
sha256: "974859dc0ff5f37bc4313244b3218c791810d03ab3470a579580279ba971a48d"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "7.4.5"
|
||||
version: "7.7.1"
|
||||
args:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -29,10 +29,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: async
|
||||
sha256: d2872f9c19731c2e5f10444b14686eb7cc85c76274bd6c16e1816bff9a3bab63
|
||||
sha256: "758e6d74e971c3e5aceb4110bfd6698efc7f501675bcfe0c775459a8140750eb"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.12.0"
|
||||
version: "2.13.0"
|
||||
boolean_selector:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -45,50 +45,50 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: build
|
||||
sha256: cef23f1eda9b57566c81e2133d196f8e3df48f244b317368d65c5943d91148f0
|
||||
sha256: ce76b1d48875e3233fde17717c23d1f60a91cc631597e49a400c89b475395b1d
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.2"
|
||||
version: "3.1.0"
|
||||
build_config:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: build_config
|
||||
sha256: bf80fcfb46a29945b423bd9aad884590fb1dc69b330a4d4700cac476af1708d1
|
||||
sha256: "4f64382b97504dc2fcdf487d5aae33418e08b4703fc21249e4db6d804a4d0187"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.1.1"
|
||||
version: "1.2.0"
|
||||
build_daemon:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: build_daemon
|
||||
sha256: "79b2aef6ac2ed00046867ed354c88778c9c0f029df8a20fe10b5436826721ef9"
|
||||
sha256: "8e928697a82be082206edb0b9c99c5a4ad6bc31c9e9b8b2f291ae65cd4a25daa"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.0.2"
|
||||
version: "4.0.4"
|
||||
build_resolvers:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: build_resolvers
|
||||
sha256: b9e4fda21d846e192628e7a4f6deda6888c36b5b69ba02ff291a01fd529140f0
|
||||
sha256: d1d57f7807debd7349b4726a19fd32ec8bc177c71ad0febf91a20f84cd2d4b46
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.4"
|
||||
version: "3.0.3"
|
||||
build_runner:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: build_runner
|
||||
sha256: "058fe9dce1de7d69c4b84fada934df3e0153dd000758c4d65964d0166779aa99"
|
||||
sha256: b24597fceb695969d47025c958f3837f9f0122e237c6a22cb082a5ac66c3ca30
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.4.15"
|
||||
version: "2.7.1"
|
||||
build_runner_core:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: build_runner_core
|
||||
sha256: "22e3aa1c80e0ada3722fe5b63fd43d9c8990759d0a2cf489c8c5d7b2bdebc021"
|
||||
sha256: "066dda7f73d8eb48ba630a55acb50c4a84a2e6b453b1cb4567f581729e794f7b"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "8.0.0"
|
||||
version: "9.3.1"
|
||||
built_collection:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -101,10 +101,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: built_value
|
||||
sha256: ea90e81dc4a25a043d9bee692d20ed6d1c4a1662a28c03a96417446c093ed6b4
|
||||
sha256: a30f0a0e38671e89a492c44d005b5545b830a961575bbd8336d42869ff71066d
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "8.9.5"
|
||||
version: "8.12.0"
|
||||
characters:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -117,38 +117,38 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: checked_yaml
|
||||
sha256: feb6bed21949061731a7a75fc5d2aa727cf160b91af9a3e464c5e3a32e28b5ff
|
||||
sha256: "959525d3162f249993882720d52b7e0c833978df229be20702b33d48d91de70f"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.3"
|
||||
version: "2.0.4"
|
||||
cherrypick:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../cherrypick"
|
||||
relative: true
|
||||
source: path
|
||||
version: "3.0.0-dev.7"
|
||||
version: "3.0.1"
|
||||
cherrypick_annotations:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../cherrypick_annotations"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.1"
|
||||
version: "3.0.1"
|
||||
cherrypick_flutter:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
path: "../../cherrypick_flutter"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.3-dev.7"
|
||||
version: "3.0.1"
|
||||
cherrypick_generator:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
path: "../../cherrypick_generator"
|
||||
relative: true
|
||||
source: path
|
||||
version: "1.1.1"
|
||||
version: "3.0.1"
|
||||
clock:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -201,18 +201,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: dart_style
|
||||
sha256: "27eb0ae77836989a3bc541ce55595e8ceee0992807f14511552a898ddd0d88ac"
|
||||
sha256: "8a0e5fba27e8ee025d2ffb4ee820b4e6e2cf5e4246a6b1a477eb66866947e0bb"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "3.0.1"
|
||||
version: "3.1.1"
|
||||
fake_async:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: fake_async
|
||||
sha256: "6a95e56b2449df2273fd8c45a662d6947ce1ebb7aafe80e550a3f68297f3cacc"
|
||||
sha256: "5368f224a74523e8d2e7399ea1638b37aecfca824a3cc4dfdf77bf1fa905ac44"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.3.2"
|
||||
version: "1.3.3"
|
||||
file:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -238,10 +238,10 @@ packages:
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: flutter_lints
|
||||
sha256: "5398f14efa795ffb7a33e9b6a08798b26a180edac4ad7db3f231e40f82ce11e1"
|
||||
sha256: "3105dc8492f6183fb076ccf1f351ac3d60564bff92e20bfc4af9cc1651f4e7e1"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.0.0"
|
||||
version: "6.0.0"
|
||||
flutter_test:
|
||||
dependency: "direct dev"
|
||||
description: flutter
|
||||
@@ -271,14 +271,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.3.2"
|
||||
http:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: http
|
||||
sha256: "2c11f3f94c687ee9bad77c171151672986360b2b001d109814ee7140b2cf261b"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.0"
|
||||
http_multi_server:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -291,10 +283,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: http_parser
|
||||
sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b"
|
||||
sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "4.0.2"
|
||||
version: "4.1.2"
|
||||
io:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -303,14 +295,6 @@ packages:
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.0.5"
|
||||
js:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: js
|
||||
sha256: c1b2e9b5ea78c45e1a0788d29606ba27dc5f71f019f32ca5140f61ef071838cf
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "0.7.1"
|
||||
json_annotation:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -323,10 +307,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: leak_tracker
|
||||
sha256: c35baad643ba394b40aac41080300150a4f08fd0fd6a10378f8f7c6bc161acec
|
||||
sha256: "6bb818ecbdffe216e81182c2f0714a2e62b593f4a4f13098713ff1685dfb6ab0"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "10.0.8"
|
||||
version: "10.0.9"
|
||||
leak_tracker_flutter_testing:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -347,10 +331,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: lints
|
||||
sha256: "3315600f3fb3b135be672bf4a178c55f274bebe368325ae18462c89ac1e3b413"
|
||||
sha256: a5e2b223cb7c9c8efdc663ef484fdd95bb243bff242ef5b13e26883547fce9a0
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "5.0.0"
|
||||
version: "6.0.0"
|
||||
logging:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -427,26 +411,26 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: pubspec_parse
|
||||
sha256: "81876843eb50dc2e1e5b151792c9a985c5ed2536914115ed04e9c8528f6647b0"
|
||||
sha256: "0560ba233314abbed0a48a2956f7f022cce7c3e1e73df540277da7544cad4082"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.0"
|
||||
version: "1.5.0"
|
||||
shelf:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: shelf
|
||||
sha256: ad29c505aee705f41a4d8963641f91ac4cee3c8fad5947e033390a7bd8180fa4
|
||||
sha256: e7dd780a7ffb623c57850b33f43309312fc863fb6aa3d276a754bb299839ef12
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.4.1"
|
||||
version: "1.4.2"
|
||||
shelf_web_socket:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: shelf_web_socket
|
||||
sha256: cc36c297b52866d203dbf9332263c94becc2fe0ceaa9681d07b6ef9807023b67
|
||||
sha256: "3632775c8e90d6c9712f883e633716432a27758216dfb61bd86a8321c0580925"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.1"
|
||||
version: "3.0.0"
|
||||
sky_engine:
|
||||
dependency: transitive
|
||||
description: flutter
|
||||
@@ -456,10 +440,10 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: source_gen
|
||||
sha256: "35c8150ece9e8c8d263337a265153c3329667640850b9304861faea59fc98f6b"
|
||||
sha256: "7b19d6ba131c6eb98bfcbf8d56c1a7002eba438af2e7ae6f8398b2b0f4f381e3"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "2.0.0"
|
||||
version: "3.1.0"
|
||||
source_span:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -544,18 +528,18 @@ packages:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: vm_service
|
||||
sha256: "0968250880a6c5fe7edc067ed0a13d4bae1577fe2771dcf3010d52c4a9d3ca14"
|
||||
sha256: ddfa8d30d89985b96407efce8acbdd124701f96741f2d981ca860662f1c0dc02
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "14.3.1"
|
||||
version: "15.0.0"
|
||||
watcher:
|
||||
dependency: transitive
|
||||
description:
|
||||
name: watcher
|
||||
sha256: "69da27e49efa56a15f8afe8f4438c4ec02eff0a117df1b22ea4aad194fe1c104"
|
||||
sha256: "5bf046f41320ac97a469d506261797f35254fa61c641741ef32dacda98b7d39c"
|
||||
url: "https://pub.dev"
|
||||
source: hosted
|
||||
version: "1.1.1"
|
||||
version: "1.1.3"
|
||||
web:
|
||||
dependency: transitive
|
||||
description:
|
||||
@@ -589,5 +573,5 @@ packages:
|
||||
source: hosted
|
||||
version: "3.1.3"
|
||||
sdks:
|
||||
dart: ">=3.7.0-0 <4.0.0"
|
||||
flutter: ">=3.18.0-18.0.pre.54"
|
||||
dart: ">=3.8.0 <4.0.0"
|
||||
flutter: ">=3.32.0"
|
||||
|
||||
@@ -5,7 +5,7 @@ publish_to: 'none'
|
||||
version: 1.0.0+1
|
||||
|
||||
environment:
|
||||
sdk: ">=3.5.2 <4.0.0"
|
||||
sdk: ">=3.8.0 <4.0.0"
|
||||
|
||||
|
||||
dependencies:
|
||||
@@ -25,11 +25,11 @@ dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
|
||||
flutter_lints: ^5.0.0
|
||||
flutter_lints: ^6.0.0
|
||||
|
||||
cherrypick_generator:
|
||||
path: ../../cherrypick_generator
|
||||
build_runner: ^2.4.15
|
||||
build_runner: ^2.5.0
|
||||
|
||||
# For information on the generic Dart part of this file, see the
|
||||
# following page: https://dart.dev/tools/pub/pubspec
|
||||
|
||||
@@ -4,7 +4,6 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:talker_flutter/talker_flutter.dart';
|
||||
|
||||
|
||||
import 'domain/repository/post_repository.dart';
|
||||
import 'presentation/bloc/post_bloc.dart';
|
||||
import 'router/app_router.dart';
|
||||
@@ -14,9 +13,11 @@ part 'app.inject.cherrypick.g.dart';
|
||||
class TalkerProvider extends InheritedWidget {
|
||||
final Talker talker;
|
||||
const TalkerProvider({required this.talker, required super.child, super.key});
|
||||
static Talker of(BuildContext context) => context.dependOnInheritedWidgetOfExactType<TalkerProvider>()!.talker;
|
||||
static Talker of(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<TalkerProvider>()!.talker;
|
||||
@override
|
||||
bool updateShouldNotify(TalkerProvider oldWidget) => oldWidget.talker != talker;
|
||||
bool updateShouldNotify(TalkerProvider oldWidget) =>
|
||||
oldWidget.talker != talker;
|
||||
}
|
||||
|
||||
@injectable()
|
||||
|
||||
@@ -4,7 +4,7 @@ part 'post_model.freezed.dart';
|
||||
part 'post_model.g.dart';
|
||||
|
||||
@freezed
|
||||
class PostModel with _$PostModel {
|
||||
abstract class PostModel with _$PostModel {
|
||||
const factory PostModel({
|
||||
required int id,
|
||||
required String title,
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user