3

I have following scenario: I have say a Vehicle class that has several fields for each vehicle component:

public WheelBase Wheels;
public EngineBase Engine;
public TrainBase DriveTrain;

For each car component there are several classes inheriting from the base class, for example we may have: V8Engine : EngineBase, V6Engine : EngineBase, I6Engine : EngineBase, and so on.

If I want to save a car's loadout in a standard database (such as a text file), and when I need to load a car I need to know which "subtype" of component should be loaded. For example I want to load a car that has a V6Engine : EngineBase, SummerTireWheel : WheelBase, ManualTrain : TrainBase.

What's the best way to store and retrieve this information? The only way I can think of is save the name of the "subtype" in the database and use a group of switches to create the correct type of component, such as:

switch(EngineType)
{
  case "V8Engine" : 
    Engine = new V8Engine();
    break;
  case "V6Engine" :
    Engine = new V6Engine();
    break;
  case "I6Engine" :
    Engine = new I6Engine();
    break;
}

This method certainly works but I feel it's very ugly. Are there more elegant ways to handle this type of scenario?

anton.burger
  • 5,637
  • 32
  • 48
  • Which mode you prefer, DataTable-DataSet or Linq/EF. –  Oct 17 '13 at 04:56
  • @user, if you're saving it to a standard rdbms, then see this question: http://stackoverflow.com/q/18427546/661933. In short an enum is a good option. – nawfal Oct 17 '13 at 05:22
  • I would love to use serialization but the fact is I cannot use it due to limitation of my tool:) So serialization is out of question. – user2503766 Oct 17 '13 at 05:54
  • 1
    @user2503766 what are the tools you have avaible? What kind of database and/or file acces do you have? – Florian Oct 17 '13 at 07:37
  • Your original code is not a bad way to do this - it's basically how *table-per-hierarchy* in EF and other ORMs works. (http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/implementing-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application) – millimoose Oct 17 '13 at 07:42
  • The engine I'm using has very crappy serialization tool, it can't serialize classes derived from other classes. So I deployed a text file based database and save everything in clear text. – user2503766 Oct 19 '13 at 04:31

1 Answers1

1

I think the best way to do this it's with XML serialization and deserializtion

XmlSerializer serializer = new XmlSerializer(typeof(List<MyClass>));

using(FileStream stream = File.OpenWrite("filename"))
{
    List<MyClass> list = new List<MyClass>();
    serializer.Serialize(stream, list);
}

using(FileStream stream = File.OpenRead("filename"))
{
    List<MyClass> dezerializedList = 
        (List<MyClass>) serializer.Deserialize(stream);
}

Also see this link: XML - Can not deserialize after serialize (How to serialization and deserialization with inheritance)

Community
  • 1
  • 1
AsfK
  • 3,328
  • 4
  • 36
  • 73