From fec758bb5c431d3d73f0854f6f4acdcb9c63c077 Mon Sep 17 00:00:00 2001 From: Bertrand Le Roy Date: Mon, 7 Oct 2013 12:39:01 -0700 Subject: [PATCH] Avoiding select n+1 when comments shapes are hidden from a list. --- .../Drivers/CommentsPartDriver.cs | 58 +++++++++++++++---- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Comments/Drivers/CommentsPartDriver.cs b/src/Orchard.Web/Modules/Orchard.Comments/Drivers/CommentsPartDriver.cs index 7c9999643..df95cefe4 100644 --- a/src/Orchard.Web/Modules/Orchard.Comments/Drivers/CommentsPartDriver.cs +++ b/src/Orchard.Web/Modules/Orchard.Comments/Drivers/CommentsPartDriver.cs @@ -22,20 +22,24 @@ namespace Orchard.Comments.Drivers { } protected override DriverResult Display(CommentsPart part, string displayType, dynamic shapeHelper) { - if (part.CommentsShown == false) - return null; - - var commentsForCommentedContent = _commentService.GetCommentsForCommentedContent(part.ContentItem.Id); - var pendingCount = new Lazy(() => commentsForCommentedContent.Where(x => x.Status == CommentStatus.Pending).Count()); - var approvedCount = new Lazy(() => commentsForCommentedContent.Where(x => x.Status == CommentStatus.Approved).Count()); return Combined( ContentShape("Parts_ListOfComments", () => { + if (part.CommentsShown == false) + return null; + // create a hierarchy of shapes var firstLevelShapes = new List(); var allShapes = new Dictionary(); - var comments = commentsForCommentedContent.Where(x => x.Status == CommentStatus.Approved).OrderBy(x => x.Position).List().ToList(); + var comments = _commentService + .GetCommentsForCommentedContent(part.ContentItem.Id) + .Where(x => x.Status == CommentStatus.Approved) + .OrderBy(x => x.Position) + .List() + .ToList(); + + var approvedCount = comments.Count(); foreach (var item in comments) { var shape = shapeHelper.Parts_Comment(ContentPart: item, ContentItem: item.ContentItem); @@ -54,10 +58,14 @@ namespace Orchard.Comments.Drivers { var list = shapeHelper.List(Items: firstLevelShapes); - return shapeHelper.Parts_ListOfComments(List: list, CommentCount: approvedCount.Value); + return shapeHelper.Parts_ListOfComments( + List: list, + CommentCount: approvedCount); }), ContentShape("Parts_CommentForm", () => { + if (part.CommentsShown == false) + return null; var newComment = _contentManager.New("Comment"); if (newComment.Has()) newComment.As().CommentedOn = part.Id; @@ -66,9 +74,39 @@ namespace Orchard.Comments.Drivers { return shapeHelper.Parts_CommentForm(EditorShape: editorShape); }), ContentShape("Parts_Comments_Count", - () => shapeHelper.Parts_Comments_Count(CommentCount: approvedCount.Value, PendingCount: pendingCount.Value)), + () => { + if (part.CommentsShown == false) + return null; + + var comments = _commentService + .GetCommentsForCommentedContent(part.ContentItem.Id); + var approvedCount = comments + .Where(x => x.Status == CommentStatus.Approved) + .Count(); + var pendingCount = comments + .Where(x => x.Status == CommentStatus.Pending) + .Count(); + + return shapeHelper.Parts_Comments_Count( + CommentCount: approvedCount, + PendingCount: pendingCount); + }), ContentShape("Parts_Comments_Count_SummaryAdmin", - () => shapeHelper.Parts_Comments_Count_SummaryAdmin(CommentCount: approvedCount.Value, PendingCount: pendingCount.Value)) + () => { + + var comments = _commentService + .GetCommentsForCommentedContent(part.ContentItem.Id); + var approvedCount = comments + .Where(x => x.Status == CommentStatus.Approved) + .Count(); + var pendingCount = comments + .Where(x => x.Status == CommentStatus.Pending) + .Count(); + + return shapeHelper.Parts_Comments_Count_SummaryAdmin( + CommentCount: approvedCount, + PendingCount: pendingCount); + }) ); }