In ASP.Net MVC, in the razor view, you can type this kind of code:
@Html.EditorFor(model => model.Name)
(in this case, it creates a textbox for the field Name of the object which is defined as the type of the model)
Now when i see the definition of the EditorFor method, it is this:
EditorFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression);
So i tried implementing that kind of method myself, but i realy don’t see what this means
Expression<Func<TModel, TValue>>
Who can explain to me what an expression is, and the Func, and the Expression of type Func?
The explanations i found where all so abstract it didn’t give me a clue….
1
The Expression<T>
class:
Represents a strongly typed lambda expression as a data structure in the form of an expression tree.
And:
The System.Linq.Expressions namespace contains classes, interfaces and enumerations that enable language-level code expressions to be represented as objects in the form of expression trees.
The abstract class
Expression
provides the root of a class hierarchy used to model expression trees.The classes in this namespace that derive from Expression, for example
MemberExpression
andParameterExpression
, are used to represent nodes in an expression tree. TheExpression
class contains static (Shared in Visual Basic) factory methods to create expression tree nodes of the various types.The enumeration type
ExpressionType
specifies the unique node types.
The Func<TResult>
(and family) of delegates:
You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. The encapsulated method must correspond to the method signature that is defined by this delegate.
The family basically encapsulates delegates for functions that have a return value.
Taken together, these are expressions that take func delegates typed take a parameter of the model type and return whatever value the lambda returns.
4
Expression is a class that enables you to take a lambda expression and transform it into a syntax tree. It is used by Linq (for example) to convert Linq queries into actual SQL queries. It is highly extensible so you could do other things with it.
http://msdn.microsoft.com/en-us/library/bb335710(v=vs.90).aspx
Have a look at http://en.wikipedia.org/wiki/Abstract_syntax_tree if you aren’t familiar with AST’s.
2
The HTML Helpers use a trick that has been called “Static Reflection” that was brought to the .NET framework to support LINQ to SQL and Entity Framework. Basically the Expression captures a Lambda which is verified at compile time (hence the Static) in a syntactic tree. The tree can be walked and can give things like the name of the member that is referenced by the body of the lambda.
Essentially, it’s a work around for .NET’s lack of a “symbol” keyword like some other languages support.
1
In this specific example, the generated HTML looks like this:
<input class="text-box single-line" id="Name" name="Name" type="text" value="ActualName" />
If the parameter was just a Func
, you would be able to get value ActualName
, but not the name of the property you’re accessing (Name
). So, to get that name, you would need it as a string
parameter (e.g. something like @Html.EditorFor(model => model.Name, "Name")
).
This is not nice, because it means you have to enter the property name twice. Worse, you lose compile-time safety: if the property name changes, you won’t get an error on the second parameter.
2