What is difference between mvvm with clean architecture and mvvm without clean architecture in Android?
Asked Answered
C

6

44

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.

Calyces answered 21/10, 2019 at 10:46 Comment(0)
F
33

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.

Foah answered 21/10, 2019 at 11:12 Comment(2)
Then how do you connect use sasese with mvvm layer ? What does "model" from mvvm represent when used together with "CA"Katinka
Clean architecture has four layers and that is a very good point that they will be separate applications, nicely said. But who told you that "MVVM as design pattern should be implemented in the Presentation Layer"? The model from the MVVM is the "Domain with database", why would you put it inside the "Interface adapters" layer (or presentation layer as you call it)?Robedechambre
A
20

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.. :)

Adara answered 21/10, 2019 at 12:33 Comment(8)
Then how do you connect use sasese with mvvm layer ? What does "model" from mvvm represent when used together with "CA"Katinka
@Katinka We can connect the useCase with MVVM layer by simply putting it in ViewModel. Model in MVVM (presentation layer) is the output of the useCase. Of course, the implementation in viewModel code depends on how you return the output of the useCase.Adara
ok interesting idea, i was thinking about Model using the usecase instead, for me the usecase is not just singel action it's a sequence of actions (steps ) probably methods that need to be called on it, so for me the model is used from the beginning and not as end result.Katinka
i guess what you mean is that the model is simple DTO for uscase interaction and not modeling as in pure mvvm pattern ? pity uncle bob did not use mvvm in his examplesKatinka
If the DTO is not enough for you, then you can convert the DTO model to the model for UI, in the viewModel. And I agree UseCase shouldn't know anything about the model in the UI (MVVM) because UseCase is simply giving an output of a process. If useCase has no output, at least it has void return when the process is finished.Adara
Well... UseCase doesn't actually know it just knows about the interface but hmm according to "CA" all input and output interfaces need to be defined in the Domain layer where the Usecase is defined so it would be strange to have mvvm model definition there but implementation in the presentation layer...Katinka
Not sure what are you talking about. The model from MVVM represents database and business logic. Business logic in Clean Architecture located in Entities and UseCases. The presentation layer is not part of the clean architecture at all, you confused it with PresentationDomainDataLayering by Martin Fowler. If you named the "Interface adapters" layer from the clean architecture as the presentation layer, then it is still not clear why you decided it is a good idea to put in this layer the business logic and database (a.k.a. Model from MVVM).Robedechambre
Yes you're correct, what I mean is most common usage in android projects, the combination of Martin Fowler architecture and Uncle Bob Clean Architecture. Just dividing the layer into presentaion, domain, and data won't assure it will implement clean architecture. One of good reference you can see on proandroiddev.com/…Adara
A
15

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
______________________________________________
Aquiculture answered 28/10, 2021 at 7:44 Comment(2)
Excellent representation of the "MVVM without clean architecture". Your interpretation of the "MVVM with Clean Architecture" is a very nice attempt to cross different types of architecture into one. And maybe under a certain point of view, you can think of it as an MVVM with Clean Architecture, but why would you do that if it already has a name, it is called PresentationDomainDataLayering by Martin Fowler (martinfowler.com/bliki/PresentationDomainDataLayering.html).Robedechambre
Nice representation. Also, in "MVVM without clean architecture" we can still keep business logic out of view models, isn't?Thinskinned
D
5

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

Durra answered 9/6, 2021 at 9:50 Comment(3)
Then how do you connect use sasese with mvvm layer ? What does "model" from mvvm represent when used together with "CA" ?Katinka
In a clean architecture, the usecase is placed in the Domain layer, and then the usecase is aware of the Repository and the Viewmodel is aware of the usecase.Durra
You confused Presentation Domain Data Layering by Martin Fowler (martinfowler.com/bliki/PresentationDomainDataLayering.html) with The Clean Architecture by Robert Martin (blog.cleancoder.com/uncle-bob/2012/08/13/…). They are similar, but not the same.Robedechambre
H
2

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 :)

Ham answered 24/8, 2022 at 7:58 Comment(1)
Clean Architecture is both a set of principles and a specific architecture. Some of the main principles are the dependency rule, the separation of concerns, and the stable abstractions principle. Clean Architecture is also a specific architecture that applies these principles to a layered structure. The architecture consists of four main layers: Enterprise Business Rules (Entities), Application Business Rules (Use Cases), Interface Adapters, Frameworks & Drivers.Robedechambre
R
1

Let's refer to the primary sources.

Main differences:

  1. 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". enter image description here
  2. 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. enter image description here
  3. 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. enter image description here

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:

  1. Enterprise Business Rules (Entities)
  2. Application Business Rules (Use Cases)
  3. Interface Adapters
  4. 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.

Robedechambre answered 30/5, 2023 at 2:5 Comment(2)
Thanks for the different point of view. This architecture subject is so confusing to me. People say that you can use Clean Architecture with MVVM, or you can use just MVVM, but then they say that for some abstract reasons you should use it with CA. And then there is modular design, which can also be implemented. And "it all depends on the complexity of your app".Anchoress
From some article that I've read: "MVVM separates your view (i.e. Activities and Fragments) from your business logic. MVVM is enough for small projects, but when your codebase becomes huge, your ViewModels start bloating. Separating responsibilities becomes hard. MVVM with Clean Architecture is pretty good in such cases. It goes one step further in separating the responsibilities of your code base. It clearly abstracts the logic of the actions that can be performed in your app."Anchoress

© 2022 - 2024 — McMap. All rights reserved.