Home > English > Using WCF RIA Services to allow projections that support Lookups

Using WCF RIA Services to allow projections that support Lookups

WCF RIA Services is quite powerful and provides a lot of flexibility. You can pretty much perform all of your persistence operations and server side processing using WCF RIA Services without really needing to create a separate service.

One aspect that seems to be a little more difficult is to provide a light version of a table for lookups. What I mean by lookups is simply the bare amount of data necessary to hydrate a ComboBox or a ListBox. Typically you don’t need to bring back your whole table or object just to provide a selector. In some cases, your tables may even have other data that is really meaningless to the user and is just for tracking purposes such as metadata that describe who made the last modification and when.

Now that we know what the problem statement is, how can we go about solving this in a manner that is generic enough to handle most any ComboBox or ListBox.

Introducing the ResultDTO object.

public class ResultDTO
    public int Value { get; set; }
    public string Name { get; set; }
    public string FriendlyName { get; set; }

With this simple class, I can now do projections that will allow me to reshape larger objects to this smaller object and only bring back the information that I need. Typically I find that I have several tables that are very wide and I only need just key information when displaying them as a ComboBox. I don’t want to bring down all the columns per record since that would be inefficient but I still want to be able to databind using the key from the underlying table and some label in my ComboBox or ListBox.

Let’s review the class that I have presented above. I first need to define a key so that WCF RIA Services can function properly. This Value property is used as the unique key to the underlying table that I am representing. Next I have two string properties: Name and FriendlyName. You don’t need both but I sometimes like the option to bind to one or the other depending on how the text is formatted in the database.

Okay, now let’s look at how you would expose a query via WCF RIA Services that reshapes the table into our new ResultDTO object:

public IQueryable<ResultDTO> ReturnReportDTO()
    var result = from c in this.ObjectContext.rs_r_Report
          select new ResultDTO()
              Value = c.rs_r_ReportIdent,
              Name = c.rs_r_ReportName,
              FriendlyName = c.rs_r_FriendlyName
    return result;

As you can see, I am simply doing a projection that is creating a new ResultDTO object from the underlying rs_r_Report object. This pattern allows us to basically project any object into our ResultDTO object and only bring back the items that we need.

By using this pattern you can now have a full and lite version of your objects so that you can support full data editing and also read-only lookups.

In the next post I will describe how we can take this a step further and create a custom object that has a nested collection and still get WCF RIA Services to allow us to work with it.
Hope this helps!

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: