tags:

views:

149

answers:

3

I am trying to design my render partial. within one render partial i have 30 other render partial functions. I am rendering one item at a time. for example, I am going to put these items in their separate renderpartial function:

  1. time
  2. username
  3. title
  4. avatar
  5. tags
  6. score
  7. user information -- each element has its separate render partial
  8. and so on

so i am going to organize the render partial functions because there is going to be so many that is going to be on one page. Is this bad practice?

A: 

Very possibly. If you can, look for things they all have in common and try to create a generic function.

Malfist
+1  A: 

I think it depends: How many lines are in your partial view? There's no point to refactoring a one-line partial view, which is what it sounds like you're doing. If I misunderstand, then it would be helpful if you could post some sample code.

I can't picture why you would want to encapsulate a single line without seeing the line you want to put in a partial view. I'd be careful of premature optimization here. e.g. You probably want something like 3 10-line partial views instead of 30 1-liners.

As far as performance goes, I doubt you'd see a serious performance hit unless you're deploying on a particularly ancient server or getting very high traffic. So yes, it will definitely be slower than putting everything in a single view because you're calling a subroutine, but I don't think it'll make a real difference to your users.

Josh Kodroff
Yes, you are correct. My partial views are only one line. But they will be used everywhere in my application. Will doing this hurt performance?
Luke101
I expanded my answer.
Josh Kodroff
+1  A: 

Instead of partial views I tend to favour HTML helpers. eg:

public static string SidebarBoxEnd(this HtmlHelper helper) { return ""; }

If you're not already familiar and comfortable with the concept there's a good article here: http://stephenwalther.com/blog/archive/2009/03/03/chapter-6-understanding-html-helpers.aspx

I prefer that method because I also tend to use a lot of graphically complex layouts, and HTML Helpers seem to give the cleanest (thus most easily maintainable) view code. eg:

<%= Html.SidebarBoxBegin("Mailing List") %> <% Html.BeginForm("Subscribe","MailingList",FormMethod.Post); %> <%= Html.Button("Join Mailing List","submit","joinmailinglist") %> <% Html.EndForm(); %> <%= Html.SidebarBoxEnd() %>

as opposed to having several times as much code in div and class tags alone.

FerretallicA
Which one do you think will perform better. The renderpartial or html helpers?
Luke101
The only instance where HTMLHelper will be noticeably slower than RenderPartial is if you're running in debug mode, which should be a non-issue for the majority of production environments. Outside of debug mode the performance of HTMLHelper leaves little to be desired.
FerretallicA