Reputation: 1443
i need help: I'm a beginner with Nhibernate. I have created a wpf application that load a datagrid binded with an observable collection. This collection is loaded with repository pattern and Nhibernate querying database. I want to modify this collection with UI (edit, add, delete).
When i click to my save button i want to persist my changes to db table. I read nhibernate documentation and i learn that there are 2 level of cache, my idea is to modify objects in first level cache, and when I am sure of my changes i want to persist. there are some best practices for doing this?
How to mark for deletion or update an object and delete or update it after "save changes" click?
Upvotes: 0
Views: 259
Reputation: 902
This should be an interesting read: Building a Desktop To-Do Application with NHibernate
Basically, you should use the ISession
object's methods, and do operations inside a transaction, i.e. ISession.BeginTransaction()
It depends on how you get your entities. If they are root entities, e.g. employee then when you delete an entity from the grid, you should keep track of these deleted entities and call delete on all of them. You should also keep track of the added entities.
Then basically what you are left with are the updated entities. NH keeps track of the state and knows if an entity was modified.
We have ISession.Save/Update/Delete.
When you have done this for every modified entity, call Commit on the transaction. This will save the changes to the database.
If your entities are not roots, but e.g. are employees addresses, then it will be enough to call save on the employee - if your mappings are correct.
Upvotes: 1