Lazy loading with NHibernate Castle Castle

Do I need to close the ISession that the Castle ISessionManager for NHibernate created? How to process transactions using these ISession ? I'm still pretty new to NHibernate.

Edit: I would like to have a lazy load, but I get this message:

Initialization [failed to lazily initialize role collection :, no session or session closed "

Here is my shared repository, which I inherit for implementing specific instances.

 [Transactional] public class Repository<TKey, TModel> : IRepository<TKey, TModel> where TKey : IComparable where TModel : class { private readonly ISessionManager _sessionManager; protected ISession Session { get { return _sessionManager.OpenSession(); } } public Repository(ISessionManager sessionManager) { _sessionManager = sessionManager; } #region IRepository<TKey,TModel> Members public virtual TModel Select(TKey key) { using (var session = _sessionManager.OpenSession()) { return session.Get<TModel>(key); } } public virtual IList<TModel> SelectWhere(Func<TModel, bool> query) { using (var session = Session) { return session.Linq<TModel>().Where(query).ToList(); } } public virtual TModel Single(Func<TModel, bool> query) { using (var session = Session) { return session.Linq<TModel>().SingleOrDefault(query); } } public virtual TModel First(Func<TModel, bool> query) { using (var session = Session) { return session.Linq<TModel>().FirstOrDefault(query); } } public virtual IList<TModel> All() { using (var session = Session) { return session.Linq<TModel>().ToList(); } } [Transaction(TransactionMode.Requires)] public virtual void Store(TModel entity) { using (var session = Session) { session.SaveOrUpdate(entity); } } [Transaction(TransactionMode.Requires)] public virtual void Store(IEnumerable<TModel> entities) { using (var session = Session) { foreach (TModel entity in entities) session.SaveOrUpdate(entity); } } [Transaction(TransactionMode.Requires)] public virtual void Remove(TModel entity) { using (var session = Session) { session.Delete(entity); } } public virtual void Remove(Func<TModel, bool> query) { IEnumerable<TModel> entities = SelectWhere(query); Remove(entities); } [Transaction(TransactionMode.Requires)] public virtual void Remove(IEnumerable<TModel> entities) { using (var session = Session) { foreach (TModel entity in entities) session.Delete(entity); } } #endregion } public class Repository<TModel> : Repository<Guid, TModel>, IRepository<TModel> where TModel : class { public Repository(ISessionManager sessionManager) : base(sessionManager) { } } public class Repository : Repository<ulong, object>, IRepository { public Repository(ISessionManager sessionManager) : base(sessionManager) { } } 

Here is an example calling this repository:

 IUserRepository userRepository = new UserRepository(); // This is actually provided by my IoC var users = userRepository.All(); foreach (var user in Users) { foreach (var picture in user.Pictures) { // I get exceptions when I do stuff like this. } } 
+5
nhibernate castle-windsor lazy-loading castle windsor-nhfacility
source share
2 answers

Yes, always ISession . See documents in ISessionManager usage .

For transactions, consider using the Automatic Transaction Facility .

SessionManager supports ATM , so it will intelligently manage ISession when necessary, taking into account transactions, even if you apparently hosted ISession .

Here's a quick and dirty sample application that uses ASP.NET MVC + Castle Automatic Transaction Facility + NHibernate

+6
source share

We use transactions using operators to process the order.

 public void Save<K>(K entity) { if (entity == null) throw new ArgumentNullException("item", "The item being saved cannot be null."); using (ISession session = GetSession()) { using (ITransaction tx = session.BeginTransaction()) { session.SaveOrUpdate(entity); tx.Commit(); } } } 

I still get a lazy loading error if I get access to objects after making changes to the same action. I fixed the error without accessing the objects after saving. Here's an explanation: NHibernate.LazyInitializationException

I believe that this is due to the incorrect preservation of the hierarchy. I have not tested it, but maybe saving the parent objects if you want to access them will fix the problem. Just putting the information I need to access after saving into local variables before saving seems to fix my problem.

0
source share

All Articles