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.


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


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 (eg 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 (eg 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 (eg with ID) to help minimize duplicating validation across the different states. Hopefully, this shields the derived entities from the common validation as well.


In my opinion , save() , and load() methods should be doing both validation and setting ID attribute . And by the way an entity without Identity attribute is not a entity at all .

In my view Identity attribute should be validated and ensured when entity is in transit eg

loading from db , loading from file or (after) saving to db such that

if loading from db fails discard the entity saving to db/file fails discard the entity .

Since validation is business log /behavior etc and a better pattern for that would be

Strategy Pattern (http://en.wikipedia.org/wiki/Strategy_pattern)

链接地址: http://www.djcxy.com/p/61452.html

上一篇: 这应该是控制器还是域的责任?

下一篇: 如何在持久化之前处理域实体验证?