Mark
Mark

Reputation: 419

Scope of serialization

3 classes

  1. organizationalUnit
  2. workFile
  3. workItem

They're in some sort of Pyramid pattern:
1. Contains one or more of 2.
2. Contains multiple of 3.
Each contains a reference to their "container":
All instances of 3 have a ref to the 2 that holds them
same for 2. in regards to 1.

How do I serialize this mess?
Will the lower tier objects be serialized and stored when I serialzie an instance of 1?
I already read this question(and this as well), that indicates the serializing the instance of ´organizationalUnit´ would do the trick, however that question is about java, not c#/.NET

Upvotes: 1

Views: 304

Answers (1)

Roman Mik
Roman Mik

Reputation: 3279

I would exclude the back references from the Serialization. Example:

 [Serialiazable]
 Public class OrganizationUnit
 {
    public int ID {get; set;}
    public IEnumerable<WorkFile> WorkFiles {set; get}
 }

 [Serialiazable]
 Public class WorkFile
 {
    public int ID {get;set;}
    [NonSerialized]
    public OrganizationUnit ParentOrgUnit {set; get;}
    public IEnumerable<WorkItem> WorkItems{set; get}
 }

 [Serialiazable]
 Public class WorkItem
 {
    public int ID {get; set;}
    [NonSerialized]
    public WorkFile ParentWorkFile {set; get;}

 }

Later, if you need to deserialize from XML (or other format) back into objects. I would loop through object and set the reference's to parents. Something like this:

foreach( OrganizationUnit unit in listOfOrganizationUnits)
{
    var workFiles = unit.Workfiles;
    foreach(Workfile workFile in workFiles)
    {
       workFile.ParentOrgUnit = unit; //Set the reference to Organization Unit
       workFile.WorkItems.foreach(i=> i.ParentWorkFile = workFile); //Set the reference to Parent Work File
    }
}

Upvotes: 3

Related Questions