Domain Managed Automatically Added Object Key

It was only starting with the development of Driven Driven Design, and I learned that you should keep your model in the correct state and when creating a new instance of the class it is recommended to use all the necessary attributes as constructor parameters.

But when working with auto-increment keys, I only have this new identifier when I call the Add method from my constant layer. If I create objects without a key, I think they will be in an invalid state because they need some kind of unique identifier.

How do I implement my architecture in order to have my identifiers before creating a new instance of my object?

+3
source share
2 answers

Random IDs Generated

The pragmatic approach here is to use random identifiers and generate them before instantiating the object, for example. in the factory. GUID is a common choice.

And before you ask: No, you will not have GUID errors : -)

Reserved Sequential IDs

If for some reason you should use a sequential identifier, you still have options:

  • Request a sequence in the database to obtain the next identifier. It depends on your DB product, e.g. Oracle).
  • , . , - , .

, DB, . . , , .

- , , , . , , . .

+4

Dmi

1) factory , . , , ,

2) . , - , , ( ). , , . (random guid) , ID (int?) null, .

0

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


All Articles