Use of CLEAN architecture in Android with Parceler
Asked Answered
T

1

10

I'm using working on a project in the CLEAN architecture where the project is broken into the "Presentation", "Domain" and "Data" modules, where the Domain module hosts the "Entities" that are basically the data models specific to this project. An example of this architecture is here.

Unlike the other two module, "Domain" is a pure Java library module, which is great for clarity and testing as it doesn't have the Android overhead, however it also means I'm now not able to use libraries like "Parceler" to which is very Android specific. Is there a way around this?

Tupungato answered 19/5, 2016 at 3:56 Comment(1)
you can create different package or flavor for testing only. and in that package crate class with serializable. so for testing it will take serializable and for production it will use Parcelable.Skinnydip
G
5

Parceler allows you to configure beans outside of the given module to generate a wrapping Parcelable via the @ParcelClass annotation. This means you can configure the given bean as a @Parcel outside of the Data layer, and in the presentation layer (or wherever else you want). See http://parceler.org/#classes_without_java_source for specifics.

The org.parceler:parceler-api module is also pure Java, it has no dependencies on the Android api. Therefore you should be free to annotate your Data module without violating the CLEAN archetecture you're seeking. The annotation compiler portion (org.parceler:parceler) of the library, however, does rely on the Android API, so you'll need to run it in the android-specific module. This leaves you with the follow:

  1. Include the parceler-api library in your Data module and annotate your Data layer beans (@Transient, @ParcelProperty, etc). If you don't need any specific configuration, you can avoid including the parceler-api as a dependency.

  2. Add the parceler and parceler-api libraries to your Android-specific module (Presentation?).

  3. Add a @ParcelClass annotation with each class from your data module you want to be an @Parcel to an arbitrary class (Application?). This will direct Parceler to generate a Parcelable for each class identified within the @ParcelClass parameter.

Galaxy answered 19/5, 2016 at 15:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.