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.
[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.
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.
History
This is an experimental solution, but it allows you to use dataset
s as dataobject
s in Silverlight. Have fun!