您想要完全用代码创建视图的 ASP.NET MVC 视图引擎吗?
最近,我创建了一个视图引擎的尖峰,其中视图是普通类,内容是通过使用有趣的 using
范围块创建的。
该代码以及一个简单的示例网站可从 http://code.google 获取。 com/p/sharp-view-engine/
在这里我想听听您对这个想法的看法。是完全奇怪还是有人喜欢它?
Recently I created a spike of a view engine, in which views are plain classes, and the content is created by using funny using
-scope blocks.
The code together with a simple sample site is available at http://code.google.com/p/sharp-view-engine/
Here I'd like to hear your opinions regarding such an idea. Is it completely weird or maybe someone likes it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我实际上不喜欢那样。
我可以同意 DSL(例如解析器组合器或用于在数据上下文中生成 XML 节点),但在这种情况下,我认为放置了太多在代码中。最终,这只会使边界变得复杂并导致代码难以维护。 (您已经可以执行相同的操作,但只需使用“标准”Web 控件即可更加详细。您始终可以在 C# 中使用
{subblock}
来限制变量范围。)我更喜欢使用的方法是带有绑定的模板(但没有“模板中的代码”)。这使得“设计师”(希望不是我,或下一个人)可以轻松地编辑他们认为合适的视图布局。但是,核心逻辑(可用控件和绑定)保留在代码中 - 整洁。 (模板的另一个优点是,如果它们位于外部,则不需要为每一个小变化都重新编译。)
简单性和可维护性就像……禅宗。
I would actually not like that.
I can agree with DSLs (such as a Parser-Combinator or for generating XML Nodes in a data-context), but in this case I think that too much is being put in code that. And, in the end, this just complicates boundaries and leads to hard-to-maintain code. (You can already do the same, but with more verbosity just using the "standard" Web Controls. You can always use
{subblock}
in C# to limit a variables scope.)The approach I prefer to use is templates with bindings (but no "code in templates"). That makes it easy for the "designer" (hopefully not me, or the next person to come along and) edit the layout of the view how they see fit. However, the core logic (the available controls and bindings) are kept in the code -- uncluttered. (Another advantage with the templates is that if they externally housed they do not require a recompile for every little change.)
Simplicity and maintainability are like ... zen.
我想说,这是一个极端的有趣想法。在我的商店中,除了布局之外,我们几乎所有内容都使用 html 约定。我们项目中唯一真正的 html 是 Spark 母版页。为了生成内容本身,我们使用一个约定引擎来生成语义 html 模型。 (我们使用 FubuMVC 中的 HtmlTags 库来构建语义模型。)
渲染多行文本框的示例约定如下所示:
这些约定通过在视图模型上的反射来触发(或者我们可以从辅助方法手动调用它们) )。输出被渲染(通过 ToString())到母版页的内容部分。我们开玩笑说很快我们甚至不需要视图引擎。
ps 这是我们处理嵌套的方式。 (您的 using 块看起来很混乱!)
Nest() 是一种扩展方法,它仅采用 HtmlTag 的 params 数组并将它们附加到父级的子级集合中。
This is an interesting idea taken to the extreme I'd say. At my shop we're using html conventions for pretty much everything except our layout. The only real html we have in the project is our Spark master page. For generating the content itself we use a convention engine that that spits out a semantic html model. (We're using the HtmlTags library from FubuMVC to build the semantic model.)
An example convention for rendering a multiline text box looks like:
These conventions get triggered from reflecting on the view model (or we can manually call them from a helper method). The output is rendered (via ToString()) into the content section of our master page. We're joking that pretty soon we won't even need a view engine.
ps here's how we handle nesting. (Your using blocks look cluttered!)
Nest() is an extension method that simply takes a params array of HtmlTag and appends them to the parent's children collection.