Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 obviously repetitive code.

like image 498
Chaddeus Avatar asked Jan 07 '11 05:01

Chaddeus


People also ask

What is Razor in MVC why it is used?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What are Razor views in MVC?

Razor is a templating engine and ASP.NET MVC has implemented a view engine which allows us to use Razor inside of an MVC application to produce HTML. However, Razor does not have any ties with ASP.NET MVC. Now, Razor Syntax is compact which minimizes the characters to be used, however it is also easy to learn.

Which is better Razor or MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

What is the difference between Razor and MVC?

A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.


1 Answers

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.

like image 158
Evan Nagle Avatar answered Nov 14 '22 23:11

Evan Nagle