Validate mapping between Clean Architecture layers with my ClassFieldEquality library (Kotlin)

Alexander Shafir
2 min readJan 8, 2021
https://depositphotos.com/37033543

Clean Architecture is current standard in Android development.

At its core is dependency rule: inner layers do not interact with outer layers.

For instance, you have domain layer (library) with entity BankAccount. This layershould be platform-agnostic.

In Android we use Kotlin, and data class is a natural choice for it. Now we may need to store this entity in database with ORM (like Room) or Realm. First one requires adding bunch of annotations on existing data class, second one requires extending RealmObject. Both approaches make your domain layer non-portable and dependent on platform (outer layer) thus violating Dependency Rule.

In order to comply with Dependency Rule, one needs to create copy of domain class and than map copy from outer layer to domain to make it consum-able for other layers. You can refer to following SO threads for

As we used Kotlin’s data class, we are restricted from using inheritance, so we need to create complete copy of it + mapper. And from compiler’s perspective these are distinct classes.

To fill this gap, I created ClassFieldEquality library.

Ideas is as follows: check that amount of fields in two classes is equal, their names and types are equal.

Library is comprised of three artifacts:

  • Android Lint module: Executes checks w/o compilation, shows results in IDE and aborts build in case of error.
  • Annotation Processor module: Generates mapper (extension function) from target class to origin class. You need to run Build first. Currently aborts silently if error is detected.
  • Annotation module: Plain annotations (currently @FieldEquality only).

Sample usage:

In your build.gradle:

repositories {
jcenter()
}

implementation "com.alexshafir.classfieldequality:annotations:1.0.0"
kapt "com.alexshafir.classfieldequality:processor:1.0.0"
lintChecks "com.alexshafir.classfieldequality:lint:1.0.0" // Android

--

--