DSL:从 DSL 规则到 C# 表达式

发布于 2024-07-22 05:23:15 字数 1175 浏览 9 评论 0原文

问题可能是复合的,让我扩展它:

  • 是否存在一个设计器(存根/框架/元设计器)来创建基于 .NET 对象公共 bool 属性的基于 AND/OR 的规则? 保存为任何 DSL/Boo/... 输出。
  • 是否可以将 DSL 输出编译为 C# 表达式?

我们的主要问题是文档和代码之间的差距。 我们的产品基于数百个用户定义的规则,我们希望加快变更请求的速度。

如果我们能够为用户提供一个简单的设计器并获取输出,那么在将其翻译/编译为 C#/IL 代码后,我们将获得快速的更改请求周期。

我知道我们的问题很具体,但欢迎任何“墙上的砖”!

示例

AC# 类,主题:

public class TestA
{
     public bool B {...}
     public bool C {...}
}

在设计器中,我们应该能够创建

  • 任何类型的图形设计器(即用于选择公共属性的下拉列表)

DSL 中的输出:

If TestA.B AND TestA.C Then Return True;

C# 中的输出:

if (testA.B && testA.C) { return true; }

更新 #1

我很高兴有一种支持使用静态类型 .NET 类的 DSL 语言。 我的意思是,如果用户可以检查代码(示例中的“Output in DSL”),我们就不需要设计器。

更新#2

根据tipp,我盯着表达式树。 几天后,我遇到了 DLinq - 我从来都不是 DLinq 的忠实粉丝,但在这种情况下,它非常适合问题领域。

  • 易于将 (A > 2 AND B < 4) OR C = 5 解析为表达式树
  • 易于创建这样的表达式
  • 非常易于序列化/反序列化
  • 基于 FlowLayoutPanel 的 GUI 可以很好地用作“表达式构建器” ”

the question is maybe composite, let me expand it:

  • does it exists a designer (stub/framework/meta-designer) to create AND/OR based rules based on .NET object public bool properties? Saved as any DSL/Boo/... output.
  • is it possible to compile the DSL output into C# expressions?

Our main problem is the gap between the documentation and the code. Our product based on hundreds of user definied rules and we want to speed up the change requests.

If we are able to give a simple designer to the users and grab the output, then after translating/compiling it into C#/IL code we have a fast change request cycle.

I know that our problem it's to specific but any "bricks in the wall" are welcome!

Example:

A C# class, subject of :

public class TestA
{
     public bool B {...}
     public bool C {...}
}

In the designer, we should able to create

  • any type of graphics designers (ie. dropdown to select public properties)

Output in DSL:

If TestA.B AND TestA.C Then Return True;

Output in C#:

if (testA.B && testA.C) { return true; }

Update #1

I would be glad with a DSL language that support using of static-typed .NET classes. I mean if the user can check the code ("Output in DSL" in the example), we don't need the designer.

Update #2

Based on the tipp, I stared with expression trees. After few days I ran into DLinq - I never was a big fan of DLinq but in this case fits the problem domain very well.

  • Easy to parse (A > 2 AND B < 4) OR C = 5 into expression trees
  • Easy to create expressions like that
  • Very easy to serialize/deserialize
  • GUI based on FlowLayoutPanel works fine as "expression builder"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猛虎独行 2024-07-29 05:23:15

你可以自己构建这样的东西。

您可以使用 Type.GetMembers() 获取类的所有公共属性的列表,

但是,我不会生成 C# 代码,而是使用表达式树。

这样,当用户更改规则时,您不需要涉及 C# 编译器。 相反,您可以将规则存储在数据库中,在运行时加载它们,然后使用 Expression.Compile() 方法创建一个委托,您可以调用该委托来运行代码。

更新:

在评论中有人问“Expression Tress 和领域特定语言有什么区别?”

答案如下:

表达式树和领域特定语言是正交的。

表达式树只是一个用于表示 C# 表达式的 API,可以在运行时方便地动态转换为委托。

DSL(领域特定语言)是一种旨在解决一小类问题的编程语言。

从本质上讲,它们是完全不同的东西。

如果您愿意,可以使用表达式树作为 DSL 实现的一部分。 Linq 将它们用于此目的。

然而,就您而言,您不需要 DSL。 您需要的是一个生成规则的用户界面(类似于 Outlook 的工作方式),然后是执行这些规则的方法。

创建 UI 只是正常的 UI 开发。

表达式树可用于实现规则。

You could build something like this your self.

You can get a list of all the public properties for a class using Type.GetMembers()

However, instead of generating C# code, I would use expression trees.

That way you don't need to involve the C# compiler when the users change rules. Instead, you can store the rules in a database, load them at runtime, and then use the Expression.Compile() method to create a delegate you can invoke to run the code.

Update:

In the comments someone asked "What is the difference between Expression Tress and domain specific languages?"

Here's the answer:

Expression trees and domain specific languages are orthogonal things.

Expression tress are just an API for representing C# expressions, that conveniently can be converted into a delegate dynamically at runtime.

A DSL, or domain specific language, is a programing language designed to solve a narrow class of problems.

They are, essentially, completely different things.

You can use expression trees as part of a DSL implementation if you like. Linq uses them for for that purpose.

In your case, however, you don't need a DSL. What you need is a user interface that generates rules (similar to the way outlook works), and then a way of executing those rules.

Creating the UI is just normal UI development.

Expression trees are what you can use to implement the rules.

闻呓 2024-07-29 05:23:15

一个鲜为人知的事实是,Windows Workflow Foundation 及其规则引擎的设计器可以托管在与 Visual Studio 分离的 Windows 窗体应用程序中。 以这种方式编写的规则可以类似地独立于实际工作流程进行评估。

请参阅WF 方案指南:工作流设计器重新托管教程:托管 WF 设计器

It's a little-known fact that the designer for Windows Workflow Foundation and its Rules Engine in particular can be hosted in a Windows Forms application separate from Visual Studio. The rules authored in this way can similarly be evaluated independent of an actual workflow.

See WF Scenarios Guidance: Workflow Designer Re-Hosting and Tutorial: Hosting the WF Designer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文