I'm learning about MVVM and Clean Architecture. Then I found some articles present about MVVM + Clean Architecture, but I still didn't get it about the difference between mvvm with clean architecture and mvvm without clean architecture. Any summary about these stuff? Thank you.
Clean architecture aims to separate the layers. Business Layer, Data Layer and Presentation Layer will be separate applications. Therefore you will increase the reusability for each one of them. MVVM as design pattern should be implemented in the Presentation Layer. Presentation Layer will interact with Business Layer (or Domain Layer) and the Business Layer will use Data Layer for sharing data.
MVVM is just part of the clean architecture in the presentation layer
. It just a set of rules on how to display the data from UseCase.
One of the benefits of using clean architecture is we can change our design pattern in presentation layer
without changing domain layer
or use case.
So for example, if we're using let say MVI and then changing to MVVM, it can be done smoothly with ease.. :)
As I understand:
MVVM without clean architecture:
______________________________________________
UI
- - - - - - - - - - - - - - - - - - - - - - -
Presenter/ViewModel (Business Logic)
______________________________________________
Repository
DataSource
______________________________________________
MVVM with Clean Architecture:
______________________________________________
UI
Presentation Layer
Presenter/ViewModel
______________________________________________
UseCases + Entity (Business Logic) Domain/Business Layer
______________________________________________
Repository
Data Layer
DataSource
______________________________________________
MVVM is just a technique to manage the View layer of whatever architecture you're using.
Clean Architecture is a way of organizing communication between layers. They aren't mutually exclusive
The Layers of MVVM with Clean Architecture The code is divided into three separate layers:
- Presentation Layer
- Domain Layer
- Data Layer
Presentation Layer
Is here, where the logic related with views and animations happens. It uses Model-View-ViewModel ( MVVM), but you can use any other pattern like MVC or MVP
Clean Architecture is a set of rules and principles that helps you maintain and architect a software system. While MVVM is a Design Pattern (similarly like other ones MVP, MVC) that is originated from Clean Architecture.
As Clean Architecture strongly recommends to separate each layer UI, Controllers, Use-cases & Entities, MVVM does the same job as it separates each of them in:
- UI -> View
- Controllers -> ViewModel
- Use-cases/Entities/APIs/Local/Repos/etc. -> Model
That's what I understood. Hope this helps :)
Let's refer to the primary sources.
Main differences:
- MVVM (https://learn.microsoft.com/en-us/dotnet/architecture/maui/mvvm) is built around a "model" (business rules), where a database is a part of the "model".
- The same goes for PresentationDomainDataLayering by Martin Fowler https://martinfowler.com/bliki/PresentationDomainDataLayering.html and Android's "whatever name is" architecture https://developer.android.com/topic/architecture (data<-domain<-UI). It is built around the "data" layer, making it somewhat difficult to switch between databases, but allows easy manipulation with UI.
- The clean architecture (https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html) is built around "business rules", where everything very flexible and the database and UI are both can be replaced at any time.
Here is a quote from the book "Clean Architecture: A Craftsman’s Guide to Software Structure and Design":
The architect can employ the Single Responsibility Principle and the Common Closure Principle to separate those things that change for different reasons, and to collect those things that change for the same reasons—given the context of the intent of the system. User interfaces change for reasons that have nothing to do with business rules. Business rules themselves may be closely tied to the application, or they may be more general. The database, the query language, and even the schema are technical details that have nothing to do with the business rules or the UI.
Thus we find the system divided into four decoupled horizontal layers — the application-independent business rules, Application-specific business rules, UI, and the Database.
Later in the book Robert Martin describes in detail how to build these 4 corresponding layers:
- Enterprise Business Rules (Entities)
- Application Business Rules (Use Cases)
- Interface Adapters
- Frameworks & Drivers.
Here is another quote, regarding the View Model from the Clean Architecture:
If the application wants to display money on the screen, it might pass a Currency object to the Presenter. The Presenter will format that object with the appropriate decimal places and currency markers, creating a string that it can place in the View Model. If that currency value should be turned red if it is negative, then a simple boolean flag in the View model will be set appropriately. Anything and everything that appears on the screen, and that the application has some kind of control over, is represented in the View Model as a string, a boolean, or an enum. Nothing is left for the View to do other than to load the data from the View Model into the screen.
As you can see ViewModel from the clean architecture is more like a "state" of the View, and the logic of the View goes to the "Presenter". In MVVM on another hand, ViewModel holds both the state and logic of the View.
In summary: MVVM and Clean Architecture are completely different and there is no way you can build an MVVM with the Clean Architecture.
© 2022 - 2024 — McMap. All rights reserved.