In Clean Architecture, particularly as advocated by Uncle Bob, the Domain layer is central to encapsulating the business logic of your application. This layer typically contains:
- Entities: Core business objects.
- Use Cases (Interactors): Specific business operations or actions.
- Repositories (Interfaces): Abstractions for data access.
When organizing your project structure in Kotlin for Android development, it's essential to maintain clarity and separation of concerns. For your specific case—text-to-speech functionality—here's how you can structure it within the Domain layer:
Recommended Package Structure
domain/
│
├── entities/
│ └── ... (your business entities)
│
├── usecases/
│ ├── TextToSpeechUseCase.kt
│ └── ... (other use cases)
│
└── repositories/
└── ... (repository interfaces)
Explanation
-
Use Cases Subpackage (usecases):
- Purpose: Encapsulates all the business actions or operations your application can perform.
- Naming: Use clear and descriptive names for each use case. For example,
TextToSpeechUseCase.kt clearly indicates its responsibility.
- Contents: Each use case class handles a specific action, such as converting text to speech, fetching data, processing user input, etc.
-
Alternative Naming Conventions:
-
Why Not Other Names?:
- Services: While domain services are a concept in Domain-Driven Design (DDD), in Clean Architecture, use cases more accurately represent the operations the application can perform.
- Features or Modules: While feature-based packaging is common, within the domain layer, it's more aligned with Clean Architecture to use layer-specific package names like
usecases, entities, and repositories.
Example
// domain/usecases/TextToSpeechUseCase.kt
package com.yourapp.domain.usecases
class TextToSpeechUseCase(
private val textToSpeechRepository: TextToSpeechRepository
) {
operator fun invoke(text: String) {
textToSpeechRepository.convertTextToSpeech(text)
}
}
In this example:
TextToSpeechUseCase encapsulates the text-to-speech operation.
- It interacts with a repository interface (
TextToSpeechRepository) defined in the repositories subpackage, adhering to dependency inversion.
Conclusion
By organizing your Domain layer with a clear subpackage structure, such as using a usecases package for operations like text-to-speech, you maintain a clean, scalable, and maintainable codebase. This approach aligns with the principles of Clean Architecture, ensuring that your business logic remains independent of other layers like data or presentation.
Summary:
Within the domain package, use a subpackage named “usecases” to contain text-to-speech functionality.