Unidirectional Mapping¶
By default, all mappings generated by AutoMapper are reversible. This means that for a mapping from A to B, the processor generates both A.asB() and B.asA(). This is convenient for two-way data synchronization, such as between domain and data layers.
However, in many cases, you only need a one-way mapping. A common example is mapping from your domain models to UI-specific models, where a reverse mapping is never required.
Disabling Reversible Mapping¶
To create a unidirectional mapping, set the reversible parameter in the @AutoMapper annotation to false.
Only One Function Will Be Generated
When you set reversible = false, the processor will only generate the primary mapping function (e.g., Source.asTarget()) and will skip generating the reverse function. This keeps your generated code lighter and more focused.
Example:
@AutoMapperModule
interface PresentationMapperModule {
/**
* This will only generate `User.asUiUser()`
* The reverse mapping `UiUser.asUser()` will NOT be generated
*/
@AutoMapper(reversible = false)
fun userMapper(user: User): UiUser
}
This is particularly useful in presentation layers (e.g., Android Views, Compose UI) where data flows in one direction, from your business logic to the UI.