Object identifier as constructor or setter method?

When you have an entity such as UserEntity, who has a property idderived from his primary key in db - should you provide a setter method, for example setId()?

Some arguments against:

  • opens the door to possible random overwrites of other UserEntities in db
  • two (or more) UserEntities can exist at any time with the same id, but with different properties. (if I pulled 3 different users from db and set their values idto the same)

Some arguments for:

  • If I don't need to create a UserEntity instance using idin the constructor (since it has a setter method), I can use the methods of the UserEntity object with temporary / fake / new user values ​​... without having to transfer it first.

Provide a setter (and not force it idin the constructor) or force install idin the constructor and remove the installer?

+5
source share
2 answers

- persistence . , ( ) . , , , . , , , , persistence UUID , .

+3

@johnnietheblack, getter . ( ), . , .

, (Andrew Driven Design), , . , , .

+1

All Articles