views:

50

answers:

3

Right now I am programming a multiplayer card game in Silverlight

I am wondering how to store a object best binary in a sql database.

I have a GameState Object which contains the current state of a game between two players. I want to store this object in a sql database that both player can access and change it anytime.

How would you do that with Silverlight + RIA Services? Especially the part where you serialize the object in Silverlight.

Thanks

+1  A: 

I would do the serialization on the server side. Add an Invoke operation to your RIA services domain context that accepts your GameState object. On the server side you can then use standard .NET serialization (personally I would recommend XML serialization instead of binary, but it shouldn't matter).

Stephan
So it's possible to send my GameState object and serialize it server-side. Doesn't it take to long to send the object from client to server
Ben
If i add a Invoke Operation i get a compiler error:Parameter types must be an entity type or one of the predefined serializable type
Ben
It shouldn't take long since RIA is just serializing it anyway. RIA serializes anything you send across to the server and should be very fast. If you are really worried about speed you could serialize the GameState object to XML on the client side and then send that back to RIA, but I think the speed difference between handling serialization on the server vs the client will likely be negligible.
Stephan
@Ben in that case you probably should serialize to XML client side and send that off to your database. As far as I know SL doesn't contain the framework classes for binary serialization, so if you want binary you will need to serialize on the server side. To solve the compiler error you should port your GameState object to the server and make it an entity. You can still work with it fine on the client, but then RIA will serialize it.
Stephan
It is simply impossible to transfer between the client and the server without serialization taking place BEFORE transfer. And a little digging into Silverlight will reveal that all means of transfer are over http (meaning binary objects must be encoded/decoded).Having said that, the point of RIA services is to hide the complexities of working with the web methods and serialization. You create the entities on the server, compile, then create the domain service. The next compile will then cause code generation in the client to consume the service.
Kirk
+1  A: 

First, you can not possibly simply serialize something at the server. It must be serialized before it can be sent to the server. But it seems that perhaps you are making things too complicated/magical.

Given what you have said, I would start with by defining my GameState object (and any other object you need) inside the Entity Framework. Include any and all fields that are needed to save the state of the game. Then you should be able to have the framework create the needed tables.

Once you have done this, add a DomainService to the web project and when you compile the objects will then be available inside your Silverlight project.

Kirk
Thanks,So you mean in case of my card playing game I create tables for the hand of the player, a table for the playground and a table for the player's deck and so on. Afterwards i have to add it to my Entity Framework and to RIA Services. That's what i did before. But i was wondering about performance.Isn't it better to just have ONE table GameState with a big binary field where the whole classes are saved into?
Ben
I don't think your going to find a way to magically shove the whole thing into some binary glob of data. If you can then perhaps. Meanwhile focus on ensuring you use relational data eliminating any duplication of information. Then you can setup your queries to include related entities in a single call to the service.
Kirk
A: 

Finally i decided to use XML serialization.

I found a great article about XML Serialization: http://www.ingebrigtsen.info/post/2008/11/29/Serialization-in-Silverlight.aspx

That's how it looks like in my Silverlight code:

public static class MySerializer
{
    public static string Serialize<T>(T data)
    {
        using (var memoryStream = new MemoryStream())
        {
            var serializer = new DataContractSerializer(typeof(T));
            serializer.WriteObject(memoryStream, data);

            memoryStream.Seek(0, SeekOrigin.Begin);

            var reader = new StreamReader(memoryStream);
            string content = reader.ReadToEnd();
            return content;
        }
    }

    public static T Deserialize<T>(string xml)
    {
        using (var stream = new MemoryStream(Encoding.Unicode.GetBytes(xml)))
        {
            var serializer = new DataContractSerializer(typeof(T));
            T theObject = (T)serializer.ReadObject(stream);
            return theObject;
        }
    }
}
Ben