什么是 DSL?我应该在哪里使用它?
我越来越多地听到有关领域特定语言的说法,以及它们如何改变您对待业务逻辑的方式,并且我已经看到 Ayende 的博客文章之类的东西,但我从来没有真正明白为什么我要把我的业务逻辑从我在提供商中使用的方法和情况中剔除。
如果您有一些使用这些东西的背景,那么您有机会用真正的外行术语来说:
- 构建 DSL 到底意味着什么?
- 您使用什么语言?
- 在什么情况下使用 DSL 有意义?
- 使用 DSL 有什么好处?
I'm hearing more and more about domain specific languages being thrown about and how they change the way you treat business logic, and I've seen Ayende's blog posts and things, but I've never really gotten exactly why I would take my business logic away from the methods and situations I'm using in my provider.
If you've got some background using these things, any chance you could put it in real laymans terms:
- What exactly building DSLs means?
- What languages are you using?
- Where using a DSL makes sense?
- What is the benefit of using DSLs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
当您需要将系统某些方面的控制权交给其他人时,DSL 非常有用。 我在规则引擎中使用了它们,您可以在规则引擎中创建一种简单的语言,让技术含量较低的人员更容易使用它来表达自己,尤其是在工作流程中。
换句话说,不是让他们学习 java:
我可以编写一个 DSL,让我说:
还有其他情况,但基本上,任何您可能想要使用宏语言、编写工作流程脚本或允许售后定制的地方 -这些都是 DSL 的候选者。
DSL's are good in situations where you need to give some aspect of the system's control over to someone else. I've used them in Rules Engines, where you create a simple language that is easier for less-technical folks to use to express themselves- particularly in workflows.
In other words, instead of making them learn java:
I can write a DSL that lets me say:
There are other situations, but basically, anywhere you might want to use a macro language, script a workflow, or allow after-market customization- these are all candidates for DSL's.
DSL 只是一个奇特的名称,并且可以表示不同的含义:
Rails(Ruby 的东西)有时被称为 DSL,因为它添加了用于讨论 Web 应用程序的特殊方法(并且也覆盖了一些内置方法)
ANT、Makefile 语法等也是 DSL,但有自己的语法。 这就是我所说的 DSL。
这种炒作的一个重要方面是:从语言的角度考虑您的应用程序确实有意义。 您想在您的应用程序中谈论什么? 这些应该是您的类和方法:
DSL is just a fancy name and can mean different things:
Rails (the Ruby thing) is sometimes called a DSL because it adds special methods (and overwrites some built-in ones too) for talking about web applications
ANT, Makefile syntax etc. are also DSLs, but have their own syntax. This is what I would call a DSL.
One important aspect of this hype: It does make sense to think of your application in terms of a language. What do you want to talk about in your app? These should then be your classes and methods:
仅供参考,一本关于 DSL 的书正在编写中,作为 Martin Fowler 签名系列的一部分。
如果它与该系列中的其他书具有相同的标准,那么它应该是一本好书。
更多信息请此处
FYI, a book on DSLs is in the pipeline as part of Martin Fowler's signature series.
If its of the same standard as the other books in the series, it should be a good read.
More information here
DSL 是自定义语言的基本编译器。 ANTLR 提供了一个很好的“免费且开放”的工具来开发它们。 最近,我一直在研究这个 DSL 的状态机语言< /a> 在新项目上使用。 我同意上面 Tim Howland 的观点,即它们可以是让其他人自定义您的应用程序的好方法。
DSL are basic compilers for custom languages. A good 'free and open' tool to develop them is available at ANTLR. Recently, I've been looking at this DSL for a state machine language use on a new project . I agree with Tim Howland above, that they can be a good way to let someone else customize your application.
如果您使用 Microsoft Visual Studio,那么您已经在使用多个 DSL——Web 表单、winforms 等的设计界面就是 DSL。 类设计器是另一个例子。
DSL 只是一组工具(至少在理论上)使特定“领域”(即可视布局)的开发变得更容易、更直观、更高效。
就构建 DSL 而言,像 Ayende 这样的人写的一些内容与“文本解析”DSL 相关,让开发人员(或最终用户)将“自然文本”输入到应用程序中,应用程序解析文本并生成某种类型代码或基于它的输出。
您可以使用任何语言来构建您自己的 DSL。 Microsoft Visual Studio 有很多可扩展点,并且模式和功能也很丰富。 实践“指导自动化工具包” 和 Visual Studio SDK 可以帮助您添加 DSL Visual Studio 的功能。
If you use Microsoft Visual Studio, you are already using multiple DSLs -- the design surface for web forms, winforms, etc. is a DSL. The Class Designer is another example.
A DSL is just a set of tools that (at least in theory) make development in a specific "domain" (i.e. visual layout) easier, more intuitive, and more productive.
As far as building a DSL, some of the stuff people like Ayende have written about is related to "text parsing" dsls, letting developers (or end users) enter "natural text" into an application, which parses the text and generates some sort of code or output based on it.
You could use any language to build your own DSL. Microsoft Visual Studio has a lot of extensibility points, and the patterns & practices "Guidance Automation Toolkit" and Visual Studio SDK can assist you in adding DSL functionality to Visual Studio.
DSL 代表领域特定语言,即专门为解决给定领域的问题而设计的语言。
例如,Markdown(用于编辑 SO 上的帖子的标记语言)可以被视为 DSL。
就我个人而言,我几乎在我正在从事的每个大型项目中都找到了 DSL 的一席之地。 大多数情况下,我需要某种类似 SQL 的查询语言。 另一种常见的用法是基于规则的系统,您需要某种语言来指定规则\条件。
在难以通过传统方式描述\解决问题的环境中,DSL 很有意义。
DSL stands for Domain Specific Language i.e. language designed specifically for solving problems in given area.
For example, Markdown (markup language used to edit posts on SO) can be considered as a DSL.
Personally I find a place for DSL almost in every large project I'm working on. Most often I need some kind of SQL-like query language. Another common usage is rule-based systems, you need some kind of language to specify rules\conditions.
DSL makes sense in context where it's difficult to describe\solve problem by traditional means.
DSL 基本上是创建您自己的小子语言来解决特定的领域问题。 这是使用方法链解决的。 点和括号是可选的语言有助于使这些表达看起来更自然。 它也可以类似于构建器模式。
DSL 本身并不是语言,而是一种应用于 API 的模式,以使调用更加不言自明。
一个例子是 Guice,Guice 用户指南 http://docs.google.com/View?docid= dd2fhx4z_5df5hw8 进一步描述了接口如何绑定到实现以及在什么上下文中。
另一个常见的例子是查询语言。 例如:
在实现中,想象每个方法返回一个新的 Query 对象,或者只是在内部更新自身。 在任何时候,您都可以通过使用 rows() 来获取所有行或 updateSomeField 来终止链,就像我在上面所做的那样。 两者都会返回一个结果对象。
我建议您也看看上面的 Guice 示例,因为每次调用都会返回一个带有新选项的新类型。 一个好的 IDE 将允许您完成,并清楚地表明您在每个点上有哪些选项。
编辑:似乎许多人认为 DSL 是一种新的、简单的、单一用途的语言,有自己的解析器。 我总是将 DSL 与使用方法链作为表达操作的约定联系起来。
DSL is basically creating your own small sublanguage to solve a specific domain problem. This is solved using method chaining. Languages where dots and parentheses are optional help make these expression seem more natural. It can also be similar to a builder pattern.
DSL aren't languages themselves, but rather a pattern that you apply to your API to make the calls be more self explanatory.
One example is Guice, Guice Users Guide http://docs.google.com/View?docid=dd2fhx4z_5df5hw8 has some description further down of how interfaces are bound to implementations, and in what contexts.
Another common example is for query languages. For example:
In the implementation, imagine each method returning either a new Query object, or just this updating itself internally. At any point you can terminate the chain by using for example rows() to get all the rows, or updateSomeField as I have done above here. Both will return a result object.
I would recommend taking a look at the Guice example above as well, as each call there returns a new type with new options on them. A good IDE will allow you to complete, making it clear which options you have at each point.
Edit: seems many consider DSLs as new, simple, single purpose languages with their own parsers. I always associate DSL as using method chaining as a convention to express operations.