简体   繁体   中英

sql from expression trees C#

I basically have a generic class called DynamicServices. The dynamicServices will look like the following

class DynamicEntityServices<T>{
     T GetById(Guid Id) { // get from db using ado.net dal 
     List<T> Search(SearchCondition searchCondition) { // filtering the data from the dal
}

Now, that I have a json spec file [basically a json schema file that defines the dynamic entity and its property / types

During runtime, I want the consumer of my service to build and send me a search condition.

I currently have my SQLDAL implementation like the following

class DynamicEntityDAL<T>{
    List<T> Search(SearchCondition condition){
      StringBuilder query = new StringBuilder();
      foreach(var field in condition.Fields){
         query.Append(" "+field.Key+" like '%"+field.Value+"%' ")

and something similar to the above, I know that the above process is right because there are practical limitations of this. Please note that I would like to actually get an expression sent in from the caller of my dynamic service in the search condition and then I will construct the query as like below

var filterExpr = new Expression()...
var paramterExpr = new ParameterExpression("categorystatus",true); // not the right ones, but will look similar to this.
var condition = new SearchCondition{ SearchExpression = filterExpr };

In DAL, I can try to convert the above to SQL using some expression visitor like
`string filterClause = ConvertToSql(condition.SearchExpression);`

I can read and implement things, but as my json schema files are dynamic, I cannot use EF. Kindly suggest the right approach so that my library can get the expression from the caller of the service and also get it translated to T-SQL and query from db.

Assuming you have a piece of JSON that represents the expression for a WHERE clause of a user query, then here are the steps of converting that to SQL:

  1. Convert JSON string to a Hierarchal object.
  2. Convert the Hierarchal object to SQL.

Assuming you already have (1) done (it looks like you can create Expression 's, presumably from the JSON), then you just need to write something that recursively traverses the Hierarchal object and converts it to SQL, depending on the type of Expression .

You're going to probably need some notion of an explicit schema (tables and columns) somewhere to dynamically compute your JOIN 's, unless this is all in one table/view. Additionally, if your schema's foreign key relationships creates a cyclic graph then you'll need to define relationships, in addition to tables and columns (which will help you specify which type of join unless you can assume INNER is always okay). This will also help you when you want to change the SELECT columns and ORDER BY columns, when/if that becomes a need.

This is coming from someone who has written similar things multiple times in the past:

  • This is very time-consuming and non-trivially to do correctly; it's better to find something that already does something similar enough to what you want, instead of writing this from scratch.
  • Leverage as many existing frameworks as possible if there isn't something already out there for you that works. For example, reusing Linq Expression s instead of rolling your own, which it looks like you've done that.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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