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.
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.
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.
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.
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.
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.
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:
- I create my first View -- no partials.
- I create my second View -- no partials.
- I create my third View by abstracting pieces of code from the first and second View into reusable partials.
- 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With