MetaLinq - Nuget Package Of The Week - 1 of N

Inspired by Scott Hanselman, I am going to blog about lesser known .NET packages. The twist is that I have generally published (although not necessarily written) these packages myself so this is flagrant self-promotion...

Anyway, first up is

MetaLinq

a library for converting (normally immutable) Linq Expression trees into a serializable, mutable object model. It was created by Aaron Erickson and I have forked it and put it on Nuget as a Portable class library.

Intro

Broadly speaking, the project allows you to serialize, modify, and deserialize LINQ Expressions.

Expression immutable = someExpression; //you can’t change immutable directly

EditableExpression mutable = EditableExpression.CreateEditableExpression(immutable);

//Do some stuff here to mutate the expression

Expression newCopy = mutable.ToExpression(); //..then do this to convert it back

Example (crazy?) use cases

  • use an EditableExpression as a API action parameter so your client can query with all the richness of LINQ. Even better, you can modify the query e.g. removing filters on columns which don't have indexes. EDIT: it's been mentioned that this could open you to exploits if you use this in a public API and don't modify the expression to remove unsafe expressions. Yes, obviously.
  • recording lambdas as event invocations for Event sourcing

Similar projects

  • Serialize.Linq does serialization, but I don't think the model is mutable
  • QueryInterceptor makes it easy to transform a standard Expression tree to another using Visitors.