Is there any easy way to query a heterogeneous collection, where the objects in the collection all derive from the same base class but some may be of one derived type and some may be of another?
For example, here’s a class hierarchy:
public class Ship
{
public string Name { get; set; }
public string Description { get; set; }
}
public class SailingVessel : Ship
{
public string Rig { get; set; }
public int NumberOfMasts { get; set; }
}
public class MotorVessel : Ship
{
public string Propulsion { get; set; }
public decimal TopSpeed { get; set; }
}
And here’s an XML document I want to query:
<?xml version="1.0" ?>
<ships xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ship xsi:type="sailingVessel">
<name>Cutty Sark</name>
<description>Tea clipper</description>
<rig>Ship</rig>
<numberOfMasts>3</numberOfMasts>
</ship>
<ship xsi:type="sailingVessel">
<name>Peking</name>
<description>Windjammer of the Flying P Line</description>
<rig>Barque</rig>
<numberOfMasts>4</numberOfMasts>
</ship>
<ship xsi:type="motorVessel">
<name>HMS Hood</name>
<description>Last British battlecruiser</description>
<propulsion>SteamTurbine</propulsion>
<topSpeed>28</topSpeed>
</ship>
<ship xsi:type="motorVessel">
<name>RMS Queen Mary 2</name>
<description>Last transatlantic passenger liner</description>
<propulsion>IntegratedElectricPropulsion</propulsion>
<topSpeed>30</topSpeed>
</ship>
<ship xsi:type="motorVessel">
<name>USS Enterprise</name>
<description>First nuclear-powered aircraft carrier</description>
<propulsion>Nuclear</propulsion>
<topSpeed>33.6</topSpeed>
</ship>
</ships>
I can query the XML document and read its contents into a list of Ship objects:
XDocument xmlDocument = XDocument.Load("Ships.xml")
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
var records = (from record in xmlDocument.Descendants("ship")
let type = record.Attribute(xsi + "type").Value
select new Ship
{
Name = (string)record.Element("name"),
Description = (string)record.Element("description")
}).ToArray<Ship>();
This returns the following:
Ship[0] (type: Ship):
Name: Cutty Sark
Description: Tea clipper
Ship[1] (type: Ship):
Name: Peking
Description: Windjammer of the Flying P Line
Ship[2] (type: Ship):
Name: HMS Hood
Description: Last British battlecruiser
Ship[3] (type: Ship):
Name: RMS Queen Mary 2
Description: Last transatlantic passenger liner
Ship[4] (type: Ship):
Name: USS Enterprise
Description: First nuclear-powered aircraft carrier
What I would really like to be able to produce, though, is this:
Ship[0] (type: SailingVessel):
Name: Cutty Sark
Description: Tea clipper
Rig: Ship
NumberOfMasts: 3
Ship[1] (type: SailingVessel):
Name: Peking
Description: Windjammer of the Flying P Line
Rig: Barque
NumberOfMasts: 4
Ship[2] (type: MotorVessel):
Name: HMS Hood
Description: Last British battlecruiser
Propulsion: SteamTurbine
TopSpeed: 28
Ship[3] (type: MotorVessel):
Name: RMS Queen Mary 2
Description: Last transatlantic passenger liner
Propulsion: IntegratedElectricPropulsion
TopSpeed: 30
Ship[4] (type: MotorVessel):
Name: USS Enterprise
Description: First nuclear-powered aircraft carrier
Propulsion: Nuclear
TopSpeed: 33.6
How can I modify the LINQ query to intialize a SailingVessel object or a MotorVessel object as appropriate, instead of a base Ship object?
Do I have to do two selects, and duplicate the object initialization for the base class properties (Name and Description) in each one? That is all I can think of but I hate the duplication of code involved. Alternatively, is there some way to initialize the properties for the base class and optionally initialize additional properties for a SailingVessel (Rig, NumberOfMasts) or a MotorVessel (Propulsion, TopSpeed) as appropriate?
Personally I would give each type a static
FromXElementmethod (or constructor), and then create aDictionary<string, Func<Ship>>like this:Then your query would be:
You could give the
Shipclass a protected constructor takingXElementto extract the common properties, leaving something like:There will certainly be a fair amount of code involved, but it will all be reasonably simple, and also easy to test.