Expose IQueryable Over WCF Service

前端 未结 6 1130
迷失自我
迷失自我 2020-11-30 14:47

I\'ve been learning about IQueryable and lazy loading/deferred execution of queries.

Is it possible to expose this functionality over WCF? I\'d like to expose a LINQ

6条回答
  •  天命终不由人
    2020-11-30 15:36

    I've been struggling with the same question, and realized that a well formed question is a problem solved.

    IQueryable basically serves to filter the query before sending it to your DB call so instead of getting 1000 records and filter only 10, you get those 10 to begin with. That filtering belongs to your Service Layer, but if you are building an API I would assume you would map it with AND/OR parameters in your URL.

    http://{host}/{entity}/q?name=john&age=21.

    So you end up with something like this:

    Filter:Column1=Value1 > http://{host}/{entity}q?column1=value1 >  SELECT *
                                                                      FROM  Entity
                                                                      WHERE Column1=Value1
    
    MVC                   > WCF                                    >  DB
    

    You can find a very good sample [here]

    Lastly, since your payload from the WCF will be most likely a JSON, you can (and should) then deserialize them in your Domain Models inside a collection. It is until this point where the paging should occur, so I would recommend some WCF caching (and since its HTTP, it's really simple). You still will be using LINQ on the WebApp side, just w/o "WHERE" LINQ clause (unless you want to dynamically create the URL expressed above?)

    For a complex OR query, you mind end up with multiple WCF queries (1 per "AND") and then concatenate them all together

提交回复
热议问题