ModelMapper 的 kotlin 解决方案在哪里
DTO(又名源类)
abstract class BaseRequirementDTO()
class AttributeRequirementDTO(
var attribute: Attribute = Attribute.STRENGTH,
var attributeValue: Long = 10
) : BaseRequirementDTO()
class LevelRequirementDTO(
var requiredLevel: Long = 1
) : BaseRequirementDTO()
我的模型(又名目的地类)
abstract class BaseRequirement() : Model()
class AttributeRequirement(
var attribute: Attribute = Attribute.STRENGTH,
var attributeValue: Long = 10
) : BaseRequirement()
class LevelRequirement(
var requiredLevel: Long = 1
) : BaseRequirement()
映射器配置
val mapper = ModelMapper()
mapper.configuration.matchingStrategy = MatchingStrategies.STRICT
mapper.createTypeMap(BaseRequirement::class.java, BaseRequirementDTO::class.java)
mapper.typeMap(AttributeRequirementDTO::class.java, BaseRequirement::class.java)
.setProvider { request -> mapper.map(request.source, AttributeRequirement::class.java) }
mapper.typeMap(LevelRequirementDTO::class.java, BaseRequirement::class.java)
.setProvider { request -> mapper.map(request.source, LevelRequirement::class.java) }
模型映射器版本:2.3.7
https://github.com/modelmapper/modelmapper/issues/182#issuecomment-660965119