Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Dataset Surrogate for Silverlight

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
1 Oct 2008CPOL2 min read 47.6K   641   17   8
Dataset surrogate for Silverlight

Introduction

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.

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

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

        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.

dataset.jpg

Background

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.

http://msdn.microsoft.com/en-us/library/ms733064.aspx

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.

http://silverlight.net/forums/p/16733/59115.aspx#59115

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.

update.jpg

History

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

License

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


Written By
Software Developer (Senior)
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralIf you want a complete solution check out SilverlightDS.com Pin
VincentGee15-Oct-09 8:15
VincentGee15-Oct-09 8:15 
GeneralRe: If you want a complete solution check out SilverlightDS.com Pin
sommelo15-Oct-09 21:54
sommelo15-Oct-09 21:54 
GeneralYou might want to look at this... Pin
Dewey2-Jun-09 17:24
Dewey2-Jun-09 17:24 
GeneralRe: You might want to look at this... Pin
sommelo12-Jun-09 20:28
sommelo12-Jun-09 20:28 
QuestionWhat is the purpose for this? Pin
Dewey27-May-09 16:20
Dewey27-May-09 16:20 
AnswerRe: What is the purpose for this? Pin
sommelo131-May-09 0:49
sommelo131-May-09 0:49 
QuestionDataset by code Pin
nbilber11-Nov-08 5:03
nbilber11-Nov-08 5:03 
AnswerRe: Dataset by code Pin
sommelo18-Dec-08 8:19
sommelo18-Dec-08 8: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.