nokiola
08-29-2006, 10:21 PM
Hi,
Following the SessionScope example from CastleProject's ActiveRecord and inspired by the OpenSessionInViewModule, I have coded a small extension to Spring.Data.Orm.NHibernate.
The new class is called SessionScope and is located Spring.Data.Orm.NHibernate.Support namespace. The SessionScope class purpose is to augment performance by caching the session, thus avoiding too much opens/flushes/closes. By embedding your code in "using SessionScope" you obtain a single session to do all your data access operation. NHibernate lazy loading available in OpenSessionInViewModule for web apps is now possible. This way you can escape the inconvenient NHibernate.LazyInitializationException received due to the fact that the session was closed before the lazy object(s) have been initialized from database.
The session is now more long lived than a transaction which is useful for unit testing.
The code for this class is attached to this post.
If someone finds it useful, please use it freely and hapilly; maybe someday will make it into Spring.Data.Orm.NHibernate official project.
Example usage:
using (new SessionScope())
{
IAccountServices accountServices = ctx["AccountServices"] as IAccountServices;
Account account = accountServices.GetAccount(20);
if (account != null)
{
account.Name = "Modified name";
accountServices.UpdateAccount(account);
}
}
Note: UpdateAccount and GetAccount are marked with [Transaction] attribute and demarcate two transactions in the same session.
Looking forward for your feedback.
Regards,
Robert
Following the SessionScope example from CastleProject's ActiveRecord and inspired by the OpenSessionInViewModule, I have coded a small extension to Spring.Data.Orm.NHibernate.
The new class is called SessionScope and is located Spring.Data.Orm.NHibernate.Support namespace. The SessionScope class purpose is to augment performance by caching the session, thus avoiding too much opens/flushes/closes. By embedding your code in "using SessionScope" you obtain a single session to do all your data access operation. NHibernate lazy loading available in OpenSessionInViewModule for web apps is now possible. This way you can escape the inconvenient NHibernate.LazyInitializationException received due to the fact that the session was closed before the lazy object(s) have been initialized from database.
The session is now more long lived than a transaction which is useful for unit testing.
The code for this class is attached to this post.
If someone finds it useful, please use it freely and hapilly; maybe someday will make it into Spring.Data.Orm.NHibernate official project.
Example usage:
using (new SessionScope())
{
IAccountServices accountServices = ctx["AccountServices"] as IAccountServices;
Account account = accountServices.GetAccount(20);
if (account != null)
{
account.Name = "Modified name";
accountServices.UpdateAccount(account);
}
}
Note: UpdateAccount and GetAccount are marked with [Transaction] attribute and demarcate two transactions in the same session.
Looking forward for your feedback.
Regards,
Robert