Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Domain Entity validation before it's persisted?

An Entity (let's say a UserEntity) has rigid rules for it's properties, and it can exist in 2 states - persisted (which means it has an id) and pre-persisted (which means it does not have an id yet).

According to the answer to this question about how to handle required properties, a "real" UserEntity should only ever be created with an id passed in to its constructor.

However, when I need to create a new UserEntity from information sent by the browser, I need to be able to validate the information before persisting into the db.

In the past, I would simply create a blank UserEntity (without an id), set the new properties, and the validate it - but, in this new, more secure way of thinking about Entities, I shouldn't ever create a new UserEntity without its id.

I don't want to create TWO places that know how to validate my UserEntity's properties, because if they ever change (and they will) it would be double the code to update and double the chances for bugs.

How do I efficiently centralize the validation knowledge of my entity's properties?

Note

One idea I had is reflected in this question, in which I consider storing the non-state properties like email, password and name in a standardized value object that would know about the rules for its properties that different services, like the Controller, Validator, and Repo, or Mapper could use.

like image 306
johnnietheblack Avatar asked Mar 22 '12 19:03

johnnietheblack


2 Answers

that's what factories are for. to the factory method you pass only the data that is required to enforce the real invariants of UserEntity (take some time to figure out what are your real invariants of UserEntity and you'd better do it with your domain experts).

in the factory method you create a new Id and pass it to the UserEntity constructor.

In this stage i don't think it is that bad to discard the instance if the validation inside the constructor fails. in the worst case - you've lost an id... it's not a case that suppose to happen quite often - most of the time the data should be validated in the client.

Of course another option is that in the factory method you first validate the parameters and only then create a new Id and pass it to the UserEntity constructor.

itzik saban

like image 55
ItzikSaban Avatar answered Oct 01 '22 02:10

ItzikSaban


I think you have a couple of options to consider:

(1) Consider your first comment:

An Entity (let's say a UserEntity) has rigid rules for it's properties, and it can exist in 2 states - persisted (which means it has an id) and pre-persisted (which means it does not have an id yet).

Here, you are already mention that validation actually depends on whether the entity has been persisted. In other words, if the entity hasn't been persisted, then it should be valid without the ID. If you continue with this domain specification, I feel the validation should act accordingly (e.g. return isValid even without an ID if the object hasn't been persisted)

(2) If you assume "valid" means the object has an ID, then you would need to generate the ID upon creation. Depending on how your IDs are generated, this could get tricky (e.g. save to database and return created ID, or generate unique identifiers somehow, or ...)

Using either approach, its probably worth implementing common base class(es) for your Entity (e.g. with ID) to help minimize duplicating validation across the different states. Hopefully, this shields the derived entities from the common validation as well.

like image 37
Chris Melinn Avatar answered Oct 01 '22 03:10

Chris Melinn