渲染C#对象为Html [英] Rendering C# Objects to Html
问题描述
我们有一大堆这应该呈现为HTML格式,这说明他们在一个弹出窗口细节域的实体。
We have bunch of Domain Entities which should be rendered to an html format, which shows their detail in a pop up window.
我会很高兴做的事像这样的:
I would be glad to do something like this:
Product product = new Product(...);
product.ToHtml(); // or: HtmlRenderer.Render(Product);
但我的主要问题是如何从背后做这个东西。
我有3个不同的答案:
but my main problem is how to do this stuff from behind. I have 3 different answers:
1。渲染通过代码:
我可以简单地写我的渲染HTML中的ToHtml方法(C#)代码 - 问题是,它的太的静态。如果您想对头部移动一点点,你应该改变代码的中间。
此外,它是很难阅读在C#中的HTML缩进。
I can simply write my code for rendering the Html inside the ToHtml Method (C#) - the problem it is that it is too static. if you would like to move a little bit the header to the middle you should change code. moreover, it is very hard to read Html indentation in C#.
2。使用XSL:
XSL文件可以轻松地管理HTML模板,并使用XSLT,我可以将XML文件到文件的正确的地方。
解析器已经被别人写的(只需要学习语法)
**为此,我们需要每一个对象可以序列化到XML。如果对象改变 - > XML将被改变 - >的XSLT需要进行变过
**这也会给我缩进HTML选项很容易,例如:添加CSS功能and\or更改HTML设计
XSL Files can easily manage the Html template and using XSLT I can transform XML file to the right place of the documents. the parser already written by someone else (just need to learn the syntax) ** for this we will need that each object could serialize to Xml. and if the object changed -> the Xml will be changed --> the xslt need to be changed too ** this will also give me the option to indent the html easily for example: adding css capabilities and\or change the html design
3。使用其他模板引擎:
写我自己的C# - > HTML模板引擎,它会读取文件中的模板(* .template),并插入在使用反射的模板合适的地方合适的物业。
**在这个解决方案,我们有我们能想到的,例如许多问题:语法应该是怎么样的?
是这件事情好吗?
%名称%%说明%
,以及我们如何处理数组?
**也许我们可以使用现有的发动机(抄网或T4-模板)?
Write my own C# -> Html template Engine so it will read the template from file (*.template) and will insert the right property in the right place of the template using reflection. ** in this solution we have many issues that we can think of, for example: how the syntax should be like? is this thing ok? %Name% %Description% and how we can handle arrays? ** maybe we can use an existing engine (Brail or T4-Templating)?
你喜欢什么?
你知道一个很好的引擎?
现在我倾向于第二种方案,但它会很慢。
What do you prefer? do you know a good engine? for now I prefer the second solution, but it gonna be very slow.
感谢
推荐答案
我不能约翰Feminella同意。集成的HTML渲染直接进入你的领域中的实体是一个完全任意和外部关注您的域的可怕的污染。就像约翰说,你会做你的实体很脆通过这样做,并打破两者的这些重要的规则:关注和单一职责的分离
I can't agree more with John Feminella. Integrating Html rendering directly into your domain entities is a horrid pollution of your domain with a completely arbitrary and external concern. Like John said, you will make your entities very brittle by doing that, and break both of those critical rules: Separation of Concerns and Single Responsibility.
这是你给的选择,#3是最接近的适当的方法来做到这一点。你不必编写自己的模板引擎。有在网上大量免费的,现成的模板引擎,将做的工作超过充分(NVelocity,StringTemplate的,抄网,等等,等等)
From the choices you gave, #3 is the closest to an appropriate way to do it. You need not write your own template engine. There are plenty free and ready-made template engines on the net that will do the job more than adequately (NVelocity, StringTemplate, Brail, etc. etc.)
请渲染它属于哪里......在演示文稿/视图,不污染水平较高的关注你的域名。
Keep rendering where it belongs...in your presentation/view, and don't pollute your domain with higher level concerns.
这篇关于渲染C#对象为Html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!