Expressive is a library that converts method IL into expression trees.
Or, at least, it attempts to, since not all methods can be converted into a single expression.
Example:
public string FullName {
get { return this.FirstName + " " + this.LastName; }
}
is converted to
x => string.Concat(x.FirstName, " ", x.LastName)
NuGet Package: http://nuget.org/List/Packages/Expressive
API Docs: https://github.com/ashmind/expressive/wiki/External-API
There are situations (mostly LINQ-related) when there is a large difference between use of a simple (for example, DB-mapped) property and a calculated property that contains some business/domain logic. Above example is the most classical one, but there are a lot more.
These cases can be solved using things like HqlGeneratorForProperty in NHibernate and rewriters/visitors in other frameworks, but this requires logic/effort duplication, and such logic has to be maintained in both places.
Expressive allows you to reuse the code directly. In addition to queries, other benefits of such reuse can be, for example, automatic generation of formula columns or transfer of logic between compiled C# and JavaScript.
All code that uses Expressive should be used in combination with integration tests since significant changes to methods may render them 'inexpressible' (there are some cases when statements can be reduced to expressions, but these cases are rather limited). One rather annoying breaking case is IL instrumentation by, for example, Visual Studio Profiler.
What already works:
- Fields/Properties
- Methods
- Operators
- Lambdas
- Object initializers (only one level deep)
- Simple
if
that can be expressed as?:
What does not work:
- Array item access
- Deep object initializers
- Statements
Note: it is completely possible to allow .NET 4.0 "statement expressions" in the results by changing the default Pipeline and replacing a Decompiler.