Is it my understanding that Helper methods are really the place where you can do the hard core logic that we would have done in lets say custom controls in ASP.NET? For instance I work for a .com which uses classic ASP.NET. The nature of our site is VERY complex, so we reuse and render different forms for thousands of products. Every product could have a different configuration form. We have a very complext RenderForm.cs custom server control that performs all the logic. Based on some configuration settings from a table in the DB, it says ok, for Product 1123 it reads the setup (that our users confugure form our internal admin system) and takes that and spits out the dynamic form (using literal controls and what not) to the p age.
是我的理解是Helper方法真的是你可以做的核心逻辑,我们可以在ASP.NET中使用自定义控件吗?例如,我为使用经典ASP.NET的.com工作。我们网站的性质非常复杂,因此我们重复使用并为数千种产品呈现不同的形式。每个产品都可以有不同的配置形式。我们有一个非常完整的RenderForm.cs自定义服务器控件,可以执行所有逻辑。基于数据库中表格的一些配置设置,它说没问题,对于产品1123,它读取设置(我们的用户从我们的内部管理系统中构思)并接受并吐出动态表单(使用文字控件,而不是到了p年龄。
So I'm thinking MVC now. Yea yea, it's all done in the View. Well partially. You're still going to have a need to have some custom logic in some .cs where it's not all embeded in your view. That would be foolish to think you're not going to have some class that will spit out some HTML..like some very hard core extensive helper methods.
所以我现在正在考虑MVC。是的,这一切都在视图中完成。好吧,部分。您仍然需要在某些.cs中使用某些自定义逻辑,而这些逻辑并非全部嵌入到您的视图中。如果认为你不会有一些会吐出一些HTML的类,那将是愚蠢的。就像一些非常硬的核心广泛帮助方法。
So my question is, are helper methods or class where you now do your custom server control type of logic? it's basically kind of the same concept in that you need a place to put your "hard core" HTML rendering logic in some class other than a controller. Your controller is not responsible for rendering. So helper methods I guess are the so-called custom server controls in a way that I have in classic ASP.NET, figuratively speaking. I just need a yes or now on is the consensus that helper methods is the place to do all my hard core reusable logic that spits out html to the page and where I can embed custom controls into my view? Looks like it.
所以我的问题是,你现在做自定义服务器控制类型逻辑的辅助方法或类吗?它基本上是一个相同的概念,因为你需要一个地方把你的“硬核”HTML渲染逻辑放在除控制器之外的某些类中。您的控制器不负责渲染。因此,我认为辅助方法就是所谓的自定义服务器控件,就像我在经典ASP.NET中一样,比喻说。我只需要一个是或者现在就是这样的共识,即帮助方法是我所有硬核可重用逻辑的地方,它将html吐出到页面,我可以在哪里嵌入自定义控件到我的视图中?看起来像。
"Helpers are essentially static classes, designed to contain the UI logic that otherwise clutters up your UI. Think of these as UI utilities." link text
“助手本质上是静态类,旨在包含UI逻辑,否则会使UI混乱。将这些视为UI实用程序。”链接文字
2 个解决方案
#1
1
Yes, that is right on. If you do it right, you will start with the HTML helpers that MVC gives you, and you will gradually build up your own set of helpers that do even more and more for your specific project. You can get to the point where your view has only a few lines of code, which say something like, "Render entire view for Product 1123". The helpers will become your own "language" of renderers specific to your project, and you will be applying configuration, validation and everything else in a very DRY (Don-t Repeat Yourself) manner. It's phenomenal.
是的,这是正确的。如果你做得对,你将从MVC为你提供的HTML助手开始,然后逐步建立你自己的帮助器组,为你的特定项目做更多的帮助。您可以到达视图只有几行代码的位置,例如“渲染产品1123的整个视图”。帮助程序将成为您自己的项目特定渲染器“语言”,您将以非常干燥(不要重复自己)的方式应用配置,验证和其他所有内容。这是非凡的。
Update: Of course, only presentation stuff should go in your helpers. The goal is to stay DRY in your views. You still need to be careful to put into your ViewModels the things that belong in the ViewModels.
更新:当然,只有演示文稿的内容应该放在你的助手中。目标是在您的观点中保持干净。您仍然需要小心将ViewModel中的内容放入ViewModel中。
#2
0
I would say "no"... or rather "only where you have to". More often than not, you can instead do the logic in the Controller (or a Service) and end up passing all the data required back to the View in ViewData. Somtimes this will mean multiple Views from one ControllerAction, less often it will mean logic in your View, and occasionally it means HtmlHelpers.
我会说“不”......或者说“只有你必须去的地方”。通常,您可以在Controller(或服务)中执行逻辑,最终将所需的所有数据传递回ViewData中的View。有时候这意味着来自一个ControllerAction的多个视图,更少的意思是你的视图中的逻辑,有时它意味着HtmlHelpers。
When you decide to use Helpers, it should be with the consideration that this means generated markup that won't be... well, in your markup. If you have (or later hire) a designer, that can be a problem. Or if you need to make a minor change to your layout, where do you go first? Your View or your Helpers?
当你决定使用Helpers时,应该考虑到这意味着生成的标记在你的标记中不会......好吧。如果你有(或后来雇用)设计师,那可能是个问题。或者,如果您需要对布局进行微小更改,那么您先去哪里?您的观点还是您的助手?
[Edit] Also should ask yourself this: where is my code more easily unit tested? In a Service class which is just handing back View Data, or in a class that builds entire chunks of HTML and returns them as a String? If you're using TagBuilder, as you probably should be, then any change in the implementation of TagBuilder (even a change of whitespace handling) will break tests on a Helper without your code changing.
[编辑]也应该问问自己:我的代码在哪里更容易进行单元测试?在一个只传递View Data的Service类中,或者在构建整个HTML块并将它们作为String返回的类中?如果您正在使用TagBuilder,那么TagBuilder的实现中的任何更改(甚至是空白处理的更改)都会破坏Helper上的测试而不会更改代码。
I'm not saying "don't use Helpers", I'm saying "don't abuse Helpers".
我不是说“不要使用助手”,我说的是“不要滥用助手”。
#1
1
Yes, that is right on. If you do it right, you will start with the HTML helpers that MVC gives you, and you will gradually build up your own set of helpers that do even more and more for your specific project. You can get to the point where your view has only a few lines of code, which say something like, "Render entire view for Product 1123". The helpers will become your own "language" of renderers specific to your project, and you will be applying configuration, validation and everything else in a very DRY (Don-t Repeat Yourself) manner. It's phenomenal.
是的,这是正确的。如果你做得对,你将从MVC为你提供的HTML助手开始,然后逐步建立你自己的帮助器组,为你的特定项目做更多的帮助。您可以到达视图只有几行代码的位置,例如“渲染产品1123的整个视图”。帮助程序将成为您自己的项目特定渲染器“语言”,您将以非常干燥(不要重复自己)的方式应用配置,验证和其他所有内容。这是非凡的。
Update: Of course, only presentation stuff should go in your helpers. The goal is to stay DRY in your views. You still need to be careful to put into your ViewModels the things that belong in the ViewModels.
更新:当然,只有演示文稿的内容应该放在你的助手中。目标是在您的观点中保持干净。您仍然需要小心将ViewModel中的内容放入ViewModel中。
#2
0
I would say "no"... or rather "only where you have to". More often than not, you can instead do the logic in the Controller (or a Service) and end up passing all the data required back to the View in ViewData. Somtimes this will mean multiple Views from one ControllerAction, less often it will mean logic in your View, and occasionally it means HtmlHelpers.
我会说“不”......或者说“只有你必须去的地方”。通常,您可以在Controller(或服务)中执行逻辑,最终将所需的所有数据传递回ViewData中的View。有时候这意味着来自一个ControllerAction的多个视图,更少的意思是你的视图中的逻辑,有时它意味着HtmlHelpers。
When you decide to use Helpers, it should be with the consideration that this means generated markup that won't be... well, in your markup. If you have (or later hire) a designer, that can be a problem. Or if you need to make a minor change to your layout, where do you go first? Your View or your Helpers?
当你决定使用Helpers时,应该考虑到这意味着生成的标记在你的标记中不会......好吧。如果你有(或后来雇用)设计师,那可能是个问题。或者,如果您需要对布局进行微小更改,那么您先去哪里?您的观点还是您的助手?
[Edit] Also should ask yourself this: where is my code more easily unit tested? In a Service class which is just handing back View Data, or in a class that builds entire chunks of HTML and returns them as a String? If you're using TagBuilder, as you probably should be, then any change in the implementation of TagBuilder (even a change of whitespace handling) will break tests on a Helper without your code changing.
[编辑]也应该问问自己:我的代码在哪里更容易进行单元测试?在一个只传递View Data的Service类中,或者在构建整个HTML块并将它们作为String返回的类中?如果您正在使用TagBuilder,那么TagBuilder的实现中的任何更改(甚至是空白处理的更改)都会破坏Helper上的测试而不会更改代码。
I'm not saying "don't use Helpers", I'm saying "don't abuse Helpers".
我不是说“不要使用助手”,我说的是“不要滥用助手”。