DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Configuring WCF Data Service using Lambda Expressions

Gil Fink user avatar by
Gil Fink
·
Apr. 21, 11 · · News
Like (0)
Save
Tweet
4.02K Views

Join the DZone community and get the full member experience.

Join For Free

one of the things that i avoid when i’m writing code is the use “magic strings”. configuring wcf data service using lambda expressions hardcoded 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.
 
 
Windows Communication Foundation Data (computing)

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • ETL, ELT, and Reverse ETL
  • Modernizing Testing With Data Pipelines
  • Autowiring in Spring
  • After COVID, Developers Really Are the New Kingmakers

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo