Architecture Overview
Architecture Overview
Section titled âArchitecture OverviewâMOFA architecture is organized into four primary layers, each with clear responsibilities and defined sources of truth. This reference provides complete details for each layer.
Layer Hierarchy
Section titled âLayer HierarchyâData flows: Datasource â Domain â Services â UI
Each layer owns its specific concerns and source of truth, with strict boundaries preventing layer-skipping.
1. Datasource Layer
Section titled â1. Datasource LayerâResponsibilities
Section titled âResponsibilitiesâ- Handles all remote data operations (GraphQL, REST APIs)
- Implements repository patterns to abstract data access
- Converts between DTOs and domain models
- Uses GraphQL with Ferry client and Strategy pattern
- No local storage - caching handled by dedicated service layer
Key Components
Section titled âKey ComponentsâRemote Data Sources
Section titled âRemote Data Sourcesâ- GraphQL Clients: Ferry-based GraphQL operations
- REST Clients: Dio-based HTTP operations
- WebSocket Clients: Real-time data connections
Repository Implementations
Section titled âRepository Implementationsâ- Concrete classes implementing domain repository interfaces
- Handle data fetching, caching coordination, and error handling
- Map DTOs to domain models
Strategy Pattern Components
Section titled âStrategy Pattern Componentsâ- Request Strategies: Build GraphQL requests avoiding circular dependencies
- Cache Handler Strategies: Manage optimistic updates and cache invalidation
- Datasource Modules: Register and configure strategies
2. Domain Layer
Section titled â2. Domain LayerâDomain models, CRUD inputs, filters, sorts, domain exceptions
Responsibilities
Section titled âResponsibilitiesâ- Defines core business entities as plain models (no business logic)
- Contains domain models, value objects, CRUD inputs, filters, sorts
- Provides interfaces for repositories
- Domain-specific exceptions and validation
Key Components
Section titled âKey ComponentsâDomain Models
Section titled âDomain Modelsâ@freezedclass UserModel with _$UserModel { const factory UserModel({ required String id, required String name, required String email, String? avatar, required DateTime createdAt, }) = _UserModel;
factory UserModel.fromJson(Map<String, dynamic> json) => _$UserModelFromJson(json);}CRUD Input Types
Section titled âCRUD Input Typesâ@freezedclass CreateUserInput with _$CreateUserInput { const factory CreateUserInput({ required String name, required String email, String? avatar, }) = _CreateUserInput;
factory CreateUserInput.fromJson(Map<String, dynamic> json) => _$CreateUserInputFromJson(json);}Filters and Sorts
Section titled âFilters and Sortsâ@freezedclass UserFilter with _$UserFilter { const factory UserFilter({ String? nameContains, String? emailContains, DateTime? createdAfter, DateTime? createdBefore, }) = _UserFilter;}
enum UserSort { name, email, createdAt }Domain Exceptions
Section titled âDomain Exceptionsâabstract class DomainException implements Exception { const DomainException(this.message); final String message;}
class UserNotFoundException extends DomainException { const UserNotFoundException(String userId) : super('User with ID $userId not found');}
class InvalidEmailException extends DomainException { const InvalidEmailException(String email) : super('Invalid email format: $email');}3. Service Layer (Application Logic)
Section titled â3. Service Layer (Application Logic)âApplication-wide services, business logic, advanced use cases, cross-cutting concerns
Responsibilities
Section titled âResponsibilitiesâ- Owns ALL business logic, advanced use cases, orchestration
- Provides reusable services for cross-cutting concerns
- Coordinates multiple repositories and domain models
- Error handling, logging, analytics, permissions, media
- Integrates with caching service for all storage scenarios
Service Naming Convention
Section titled âService Naming Conventionâ- Business logic:
[Feature]Service(e.g.,UserService) - Cross-cutting concerns:
[Concern]OrchestratorService(e.g.,DataMergeOrchestratorService)
4. UI Layer
Section titled â4. UI LayerâResponsibilities
Section titled âResponsibilitiesâ- Manages presentation logic and user interface components
- Uses notifiers (Riverpod) to manage UI state and react to application logic changes
- Interacts with the application logic and domain layers to display and update data
- Contains UI tests to ensure correct behavior
Key Components
Section titled âKey ComponentsâEvery feature must handle these states:
- Loading: Show appropriate loading indicators
- Error: Display user-friendly error messages with retry option
- Empty: Show empty state with helpful guidance
- Success: Display the actual content
Supporting Components
Section titled âSupporting ComponentsâCore Folder
Section titled âCore FolderâHouses core features that other features depend on:
- Auth Feature: Authentication logic shared across features
- Router: Centralized navigation logic and route definitions
- Service Locators: Riverpod providers for dependency injection
- Configuration: Optional flavor configuration and app-wide settings
Caching Service
Section titled âCaching ServiceâHandles all caching scenarios:
- Secure Cache: Sensitive data storage (tokens, credentials)
- Simple Cache: Basic data caching (preferences, settings)
- Complex Cache: Advanced scenarios (offline data, sync strategies)
Generic Packages
Section titled âGeneric PackagesâReusable, business-logic-independent functionality:
- I18n Package: Internationalization and localization utilities
- Assets Package: Asset management and resource handling
Data Flow Rules
Section titled âData Flow RulesâStrict Layer Hierarchy
Section titled âStrict Layer Hierarchyâ- UI â Services (business logic calls)
- Services â Domain (models & interfaces)
- Services â Datasource (via domain interfaces)
- Domain â Datasource (defines repository contracts)
Cross-Layer Integration
Section titled âCross-Layer Integrationâ- Caching Service: Integrates with Services layer for all storage needs
- Core Folder: Provides shared features, service locators, and configuration to all layers
- Generic Packages: Used by UI and Services layers as needed
Prohibited Patterns
Section titled âProhibited Patternsâ- â UI directly calling Datasource
- â Datasource importing Services
- â Domain containing business logic
- â Local storage in Datasource layer
- â Feature-to-feature imports (use Core folder instead)
This architecture ensures maintainability, testability, and scalability while providing clear boundaries and responsibilities for each layer.