2012-01-31 97 views
2

我有查看顯示問題,答案,對答案的註釋和問題。渲染動作性能

要顯示我想用這樣的所有數據:

[HttpGet, ChildActionOnly] 
    public PartialViewResult RenderQuestion(int questionId, int page, int pageSize, string sort)//For question display 
    { 
     var question = questionsService.GetQuestion(questionId); 
     var author = userService.GetUser(question.AuthorId); 
     var commentIds = commentService.GetTopCommentIds(questionId, int.MaxValue, CommentType.Question); 
     var answerIds = answerService.GetAnswerIdsByQuestion(page, pageSize, questionId, sort); 
     var model = new QuestionModel{ Question = question, Author = author, CommentIds = commentIds, AnswerIds = answerIds} 
     return PartialView("_Question", model); 
    } 
    [HttpGet, ChildActionOnly] 
    public PartialViewResult RenderAnswer(int answerId) 
    { 
     var answer = answerService.GetAnswer(answerId); 
     var author = userService.GetUser(answer.AuthorId); 
     var commentIds = commentService.GetTopCommentIds(answerId, int.MaxValue, CommentType.Answer); 
     var model = new AnswerModel { Answer = answer, Author = author, CommentIds = commentIds}; 
     return PartialView("_Answer"); 
    } 

    [HttpGet, ChildActionOnly] 
    public PartialViewResult RenderComment(int commentId, CommentType commentType) 
    { 
     var comment = commentService.GetComment(commentId, commentType); 
     var author = userService.GetUser(comment.AuthorId); 
     var model = new CommentModel { Comment = comment, Author = author}; 
     return PartialView("_Comment"); 
    } 

而在例如用於問題,我會在循環迭代Model.AnswerIds和呼叫@{ Html.RenderAction("RenderAnswer", new {answerId}) };和Model.CommentIds呼喚我的部分景色@{ Html.RenderAction("RenderComment", new {commentId}) };

我想知道,這是一種很好的視角分解方法,並且這種方式會對通常造成的性能造成不良影響。@Html.RenderAction調用。

回答

2

不幸的是,這會導致性能不佳。 RenderAction的速度並不快。

您還將多次實例化您的控制器(可能會多次打開數據庫)。

我建議你把所有東西都放入一個專門的控制器動作中。