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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With