//Private private Comment AddComment(CommentInputParrams newValues) { using (ISession session = NHibernateSession.OpenSession()) { try { HttpContextWrapper context = (HttpContextWrapper)Request.Properties["MS_HttpContext"]; Comment parentComment = session.QueryOver<Comment>().Where(p => p.CommentId == newValues.CommentId).List()[0]; IEnumerable<Comment> forLftOnlyUpdate = session.QueryOver<Comment>() .Where(p => p.lft >= parentComment.rgt) .And(p => p.rgt < parentComment.rgt) .List(); IEnumerable<Comment> forRgtOnlyUpdate = session.QueryOver<Comment>() .Where(p => p.rgt >= parentComment.rgt) .And(p => p.lft < parentComment.rgt) .List(); IEnumerable<Comment> forBothUpdate = session.QueryOver<Comment>() .Where(p => p.rgt >= parentComment.rgt) .And(p => p.lft >= parentComment.rgt) .List(); Comment newComment = new Comment(); newComment.Text = newValues.Text; newComment.ParentCommentId = newValues.CommentId; newComment.ReplayToUser = parentComment.Creator; newComment.lft = parentComment.rgt; newComment.rgt = parentComment.rgt + 1; newComment.indent = parentComment.indent + 1; newComment.ParentCommenterName = parentComment.CommenterName; newComment.CreationDate = DateTime.Now; if (context.User.Identity.IsAuthenticated) { newComment.Creator = session.QueryOver<UserProfile>().Where(u => u.UserName == context.User.Identity.Name).List()[0]; newComment.CommenterName = context.User.Identity.Name; } else { newComment.Creator = null; newComment.CommenterName = newValues.Name; } using (var tx = session.BeginTransaction()) { // Enable batch updates in nHibernate congfig (performance hint) // ADO.Net is faster on this matter because we can use update statment on multiple rows // Maybe this can be accomplished through nHibernate but I have no time to read best practises in such short notice foreach (Comment cc in forLftOnlyUpdate) { cc.lft += 2; session.SaveOrUpdate(cc); } foreach (Comment cc in forRgtOnlyUpdate) { cc.rgt += 2; session.SaveOrUpdate(cc); } foreach (Comment cc in forBothUpdate) { cc.lft += 2; cc.rgt += 2; session.SaveOrUpdate(cc); } session.SaveOrUpdate(newComment); tx.Commit(); } return newComment; } catch (Exception e) { // show error code return null; } } }