Clean Architecture Lecture Notes

Jul 18, 2024

Clean Architecture in Software Development

Introduction to Clean Architecture

  • Clean Architecture: A blueprint for a modular system that adheres to the principle of separation of concerns.
  • Layered Structure: Divides software into layers to simplify development and maintenance.
  • Benefits: Reusability, independent development & updates, scalability, readability, testability, and maintainability.

Main Layers of Clean Architecture

  1. Presentation Layer

    • Content and Event Handling: Displays app content and triggers events.
    • Components: Pages, state management (bloc, riverport), widgets.
  2. Domain Layer

    • Business Logic: Central part with no external dependencies.
    • Components: Entities, repository interfaces, use cases.
    • Pure Dart Code: Contains only business logic, not implementation details.
    • Use Cases: Application-specific business rules and interaction handling.
  3. Data Layer

    • Data Retrieval: Handles API calls and local database interactions.
    • Components: Repositories, data sources (remote & local), models.

Additional Supporting Layers

  • Resources: General resources that support the main layers.
  • Shared Library: Common library to be used across multiple features.

Folder Structure

  • Features Folder: Contains subfolders for each feature subdivided into presentation, domain, and data layers.
  • Config and Core: Contain project configurations and shared resources.

Implementing Clean Architecture

  • Step-by-Step Project Creation: Example with a news app that retrieves data from a REST API and caches it locally.
  • State Management: Using flutter_bloc and equatable packages for managing state and equality comparisons.
  • Service Locator: Using get_it for dependency injection.
  • Local Database: Using floor to create a local database.
  • Network Requests: Using retrofit with Dio HTTP client.

Detailed Layer Implementation

Presentation Layer

  • Set up pages, state management, widgets.
  • Handle app bar and body content.

Domain Layer

  • Define entities and create classes based on business logic.
  • Set up repository interfaces and use cases.

Data Layer

  • Implement repository methods for server and database interactions.
  • Define data sources and model data from JSON.

Example Project - News App

  • Step 1: Add necessary packages (flutter_bloc, equatable, get_it, floor, retrofit, etc).
  • Step 2: Create folder structure and set up initial configurations (theme, constants).
  • Step 3: Implement domain layer (entities, repositories, use cases).
  • Step 4: Implement data layer (API service, data source implementation, model creation).
  • Step 5: Set up and use a local database (floor setup, data access objects).
  • Step 6: Presentation layer setup (UI components, state management).
  • Step 7: Dependency injection using get_it service locator.
  • Step 8: Final integration and testing with UI components.

Key Takeaways

  • Clean Architecture improves modularity, maintainability, and testability of software projects.
  • Layer separation ensures each layer focuses on a specific aspect of the application.
  • Dependency injection simplifies object management and enhances testability.
  • Real-world example (news app) provides practical insights into implementing clean architecture.