How alanlapierre/habit-tracker works
Main Components
• Domain Layer:
• Contains business logic, domain models (e.g., Habit, HabitStatistics), exceptions, and utility classes.
• It includes services (HabitFinder, HabitStatisticsCalculator), use cases (AddHabitUseCase, CompleteHabitUseCase, etc.), mappers, DTOs (in dto package), and exception handling (HabitErrorHandler).
• Infrastructure Layer:
• Split into two major sub-layers:
▪ Persistence Sub-layer: Includes MongoDB connection (MongoDBConnection) and repository implementation (HabitRepositoryImpl) that implements the domain-level repository interface.
▪ UI Sub-layer: Manages the user interaction. Contains the HabitTrackerController, menu configuration (Menu, MenuOption, MenuConfiguration), action implementations (e.g., AddHabitAction, CompleteHabitAction, etc.), presenters (ConsoleMenuPresenter), selectors (HabitSelector), and UI exception handling (UIErrorHandler). There are also utility classes such as ConsoleManager for handling console IO.
• Main Application:
• The Main.kt file that is the entry point to the application. It is responsible for initializing and connecting the Domain and Infrastructure layers.
Relationships and Data Flow
• The Main entry point initializes:
• The Domain layer, setting up all the services, use cases, and value objects.
• The Infrastructure layer’s UI components (controller, actions, presenters) that interact with the user via the command line.
• The UI controller serves as a mediator:
• It receives user input, triggers the corresponding use cases in the Domain layer and processes the results.
• Persistence:
• The repository abstraction defined in the Domain layer is implemented by HabitRepositoryImpl. The Domain communicates with persistence via this repository interface.
Architectural Patterns and Design Principles
• Layered / Clean Architecture:
• Clear separation among Domain (business logic), Infrastructure (implementation details – UI and persistence), and the application bootstrapping (Main).
• SOLID Principles in Action:
• Single Responsibility: Each class has a well-defined role (e.g., ConsoleManager for console IO, HabitRepositoryImpl solely for data persistence).
• Open/Closed and Dependency Injection: New actions can be added to the UI without altering the Menu logic.
• Interaction flows use dependency injection and interfaces to ensure consistent, testable, and isolated layers.
Relevant Technologies
• Kotlin is the primary programming language.
• Gradle is used as the build tool.
• MongoDB (as suggested by the MongoDBConnection) for persistence.
• The use of interfaces for actions, presenters, and repository access ensures loose coupling.
Elements
• Annotations or legends to denote SOLID principles where they are applied (e.g., label “SRP” near ConsoleManager, “OCP” near menu actions).
a. A block labeled “Main (Main.kt)” at the top or center.
• Left: Domain Layer with sub-blocks for Entities (Habit, HabitStatistics), Use Cases (Add, Delete, Complete, etc.), Services, Mappers, DTOs, Exceptions, and Utilities.
• Right: Infrastructure Layer, divided into:
• UI Part: with blocks for HabitTrackerController, Menu (Menu, MenuConfiguration, MenuOption, ConsoleMenuPresenter, HabitSelector, etc.), and Action implementations.
• Persistence Part: showing HabitRepositoryImpl and MongoDBConnection.
• E.g., Menu → Action → Use Case → Repository.
d. Use annotations to indicate the responsibilities and note technology choices.