什么是DSL,我应该在哪里使用它?

时间:2022-05-19 19:44:57

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.

我越来越多地听到有关领域特定语言被抛出以及它们如何改变你对待业务逻辑的方式,我已经看到了Ayende的博客帖子和事情,但我从来没有真正理解为什么我会把我的业务远离我在提供商中使用的方法和情况的逻辑。

If you've got some background using these things, any chance you could put it in real laymans terms:

如果你有一些背景使用这些东西,你有可能把它用真正的外行术语:

  • What exactly building DSLs means?
  • 究竟构建DSL究竟意味着什么?

  • What languages are you using?
  • 你用的是哪种语言?

  • Where using a DSL makes sense?
  • 使用DSL有意义吗?

  • What is the benefit of using DSLs?
  • 使用DSL有什么好处?

7 个解决方案

#1


17  

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.

在需要将系统控制权的某些方面提供给其他人的情况下,DSL是很好的。我在Rules Engines中使用过它们,在那里你可以创建一种简单的语言,让技术含量较低的人更容易表达自己 - 特别是在工作流程中。

In other words, instead of making them learn java:

换句话说,而不是让他们学习java:

DocumentDAO myDocumentDAO = ServiceLocator.getDocumentDAO();
for (int id : documentIDS) {
Document myDoc = MyDocumentDAO.loadDoc(id);
if (myDoc.getDocumentStatus().equals(DocumentStatus.UNREAD)) {
    ReminderService.sendUnreadReminder(myDoc)
}

I can write a DSL that lets me say:

我可以写一个让我说的DSL:

for (document : documents) {
if (document is unread) {
 document.sendReminder
}

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的候选者。

#2


6  

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.

DSL代表领域特定语言,即专门为解决给定区域中的问题而设计的语言。例如,Markdown(用于编辑SO上的帖子的标记语言)可以被视为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的地方。通常我需要某种类似SQL的查询语言。另一种常见用法是基于规则的系统,您需要某种语言来指定规则\条件。

DSL makes sense in context where it's difficult to describe\solve problem by traditional means.

DSL在上下文中很有意义,通过传统方式难以描述\解决问题。

#3


4  

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.

如果您使用的是Microsoft Visual Studio,那么您已经在使用多个DSL - Web表单,winforms等的设计界面就是DSL。类设计器是另一个例子。

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.

DSL只是一组工具(至少在理论上)使特定“域”(即可视布局)的开发更容易,更直观,更高效。

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.

就构建DSL而言,人们喜欢Ayende所写的一些内容与“文本解析”dsls有关,让开发人员(或最终用户)将“自然文本”输入到应用程序中,该应用程序解析文本并生成某种类型基于它的代码或输出。

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。 Microsoft Visual Studio具有许多可扩展性点,“Guidance Automation Toolkit”和Visual Studio SDK的模式和实践可以帮助您向Visual Studio添加DSL功能。

#4


2  

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.

DSL是自定义语言的基本编译器。 ANTLR提供了一个良好的“*开放”工具来开发它们。最近,我一直在研究这个DSL,以便在新项目中使用状态机语言。我同意上面的Tim Howland,他们可以成为让别人自定义您的应用程序的好方法。

#5


2  

FYI, a book on DSLs is in the pipeline as part of Martin Fowler's signature series.

仅作为Martin Fowler签名系列的一部分,一本关于DSL的书正在筹备之中。

If its of the same standard as the other books in the series, it should be a good read.

如果它与该系列中的其他书籍具有相同的标准,那应该是一个很好的阅读。

More information here

更多信息在这里

#6


1  

DSL is just a fancy name and can mean different things:

DSL只是一个奇特的名字,可能意味着不同的东西:

  • 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

    Rails(Ruby的东西)有时被称为DSL,因为它添加了特殊的方法(并覆盖了一些内置的方法)来讨论Web应用程序

  • ANT, Makefile syntax etc. are also DSLs, but have their own syntax. This is what I would call a DSL.

    ANT,Makefile语法等也是DSL,但有自己的语法。这就是我所谓的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:

这种炒作的一个重要方面:用语言来思考你的应用程序是有意义的。你想在你的应用程序中谈什么?这些应该是你的类和方法:

  1. Define a "language" (either a real syntax as proposed by others on this page or a class hierarchy for your favorite language) that is capable of expressing your problem.
  2. 定义能够表达问题的“语言”(本页面上其他人提出的真实语法或您喜欢的语言的类层次结构)。

  3. Solve your problem in terms of that language.
  4. 用该语言解决您的问题。

#7


0  

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.

DSL基本上是创建自己的小子语言来解决特定的域问题。这是使用方法链解决的。点和圆括号是可选的语言有助于使这些表达看起来更自然。它也可以类似于构建器模式。 DSL本身不是语言,而是应用于API的模式,使调用更加自我解释。

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.

一个例子是Guice,Guice用户指南http://docs.google.com/View?docid=dd2fhx4z_5df5hw8进一步描述了接口如何绑定到实现,以及在什么情况下。

Another common example is for query languages. For example:

另一个常见的例子是查询语言。例如:

NewsDAO.writtenBy("someUser").before("someDate").updateStatus("Deleted")

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.

在实现中,假设每个方法返回一个新的Query对象,或者只是在内部更新它自己。在任何时候你都可以通过使用例如rows()来获取所有行来终止链,或者像我在上面所做的那样使用updateSomeField。两者都将返回一个结果对象。

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.

我建议你看一下上面的Guice例子,因为每次调用都会返回一个带有新选项的新类型。一个好的IDE将允许您完成,明确您在每个点上有哪些选项。

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.

编辑:似乎很多人认为DSL是一种新的,简单的,单一用途的语言,有自己的解析器。我总是将DSL与使用方法链接作为表达操作的约定相关联。

#1


17  

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.

在需要将系统控制权的某些方面提供给其他人的情况下,DSL是很好的。我在Rules Engines中使用过它们,在那里你可以创建一种简单的语言,让技术含量较低的人更容易表达自己 - 特别是在工作流程中。

In other words, instead of making them learn java:

换句话说,而不是让他们学习java:

DocumentDAO myDocumentDAO = ServiceLocator.getDocumentDAO();
for (int id : documentIDS) {
Document myDoc = MyDocumentDAO.loadDoc(id);
if (myDoc.getDocumentStatus().equals(DocumentStatus.UNREAD)) {
    ReminderService.sendUnreadReminder(myDoc)
}

I can write a DSL that lets me say:

我可以写一个让我说的DSL:

for (document : documents) {
if (document is unread) {
 document.sendReminder
}

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的候选者。

#2


6  

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.

DSL代表领域特定语言,即专门为解决给定区域中的问题而设计的语言。例如,Markdown(用于编辑SO上的帖子的标记语言)可以被视为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的地方。通常我需要某种类似SQL的查询语言。另一种常见用法是基于规则的系统,您需要某种语言来指定规则\条件。

DSL makes sense in context where it's difficult to describe\solve problem by traditional means.

DSL在上下文中很有意义,通过传统方式难以描述\解决问题。

#3


4  

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.

如果您使用的是Microsoft Visual Studio,那么您已经在使用多个DSL - Web表单,winforms等的设计界面就是DSL。类设计器是另一个例子。

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.

DSL只是一组工具(至少在理论上)使特定“域”(即可视布局)的开发更容易,更直观,更高效。

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.

就构建DSL而言,人们喜欢Ayende所写的一些内容与“文本解析”dsls有关,让开发人员(或最终用户)将“自然文本”输入到应用程序中,该应用程序解析文本并生成某种类型基于它的代码或输出。

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。 Microsoft Visual Studio具有许多可扩展性点,“Guidance Automation Toolkit”和Visual Studio SDK的模式和实践可以帮助您向Visual Studio添加DSL功能。

#4


2  

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.

DSL是自定义语言的基本编译器。 ANTLR提供了一个良好的“*开放”工具来开发它们。最近,我一直在研究这个DSL,以便在新项目中使用状态机语言。我同意上面的Tim Howland,他们可以成为让别人自定义您的应用程序的好方法。

#5


2  

FYI, a book on DSLs is in the pipeline as part of Martin Fowler's signature series.

仅作为Martin Fowler签名系列的一部分,一本关于DSL的书正在筹备之中。

If its of the same standard as the other books in the series, it should be a good read.

如果它与该系列中的其他书籍具有相同的标准,那应该是一个很好的阅读。

More information here

更多信息在这里

#6


1  

DSL is just a fancy name and can mean different things:

DSL只是一个奇特的名字,可能意味着不同的东西:

  • 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

    Rails(Ruby的东西)有时被称为DSL,因为它添加了特殊的方法(并覆盖了一些内置的方法)来讨论Web应用程序

  • ANT, Makefile syntax etc. are also DSLs, but have their own syntax. This is what I would call a DSL.

    ANT,Makefile语法等也是DSL,但有自己的语法。这就是我所谓的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:

这种炒作的一个重要方面:用语言来思考你的应用程序是有意义的。你想在你的应用程序中谈什么?这些应该是你的类和方法:

  1. Define a "language" (either a real syntax as proposed by others on this page or a class hierarchy for your favorite language) that is capable of expressing your problem.
  2. 定义能够表达问题的“语言”(本页面上其他人提出的真实语法或您喜欢的语言的类层次结构)。

  3. Solve your problem in terms of that language.
  4. 用该语言解决您的问题。

#7


0  

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.

DSL基本上是创建自己的小子语言来解决特定的域问题。这是使用方法链解决的。点和圆括号是可选的语言有助于使这些表达看起来更自然。它也可以类似于构建器模式。 DSL本身不是语言,而是应用于API的模式,使调用更加自我解释。

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.

一个例子是Guice,Guice用户指南http://docs.google.com/View?docid=dd2fhx4z_5df5hw8进一步描述了接口如何绑定到实现,以及在什么情况下。

Another common example is for query languages. For example:

另一个常见的例子是查询语言。例如:

NewsDAO.writtenBy("someUser").before("someDate").updateStatus("Deleted")

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.

在实现中,假设每个方法返回一个新的Query对象,或者只是在内部更新它自己。在任何时候你都可以通过使用例如rows()来获取所有行来终止链,或者像我在上面所做的那样使用updateSomeField。两者都将返回一个结果对象。

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.

我建议你看一下上面的Guice例子,因为每次调用都会返回一个带有新选项的新类型。一个好的IDE将允许您完成,明确您在每个点上有哪些选项。

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.

编辑:似乎很多人认为DSL是一种新的,简单的,单一用途的语言,有自己的解析器。我总是将DSL与使用方法链接作为表达操作的约定相关联。