开发者

In ASP.NET MVC (3.0/Razor), do you prefer multiple views, or conditionals within views? Why?

For my new web app, I'm debating on using multiple views, or conditionals within views.

An example scenario would be showing different info to users who are authenticated vs non-authenticated. This could be handled a couple ways.

  1. In the controller, check IsAuthenticated and return a view based on that
  2. In the view, check IsAuthenticated and show blocks of info based on that

Pros of multiple views: Smaller, less complicated view - next to no logic in the view

Pros of single views: less view files to maintain

The obvious cons are the opposites of the pros: more files to maintain or more complicated view files.

Which do you prefer? Why? Any pros/cons I haven't outlined here?

Update: Assume each view uses a layout page and partial views to abstract the obvi开发者_高级运维ously repetitive code.


This sounds like a nice venue to discuss the merits of avoiding premature generalization. As the cousin to premature optimization, PG can be just as crippling. I say this because I often prematurely generalize and it tends to dissuade the ladies from flirting with me, laughing at my hilarious jokes, etc.

See: http://ryanfarley.com/blog/archive/2004/04/30/570.aspx

My general rule of thumb is this:

Repeat yourself twice.
When you're about to repeat yourself a third time, create an abstraction

I tend to follow this principle in my Views and my Partials:

  1. I create my first View -- no partials.
  2. I create my second View -- no partials.
  3. I create my third View by abstracting pieces of code from the first and second View into reusable partials.
  4. I repeat until the Mountain Dew is all gone.

Though my answer to your question may seem overt, I think the point I'm trying to make is that, as developers, we tend to enjoy wasting a great deal of time contemplating the different ways that we can abstract away more and more layers from our individuated instantiations. Ironically, an abstraction is only valuable insofar as it reduces the necessity of repetition, and repetition is harmful only insofar as it reduces the likeliness that you'll accomplish anything, so a repetitive desire to over-abstract is just as detrimental as coding with a bunch of ON ERROR RESUME NEXT's.

I doubt that helped. But, alas.


I prefer a single view if it's simply an "if x display y" situation. Anything more than that and it can get out of control pretty easily. Reducing the duplicate html is worth the tradeoff of a small amount of simple logic, though.

I suspect the answers on this will be pretty much split down the middle because each side has its own merits.


i'd say it depends on how different the 2 scenarios are. if it's a major difference or a difference, do a separate view. if it's a difference that appears on multiple pages (like showing login controls vs. a signout button), make it into a separate partialview. for a couple of tiny differences, an if block is ok


I would say start with a single view...then depending on how complicated the difference between authenticated and unauthenticated views get, you can create multiple views.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜