Click here to Skip to main content
13,147,050 members (62,245 online)
Click here to Skip to main content
Add your own
alternative version


3 bookmarked
Posted 29 Jul 2012

Custom DomainService Metadata Properties

, 29 Jul 2012
Rate this:
Please Sign up or sign in to vote.
This brief tutorial pretends to demonstrate how to add custom properties inside DomainService's metadata class


This tip simply adds a custom property in the metadata generated by the DomainService metadata.

Many times, we need to add some extra properties in our metadata generated, for example, when we have to calculate something or concatenate some strings.

So, let's get started with this.

The Normal Feature

In this case, we must create a Silverlight Business Application. So, we concentrate on the ASP.NET Web Project. After that, we need to create a database file with two tables. Just like this:

At this point, we need to create the ADO.NET Entity Data Model, the Domain Service class and the metadata for that Domain Service. So, the Web ASP.NET Application looks like:

In the DomainService1.metadata.cs, you should replace the implementation of GetCities() method. Add these lines of code:

public IQueryable<City> GetCities()
    return this.ObjectContext.Cities.Include("Country").OrderBy(c=>c.CityName);

Just finished this part of the tutorial.

Using the Code

Now in our Silverlight Application project, expand Views/Home.xaml and open it. After that, just add the following XAML code:

    <riacontrols:DomainDataSource x:Key="cityDDS" AutoLoad="True" QueryName="GetCities" >
            <data:DomainService1 />
<sdk:DataGrid Grid.Row="0" 

AutoGenerateColumns="False" Margin="10" 

ItemsSource="{Binding Source={StaticResource cityDDS}, Path=Data}">
          <sdk:DataGridTextColumn Header="CityID" 

          Binding="{Binding CityID}" />
          <sdk:DataGridTextColumn Header="City Name" 

          Binding="{Binding CityName}" />
          <sdk:DataGridTextColumn Header="Country Name" 

          Binding="{Binding Country.CountryName}" />
          <sdk:DataGridTextColumn Header="CountryID" 

          Binding="{Binding Country.CountryID}" />

In the code above, we add a simple DataGrid showing simple data that we retrieve from the Web project via WCF RIA Services.

So, we go back to the Web project and add our Metadata extension(DomainService1.partial.cs) just like this:

using System.Runtime.Serialization;

namespace MetadataExtension.Web
    public partial class City
        public string CityData
            get { return this.CityID.ToString() + " - " + this.CityName;}

    public partial class Country
        public string CountryData
            get { return this.CountryID.ToString() + " - " + this.CountryName;}

In the code above, you must note that we add a partial class to share the existing properties on the metadata class.

Now, we need to compile our Web project and we haven't got any error.

In our Home.xaml file, we add the following lines of XAML code:

<sdk:DataGrid Grid.Row="1" AutoGenerateColumns="False" 

Margin="10" ItemsSource="{Binding Source={StaticResource cityDDS}, Path=Data}">
         <sdk:DataGridTextColumn Header="CityID" 

         Binding="{Binding CityData}" />
         <sdk:DataGridTextColumn Header="Country Name" 

         Binding="{Binding Country.CountryData}" />

After that, we can Run our project (F5) and see our two DataGrids showing our application.

Points of Interest

Note that we have two DataGrids in our demo. The first one shows the data directly from our DomainService metadata. The last DataGrid shows the data through the custom metadata property located on the Web project: DomainService.partial.cs file.

So, I hope you can find this brief tutorial useful. The usage of this technique is very important in some situations.

What's Next

Nothing. Just comment, rate and bookmark it if you like.


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


About the Author

Christian Amado
Software Developer
Paraguay Paraguay
Christian Amado is a Microsoft MVP, software engineer, pro developer and trainer with over 13 years of experience building application for Windows Desktop and the Web. Located in Asuncion, Paraguay, He is well involved in many Microsoft's technologies including XAML, C#, WCF, ADO.NET and ASP.NET.

Christian is quick to embrace new development technologies and has won some awards in programming competitions organized by communities. He is also involved in the Microsoft Student Partner program helping nominees students and is the Group Leader in the only Windows Phone Developers Group in Paraguay.

He holds a several Microsoft certifications including Microsoft Certified Professional Developer (MCPD), Microsoft Certified IT Professional, Microsoft Certified Technology Specialist and Microsoft Office Specialist.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Dulce Barrios5-Sep-12 16:27
memberDulce Barrios5-Sep-12 16:27 

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 | Terms of Use | Mobile
Web03 | 2.8.170915.1 | Last Updated 30 Jul 2012
Article Copyright 2012 by Christian Amado
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid