Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Dataset Surrogate for Silverlight

0.00/5 (No votes)
1 Oct 2008 1  
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.

//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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here