Sub topic 13
Checking Boundaries Automatically
Assume you know Dart imports, Flutter project structure, and the repository pattern. This chapter focuses on enforcing feature boundaries with tools, rather than relying only on team agreement.
Why automatic checks matter
A feature-first structure works only when its dependency rules remain clear. Without checks, a developer may import a private widget, repository, or state object from another feature because that import is convenient. The code may compile, but the features slowly become coupled.
The Dart analyzer checks syntax, types, and many language rules. It does not automatically know that checkout must not import cart/data/. Architectural boundaries need additional checks.
A useful rule set is:
- A feature may import its own
data,domain, andpresentationcode. - A feature may import approved files from
core. - A feature may import another feature only through that feature's public entry file.
- The app shell may compose features and provide implementations.
- Tests may use the code they are testing, but should follow the same public API rules.
A small import-boundary check
You can start with a simple script before adopting a specialized architecture lint package. The following Dart program scans relative imports inside lib/features. It rejects an import from one feature into another feature's internal file. Each feature's public file, such as cart.dart, remains allowed.
Save it as tool/check_boundaries.dart.
import 'dart:io';
final importPattern = RegExp(r'''import\s+['"]([^'"]+)['"]''');
final featurePattern = RegExp(r'''features/([^/]+)/(.+)''');
void main() {
final violations = <String>[];
final featureRoot = Directory('lib/features');
if (!featureRoot.existsSync()) {
stderr.writeln('Missing lib/features directory.');
exitCode = 2;
return;
}
for (final entity in featureRoot.listSync(recursive: true)) {
if (entity is! File || !entity.path.endsWith('.dart')) continue;
final sourceFeature = _featureName(entity.path);
if (sourceFeature == null) continue;
final source = entity.readAsStringSync();
for (final match in importPattern.allMatches(source)) {
final importedPath = match.group(1)!;
final imported = featurePattern.firstMatch(importedPath);
if (imported == null) continue;
final targetFeature = imported.group(1)!;
final targetPath = imported.group(2)!;
if (targetFeature != sourceFeature &&
targetPath != '$targetFeature.dart') {
violations.add(
'${entity.path}: import only $targetFeature.dart '
'from feature $targetFeature, not $targetPath',
);
}
}
}
if (violations.isNotEmpty) {
stderr
..writeln('Feature boundary violations:')
..writeAll(violations, '\n');
exitCode = 1;
} else {
stdout.writeln('Feature boundaries passed.');
}
}
String? _featureName(String path) {
final match = RegExp(r'lib/features/([^/]+)/').firstMatch(path);
return match?.group(1);
}Run it from the project root:
dart run tool/check_boundaries.dartThe script checks relative and package-style paths containing features/<name>/. A real project should also decide whether generated files, tests, and special integration modules need different rules.
Add the check to continuous integration
Run boundary checks alongside formatting, analysis, and tests. A failure should stop the build so an invalid dependency does not reach the main branch.
- name: Check feature boundaries
run: dart run tool/check_boundaries.dart
- name: Analyze Dart code
run: dart analyze
- name: Run tests
run: flutter testWhen simple checks are not enough
Use a dedicated lint plugin when rules depend on more than path names. Examples include allowing imports only from specific layers, detecting dependency cycles, or checking that presentation code does not construct data sources directly.
Keep the rules small and documented. A boundary that nobody understands will eventually be disabled. Review exceptions explicitly, and add a test or check for every important architectural decision.
Exercise
A profile page imports features/cart/data/in_memory_cart_repository.dart because it needs the cart item count. What change follows the boundary rule?
Show answer
Expose the needed behavior through cart.dart, or define a small shared contract in core/contracts and provide its implementation from the app shell. Do not import the cart data file directly.