How to handle the required properties for domain objects?

I have a UserEntity that is ultimately stored in the database according to it id. In this case, the property is idclearly sensitive, because changing this will cause the UserEntity to be saved through another UserEntity if it is saved later.

Therefore, I would like to help in safety with what happens in this case ...

. Option 1. CHECK idto pass it to the constructor, thereby deleting the Setter?

  • This means that every user item that the repository will be saved will be valid (since it has a minimum requirement id).
  • The downside is that my controller will not be able to create a new UserEntity (), because there will not be one for it id.

Parameter 2. Allow identifier in constructor equal to zero, but do not provide a setter

  • This will essentially create two types of UserEntity objects: those that were saved and those that did not.
  • I could make a new UserEntity () anywhere, populate it with its values, then pass it to the repository, which will then save it, create a new UserEntity with a new one idand return it.
  • This is cool, but I need to double check that my object has idanywhere I want to use it to save / the / etc domain logic to make sure it is a saved object.

Option 3. Don't I worry so much and give him a setter?

  • id UserEntity UserEntity db.
  • UserEntity(), id , .
0
1

, .

, , , .

id , ( ).

+3

All Articles