Exchanging Value Object in the database

0

I'm reading the DDD book (Eric Evans) that says implementations of Value Object are immutable and if you want to change it, you'll have to create another one. In more common examples where you have a class Pessoa and another class Endereco , you re-create the address instead of changing the object, this works ok.

I also read link and link . But I have something a little different that doubts have come up.

I'm working on a billing system, and it contains 4 tables

Pessoa - campos: idPessoa, Nome -> <<Entity>>
Celular - campos: idCelular, Numero -> <<Entity>>
Vigencia - campos: idVigencia, idPessoa, idCelular, dataInicio, dataFim -> <<Value Object ou Entity????>> 

The Person table would be the company employee registry, suppose that it has around 10 thousand records The Cellular table is data of all cellular that the company has, suppose some 3 thousand records

The Vigencia table is the associative of Person X Cellular, for example, "Joao" has the cell phone "11 91234-5678" on the initial date "01-01-2016" and the end date would be null meaning that this cell phone is with it, and this record would have an "idVigencia" 1, which is not related to the business.

Now that we are on 08/20/2016, we learn that "João" is no longer the owner of this cell phone, and now it is Maria. Then in the "Vigencia" table, I would update the "end date" from "João" to "31-07-2016" and create another record for "Maria" with the "date starts" on "01-08-2016" , so says the cell phone "changed" owner

Ligacao - campos: idLigacao, idVigencia, etc... -> <<Value Object>>

In the table of connections, we have connections of the months 05, 06, 07 and 08 of 2016, where this cell is with João, then the FK of the field idVigência of the Connection table will be the number 1 in this case, only with the change of "Vigencia" of "João" the month 08/2016 would be with the wrong FK and should be reprocessed.

So, "Vigencia" would be a Value Object? (which is also my doubt, because it could be an Entity, but I do not see an identity in the "idVigencia", maybe in the set of fields)

If it is a < > the validity would be immutable and when updating the "John" putting the "end date" to "31-07-2016", would have to discard the object and create it again, only that it enters into the problem with respect to FK "idVigencia" of "Connection" table

What is the best way to implement this with DDD?

    
asked by anonymous 18.08.2016 / 05:36

1 answer

0

It is a typical problem of creating Value Object structured on a relational basis. You begin to create VO ID by necessity, even though you know that a VO has no ID by definition.

To really help solve your problem, I would need to know more about the business. But the little you've gone through, I'll risk the following:

  • If you remove idPeriodo - which I assume is FK for Vigência , your problem is solved

And, in order to relate the links to validity, you can only use the dates. So you are free to keep your dates or any other property of Effectiveness, without having to re-create all the links.

To search for links in a lifetime, you can use idCelular more dates. I think it will be performative enough.

    
19.08.2016 / 10:47