简体   繁体   English

在DataTable上构建表达式,设计建议?

[英]Build expression on DataTable, design suggestion?

I have Questions datatable with columns Question_Id, value, enable. 我有问题数据表,列有Question_Id,value,enable。 This will be loaded with list of question with answers. 这将加载问题列表和答案。

UI controls will be created based on the Questions datatable with question has enable set to true. UI控件将基于Questions数据表创建,问题已将enable设置为true。 If user selects / changes answer for any of the question, the value will be updated in the questions table that will result is enable/disable the other related question if any. 如果用户选择/更改任何问题的答案,则会在问题表中更新该值,如果有的话,将启用/禁用其他相关问题。

This will be defind in an expression like,basically, 'Enable X, if Y == (someValue) AND /OR .....' here X, Y are question. 这将在一个表达式中定义,基本上,'启用X,如果Y ==(someValue)和/或......'这里X,Y是问题。

I would like use Expression tree in Linq.Expression to handle this but new to Expression evaluation and looking for good design approch to implement. 我想在Linq.Expression中使用Expression树来处理这个问题,但是对Expression进行了新的评估并寻找好的设计方法来实现。

Can some one guide me on this? 有人可以指导我吗?

If I am understanding the question correctly, you are building a list of questions that should be dynamically updated based on the answers to those questions. 如果我正确理解了这个问题,那么您正在构建一个问题列表,这些问题应根据这些问题的答案进行动态更新。 For example, if a question #1 is answered, then question #2 should be disabled (or something more complex). 例如,如果回答问题#1,则应禁用问题#2(或更复杂的问题)。

I'm a big fan of dynamic code generation (eg expression trees), but this may actually be addressed more simply with an XML structure since it lends itself to representing relationships much better than a single DataTable . 我是动态代码生成的忠实粉丝(例如表达式树),但实际上可以通过XML结构更简单地解决这个问题,因为它有助于比单个DataTable更好地表示关系。

<question id="1">
    <if valueToCompare="Foo" operator="EqualTo">
        <disable questionId="5" />
        <removeOption questionId="6" optionId="2" />
    </if>
</question>

Whenever the user's answer to a question changes, simply look at the corresponding XML node and act accordingly. 每当用户对问题的回答发生变化时,只需查看相应的XML节点并采取相应措施即可。

You can evaluate and rebuild your controls in real-time without the need for compiled expressions and the performance will be fine unless you have extremely complex controls. 您可以实时评估和重建控件,而无需编译表达式,除非您具有极其复杂的控件,否则性能将会很好。

However, if you want to learn about expressions, you could certainly extrapolate and construct them from an XML structure and create delegates which are invoked whenever a question changes. 但是,如果您想了解表达式,您当然可以从XML结构中推断和构造它们,并创建在问题发生变化时调用的委托。 For example, if answering question #1 should disable question #2, you could turn this logic into a delegate that is fired whenever question #1 changes. 例如,如果回答问题#1应禁用问题#2,则可以将此逻辑转换为在问题#1发生更改时触发的委托。

This would require initially parsing your data source (XML or otherwise--you could certainly store simple logic statements in a DataTable as strings or multiple columns and parse those) and turning conditional statement definitions into expression trees. 这将需要最初解析您的数据源(XML或其他 - 您当然可以将简单的逻辑语句存储在DataTable作为字符串或多列并解析它们)并将条件语句定义转换为表达式树。 This could be quite simple or complex depending on how complicated your want your decision trees to be. 这可能非常简单或复杂,具体取决于您希望决策树的复杂程度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM