Click here to Skip to main content
14,037,223 members
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 25 Apr 2011
Licenced CPOL

Configuring WCF Data Services Using Lambda Expressions

, 25 Apr 2011
Rate this:
Please Sign up or sign in to vote.
A simple solution for using lambda extensions instead of strings while configuring a WCF Data Service

Configuring WCF Data Service using Lambda Expressions

One of the things that I avoid when I’m writing code is the use of "magic strings".

Hardcoded strings are a code smell and should be rarely used. When using the WCF DataServiceConfiguration 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 extend 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 a compilation error instead of a runtime error. Also, this way is less error prone and is more clean. Since the InitializeService method is being called only once, the impact on performance is negligible.


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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gil Fink
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website:

You may also be interested in...


Comments and Discussions

QuestionNice, but didn't work for me Pin
Rahman Mahmoodi30-Apr-15 15:53
memberRahman Mahmoodi30-Apr-15 15:53 
GeneralMy vote of 5 Pin
Laurentiu Macovei16-Feb-13 11:05
memberLaurentiu Macovei16-Feb-13 11:05 
GeneralRe: My vote of 5 Pin
Gil Fink16-Feb-13 19:44
memberGil Fink16-Feb-13 19:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190424.1 | Last Updated 25 Apr 2011
Article Copyright 2011 by Gil Fink
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid