Click here to Skip to main content
14,775,888 members
Articles » Web Development » Silverlight » General
Posted 1 Oct 2008


17 bookmarked

Dataset Surrogate for Silverlight

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
1 Oct 2008CPOL
Dataset surrogate for Silverlight


I was recently working on a project where all entities are transferred between the clients and the server in datasets. Suddenly somebody had the idea that we could write a read only client in Silverlight.

So I spent some time searching on the internet for a solution. But nobody had found any solution. Therefore, I started this little project. At the moment, it's only a one way communication from server to the client. But I think it should be easy to extend the solution to transfer the data from Silverlight back into a (untyped) dataset.

Using the Code

This is my example of a WCF-service. I have a method GetDataset that returns an ExampleDataSet. I added to the service-class a surrogate-attribute named [DataSetSerializableTypes] that is able to transform the dataset to an dataobject that Silverlight can use. This works with all typed and untyped datasets.

//This surrogate is converting the dataset to an dataobject 
//that can be consumed in Silverlight
public class DatasetService
    public ExampleDataSet GetDataset()
        ExampleDataSet ds = new ExampleDataSet();
        ExampleDataSet.AnimalRow aRow = ds.Animal.NewAnimalRow();
        aRow.Id = Guid.NewGuid();
        aRow.Name = "Monkey";

        ExampleDataSet.FoodRow fRow = ds.Food.NewFoodRow();
        fRow.Id = Guid.NewGuid();
        fRow.Name = "Banana";
        fRow.Amount = 25;

        return ds;

The only thing you have to take care of is to delete the namespace in the dataset. Otherwise this solution does not work.



First Step - Data Contract Surrogates

I found out that WCF had a powerful mechanism to change the serialization of everything that is sent between a service and a client.

You need first to implement the DataSetSerializableTypesAttribute interface. This is the attribute that is used to tell the WCF-service that you want to override the default serialization for a certain type. Then you will need a second interface named IdataContractSurrogate that is the one which is actually doing the transforming job from dataset to an dataobject.

Second Step - Generation of a Type on Runtime

In .NET, you can write assembler code that is able to generate a new type on runtime. This mechanism is very very powerful. On the internet, I found some ideas about how to create a class builder.

I changed this code a little bit, the code is now able to transform the entire dataset into a dataobjects collections.

Third Step - Silverlight Proxy

The main idea is that when Visual Studio reads the WCF metadata, it will only see the dataobject and the Silverlight proxy will be automatically generated correctly. You can even change the dataset and the only thing you need to do is just update the service reference.



This is an experimental solution, but it allows you to use datasets as dataobjects in Silverlight. Have fun!


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


About the Author

Software Developer (Senior)
Switzerland Switzerland
No Biography provided

Comments and Discussions

GeneralIf you want a complete solution check out Pin
Vincegee5-Oct-09 9:15
MemberVincegee5-Oct-09 9:15 
GeneralRe: If you want a complete solution check out Pin
sommelo15-Oct-09 22:54
Membersommelo15-Oct-09 22:54 
GeneralYou might want to look at this... Pin
Dewey2-Jun-09 18:24
MemberDewey2-Jun-09 18:24 
GeneralRe: You might want to look at this... Pin
sommelo12-Jun-09 21:28
Membersommelo12-Jun-09 21:28 
QuestionWhat is the purpose for this? Pin
Dewey27-May-09 17:20
MemberDewey27-May-09 17:20 
AnswerRe: What is the purpose for this? Pin
sommelo131-May-09 1:49
Membersommelo131-May-09 1:49 
QuestionDataset by code Pin
nbilber11-Nov-08 6:03
Membernbilber11-Nov-08 6:03 
AnswerRe: Dataset by code Pin
sommelo18-Dec-08 9:19
Membersommelo18-Dec-08 9:19 

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.