NHibernate + ViewModel +MVC + Optimistic lock
Ok we have the following senario, but how do you do Optimistic Locking
we have a Customer Class
- Name
- Address
- Version
The version is a collision varible (int) which when the row is edited the version will increase by 1.
Now we have Martin who open Customer 129, as with a view model the model object is flattened into the View Model then passed onto a Customer Edit Screen.
David in the mean time also opens the Customer 129 for edit, changes the name and saves.
Martin, who was still editing the customer (now an old version), goes to save his changes, at this point I would hope to see an Lock error to be thrown
the Question is how do you handle Optimistic locking with NH (using FluentNHibernate for mappings), would you do
- have to push the verion number into the ViewModel? then when 开发者_运维知识库it has been submitted with an update, load in the current Model object and check the version Numbers
- Or is threre a better way (I know NH would do versioning if I Load, Edit and Update in the same session)
one extra thing, this scenario will be used on a farm, not sharing a cache
many thanks in advance
This is essentially an offline lock, which NHibernate doesn't deal with.
What you need to do is something like:
Edit Request:
- Load entity
- Push entity into ViewModel (including Version Number)
- etc...
Save Request:
- Load entity (again)
- Check Version Number from view model against entity
- if not the same show error/reconciliation options
- else update entity, etc...
精彩评论