1

This is a three part question.

One: Would using a

Dictionary<String,Object>

be a good way of saving data where it would be

Dictionary<Key,Value>

as the basis?

Two: What would be a better way without using app.settings or xml?

Three: How would you serialize this(Or the better solution) into a binary format that is compact and serializes quickly?


Xml is very verbose and I was looking for binary due to it's size.
Also, if I used Dictionary, it would have been easy to make a settings class with simply

GetValue(Key) and SetValue(Key,Obj)
or object.SetValue(Settings,Key) using extensions.

I am making my application open for plugins so I wanted a uniform spec for settings that all applications use.

Rolling my own class using Dictionary as the basis made sense if I could serialize it into a file.

akshaykarthik
  • 1,055
  • 2
  • 13
  • 30
  • Why not just use app.settings? What's wrong with it? – Jouke van der Maas May 23 '10 at 12:17
  • xml is very verbose and I was looking for binary due to it's size. Also, if I used Dictionary, it would have been easy to make a settings class with simply GetValue(Key) and SetValue(Key,Obj) or object.SetValue(Settings,Key) using extensions. I am making my application open for plugins so I wanted a uniform spec for settings that all applications use. Rolling my own class using Dictionary as the basis made sense if I could serialize it into a file. – akshaykarthik May 23 '10 at 12:31

1 Answers1

1

Try using protobuf-net. This would solve all three of your problems. I looked at it last week, and replaced my entire xml-based messaging infrastructure in just a couple of days. Marc Gravell is a frequent contributor on here, and he was very prompt and helpful in his replies to my queries.

Carlos
  • 5,991
  • 6
  • 43
  • 82
  • How would I use this for settings? Can it serialize a dictionary? – akshaykarthik May 23 '10 at 13:39
  • Yes, you can serialize a dictionary. In fact, I did this in my solution. For "Settings" I take it you mean that you want to store some values and restore them later, as in save/load? This would be easy. You'd take your object that you want to save, generate a serialized representation, and save that. Then deserialize it when you like. The only issue is that protobufs saves stuff in a very compact binary form, so you wont be able to visually recognize that the stuff is saved correctly. But it will still work. – Carlos May 23 '10 at 13:50
  • Does it support Generics in that a dictionary would still serialize? Since this is a wrapper around a dictionary, would protobuf-net serialize the dictionary instance inside the settings class? Basically I am implimenting a key-value system in the dictionary where both the key and the value need to be serialized. sorry if I am sounding a bit noobish, I am still learning programming. – akshaykarthik May 23 '10 at 14:19
  • I think there's an issue around serializing object, because it doesn't quite know how to do it. That's where I had to make some small changes to my structure. Marc says V2, which is out sometime soon, will address this. BTW, there's a stackoverflow question about this somewhere, but I forget what it was. But the question specifically has an example where someone tries to serialize object, and Marc came up with a solution. – Carlos May 23 '10 at 14:32
  • @Carlos Sorry, but if you only want to directly serialize a "Settings" object, which inherits from Dictionary, I don't see how this is possible without a wrapper / container object with protobuf-net. – kitsune Jul 04 '11 at 08:37
  • @kitsune: What seems to be the issue? In my solution, I have a message class containing a dictionary, and there are appropriate attributes on both the class and the member. Also, since the message is inherited from a parent, the parent class also has an attribute specifying the child as being protobuffable. Apart from that, it just seems to work. Perhaps this is what you're missing? – Carlos Jul 05 '11 at 08:56