architecture
Architectural layer enforcement via good_intentions. Use when planning features, refactoring, adding classes, moving code between packages, reviewing dependency direction, or reasoning about where new code should live. Covers layer rules, progressive validation, @PartOf composition, and how to think about problem decomposition within Bestie's layered architecture.
- License 许可
- MPL-2.0
- 所属仓库
- jolexxa/bestie
- 最近更新
- 2026-10-15
- Stars
- ★ 13
- Forks
- 1
01 前言 Front Matter
- name
- architecture
- description
- Architectural layer enforcement via good_intentions. Use when planning features, refactoring, adding classes, moving code between packages, reviewing dependency direction, or reasoning about where new code should live. Covers layer rules, progressive validation, @PartOf composition, and how to think about problem decomposition within Bestie's layered architecture.
03 SKILL.md 正文
Architecture Guide
Bestie uses good_intentions to enforce layered architecture at build time. Every public concrete class in an annotated package must carry an intention annotation. Violations are build errors. This is not optional — the build hook runs on dart run, dart test, and dart build.
The Layer Stack
Dependencies flow downward only. Higher layers may depend on lower layers. Never the reverse. Siblings at the same layer cannot depend on each other.
@view UI components, widgets, pages
@viewModel Presentation logic (cubits, blocs, LogicBloc adapters)
@useCase Business orchestration across repositories
@repository Domain data gateways, bounded contexts, state machines
@dataSource External system adapters (APIs, databases, file I/O, FFI)
@model is cross-cutting — any layer can depend on a model. Models carry no layer enforcement.
What the Annotations Mean in Practice
| Annotation | This class... | Can depend on... |
|---|---|---|
@view |
Renders UI, composes other views, hosts providers | @viewModel, @useCase, @repository, @dataSource (subject to claiming), @model |
@viewModel |
Translates domain state into view state, handles user intent | @useCase, @repository, @dataSource (subject to claiming), @model |
@useCase |
Orchestrates multiple repositories for a business operation. One use case per feature, not per operation — group related commands as methods on a single facade class (e.g. SessionUseCase.reload() + .retry() + .contextSizeFor()). Matches the pragmatic style endorsed by the Flutter architecture guide. |
@repository, @dataSource (subject to claiming), @model |
@repository |
Owns a bounded context, transforms and composes data sources | @dataSource, @model |
@dataSource |
Talks to external systems (HTTP, FFI, filesystem, DB) | @model only |
@model |
Data class, value object, enum, utility | Other @model classes |
Packages That Use Intentions
A package is annotated and validated by good_intentions if its pubspec.yaml lists intentions as a dependency. Grep the workspace for that if you need the current list — don't rely on a hardcoded enumeration here, since packages get renamed, split, and added often enough that a static list goes stale fast.
Packages without intentions in their pubspec are not validated.
Progressive Validation (Claiming)
This is the most important rule to understand for day-to-day work.
Claiming means: when a class at layer N depends on a class at layer M (where N is higher), that lower-layer class becomes "claimed" by layer N. Once claimed, only layer N can access it directly — everyone else must go through layer N.
Example: If UserRepo (@repository) depends on UserApi (@dataSource), then UserApi is claimed by the repository layer. A @viewModel that tries to depend on UserApi directly will get a build error — it must go through UserRepo instead.
Why this matters for refactoring: When you introduce a wrapper (repository wrapping a data source, use case wrapping a repository), all existing consumers above that layer must migrate to use the wrapper. The build will tell you exactly which ones.
When there's no wrapper yet: If a @viewModel depends directly on a @dataSource and no @repository wraps that data source, good_intentions issues a warning (not an error). This is progressive — it won't break your build until someone actually wraps that data source.
@PartOf — Composition Without Layer Pollution
Use @PartOf(OwnerType) when a class is an implementation detail of another class. The @PartOf class inherits the owner's layer.
@repository
class ThingRepository {
const ThingRepository(this.downloadService, this.recommender);
final ThingDownloadService downloadService;
final ThingRecommender recommender;
}
@PartOf(ThingRepository)
class ThingDownloadService { ... }
@PartOf(ThingRepository)
class ThingRecommender { ... }
Rules:
- Only the owner and sibling
@PartOfclasses can depend on a@PartOfclass. @PartOfsiblings (same owner) can depend on each other freely.@PartOfchains are allowed:Inner->@PartOf(Helper)->@PartOf(Repository)resolves to the repository's layer.
Relaxed @PartOf with @model
When a class is logically owned by a parent but needs to be read by higher layers (e.g., state machine states), use both annotations:
@model
@PartOf(ThingRepository)
class ThingState { ... }
@model takes precedence for validation (any layer can depend on it), but the @PartOf relationship still appears in the architecture diagram. This is the standard pattern for state machine states that views need to read.
How to Think About New Code
"Where does this class go?"
- Does it render UI? ->
@view - Does it manage view state / presentation logic? ->
@viewModel - Does it orchestrate multiple repositories? ->
@useCase - Does it own a bounded context and transform data? ->
@repository - Does it talk to an external system? ->
@dataSource - Is it just data / a value object / utility? ->
@model - Is it an implementation detail of another class? ->
@PartOf(Owner)
"Where does this class live (which package)?"
Bestie's package structure maps to layers, not features — a top-level directory under packages/ corresponds to a layer tier (e.g. a data/ directory holds @dataSource packages, a domain/ directory holds @repository packages), and the app package holds views, view models, use cases, and feature-local repositories. Check the current top-level packages/ layout rather than trusting a hardcoded list, since package names and groupings shift as the codebase evolves.
"I need a new feature — how do I structure it?"
In the app package, features follow this structure:
features/my_feature/
my_feature.dart # barrel export
domain/
my_feature_use_case.dart # @useCase
my_feature_repository.dart # @repository
state/
my_feature_cubit.dart # @viewModel
my_feature_state.dart # state types
view/
my_feature_page.dart # @view
components/
my_widget.dart # @view
Not every feature needs every layer. A simple feature might just be a @viewModel + @view. Only add layers when they earn their keep.
"I'm refactoring and need to move code between layers"
- Check what depends on the class — if you move a class to a different layer, every consumer must still satisfy the dependency rules.
- Moving down is easy — a class that was
@viewModelbecoming@repositorywon't break its consumers (they were already above it). - Moving up is breaking — a class that was
@dataSourcebecoming@repositorymeans other@repositoryclasses can't depend on it anymore (sibling rule). - Introducing a wrapper triggers claiming — if you wrap a
@dataSourcein a new@repository, all classes above the repository layer that previously accessed the data source directly must now go through the repository. That's fine, just needs a little refactoring when it happens. @PartOfis your friend — if a refactor splits a large class into helpers, use@PartOfto keep them as implementation details without creating new layer boundaries. Try not to publicly expose classes that are just implementation details.
"This dependency feels wrong but I need it"
If you're tempted to break the rules, ask:
- Should the target be a
@modelinstead? Cross-cutting data doesn't need layer enforcement. - Should there be an intermediate layer? Maybe you need a
@useCaseor@repositoryto mediate. - Is this an implementation detail? Maybe
@PartOfis the right call.
Testing Implications
Architecture enforcement means:
- Mock at layer boundaries. A
@viewModeltest mocks the@useCaseor@repositoryit depends on — never reaches down to@dataSourcedirectly. @PartOfclasses are tested through their owner (or in isolation with the owner mocked/faked).@modelclasses are pure data — test them directly, no mocking needed.
Build Hook
Validation runs from one hook, in the app package — packages/bestie/hook/build.dart:
import 'package:good_intentions/good_intentions.dart';
Future<void> main(List<String> args) async =>
ArchitectureValidator.validate(args);
This runs automatically. Architecture violations are build errors — they block dart run, dart test, and dart build. The hook also generates lib/architecture.g.puml showing the dependency graph with violations in red.
04 同仓库更多 Skills
simplify
identify and propose complexity simplifications
logic-blocks
Reference for Dart LogicBlocks — a statechart/state machine library ported from Chickensoft LogicBlocks for C#. Use when writing, debugging, or reviewing any LogicBlock state machine code in this Dart project. Covers state lifecycle, transitions, inputs, outputs, bindings, blackboard, async work, file organization, the LogicBloc adapter for nocterm, and pitfalls.
hunt-leaks
Hunt for leaking abstractions in a feature or layer of the codebase. Diagnoses architectural smells — closure injection, skipped layers, annotation/diagram mismatches, duplicate domain types, singleton fields for plural concerns, state enum sprawl, mutable-shared-state reads, etc — and proposes targeted refactors. Use when the user asks to "review", "audit", or "look for leaks" in a feature, or wants to validate that a recent feature isn't bolted on. See also `architecture` and `logic-blocks` skills.
architecture
Architectural layer enforcement via good_intentions. Use when planning features, refactoring, adding classes, moving code between packages, reviewing dependency direction, or reasoning about where new code should live. Covers layer rules, progressive validation, @PartOf composition, and how to think about problem decomposition within Bestie's layered architecture.
simplify
identify and propose complexity simplifications
logic-blocks
Reference for Dart LogicBlocks — a statechart/state machine library ported from Chickensoft LogicBlocks for C#. Use when writing, debugging, or reviewing any LogicBlock state machine code in this Dart project. Covers state lifecycle, transitions, inputs, outputs, bindings, blackboard, async work, file organization, the LogicBloc adapter for nocterm, and pitfalls.