Dart
Summary - Architecture
Combine clean, repository and DI to write an architecture skeleton.
Summary - Architecture
This group covered Dart architecture: clean architecture (layer separation), feature-based (feature-oriented organization), repository (data access abstraction), dependency injection, SOLID (design principles).
Core: dependencies flow inward; the domain layer depends on nothing. Repository hides the data source; DI provides interface-based dependencies. SOLID is the foundation of maintainable, testable code.
These patterns work together: clean architecture sets the frame, feature-based the organization, repository the data layer, DI the dependency management, SOLID the class design.
Exercise Summary: entity + repository + container
Define an Order class with final String id; final int total; Order(this.id, this.total);. Define an abstract class OrderRepository with Order find(String id);. Define a class MockOrderRepo implements OrderRepository with @override Order find(String id) => Order(id, 250);. Define a class Container with final Map<String, dynamic> _items = {}; void register(String key, dynamic item) => _items[key] = item; T resolve<T>(String key) => _items[key] as T;. In main create final c = Container();; call c.register('repo', MockOrderRepo());; add final repo = c.resolve<OrderRepository>('repo');; final order = repo.find('ord-1'); and print('${order.id}: ${order.total}');.
Show example solution
class Order {
final String id;
final int total;
Order(this.id, this.total);
}
abstract class OrderRepository {
Order find(String id);
}
class MockOrderRepo implements OrderRepository {
@override
Order find(String id) => Order(id, 250);
}
class Container {
final Map<String, dynamic> _items = {};
void register(String key, dynamic item) => _items[key] = item;
T resolve<T>(String key) => _items[key] as T;
}
void main() {
final c = Container();
c.register('repo', MockOrderRepo());
final repo = c.resolve<OrderRepository>('repo');
final order = repo.find('ord-1');
print('${order.id}: ${order.total}');
}
Test yourself
-
What do Repository + DI provide together?
- Testable, swappable design
- Fewer lines of code
- Faster UI
- Automatic code generation
Show answer
Testable, swappable design — Repository abstracts, DI injects; together they provide testability.
-
Match the architecture concepts
- Entity
- Domain model
- Repository
- Contract
- Container
- DI container
- Feature
- Module
-
The Repository interface
____ class OrderRepository { }
Show answer
abstract class OrderRepository { }
Next lesson: Integration tests