Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats a good use case for .net 4.0 Expression Trees?

This one was inspired by my language-guru co-worker who can't seem to find a good use for them, and after a few lame attempts of my own, I'd have to agree.

Now I know these concepts tend to flow a lot more easily once you get some good practical reasons down.

At the moment it seems as though its only purpose is to allow you to write a Linq provider?

Is that it?? Is there any other benefits to this?

like image 604
KevinDeus Avatar asked Oct 07 '09 16:10

KevinDeus


People also ask

Why would you use an expression tree?

When you want to have a richer interaction, you need to use Expression Trees. Expression Trees represent code as a structure that you can examine, modify, or execute. These tools give you the power to manipulate code during run time. You can write code that examines running algorithms, or injects new capabilities.

What is .NET expression?

NET, Expression is an abstract class which contains static methods and inherited by various types (like ParameterExpression, MethodCallExpression, BinaryExpression etc.) to create expression tree nodes of specific types. Also all these expression-specific expression tree types are defined in System. Linq.

What is query expression trees?

Expression tree is an in-memory representation of a lambda expression. It holds the actual elements of the query, not the result of the query. The expression tree makes the structure of the lambda expression transparent and explicit.


3 Answers

Expression tree are so powerful because they let you treat code like data. Users are accustomed to building up data, saving it and coming back to it later.

Expression trees let you do the same thing with code. For example you can take your user's input (check-boxes, number ranges, etc.) and translate it into an Expression tree. That expression tree can then be executed, or stored off for later use. Very cool.

Think of the practical uses around reporting like building up and saving data filters and data mappings. Another practical use would be to support custom work flows in your application based on user defined rules.

Here's a bit of MSDN code on serializing expression trees (http://code.msdn.microsoft.com/exprserialization) that should get the ideas flowing.

like image 190
Bryan Hunter Avatar answered Oct 23 '22 11:10

Bryan Hunter


You can use Expression Trees to transform a domain language into executable code.

like image 9
ChaosPandion Avatar answered Oct 23 '22 09:10

ChaosPandion


A Solution looking for a problem eh?

Expression trees allow you to present code as a transformable data-structure, hence they are perfect for transforming between languages Linq To SQL being the most powerful currently.

An other use apart from DSLs (which is transformation) is parallelization (which is spliting) and example in that space is PLINQ.

like image 5
Jan Bannister Avatar answered Oct 23 '22 11:10

Jan Bannister