简体   繁体   English

在ASP.NET MVC(3.0 / Razor)中,您更喜欢视图中的多个视图或条件吗? 为什么?

[英]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 在控制器中,检查IsAuthenticated并根据该视图返回视图
  2. In the view, check IsAuthenticated and show blocks of info based on that 在视图中,选中IsAuthenticated并根据该信息显示信息块

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. 更新:假设每个视图使用布局页面和部分视图来抽象明显重复的代码。

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. 作为早产优化的表亲,PG可能同样瘫痪。 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 请参阅: 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. 具有讽刺意味的是,抽象只有在减少重复的必要性的情况下才有价值,重复只有在降低你完成任何事情的可能性时才是有害的,因此过度抽象的重复欲望与编码一样有害。一堆ON ERROR RESUME NEXT的。

I doubt that helped. 我怀疑这有帮助。 But, alas. 可惜。

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 对于一些微小的差异,if块是可以的

I prefer a single view if it's simply an "if x display y" situation. 如果它只是一个“if x display y”情况,我更喜欢单一视图。 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. 但是,减少重复的html值得对少量简单逻辑进行权衡。

I suspect the answers on this will be pretty much split down the middle because each side has its own merits. 我怀疑这个问题的答案会在中间分裂,因为每一方都有自己的优点。

我会说从一个视图开始......然后根据经过身份验证和未经身份验证的视图之间的差异有多复杂,您可以创建多个视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM