David Kennedy’s Tech Ramblings

Just another WordPress.com weblog

Persisting Specifications – Metalinq September 4, 2009

Filed under: Uncategorized — dotnetdave @ 10:00 pm

Something that I utilised several months back for a CEP (complex event processing) project, this is based on the project available at http://metalinq.codeplex.com/

It also requires a more recent version of LinqKit, the one which includes a PredicateBuilder which operates with Expression<Func<T, bool>> rather than Predicate<T> as per older versions of LinqKit.

In short, this allows us to persist and re-hydrate expressions, allowing us to entirely separate our specification from our data. Consider the following example:

 

using System; using System.Collections.Generic; using System.Linq; using System.Text; using LinqKit; using System.Linq.Expressions; using System.Xml.Serialization; using System.IO; using ExpressionBuilder; namespace Org.Suresoft.RulesEngine { public class ScratchClass { public string SomeProp { get; set; } public int SomeNumber { get; set; } } public class ScratchPad { public void DoStuff() { Expression<Func<ScratchClass, bool>> testing = PredicateBuilder.False<ScratchClass>(); EditableExpression expB = EditableExpression.CreateEditableExpression(testing); XmlSerializer serial = new XmlSerializer(typeof(EditableLambdaExpression)); StringBuilder sb = new StringBuilder(); TextWriter writer = new StringWriter(sb); serial.Serialize(writer, expB); var recovered = serial.Deserialize(new StringReader(sb.ToString())) as EditableExpression; var done = recovered.ToExpression() as LambdaExpression; } } }

As you can see, the ability to build up our specifications following the predicate pattern and serialize/deserialize same opens up a whole world of possibilities. In the case of our project, we utilised a Spring-injected repository which in turn made use of Fluent nHibernate for persistence.

Advertisements
 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s