0

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?

davymartu
  • 1,393
  • 3
  • 15
  • 34
  • What's your NHibernate ISession management strategy? – stuartd Jan 31 '13 at 16:30
  • If i understand your question, i want to develope a MVC web app with the same framework. Then i open and close my Session every request. but i'm learning and experimenting,there are another better strategy for wpf app? – davymartu Jan 31 '13 at 16:47
  • The [first level cache is kept in the Session](http://stackoverflow.com/questions/337072/what-is-first-and-second-level-caching-in-hibernate) - you might want to look into [reconnecting to the session with Merge or SaveOrUpdateCopy](http://www.codinginstinct.com/2009/11/nhibernate-feature-saveorupdatecopy.html) – stuartd Jan 31 '13 at 17:05

1 Answers1

1

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.

stuartd
  • 70,509
  • 14
  • 132
  • 163
h.alex
  • 902
  • 1
  • 8
  • 31