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
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Configuring WCF Data Service using Lambda Expressions

Configuring WCF Data Service using Lambda Expressions

Gil Fink user avatar by
Gil Fink
·
Apr. 21, 11 · News
Like (0)
Save
Tweet
Share
4.18K 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

  • Why Open Source Is Much More Than Just a Free Tier
  • Top Five Tools for AI-based Test Automation
  • Why You Should Automate Code Reviews
  • How to Quickly Build an Audio Editor With UI

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • 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: