ASP.MVC: Implementing a non-templated view engine?

拥有回忆 提交于 2019-12-22 10:59:42

问题


I'm fairly new to ASP.MVC. For our new Web application I'm considering between two options for view engines:

  1. Using some of the available view engines (the default one, NVelocity, Brail, etc.) as a primary view code generator.
  2. Implementing an application-specific C# DSL for generating HTML code so that the main bulk of the view code is stored in C# classes (using some sort of smart HTML generation like the one described by Jimmy Bogard).

The idea behind 2. would be to reduce the need for writing direct HTML code each time a new view is needed, but I'm not sure if this is a good approach. One drawback of it is that it would not be possible to change the generated code without recompiling the project.

What do you think?


回答1:


I've seen approaches like this used in other projects, and in general they're more trouble than they're worth. The flexibility you lose is just too high a price to pay for the automatic-ness of the generation phase. Think of how often requirements turn out to apply just to one special case, and now imagine how you'd have to handle that here.

Additionally, if your views are causing you that much pain, I would suggest you might not be using views correctly. You should see very little repetition in your views across the site. Common pieces should be refactored into separate chunks and pulled in from there, for example.



来源:https://stackoverflow.com/questions/819667/asp-mvc-implementing-a-non-templated-view-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!