blah blah blah is here! blah blah » Close

up1down
link

I've got a problem and I don't know what jargon would accurately describe it so I'll just put it out there...

public class MyClass
{

public List<Device> Devices;
public Library GlobalLibrary;

....

}


The GlobalLibrary is a hierarchy of items. At the top of this hierarchy is a schedule. It ties all the other items together. Each Device in Devices links to a single schedule. There can be multiple schedules in GlobalLibrary.

The contents of both (Devices and GlobalLibrary) are initially read in via an XML file (when the app starts) and are added to or edited via the UI. When this occurs, the updated objects are written out to the XML files.

The link between the two is maintained via a GUID. So, a given schedule in the GlobalLibrary has a GUID associated with it. Since it's the top of a large hierarchy there are many child elements associated with it (looking at the XML). Since I don't want to repeat this data in the devices XML file I simply want to use the GUID to link the two. Basically the GlobalLibrary is a database with that GUID being the key index.

So, here is what I am attempting...

I need to have each Device in list Devices maintain a reference to GlobalLibrary so that I may get at the schedule contained within and iterate through the GUIDs. Something like...

public class Device
{
[XmlIgnore]
public Library Library { get; set; }

[XmlAttribute("schedule_guid")]
public Guid Guid { get; set; }

[XmlIgnore]
public Schedule Schedule
{
get
{
Schedule sched = new Schedule();
foreach (Schedule s in this.Library.Schedules)
{
if (s.Guid == this.Guid)
{
sched = s;
break;
}
}
return sched;
}

set
{

}
}

}



I think this would work fine. My problem is how to assign the reference to the GlobalLibrary? Consider that the majority of the Device objects are created when the XML is deserialized. I could create a method in the Device class that takes a reference to a Library and assigns it to the Library property of the object but when would it be called? I can't wrap my mind around what seems to be a simple issue. Any thoughts?

last answered one year ago

1 answers

link

Well, if a Device object were not being created via deserialization, then the ideal place to set the Library property would be in a Device constructor which takes a parameter of type Library.

However, this wouldn't work if a Device object were being created via deserialization because the deserializer only uses a parameterless constructor and so there would be no way of telling in which Library object the Device's associated Schedule resides. In that scenario, you'd therefore have to set the property manually post-deserialization.

So, the best you can do to cover both scenarios would be:

public class Device
{
[XmlIgnore]
public Library Library { get; set; }

[XmlAttribute("schedule_guid")]
public Guid Guid { get; set; }

[XmlIgnore]
public Schedule Schedule
{
get
{
if (this.Library == null) return null;
Schedule sched = null;
foreach (Schedule s in this.Library.Schedules)
{
if (s.Guid == this.Guid)
{
sched = s;
break;
}
}
return sched;
}
}

public Schedule(){}

public Schedule(Library lib)
{
Library = lib;
}

}

// create other than via deserialization
Device dev = new Device(lib);

// create via deserialization
Device dev = new Device(); // called automatically
dev.Library = lib; // called manually

eeboy
499

Thanks! Not quite the elegant solution I had hoped for but it certainly works.

Feedback