您会考虑使用 CodeSmith 这样的代码生成器来编写哪些代码?
我使用 CodeSmith 作为 PLINQO 模板,从我的数据库对象构建我的 DAL;效果很好!
我相信这是代码生成器应用程序的主要用途,但我很好奇......您还会考虑使用代码生成器来执行哪些其他代码?您是否有经常使用的 CodeSmith 模板(如果有,它的作用是什么)?
I use CodeSmith for the PLINQO templates, to build my DAL from my DB objects; it works great!
I believe that's the primary use of code generator apps, but I'm curious... what other code would you consider using a code generator for? Do you have any CodeSmith templates that you use frequently (if so, what does it do)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有使用过 CodeSmith,但我已经完成了相当多的代码生成。值得注意的是,我为 WiMAX 系统编写了大部分配置管理 (CM) 系统,其中为 3 个不同的平台生成了 CM 代码。唯一的区别是每个平台的 CM 模型。
该模型采用自定义的领域特定语言 (DSL),我们为其构建了一个解析器。该语言是一种基本的容器/元素样式,其中容器可以嵌套并具有标识符,元素是预定义的类型。文档是元素和容器的属性。您可以将 Lua 片段添加到元素和容器定义中以进行语义验证(例如,该值位于正确的位置)范围,如果它是 IP 地址,则它是否在其他地方定义的 CIDR 范围内,等等)。
解析器生成一个语法树,然后我们将其推送到模板中。模板语言是 StringTemplate 的部分 C 实现。我们用它来生成:
现在回想起来,我应该直接使用 Lua 作为 DSL。它会更加冗长,但是如果已经有了解析器并且有很多可供我使用的 Lua 模板选项,将会节省大量的开发工作。
对于具有重复结构和明确定义这些事情需要做什么的规则的事物来说,代码生成可能是一件很棒的事情。
I haven't used CodeSmith, but I've done a fair bit of code generation. Noteably I wrote most of a configuration management (CM) system for a WiMAX system, where the CM code was generated for 3 different platforms. The only difference was the CM model for each platform.
The model was in a custom Domain Specific Language (DSL) that we built had a parser for. The language was a basic container/element style where containers could nest and have an identifier, and elements were of pre-defined types. Documentation was an attribute of elements and containers. You could add Lua snippets to the element and container definitions to do semantic validation (e.g., the value is in the correct range, if it's an IP address is it in a CIDR range defined elsewhere, etc.).
The parser generated a syntax tree that we then pushed at templates. The template language was a partial C implementation of StringTemplate. We used it to generate:
In retrospect, I should have simply used Lua directly as the DSL. It would have been more verbose, but having the parser already there and lots of Lua templating choices available to me would have saved a lot of development effort.
For things that have a repetivie structure and well defined rules about what those things need to do, code generation can be a wonderful thing.