Sub topic 7
Dependency Rules Between Features and Shared Code
Assume you can build Flutter widgets, use Dart imports and exports, and understand the repository pattern. This chapter focuses on keeping feature dependencies controlled as an app grows.
Why dependency rules matter
A feature is a business area such as cart, catalog, or checkout. Each feature should be able to change without forcing unrelated features to change.
A dependency exists when one part of the app imports or calls another part. Uncontrolled dependencies create a chain of changes:
- A checkout screen imports a private cart controller.
- The cart controller changes its state type.
- Checkout breaks even though its own behavior did not change.
- Developers then add more special cases to restore the connection.
A useful rule is:
> Features may depend on shared contracts, but they should not depend on another feature's private implementation.
The app shell is allowed to connect the pieces.
Dependency direction inside a feature
Keep dependencies moving inward toward stable business rules:
- Presentation code displays state and handles user actions.
- Domain code defines business concepts and interfaces.
- Data code talks to APIs, databases, or device storage.
- Shared core code contains stable utilities and contracts used by several features.
For example, a cart screen can call a CartRepository interface from the domain layer. The concrete API repository belongs in the data layer. The screen does not need to know whether data comes from HTTP or memory.
Avoid this direction:
presentation -> data implementation -> presentationThat cycle makes testing and replacement difficult. Prefer:
presentation -> domain interface <- data implementationThe arrow points from the importer to the imported code. The data implementation still satisfies the domain interface, but presentation code never imports it directly.
Rules for feature and shared-code imports
Use rules that are easy to explain during code review.
| Import direction | Decision | Example |
|---|---|---|
| Feature presentation to its own domain | Allow | `cart/presentation` imports `cart/domain` |
| Feature data to its own domain | Allow | `cart/data` implements `CartRepository` |
| Feature to shared contract | Allow | `checkout` imports `CartSummary` |
| Feature to another feature's private file | Avoid | `checkout` imports `cart/presentation/cart_controller.dart` |
| Shared code to a feature | Reject | `core` imports `cart` |
Shared code must remain generic. If core/widgets needs a cart-specific condition, the condition probably belongs in the cart feature.
Communicating across features
When one feature needs information from another, define a small contract in shared code. A contract is an interface that describes what callers need without exposing how the value is produced.
abstract interface class CartSummary {
int get itemCount;
}
class CheckoutViewModel {
CheckoutViewModel(this.cart);
final CartSummary cart;
String itemLabel() => 'Items: ${cart.itemCount}';
}
class CartService implements CartSummary {
CartService(this._items);
final List<String> _items;
@override
int get itemCount => _items.length;
}
void main() {
final cart = CartService(['book', 'lamp']);
final checkout = CheckoutViewModel(cart);
print(checkout.itemLabel());
}CheckoutViewModel knows only about CartSummary. It does not import CartService or any cart screen. The app shell can provide CartService at runtime. A test can provide a small fake implementation instead.
Enforcing the boundary
Document the rules and check them automatically when possible. Dart has no universal built-in feature-boundary checker, so teams commonly combine:
- Code review rules.
- Import-lint configuration.
- Tests that scan imports.
- Separate Dart packages for features that need stronger isolation.
Start with naming and directory conventions. Later, move a stable feature into its own package. Package boundaries make private files genuinely private because another package cannot import them unless they are exported.
Exercise
Exercise
The checkout feature needs the number of items in the cart. Should it import the cart controller directly, or should it use a shared interface? Why?
Show answer
It should use a small shared interface such as CartSummary. The app shell supplies the cart implementation, so checkout depends on a stable contract rather than the cart feature's private state.