I have a typed data set and I want to pass tables(which are created by .net) or collection of rows instead of objects(which I would be creating) or collecion of objects to the client side. Silverlight framework doesn't support system.data.datatable.
-
1Good question is a half of answer. If framework does not support it it is not possible. WCF will not help if type is not available in framework. – Dmitry Harnitski Mar 01 '13 at 22:22
-
Let me get this right. You've got some TypedDataSet on the server and you want Silverlight to automatically generate the Types on the client side, and then you want to serialize the TypedDataSet on the server to a bytestream, and deserialize it on the client side? – Chui Tey Mar 05 '13 at 14:22
-
@ChuiTey calls to service methods, through service reference returns object instances of the types which exist within the service reference. I can return collections of custom types using the functions. I'd like to return as typed data set types. – Uğur Gümüşhan Mar 05 '13 at 14:27
3 Answers
you don't need to add datacontract attribute to types you don't own. You can implement IDataContractSurrogate to replace instances of client-unknown types with known-type instances (for example lightweight datatable POCO).
If you used code-first approach, you wouldn't have this extra projection-copy operation between typed dataset class objects and your own POCO objects on serialization/deserialization (and you would have full control over data object types (POCOs)).
I find useful to use Json.Net 'any object to JObject' convertor (pretty fast and customizable) as first step before converting to something else:
public static class JsonExtensions
{
public static object Normalize(this JToken token)
{
var type = token.GetType();
if (type == typeof(JObject))
{
return (token as JObject).OfType<JProperty>().ToDictionary<JProperty, string, object>(property => property.Name, property => property.Value.Normalize());
}
if (type == typeof(JProperty))
{
var property = token as JProperty;
//return new DictionaryEntry(property.Name, property.Value.Normalize());
return new KeyValuePair<string, object>(property.Name, property.Value.Normalize());
}
if (type == typeof(JValue))
{
return (token as JValue).Value;
}
if (type == typeof(JArray))
{
//return (token as JArray).OfType<JValue>().Select(value => value.Normalize()).ToArray();
return (token as JArray).Select(value => value.Normalize()).ToArray();
}
throw new NotImplementedException();
//return null;
}
}
public class TestClass
{
public string StringProperty { get; set; }
public int IntProperty { get; set; }
public TestClass RefProperty { get; set; }
}
private static string DataContractXmlSerialize<T>(T source)
{
var serializer = new DataContractSerializer(source.GetType());
using (var ms = new MemoryStream())
{
serializer.WriteObject(ms, source);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
Usage:
var test = new TestClass()
{
StringProperty = "StringProperty",
IntProperty = int.MaxValue,
RefProperty = new TestClass() { IntProperty = int.MinValue }
};
var jObj = JObject.FromObject(test);
var dict = jObj.Normalize();
var serializedDict = DataContractXmlSerialize(dict);
as you can see - output is WCF-serializable (standard dictionary being serialized produces not very nice xml with but you can use your own serializable dictionary)

- 2,431
- 16
- 18
-
I pass data without serialization and it works pretty well. I am looking for ways to pass objects without creating classes for each entity – Uğur Gümüşhan Mar 04 '13 at 15:41
-
You can implement universal method for conversion any entity to hierarchy set of dictionary
– SalientBrain Mar 04 '13 at 18:03 -
that's what I do. what I am trying to do is making the "object in that dictionary" an instance of a type which is created by a typed dataset. – Uğur Gümüşhan Mar 05 '13 at 07:23
-
I mean deep recursive conversion - when each non-primitive object becomes dictionary
. You can cache property getters for certain types to speed up reflection based conversion. – SalientBrain Mar 05 '13 at 08:27 -
-
You simply cannot use the ADO.NET implementation of a DataTable in your Silverlight client, but there are alternatives.
However, this blog post has an alternative DataTable implementation that you can serialize and can support in Silverlight.

- 2,310
- 1
- 13
- 19
-
This is similar to simple datatable but not typed dataset 'entity' class. – SalientBrain Mar 03 '13 at 17:53
-
I pass data without serialization and it works pretty well. I am looking for ways to pass objects without creating classes for each entity – Uğur Gümüşhan Mar 04 '13 at 15:41
If you want to access data in Silverlight application you should use RIA Services. You should create custom DTO object and create list of DTO objects from your DataTable rows and return it from RIA Service.
You get started with RIA Services follow MSDN at http://msdn.microsoft.com/en-us/library/ee707376(v=vs.91).aspx

- 29
- 1