以下问题:在具有Spring-Boot
和Kotlin
的客户端/服务器环境中,客户端希望创建类型A的对象,因此通过RESTful端点将数据发布到服务器。
实体A在Kotlin中被实现为data class
,如下所示:
data class A(val mandatoryProperty: String)
从业务角度来说,属性(也是主键)绝不能为空。但是,客户端不知道它,因为服务器上的Spring @Service Bean会非常昂贵地生成它。
现在,在端点Spring尝试将客户端的有效负载反序列化为类型A的对象,但是,mandatoryProperty
在该时间点是未知的,这将导致映射异常。
解决这个问题的几种方法,其中没有一个让我感到惊讶。
data classes
无法扩展,它意味着将类型A的属性复制到DTO中(强制属性除外)并将它们复制过来。此外,当A增长时,DTO也必须增长。@Pattern
约束。因此每个虚拟值都是有效的主键,这让我感觉很糟糕。我可能监督的任何其他方式更可行吗?
我不认为对此有一个通用的答案......所以我只会给你关于你变种的2美分......
您的第一个变体具有其他任何实际上没有的好处,即您不会将给定对象用于其他任何设计的对象(即仅用于端点或后端目的),但这可能会导致繁琐的开发。
第二个变体很好,但可能会导致其他一些开发错误,例如当你以为你使用了实际的A
,但你宁愿在DTO上操作。
变体3和4在这方面类似于2 ...你可以使用它作为A
,即使它只具有DTO的所有属性。
所以......如果你想走安全的路线,也就是说,没有人应该将这个物体用于其他任何事情,那么你应该使用第一个变体。 4听起来像是一个黑客。 2和3可能还可以。 3因为当你用它作为DTO时你实际上没有mandatoryProperty
...
尽管如此,你有你最喜欢的(2)并且我也有一个,我将专注于2和3,从2开始使用子类方法和sealed class
作为超类型:
sealed class AbstractA {
// just some properties for demo purposes
lateinit var sharedResettable: String
abstract val sharedReadonly: String
}
data class A(
val mandatoryProperty: Long = 0,
override val sharedReadonly: String
// we deliberately do not override the sharedResettable here... also for demo purposes only
) : AbstractA()
data class ADTO(
// this has no mandatoryProperty
override val sharedReadonly: String
) : AbstractA()
一些演示代码,演示了用法:
// just some random setup:
val a = A(123, "from backend").apply { sharedResettable = "i am from backend" }
val dto = ADTO("from dto").apply { sharedResettable = "i am dto" }
listOf(a, dto).forEach { anA ->
// somewhere receiving an A... we do not know what it is exactly... it's just an AbstractA
val param: AbstractA = anA
println("Starting with: $param sharedResettable=${param.sharedResettable}")
// set something on it... we do not mind yet, what it is exactly...
param.sharedResettable = UUID.randomUUID().toString()
// now we want to store it... but wait... did we have an A here? or a newly created DTO?
// lets check: (demo purpose again)
when (param) {
is ADTO -> store(param) // which now returns an A
is A -> update(param) // maybe updated also our A so a current A is returned
}.also { certainlyA ->
println("After saving/updating: $certainlyA sharedResettable=${certainlyA.sharedResettable /* this was deliberately not part of the data class toString() */}")
}
}
// assume the following signature for store & update:
fun <T> update(param : T) : T
fun store(a : AbstractA) : A
样本输出:
Starting with: A(mandatoryProperty=123, sharedReadonly=from backend) sharedResettable=i am from backend
After saving/updating: A(mandatoryProperty=123, sharedReadonly=from backend) sharedResettable=ef7a3dc0-a4ac-47f0-8a73-0ca0ef5069fa
Starting with: ADTO(sharedReadonly=from dto) sharedResettable=i am dto
After saving/updating: A(mandatoryProperty=127, sharedReadonly=from dto) sharedResettable=57b8b3a7-fe03-4b16-9ec7-742f292b5786
我还没有告诉你丑陋的部分,但你已经自己提到了......你怎么把你的ADTO
变成A
而反之亦然?我会把它留给你。这里有几种方法(手动,使用反射或映射实用程序等)。该变体从非DTO特定属性中清楚地分离了所有特定于DTO的DTO。但是它也会导致冗余代码(所有override
等)。但至少你知道你操作的对象类型,并可以相应地设置签名。
像3这样的东西可能更容易设置和维护(关于data class
本身;-))并且如果你正确设置边界,甚至可能很清楚,当那里有一个null
而不是......所以显示那个例子太。首先从一个相当恼人的变体开始(当你尝试访问变量时,如果它还没有设置它会引发异常,这很烦人),但至少你在这里备用了!!
或null
-checks:
data class B(
val sharedOnly : String,
var sharedResettable : String
) {
// why nullable? Let it hurt ;-)
lateinit var mandatoryProperty: ID // ok... Long is not usable with lateinit... that's why there is this ID instead
}
data class ID(val id : Long)
演示:
val b = B("backend", "resettable")
// println(newB.mandatoryProperty) // uh oh... this hurts now... UninitializedPropertyAccessException on the way
val newB = store(b)
println(newB.mandatoryProperty) // that's now fine...
但是:即使访问mandatoryProperty
将抛出Exception
它在toString
中是不可见的,如果你需要检查它是否已经被初始化(即通过使用::mandatoryProperty::isInitialized
)它也看起来不错。
所以我告诉你另一个变体(同时我最喜欢的,但是...使用null
):
data class C(val mandatoryProperty: Long?,
val sharedOnly : String,
var sharedResettable : String) {
// this is our DTO constructor:
constructor(sharedOnly: String, sharedResettable: String) : this(null, sharedOnly, sharedResettable)
fun hasID() = mandatoryProperty != null // or isDTO, etc. what you like/need
}
// note: you could extract the val and the method also in its own interface... then you would use an override on the mandatoryProperty above instead
// here is what such an interface may look like:
interface HasID {
val mandatoryProperty: Long?
fun hasID() = mandatoryProperty != null // or isDTO, etc. what you like/need
}
用法:
val c = C("dto", "resettable") // C(mandatoryProperty=null, sharedOnly=dto, sharedResettable=resettable)
when {
c.hasID() -> update(c)
else -> store(c)
}.also {newC ->
// from now on you should know that you are actually dealing with an object that has everything in place...
println("$newC") // prints: C(mandatoryProperty=123, sharedOnly=dto, sharedResettable=resettable)
}
最后一个有好处,你可以再次使用copy
方法,例如:
val myNewObj = c.copy(mandatoryProperty = 123) // well, you probably don't do that yourself...
// but the following might rather be a valid case:
val myNewDTO = c.copy(mandatoryProperty = null)
最后一个是我最喜欢的,因为它需要最少的代码并使用val
代替(所以也不可能意外覆盖或者你在副本上操作)。如果你不喜欢使用mandatoryProperty
或?
,你也可以为!!
添加一个访问器,例如:
fun getMandatoryProperty() = mandatoryProperty ?: throw Exception("You didn't set it!")
最后,如果你有一些辅助方法,如hasID
(isDTO
或其他),从上下文中也可以清楚地知道你正在做什么。最重要的可能是建立一个每个人都理解的惯例,因此他们知道何时应用什么或何时期望某些特定的东西。