DDD: how to hide specific aggregate root constructors from integration layers in Kotlin

I am new to DDD, and even after reading the blue and red books, I still have some questions about how to convert some principles into code, in particular using Kotlin and Java.

For example, I identify the root of the client aggregate, which receives some parameters necessary for creation, for example, Name and Address:

class Client: AggregateRoot {

    var clientId: ClienteId
    var name: Name
    var address: Address

    constructor(name: Name,address: Address) : super(){
        // validations ....
        this.name = name
        this.address = address
    }

The simple part: To create a new client, I get a DTO inside the RS service and try to create a new Client class that passes the above parameters if everything was strong and all the rules are followed. I am sending a new client instance to the repository, pretty direct. / p>

clientRepository.store(client)

: , , , .

override fun getById(id: Long): Client {
  val clientEntity = em.find(...)
  val client: Client(.....) //But I need another constructor with ClientId
  return client
}

, , ClientId

constructor(clientId: ClienteId,name: Name,address: Address) : super(){

, , :

  • . #, .
  • - Java Kotlin, , ​​?

: , , , , :

client.addAddress(address)

, .

+4
1

, , Aggregate , , ( Application Presentation).

:

  • , . , ORM, . , . ORM .

  • . , Aggregate , , , .

:

// what you want the upper layers to see
interface Client {
    void addAddress(address);
}

// the actual implementations
public class ClientAggregate implements Client 
{
    void rehidrate(clientId,name,address){...}
    void addAddress(address){...}
}

public class ClientRepository
{
    // this method returns Client (interface)
    Client getById(id){
        val clientEntity = em.find(...)
        val client = new ClientAggregate()
        client.rehydrate(clientEntity.id, clientEntity.name, clientEntity.address)
        return client //you are returning ClientAggregate but the other see only Client (interface)
    }
}

. , Ubiquitous, Aggregate. , , . ; , . :

class Client {
    constructor(){ //some internal initializations, if needed }
    void register(name){ ... }
}
+3

Source: https://habr.com/ru/post/1695430/


All Articles