Wednesday 1 July 2009

Using expression trees for more elegant code

Caching WCF responses without making a mess in the code base is often problematic due to connection pooling limitations and the requirement to close connections after use.

Many implementations end up something like: attempt to retrieve entity from cache > if absent, instantiate client proxy > retrieve entity from service > close connection > add entity to cache.

Problems arise because it is possible that entities for a single solution may need to be supplied by numerous services and the service method signatures may differ for various entities or collections of entities. This makes it difficult to create a generic wrapper capable of populating any number of varying entity types let alone the associated CRUD calls for entity persistence, etc. Entity identifiers may differ in type between services or even within a service. When a client side caching mechanism is required, the code becomes cluttered with the numerous caching calls and connection instantiations and tear-downs.

Any one who has worked on large WCF driven apps knows that this kind of clutter is butt-ugly and a pain to maintain or add features to.

Expression Trees to the rescue:

public static class Services
{
/// <summary>Gets the item.</summary>
/// <typeparam name="TProxy">The type of the proxy.</typeparam>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <typeparam name="TIdentity">The type of the identity.</typeparam>
/// <param name="expression">The expression.</param>
/// <param name="proxy">The proxy.</param>
/// <param name="id">The id.</param>
/// <returns></returns>
/// <remarks>Method first checks the cache for an entity with a matching id, if found, it is returned.
/// If the cache does not contain the entity, the wcf service method in the supplied expression tree
/// is used to obtain the entity which is then added to the cache.</remarks>
/// <example>Product p = GetItem((client, identifier) => client.GetProduct(identifier), new CatalogServiceClient(), 123);</example>
private static TEntity GetItem<TProxy, TEntity, TIdentity>(Expression<Func<TProxy, TIdentity, TEntity>> expression, TProxy proxy, TIdentity id)
where TEntity : class
where TProxy : ICommunicationObject
{
TEntity item = Cache.GetItem<TEntity, TIdentity>(id);
if (item == null)
{
try
{
var originalDelegate = expression.Compile();
item = originalDelegate.Invoke(proxy, id);
}
finally { proxy.Close(); }
Cache.AddItem<TEntity, TIdentity>(item);
}
return item;
}
}

No comments: