Developer forum

Forum » Development » Serialize/Deserialize Query Expressions

Serialize/Deserialize Query Expressions

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I have a situation where I want to add Query expressions in the SwiftProduct.query programmatically.

I have used the QueryService for reading the Query file, I could manipulate the Parameters of the query but I could not find a way to interact with the Expressions.

I see that the query has a property called Expression where it is stored the entire logic of the grouping end expressions.

I suppose there should be a way to interact with the logic for adding (or removing) expressions, but I could not find it yet.

Anybody can point me in the right direction?

Thank you,
Adrian


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Adrian

Have a look at this code to see if that helps you.

https://dev.azure.com/dynamicwebsoftware/Dynamicweb/_git/Dynamicweb10?path=/Dynamicweb.Application.UI/Commands/Repositories/QueryExpressionSaveCommand.cs

Remember not to write hundreds/thousands/too-many expressions to the query as it will probably break or become slow.

BR Nicolai

 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply

Hi all,

Let me fill in a little info about the expression concept.

When we made queries, we took inspiration from the way .NET deals with expressions (System.Linq.Expressions). The idea is that expressions are composed. You start with a root of some type, and then create an expression structure by composing different types of expressions into an expression tree. That means that a query only has a single root expression, which in 999 out of 1000 cases is going to be a "GroupExpression". Multiple other expressions are then added to the group expression and from there, the structure can become as simple or as complex as is required by nesting expressions.

Due to this, expressions are easiest to work with in a visual manner. You can interact with expressions programmatically, but you need to traverse the structure using a recursive visitor to find the right expression object. We do not provide an API for that in Dynamicweb, so you would need to create one yourself.

- Jeppe

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai, Jeppe

Thank you very much for the details. I ended up casting the expression into a "GroupExpression" and then parse through the various groups.

In my case, I keep a "Master Query" with the expressions that are constant and I always update the master query by adding another group at the end of the MasterQuery.

It's not pretty but it seems to work.

In my case, I have a bunch of properties coming from an import and I cannot expect the admins to always edit them manually after each import, especially considering that the structure may vary slightly after each import, depending on the products included in the import.

I am happy so far with the result.

Thank you very much for the details.

Adrian

 

You must be logged in to post in the forum