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

Dataset Surrogate for Silverlight

, 1 Oct 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

sommelo1
Software Developer (Senior)
Switzerland Switzerland
No Biography provided

Comments and Discussions

 
GeneralIf you want a complete solution check out SilverlightDS.com PinmemberVincegee5-Oct-09 9:15 
GeneralRe: If you want a complete solution check out SilverlightDS.com Pinmembersommelo15-Oct-09 22:54 
GeneralYou might want to look at this... PinmemberDewey2-Jun-09 18:24 
GeneralRe: You might want to look at this... Pinmembersommelo12-Jun-09 21:28 
QuestionWhat is the purpose for this? PinmemberDewey27-May-09 17:20 
AnswerRe: What is the purpose for this? Pinmembersommelo131-May-09 1:49 
QuestionDataset by code Pinmembernbilber11-Nov-08 6:03 
AnswerRe: Dataset by code Pinmembersommelo18-Dec-08 9:19 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 1 Oct 2008
Article Copyright 2008 by sommelo1
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid