Search code examples
nhibernatenhibernate-mapping

NHibernate versioning


I am currently interesting in creating mapping for entities with a version field. However, I'm confused about how to work with versioning in a web environment. In most examples i have seen an entity is updated within a using statement that happens instantly.

In a web example the entity gets fetched in the HTTP GET and not updated until the client invokes an HTTP POST. What I normally do in the post is to fetch the entity again, update data and store it. But that entity could have changed in the time between my GET and POST. Should I check the version field myself before i save or is there a better way?


Solution

  • Yes, I think you'll need to check this yourself. One way you could do this is when you retrieve the entity in the initial get and send it back to the caller, include the version number in the response. When you do your post, include the version number. On the server side, when you re-read the attached nHibernate entity, get the current version number from it. If your version number is out of date, stop the update.