I am writing a post following a question from @Silas around the dynamic.cs file that is present in the generative objects Framework, wondering if this is a re-implementation of something that exist in .NET Framework. @Garth this is a topic that might be of interest to you too, for its impact it can have for the migration to .NET Core and also some discussions we already started around GraphQL.
This is an interesting topic, as there could be other design options to achieve what is done with dynamic Linq queries in generative objects and is a question also related to the APIs, potential use of GraphQL etc.
The need for dynamic queries comes from the fact that we had the requierement to make api calls to query data with filter predicate expressions, for example “give me all contracts for customer X with date after july 2019”. These queries needs to be expressed in text format, as part of the http request. And server side, these text queries need to be interpreted to actually query the data.
The solution I choose at that time was to follow the Linq syntax, and then, server side, apply these Linq queries, since Linq is the mechanism we use server side to query data.
The problem was that it is not straightforward to convert text Linq queries to actual Linq queries. For example, if the query text for a query over contracts is :
“Customer.Name == \“MyCustomer\” && Date > \“2019-06-20\””
The actual Linq query should be :
contracts.Where(c => c.Customer.Name == “MyCustomer” && Date > new DateTime(2019,6,20))
The dynamic.cs class that I added is to actually allow to do these text queries over Linq and allow this syntax:
contracts.Where(“Customer.Name == \“MyCustomer\” && Date > \“2019-06-20\””)
In my understanding, this support does not exist by default in .NET Framework, but I can be wrong because I never check if such support was added in most recent version of .NET !
Here is where I found the initial implementation of dynamic.cs :
And here is, from my old blog, the explanation of an addition I made to the original dynamic.cs extention :
Now the questions are :
- Is there direct support for dynamic Linq queries in .NET Framework, and in .NET Core ? And does it support the Contains keyword I added ?
- Are there other options for supporting dynamic queries ? especially if we move to GraphQL APIs, is there any suggested mechanism for queries ?