整理 T4 模板
我已经开始使用 T4 模板进行一些 DAL 增强。
然而,我发现它们很快就会变得笨拙,并且经常生成格式不良的代码。
我想知道是否有人对组织这些模板以及最后获得良好的格式化代码有任何好的建议。
到目前为止,我的经验是:
- 坚持现有已发布模板的功能(例如 SubSonic)
- 如果出现缩进,请尝试在最近的
#>
- 大量注释后添加一个空行。
I have begun using T4 Templates for a few DAL enhancements.
However, I find they quickly get unwieldy and often produce poorly formatted code.
I am wondering if anyone has any good tips on organising these templates, and on getting great formatted code at the end.
So far my experience is:
- Stick to what existing published templates do (e.g. SubSonic)
- If indentation is playing up attempt a blank line after the closest
#>
- Lots of comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将模板拆分为控制/数据准备逻辑和执行输出渲染的“视图”。
首先设置您需要的数据,然后用尽可能少的逻辑编写输出。
如果您可以将输出打包成可重用的块,那么您可以将它们放入方法包装器中的 <#+ #> 中。块并以一组方法调用的形式完成结构工作。
Split your templates into control/data preparation logic and 'views' that do the output rendering.
Set up the data you need first and then write the output with the most minimal logic possible.
If you can parcel up the output into reusable chunks, then you can put those in method wrappers in <#+ #> blocks and do the structural work as a set of method calls.
T4 最终结果缩进
T4 模板将按照模板中的定义格式化代码。缩进如文件本身所定义。为了获得出色的缩进最终结果,T4 模板通常会看起来很奇怪且奇怪的缩进。但代码是有颜色的,所以模板的代码通常就很好。
T4 组织
我通常有一个解决方案文件夹,其中包含所有可重用的 T4 模板(如果有很多模板,也可以将它们放入子文件夹中)。我给他们提供了
ttinclude
文件扩展名。其他的只是包括这些并放在需要的地方。T4 end result indentation
T4 templates will format code as defined in the template. Indents are as defined in the file itself. To have great indented end result, T4 templates will often look strange and odd indented. But code is coloured so template's code is usually just fine.
T4 organization
I usually have a solution folder with all reusable T4 templates (if lots of them they can be put in subfolders as well). I give them the
ttinclude
file extension. Others just include these and are put in place where needed.使用第三方格式化工具在生成后格式化代码,例如 Teleriks Just Code
Use a third party formatting tool to Format your code after generation like Teleriks Just Code