One model, many facets.
Kotools Facet is a Kotlin SDK that lets you define a faceted class once and project it across every layer of your application.
In a typical Kotlin project, a single concept needs a parallel data structure to communicate with external systems. For example, representing a user accessible via HTTP that needs persistence would look like this:
data class UserDto(val email: String) {
fun toUser(): User = User(email)
}
data class User(val email: String) {
fun toUserDto(): UserDto = UserDto(email)
fun toUserEntity(): UserEntity = UserEntity(email)
}
data class UserEntity(val email: String) {
fun toUser(): User = User(email)
}This pattern leads to:
- Boilerplate code: Redundant data classes and mappers.
- Maintenance burden: A single property change ripples through every layer.
With Kotools Facet, the faceted class is the only model. Declare each facet in it, and this SDK takes care of the rest at compile-time.
// Client code:
@Faceted
data class User(val email: String) {
object Dto : BidirectionalFacet<User>
object Entity : BidirectionalFacet<User>
}
// Generated code:
fun User.toUserDto(): UserDto = UserDto(email)
fun User.toUserEntity(): UserEntity = UserEntity(email)
data class UserDto(val email: String) {
fun toUser(): User = User(email)
}
data class UserEntity(val email: String) {
fun toUser(): User = User(email)
}This solution provides several benefits:
- Single source of truth:
Useris declared once. No parallel data classes and mappers to maintain. - Easy maintenance: Changing one property in
Userpropagates across facets.
Note
Kotools Facet is not just another mapper generator.
Classic mapper generators answer "How do I convert A to B?". They require
UserDto and UserEntity to exist.
Kotools Facet answers "Why do I need B at all?". It requires only the
faceted class (User).
Kotools Facet ships following modules:
facet-core-dslthat provides@Facetedand DSL for declaring facets.facet-core-kspthat generates projections at compile-time.
When released, setup will require a KSP plugin and the modules for your stack:
// In build.gradle.kts file:
plugins {
id("com.google.devtools.ksp") version "<ksp-version>"
}
dependencies {
ksp("org.kotools:facet-core-ksp:<facet-version>")
implementation("org.kotools:facet-core-dsl:<facet-version>")
}- Glossary: Vocabulary used across this project's documentation.
- Kotlin features support: Supported and unsupported Kotlin features.
- Roadmap: What's planned next.
Kotools Facet is commercial software. To get notified when it ships and to discuss licensing, join the waiting list by sending an email to contact@kotools.org with the subject "Kotools Facet – Waiting List".
The SDK modules are distributed under a commercial license. To enquire about licensing, contact us at contact@kotools.org.