Introducing Clean Architecture with MVVM

Tram Ho

Introduction to Clean Architecture

This is a structure that helps to separate functions across layers. Through this model, there is a lot of support through thinking carefully and effectively, recognizing mismatch between Use Cases and Entities, setting direction in the system. It also aims for maximum independence of any library or tool, so that it is suitable for testing and replacing them.

Apply with MVVM

  • Domain Layer: Entites + Use Cases + Gatway Protocols
  • Data Layer: Gateway Implementations + API (Network) + Database
  • Presentation Layer: ViewModels + Views + Navigator + Scene Use Cases

Solution

Go into details

Domain class

Entities

Contains Business logic. The most important class, where you do problem solving – purpose when building the app. An entity can be an object with methods, or it is a collection of struct and functions. It doesn’t matter, entities can be used by many different ways in the same application. Entites can be simply structures data:

Use Cases

There are no apploaction-specific rules yet. It encapsulates and deploys all use cases in the system. Use cases structure data flows to and from entites, and direct those entities to Critical Business Rules -> the purpose of the use cases. UseCases are protocols, to do specific things like:

Gateway Protocols

Basically, the gateway is just an abstraction that will show the work that is implemented below. It can be a Data Store (pattern Repository), an API gateway, and so on. For example, with Database gateway there are methods to make application requests. However, do not try to hide all important rule rules through the gateway. All database queries must be relatively simple like the CRUD method, and of course some filters are also accepted.

Data class

The Data class contains Gateway implementations and 1 or more Data Stores. Gateway is the response to dispatch data from Data Stores. The Data Store can be online or offline (eg presistent database). The Data class depends only on the Domain class.

Gateway Implementations

UserDefaults

APIs

Map JSON Data to Domain Entities using ObjectMapper:

CoreData Repositories

Map CoreData Entities to Domain Entitites and vice versa:

So we have finished part 1, about combining the MVVM model and the Clean Architectures architecture. See you on part 2. Original article Mr. Tuan

Share the news now

Source : Viblo