Entity (say UserEntity) has strict rules for its properties and can exist in two states - persisted (which means that it has id) and previously saved (which means that it does not have idyet).
id
In accordance with the answer to this question about how to handle the required properties , a "real" UserEntity should only be created with the idone passed to its constructor.
However, when I need to create new UserEntityfrom the information sent by the browser, I need to be able to check the information before continuing in db.
new UserEntity
In the past, I simply created an empty UserEntity (without id), set new properties, and checked it - but in this new safer way of thinking about objects, I should not ever create a new UserEntity without id.
I don’t want to create TWO places that know how to check my UserEntity properties, because if they ever change (and they will), it will be twice as much code to update and double the chances of errors.
How to effectively centralize the knowledge of validating my object properties?
Note
One of my ideas was reflected in this question in which I consider the preservation of non-state properties, such as email, password and name, in a standardized value object that I know about the rules of its properties that can be used by different services, such as the Controller, Validator and Repo, or Mapper.
what plants are intended for. to the factory method, you only transfer the data necessary to force the actual UserEntity invariants to be applied (take some time to figure out what your real UserEntity invariants are, and you better do this with the help of your domain specialists).
factory UserEntity.
, , , . - ... , - .
, , factory UserEntity.
itzik saban
, :
(1) :
(, UserEntity) , - ( id) pre-persisted ( , ).
, , . , , . , , (, return isValid , )
(2) , "" , , . , , (, - ...)
, , (, ), . , .
-, save() load() , ID. , , Identity .
Identity , , .
db, () db,
db , db/ .
- -/ ..,
(http://en.wikipedia.org/wiki/Strategy_pattern)
- .
. , , , , . , (, , , , ; , , , ..).
, (, ) - . , , , . , ( , , ), , (, ).
. , , . -.
, , , ( , - ). , , , . , .