Skip to content

Latest commit

 

History

History
166 lines (136 loc) · 7.77 KB

File metadata and controls

166 lines (136 loc) · 7.77 KB

Clean Architecture Implementation

This chat application follows Clean Architecture principles with proper separation of concerns across three main layers.

🏗️ Architecture Overview

┌─────────────────────────────────────────────────────────────┐
│                    PRESENTATION LAYER                       │
│  ┌─────────────────┐  ┌─────────────────┐  ┌──────────────┐ │
│  │   ChatScreen    │  │  ChatViewModel  │  │  ChatTheme   │ │
│  │   (Compose UI)  │  │    (State)      │  │  (Styling)   │ │
│  └─────────────────┘  └─────────────────┘  └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
                               │
                               ▼
┌─────────────────────────────────────────────────────────────┐
│                     DOMAIN LAYER                           │
│  ┌─────────────────┐  ┌─────────────────┐  ┌──────────────┐ │
│  │   Use Cases     │  │     Models      │  │ Repositories │ │
│  │  (Business      │  │   (Entities)    │  │ (Interfaces) │ │
│  │   Logic)        │  │                 │  │              │ │
│  └─────────────────┘  └─────────────────┘  └──────────────┘ │
└─────────────────────────────────────────────────────────────┘
                               │
                               ▼
┌─────────────────────────────────────────────────────────────┐
│                      DATA LAYER                            │
│  ┌─────────────────┐  ┌─────────────────┐  ┌──────────────┐ │
│  │ Repository Impl │  │  Data Sources   │  │   Entities   │ │
│  │ (Concrete Impl) │  │  (In-Memory)    │  │  (DTOs)      │ │
│  └─────────────────┘  └─────────────────┘  └──────────────┘ │
└─────────────────────────────────────────────────────────────┘

📁 Project Structure

src/commonMain/kotlin/org/cmppractice/app/
├── domain/                          # 🎯 DOMAIN LAYER
│   ├── model/
│   │   └── Message.kt               # Domain entities
│   ├── repository/
│   │   └── MessageRepository.kt     # Repository interface
│   └── usecase/
│       ├── GetMessagesUseCase.kt    # Business logic
│       ├── SendMessageUseCase.kt
│       ├── ClearMessagesUseCase.kt
│       └── GenerateAutoResponseUseCase.kt
├── data/                            # 💾 DATA LAYER
│   └── ChatRepository.kt            # Repository implementation
├── ui/                              # 🎨 PRESENTATION LAYER
│   ├── ChatScreen.kt                # UI components
│   ├── ChatViewModel.kt             # State management
│   ├── ChatTheme.kt                 # Styling
│   └── TypingIndicator.kt
├── di/                              # 🔧 DEPENDENCY INJECTION
│   └── AppModule.kt                 # Dependency container
└── App.kt                           # Application entry point

🎯 Domain Layer (Business Logic)

Models

  • Message: Core business entity representing a chat message
  • User: Represents a chat participant

Use Cases (Business Logic)

  • GetMessagesUseCase: Retrieves all messages
  • SendMessageUseCase: Handles message sending with validation
  • ClearMessagesUseCase: Clears all chat history
  • GenerateAutoResponseUseCase: Generates smart auto-responses

Repository Interface

  • MessageRepository: Defines contract for data access

💾 Data Layer (Data Access)

Repository Implementation

  • ChatRepositoryImpl: Concrete implementation using in-memory storage
  • MessageEntity: Data transfer object for persistence layer

Key Features:

  • Flow-based reactive data streams
  • Entity-to-domain model mapping
  • UUID-based message identification

🎨 Presentation Layer (UI)

ViewModel

  • ChatViewModel: Manages UI state using domain use cases
  • Reactive state management with StateFlow
  • Clean separation from business logic

UI Components

  • ChatScreen: Main chat interface
  • MessageBubble: Individual message display
  • TypingIndicator: Animated typing indicator

🔧 Dependency Injection

AppModule

Provides all dependencies following the dependency rule:

class AppModule {
    // Data Layer
    private val messageRepository: MessageRepository = ChatRepositoryImpl()
    
    // Domain Layer (Use Cases)
    private val getMessagesUseCase = GetMessagesUseCase(messageRepository)
    private val sendMessageUseCase = SendMessageUseCase(messageRepository)
    private val clearMessagesUseCase = ClearMessagesUseCase(messageRepository)
    private val generateAutoResponseUseCase = GenerateAutoResponseUseCase()
    
    // Presentation Layer
    val chatViewModel: ChatViewModel = ChatViewModel(...)
}

✅ Clean Architecture Benefits

1. Separation of Concerns

  • Each layer has a single responsibility
  • Business logic is isolated in the domain layer
  • UI logic is separate from business logic

2. Dependency Rule

  • Dependencies point inward (toward domain)
  • Domain layer has no external dependencies
  • Data and UI depend on domain abstractions

3. Testability

  • Use cases can be tested independently
  • Repository can be mocked for testing
  • ViewModel tests don't require UI framework

4. Maintainability

  • Changes in one layer don't affect others
  • Easy to add new features or modify existing ones
  • Clear structure makes code easier to understand

5. Scalability

  • Easy to add new use cases
  • Can switch data sources without affecting business logic
  • UI can be completely replaced without touching domain

🔄 Data Flow

  1. User Interaction → ChatScreen
  2. UI Event → ChatViewModel
  3. Business Logic → Use Cases
  4. Data Access → Repository Interface
  5. Data Storage → Repository Implementation
  6. Data Return → Flow back through layers
  7. UI Update → StateFlow triggers recomposition

🚀 Future Enhancements

This architecture makes it easy to add:

  • Database persistence (just implement MessageRepository)
  • Network communication (add network data source)
  • User authentication (add auth use cases)
  • Message encryption (add security use cases)
  • File sharing (extend message domain model)

The clean separation ensures that adding these features won't require major refactoring of existing code.