Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 370 Vote(s) - 3.51 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Validation and DDD - kotlin data classes

#1
In Java I would do validation when creating constructor in domain object, but when using data class from kotlin I don't know how to make similar validation. I could do that in application service, but I want to stick to domain object and it's logic. It's better to show on example.

public class Example {

private String name;

Example(String name) {
validateName(name);
this.name = name;
}
}

In Kotlin I have just a data class is there a way to do it similarly to Java style?

data class Example(val name: String)
Reply

#2
You can get a similar effect by using *[companion factory method][1]*
:

data class Example private constructor(val name: String) {
companion object {
operator fun invoke(name: String): Example {
//validateName
return Example(name)
}
}
}

...

val e = Example("name")
e.name //validated

[1]:

[To see links please register here]

Reply

#3
You can put your validation code inside an [`initializer block`](

[To see links please register here]

). This will execute regardless of whether the object was instantiated via the primary constructor or via the `copy` method.

data class Example(val name: String) {
init {
require(name.isNotBlank()) { "Name is blank" }
}
}

A simple example:

fun main() {
println(Example(name = "Alice"))
println(try { Example(name = "") } catch (e: Exception) { e })
println(try { Example(name = "Bob").copy(name = "") } catch (e: Exception) { e })
}

Produces:

Example(name=Alice)
java.lang.IllegalArgumentException: Name is blank
java.lang.IllegalArgumentException: Name is blank
Reply

#4
You may want to use the interface to hide the data class.
The amount of code will increase slightly, but I think it's more powerful.

```
interface Example {
val id: String
val name: String

companion object {
operator fun invoke(name: String): Example {
// Validate ...

return ExampleData(
id = UUID.randomUUID().toString(),
name = name
)
}
}

fun copy(name: String): Example

operator fun component1() : String
operator fun component2() : String
}

private data class ExampleData(override val id: String, override val name: String): Example {
override fun copy(name: String): Example = Example(name)
}
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through