NHibernate, Lazy Loadind and Session as Singleton
Im new in Nhibernate. I have application with lazy loading. I want to write method
public User GetUser(int id)
in my UserPersister class. Later, in application I want use some referenced property like User.Role or User.Address. It wan`t work if I close Session that I used to retreive user. My first idea was to create Singleton Session and I will be able to get all data then. I read some articles that it is bad idea becouse of performance and m开发者_JS百科emory leaking. Is it true? What is the solution of this problem ?
Regards Martin
Have a look at Effectus for a simple approach to WPF + NHibernate.
First of all remember that NH session != sqlconnection. Having global (singleton) session is generally not good idea even in wpf because you can reach multithread scenario sooner or later. But i definitelly wouldn't argue against with performance nonsense in your case. I would suggest you to open it for shortest possible time to complete the usecase. Perform kind of analysis where you identify usecases of your app as "sessions" with limited lifespan. For example shopping cart. You start and you finish. You can make session live until you finish such usecase. Then throw it away...
精彩评论