Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the System.Linq.Expressions.ExpressionVisitor.VisitExtension and the System.Linq.Expressions.ExpressionType.Extension for?

The System.Linq.Expressions.ExpressionVisitor has a method named VisitExtension which seems to do nothing other than call the VisitChildren method on the Expression being visited.

protected internal virtual Expression VisitExtension(Expression node)
{
    return node.VisitChildren(this);
}

I understand what the VisitChildren does. I also understand that this virtual implementation can be and is perhaps meant to be overriden. So I gather from the documentation of the method on MSDN, which is sparing in words and briefly remarks:

Visits the children of the extension expression. This can be overridden to visit or rewrite specific extension nodes. If it is not overridden, this method will call VisitChildren, which gives the node a chance to walk its children. By default, VisitChildren will try to reduce the node.

I do not find this explanation helpful. Specifically, the phrase that jets me out of my abilities of comprehension is "or rewrite specific extension nodes."

I understand the rest of it, which pertains to the reduction or breaking down of the expression into sub-expressions.

Also in the same namespace is an enumeration named ExpressionType, the purpose of which I understand very well. But of all its members, there is one member named Extension which I cannot map to any syntactical token I am currently aware of.

The documentation in this instance, too, is frustratingly laconic. It describes the value Extension as follows:

An extension expression.

It is obvious that the two -- ExpressionType.Extension and ExpressionVisitor.VisitExtension -- are related.

But what is an extension? Surely, as is glaringly obvious, extension methods have no place in this context. Which syntactical artifact does the expression extension refer to here?

like image 956
Water Cooler v2 Avatar asked Dec 16 '14 13:12

Water Cooler v2


People also ask

What is system LINQ expressions for?

System.Linq.Expressions is for hand building (or machine generating) expression trees. I have a feeling that given the complexity of building more complicated functionality that this namespace is under used. However it is exceedingly powerful.

What is dynamic expression visitor in LINQ?

Represents a visitor or rewriter for expression trees. System. Linq. Expressions. Dynamic Expression Visitor This class is designed to be inherited to create more specialized classes whose functionality requires traversing, examining or copying an expression tree. Initializes a new instance of ExpressionVisitor.

What is language integrated in LINQ?

The critical aspect of the Language Integrated part of Linq is that the resulting language constructs are first class parts of the resulting code. Rather than simply resulting in a function they provide the way the function was constructed (as an expression) so that other aspects of the program can manipulate it.

What is dynamic expressionvisitor in Java?

Dynamic Expression Visitor This class is designed to be inherited to create more specialized classes whose functionality requires traversing, examining or copying an expression tree. Initializes a new instance of ExpressionVisitor. Determines whether the specified object is equal to the current object. Serves as the default hash function.


1 Answers

In this case, extension does not represent any kind of built-in syntax, but correspond to nodes that applications can define and assign arbitrary meaning to.

That concept is very useful when one manipulates Expression trees in an application, as these extension nodes can be fully integrated into what is otherwise a normal expression tree.

For example, I defined a subclass of System.Linq.Expressions.Expression with node type ExpressionType.Extension in order to extend Entity Framework's LINQ to understand the type of composite primary key that was in use at my company.

The extension expression type was useful because it let me use a two-step approach:

  • In the first step, an expression visitor would regularize every appearance of that composite key into a node of my custom type;
  • In the second step, the expression visitor that was tasked with translating the expression to something that Entity Framework would be able to handle could simply check on the type;

Example: let us say that I had LINQ code that was written:

from e in table where e.FirstKey == e.SecondKey select e;

Where FirstKey and SecondKey were both composite database keys (that is, there are two database columns FirstKey1 and FirstKey2 for FirstKey, and similarly for SecondKey).

Then the first visitor would transform both e.FirstKey and e.SecondKey to CustomKeyExpression nodes, functionally transforming it to:

from e in table where Key(e.FirstKey1, e.FirstKey2) == Key(e.SecondKey1, e.SecondKey2) select e;

And in the second visitor, when I would visit EqualExpression, I would check that both children were CustomKeyExpressions, and make the appropriate transformation:

from e in table where e.FirstKey1 == e.SecondKey1 && e.FirstKey2 == e.SecondKey2;
like image 173
Jean Hominal Avatar answered Nov 10 '22 00:11

Jean Hominal