Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a lambda expression to a WCF service?

Tags:

c#

.net

wcf

My current project is using the IDesign architecture, so all of my layers are services. I wanted to have my Read method in the CRUD of my resource access layer take a predicate in the form of a lambda expression as well as a list of related objects to pull. This way the resource access layer will be very generic.

[OperationContract]
Result<MyObject> ReadObjects(Func<MyObject, bool> predicate, string[] includes);

Now I have come to discover something that should have been obvious, and that is that I cannot serialize lambda expressions. I looked into parsing a string into a lambda expression, but that is a no go as well.

Is there any method that I can use to pass a lambda expression to a service? Is there a better way to do what I am trying to do?

like image 326
Alec Avatar asked Jun 22 '11 16:06

Alec


1 Answers

We have to solve this problem in LINQ-to-Just-About-Everything. For example, when doing LINQ-to-SQL:

var results = from c in customers where c.City == "London" select c.Name;

somehow the content of the lambdas c=>c.City == "London" and c=>c.Name need to end up on the SQL server in a form the server understands. Clearly we cannot persist the lambdas to the server.

Instead what we do is turn the lambdas into Expression Trees, analyze the expression trees at runtime, build an actual string of SQL out of it, and send that string to the server for processing.

You can do the same thing. Create a query language for your server. On the client side, turn the lambdas into expression trees. Analyze them at runtime, turn the result into a string in your query language, and then send the query to the service.

If you're interested in how this works in LINQ, the LINQ-to-SQL architect Matt Warren has written a long series of blog articles on how to do it yourself:

http://blogs.msdn.com/b/mattwar/archive/2008/11/18/linq-links.aspx

like image 52
Eric Lippert Avatar answered Sep 20 '22 15:09

Eric Lippert