如何通过 C# 定义 DSL
对于一个夜间项目,我想编写一个可在 .NET 应用程序中使用的验证组件,以对对象、输入参数和后置条件进行通常且繁琐的验证。
我的第一个想法是将所有这些验证设置逻辑转储到 XML 配置文件中,并为希望在代码中使用它的人们提供一个流畅的界面。
因为我想提供一些实际可用的东西,所以我考虑提供一种专门的 DSL(领域特定语言)。问题是我应该使用什么工具来做到这一点?
我考虑过使用正则表达式手动解析它。但就我个人而言,我想要一些更……有用的东西。
那么你有什么建议呢?
For a little night project I would like to write a validation component that could be used in .NET application to do the usual and tedious validation of object, input parameters and post conditions.
My first idea was to dump all this validation setup logic into a XML configuation file and provide a liquid interface for the people that would like to have it in code.
Because I would like to deliver something that is actually usable I thought about providing a specialized DSL (domain specific language). The question is what tools should I use to do this?
I thought about parsing it by hand using regex. But personally I would like to have something more...usable.
So what would you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
听起来您正在谈论实施 .Net 4.0 的功能之一,代码契约。
所以我想我推荐的工具是 VS.Net 2010。
It sounds like you're talking about implementing one of .Net 4.0's features, code contracts.
So I guess my recommended tool would be VS.Net 2010.
如果您专门关注 DSL,请查看 ANTLR 项目。过去我们在我的公司使用它非常成功。
If you're looking specifically at a DSL, have a look at the ANTLR project. We've used it at my company quite successfully in the past.
DSl 的问题是它们很少在孤立时有效。为了有助于编写真正的软件,您确实需要能够将 DSL 嵌入到宿主语言中。例如,比较 Linq 的工作方式与直接 SQL 的工作方式。另一个很好的例子是 VB 中的 XML 文字功能。两者都允许您在通用 PL 中编写真实的代码,并将其与更简单的声明性 DSL 代码交织在一起。
结果是比独立的 SQL 或简单的 XML 编辑器更强大的东西。
不幸的是,这样做的缺点是 C# 和 VB 都不提供任何元编程功能,因此对于主流 .net 开发人员来说,实现这一点的唯一方法就是构建自己的语言。如果您只是为了好玩而做这件事,您也许可以修改 mono C# 编译器以将您感兴趣的功能添加到该语言中。另一种选择可能是尝试红宝石。它有一个灵活的语法,可以让你摆脱很多疯狂。然而,就我个人而言,我更喜欢 hacked C# 方法。
The thing with DSl's is that they rarely are effective in isolation. To be useful for writing real software, you really need to be able to embed the DSL inside a host language. Compare, for example, the way Linq works vs just straight SQL. Another good example is XML literal feature in VB. Both let you write real code, in a general purpose PL and inter weave it with simpler declaritive DSL code.
The result is something much more powerful than stand alone SQL or a simple XML editor.
The downside to this, unfortunately, is that neither C# nor VB offers any meta programming features, so the only way to do that for mainstream .net devs is to build your own language. If this is something you are doing just for fun you might be able to modify the mono C# compiler to add the features you are interested on to the language. Another alternative might be to try ruby. It has a flexible syntax whic let's you get away with a lot of crazyness. Personaly, however, I would prefer the hacked C# approach.
可能想要查看在 Boo 中构建领域特定语言(Boo 是一种 CLR 语言,概念应该带有转移到 C#)。示例项目是简单状态机。
Might want to check out Building Domain Specific Languages in Boo (Boo is a CLR language, concepts should carry over to C#). An example project is Simple State Machine.
看看 Microsoft 的 Oslo,不确定它是否符合您的要求,但我知道您可以构建 DSL 解析器并指定语法,并让它生成可以根据语法解析数据的类库。
有关“M”语言及其使用方法的更多详细信息,请参见
Take a look at Oslo from Microsoft, not sure if it does what you want, but I know that you can build DSL parsers and specify grammars and have it generate class libraries that can parse data based on the grammar.
More details of the "M" language and how to use it are here
为什么要走那么远?首先使用泛型和流畅的界面。从简单开始,然后通过一些制作来完成。如果处理流畅界面的阻力太大,那么可以考虑使用 DSL。
Why go so far? Start off using generics and a fluent interface. Start simple and work it through some production. If the friction is too high dealing with a fluent interface, then look at using a DSL.
您可以尝试 Visual Studio 的 DSL 工具。
You can try DSL Tools for Visual Studio.
您应该查看 Irony 项目 http://irony.codeplex.com/
You should look at Irony project at http://irony.codeplex.com/