Click here to Skip to main content
Click here to Skip to main content

Custom DomainService metadata properties

By , 29 Jul 2012
 

Introduction

This article simply add 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 this 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:

<navigation:Page.Resources>
    <riacontrols:DomainDataSource x:Key="cityDDS" AutoLoad="True" QueryName="GetCities" >
        <riacontrols:DomainDataSource.DomainContext>
            <data:DomainService1 />
        </riacontrols:DomainDataSource.DomainContext>            
    </riacontrols:DomainDataSource>
</navigation:Page.Resources>
<sdk:DataGrid Grid.Row="0" AutoGenerateColumns="False" Margin="10" ItemsSource="{Binding Source={StaticResource cityDDS}, Path=Data}">
      <sdk:DataGrid.Columns>
          <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}" />
      </sdk:DataGrid.Columns>
</sdk:DataGrid>

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
    {
        [DataMember]
        public string CityData
        {
            get { return this.CityID.ToString() + " - " + this.CityName;}
        }
    }

    public partial class Country
    {
        [DataMember]
        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 out 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:DataGrid.Columns>
         <sdk:DataGridTextColumn Header="CityID" Binding="{Binding CityData}" />
         <sdk:DataGridTextColumn Header="Country Name" Binding="{Binding Country.CountryData}" />
     </sdk:DataGrid.Columns>
</sdk:DataGrid> 

After that we can Run our project (F5) and see our two DataGrids showing our Application

Points of Interest

Note that we have two DataGrid 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 projectDomainService.partial.cs file.

So, I hope you can find useful this brief tutorial where I can share with you. It's very important the usage of this technique in some situations. 

What's next 

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

You can visit my Spanish Blog too.  

License

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
Member
I'm from Asuncion, Paraguay I live and work here. I've been a software developer for 10+ years, working with .NET and web standards.
 
During the day I'm a .NET full time developer, working with developers (SCRUM on it!) to help them to bring outstanding WPF, Silverlight and web applications to the Market.
 
In my free time I'm Tien Shan Pai Kung Fu & Kuk Sool Won student. I'm trying to learn mobile development (Android & Windows Phone) and working very hard on it!

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5 PinmemberDulce Barrios5 Sep '12 - 16:27 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 30 Jul 2012
Article Copyright 2012 by Christian Amado
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid