design patterns - Serialization and Deserialization When References Are Involved in C# -


i have singleton class called manager holds list of object instances:

static class manager {     static list<foo> foos = new list<foo>(); } 

i have collection of object instances using class called meter uses references items in list foos:

class meter {     public foo myfoo = null; }  ...  public void createmeter(int userchoice) {     meter mymeter = new meter();     mymeter.myfoo = manager.foos[userchoice]; } 

when application saves project file serializes instances of foo in foos along instances of meter.

my problem how deserialize arrangement. following:

  • deserialize project-wide instances of foo reconstruct manager.foos
  • deserialize meter instance includes instance of foo myfoo property
  • search manager.foos , find matching reference mymeter.myfoo , assign reference manager.foos.

this seems me clunky , not easy extend. rather meter factory doesn't need search manager.foos during deserialization because in future meter might take it's refence foo instance other places, not manager.

is there simple flexible alternative solution deserialization problem references objects can reconstructed?

serialisation hard :)

doing automatically in way doesn't mess format pain. common trick here assign unrelated opaque key while de/serialising, using central map. can see in datacontractserializer enabling reference-tracking in constructor. key used check existing objects substitute.

personally, when gets complex imo time use pre-canned aerializer; inside dedicated library bit challenging. approach use (protobuf-net) pretty similar, harder read (binary dense output, etc).


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -