Clean Architecture Guide.
Introduction
Clean architecture is an architectural style or set of guidelines used for the best application design and it is recommended for building robust and high-quality apps.
This article takes in the concept of clean architecture in the android application.
Guides of Clean Architecture
Clean architecture aims in designing systems with the following characteristics
- Testable
- UI Independent that is UI can easily be changed without changing the entire system.
- Point dependencies inwards that are nothing on the inner circle (application and business rule) should depend on anything on the outer circle.
- Split your application's code into layers.
- Place abstractions in the inner layers.
- Put implementation in the outer layers.
- Independency of database framework, external agencies, and libraries
Principles followed when building application
Separation of concern
Avoid writing your entire code on the UI(activity or fragment). The UI should only consist of logic that handles the UI.
Drive UI from data models
Data models represent the data of the application they are independent of the UI element and other application components.
Clean Architecture Flow
- UI request for data from the presenters(ViewModel)
- Usecase which is connected to the ViewModel request the repository if there is project data in the database.
- If the result is positive the repository creates a data transfer object and returns the data to the useCase.
- If there is no data in the database the repository requests the network and saves the data to the database.
- Usecase in turn returns the data to the presenter which then returns it to the UI.
Terms
1. Entities
Are the enterprise wide-business rule that encapsulates most business rules and has data transfer objects such that any external changes do not affect the rule.
2.Usecases
Is the application-specific business rule of software it is isolated from any changes from the database, common frameworks, and the UI.
3.Repository
Converts data from Usecase and repository to formate applicable to the database and web.
4. Frameworks and Drivers
The outermost layer consists of the web, database, and HTTP clients.
Layers In Clean Architecture.
UI Layer
Displays data on users screen, whenever data changes either due to user interaction eg button click of external input e.g network response.
It comprises of:-
- UI element to render data that are built through Views or jetpack compose.
- State holders e.g view models that hold data and expose it to the UI element and handle logic.
Data Layer
It contains the business logic that is what gives value to the application
It is made up of:-
- Repositories and each can have single to many data sources.
when creating a repository create for each different type of data you handle in the application. example AuthRepository MainRepsitory.
Repository purpose
- Exposes data to the rest of the application
- Centralize changes to the data
- Resolve conflict between multiple data source
- Abstracting sources of data from the rest of the application.
- Connecting business logic.
Domain Layer
This is the optional layer between the UI and the data layer. It is responsible for encapsulating complex and simple business logic that is used by multiple view models. It mainly comprises of
- UseCases
- interactors(repository interfaces)
Domain layer purpose
- Avoids code duplication
- Improves code readability
- Improves testability of the codebase.
- Avoids large classes by splitting responsibilities.
Conclusion
In this article, we have gone through what is clean architecture, and the guides of clean architecture principles followed when designing an application. The flow of clean architecture, terms used, and individual layers of clean architecture.
Get Example in this GitHub Repository