Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

c# - How can I pass a lambda expression to a WCF service?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...