Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RealmDb : Clean architecture in Android

Tags:

I am evaluating RealmDb, I feel RealmDb is tightly coupled with model layer. Which makes me feel if tomorrow I need to replace with some other local database than it will be a huge refactoring effort.
My question is how to achieve clean architecture with RealmDB? Any examples I can follow?

like image 630
Signcodeindie Avatar asked May 07 '16 06:05

Signcodeindie


2 Answers

Realm just makes it easier to re-use your database models as your view models if you want. But there is nothing stopping you having data layer entities and view layer entities and then doing the mapping on the boundaries.

E.g.

// Data layer
public class FooEntity extends RealmObject {

  // Realm fields and methods... 

  public static FooEntity fromViewModel(FooViewModel viewModel) {
    // Convert to entity
  }

  public static FooViewModel toViewModel(FooEntity entity) {
    // Convert to view model
  }


}

// View layer
public class FooViewModel {
  // Standard POJO used by the View layer
}

In many cases that is most likely overkill, but it will give you the seperation you want.

like image 141
Christian Melchior Avatar answered Sep 28 '22 02:09

Christian Melchior


There are different architectures where the main objective is to isolate the domain of our application of the frameworks as Clean Architecture, Onion Architecture, Hexagonal Architecture.

To achieve this isolation, the implementation details are hidden as the data layer. A boundary or port is defined in the domain layer defining how the stored data is accessed but without knowing how they are stored, the repository pattern is usually used to implement this architecture requirement.

With these requirements we will generally have a persistence model and a differentiated domain entities. Persistence models can and should be coupled to the framework we are using for example Realm.

I leave a diagram where you can better understand it graphically.

Clean Architecture

like image 43
xurxodev Avatar answered Sep 28 '22 02:09

xurxodev