Configuring WCF Data Services using Lambda Expressions

April 21, 2011

no comments

Configuring WCF Data Services using Lambda Expressions


One of the things that I avoid when I’m writing code is the use “magic strings”. Configuring WCF Data Service using Lambda ExpressionsHardcoded strings are a code smell and should be rarely used. When using WCF Data Service configuration object you’ll have to pass the entity set’s string name to the configuration methods which as I wrote I would like to avoid. This is why in today’s post I’m going to extend the DataServiceConfiguration object to support lambda expressions instead of string parameters.


Data Service Configuration Extensions


I’ve created a simple static class that includes two new extension methods: SetEntitySetAccessRule<DataSource> and SetEntitySetPageSize<DataSource>. Both of the methods extends the DataServiceConfiguration object and add the functionality of receiving a lambda expression that will hold the entity set name. Here is the class implementation:



public static class DataServiceConfigurationExtensions
{
  public static void SetEntitySetAccessRule<DataSource>(this DataServiceConfiguration config,
    Expression<Func<DataSource, object>> expression,
    EntitySetRights rights)
    where DataSource : class
  {      
    string entitySetName = GetEntitySetName(expression);
    config.SetEntitySetAccessRule(entitySetName, rights);
  }
 
  public static void SetEntitySetPageSize<DataSource>(this DataServiceConfiguration config,
    Expression<Func<DataSource, object>> expression,
    int pageSize)
    where DataSource : class
  {
    string entitySetName = GetEntitySetName(expression);
    config.SetEntitySetPageSize(entitySetName, pageSize);
  }
 
  private static string GetEntitySetName<DataSource>(Expression<Func<DataSource, object>> expression)
  {
    MemberExpression memberExpression = expression.Body as MemberExpression;
    if (memberExpression == null)
    {
      throw new ArgumentException(“Must be a member expression”);
    }
    return memberExpression.Member.Name;
  }
}

And here is how you’ll use this implementation while configuring your data service:



public class SchoolDataService : DataService<SchoolEntities>
{     
  public static void InitializeService(DataServiceConfiguration config)
  {
    config.SetEntitySetAccessRule<SchoolEntities>(c => c.Courses, EntitySetRights.All);
    config.SetEntitySetPageSize<SchoolEntities>(c => c.Courses, 10);  
  
    config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
  }
}

Now, if a name of an entity set will change you’ll get compilation error instead of runtime error. Also this way is less error prone and it is more clean. Since the InitializeService method is being called only once then the impact on performance is negligible.


Summary


Using “magic strings” is a bad habit. In this post I showed a simple solution for using lambda extensions instead of strings while configuring a WCF Data Service.


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>