用于 Linq 查询的 DSL - 寻找想法
我目前正在使用一个 CMS,它使用 ORM 及其自己的定制查询语言(即带有 select/where/orderby 之类的语句)。 我将这种迷你语言称为 DSL,但我可能用错了术语。
我们正在为此 CMS 编写控件,但我不想将控件耦合到 CMS,因为我们对是否要长期继续使用此 CMS 有一些疑问。
通过使用我们自己的 DAL/抽象层或其他层,我们可以相当轻松地将控件与 CMS 分离。
然后我记得在大多数 CMS 控件上,它们提供了一个属性(设计时可编辑),用户可以在其中键入查询来控制数据源中填充的内容。 不错的功能 - 问题是我如何抽象这个功能?
然后我想到也许存在一个 DSL 框架,它可以为我提供一种简单的查询语言,可以在运行时转换为 LINQ 表达式。 从而将我与 CMS 的查询 DSL 解耦。
这样的事情存在吗? 我是在浪费时间吗? (可能是后者)
谢谢
I am currently using a CMS which uses an ORM with its own bespoke query language (i.e. with select/where/orderby like statements). I refer to this mini-language as a DSL, but I might have the terminology wrong.
We are writing controls for this CMS, but I would prefer not to couple the controls to the CMS, because we have some doubts about whether we want to continue with this CMS in the longer term.
We can decouple our controls from the CMS fairly easily, by using our own DAL/abstraction layer or what not.
Then I remembered that on most of the CMS controls, they provide a property (which is design-time editable) where users can type in a query to control what gets populated in the data source. Nice feature - the question is how can I abstract this feature?
It then occurred to me that maybe a DSL framework existed out there that could provide me with a simple query language that could be turned into a LINQ expression at runtime. Thus decoupling me from the CMS' query DSL.
Does such a thing exist? Am I wasting my time? (probably the latter)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不能完全回答您的问题,但是 LINQ 有一个扩展,允许您将 LINQ 查询的谓词指定为字符串,名为 动态 LINQ,所以如果你想将条件存储在一些基于字符串的格式,您可能可以在此基础上构建您的语言。 您仍然需要找到一种方法来表示不同的子句(where/orderby/等),但对于作为参数传递给这些子句的谓词,您可以使用动态 LINQ。
请注意,动态 LINQ 允许您解析字符串,但据我所知,没有任何方法可以将现有的表达式树转换为该字符串...因此需要做一些工作来做到这一点。
(但我不确定我是否完全理解这个问题,所以也许我完全是:-))
this isn't going to answer your question fully, but there is an extension for LINQ that allows you to specify predicates for LINQ queries as strings called Dynamic LINQ, so if you want to store the conditions in some string-based format, you could probably build your language on top of this. You'd still need to find a way to represent different clauses (where/orderby/etc.) but for the predicates passed as arguments to these, you could use Dynamic LINQ.
Note that Dynamic LINQ allows you to parse the string, but AFAIK doesn't have any way to turn existing Expression tree into that string... so there would be some work needed to do that.
(but I'm not sure if I fully understand the question, so maybe I'm totally of :-))