johnnietheblack
johnnietheblack

Reputation: 13310

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.

Upvotes: 5

Views: 1364

Answers (5)

Jeroen
Jeroen

Reputation: 989

I don't see the problem with persisting invalid data. What is valid or not is a business concern and can sometimes depends on the situation. The database doesn't care about these business rules.

If I have to fill out a big form online and the very last step requires me to enter my credit card information and I don't have my card ready, I'll have to discard all that information and the next time enter it all over again (which won't happen because I rather go somewhere else). I would like that application to store the information I already gave and later on I can make it functionally valid. As long as it isn't valid, I can't order things online.

Upvotes: -1

Derek Greer
Derek Greer

Reputation: 16252

The topic of how to do validation correctly is somewhat of a grey area.

Validation is typically cast as Invariant and Contextual validation. Invariant validation pertains to those things that, according to your problem domain, have to be present in order for your model to function properly in its intended role. Contextual validation pertains to state that's valid within a given usage context (e.g. A Contact used for emailing needs an email address, but doesn't need phone number; a Contact used for catalog mailings needs a mailing address, but doesn't need an email, etc.).

If you want to be architecturally pure, then technically the concerns of input validation (what your customers are typing into a user interface) and the state of a given entity are two different concerns. Ideally, your domain should have no knowledge of the particular type of application it's written for and therefore shouldn't be burdened with providing error messages suitable for use, either directly or indirectly, in displaying error messages back to the user. This presents a bit of an issue, since it can lead to duplicate or triplicate error checking (client side, service side, domain-level), so many opt for a more pragmatic approach of dealing with most validation external to the entity (e.g. validating an input model prior to entity creation).

Upvotes: 0

Chris Melinn
Chris Melinn

Reputation: 2076

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.

Upvotes: 2

sakhunzai
sakhunzai

Reputation: 14470

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 e.g

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)

Upvotes: 0

ItzikSaban
ItzikSaban

Reputation: 296

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

Upvotes: 4

Related Questions