Handling entities validations in RavenDB
This post came out of a stack overflow question. The user had the following code:
But he noted that this will not work for other approaches, such as this:
This is because RavenDB tracks the entity and will persist it if there has been any changes when SaveChanges is called.
The question was:
Is there someway to get RavenDB to store only a snapshot of the item that was stored and not track further changes?
The answer is, as is often the case if you run into hardship with RavenDB, you are doing something wrong. In this particular case, that wrongness is the fact that you are trying to do validation manually. This means that you always have to remember to call it, and that you can’t use a lot of the good stuff that RavenDB gives you, like change tracking. Instead, RavenDB contains the hooks to do it once, and do it well.
This will be called by RavenDB whenever we save to the database. We can now write the validation / registration code like this:
And that is all that she wrote.