I am currently working on a C# MVC project combined with Entity Framework, where I have a view page (Create page) on which the users will need to fill in the required data. I got a model something like the code below. Where the user has a Boat and can select its properties.
public class Boat {
public string Name{ get; set; }
}
public class Slowboat : Boat {
public int Length { get; set; }
public virtual Engine SpeedSource { get; set; }
}
public class Speedboat : Boat {
public virtual List<?> SpeedSource { get; set; }
public int Topspeed {get; set; }
}
public class Engine {
public string EningeType { get; set; }
public int Horsepower { get; set; }
public string Manufacturer { get; set; }
}
public class Sail {
public int SailCount { get; set; }
public List<int> SailSizes { get; set; }
public string Manufacturer { get; set; }
}
Everything goes well, a Speedboat has a couple of sails or an engine. The problem arises when a Speedboat has one or more Engine(s) and Sail.
I tried using List<object> type, but this didn't store my data into my database on posting. A kind of wrapper class like underneath is quick solution but there needs to be a better one in my opinion.
public class SpeedPower {
public Engine Engine_ { get; set; }
public Sail Sail_ { get; set; }
}
So my question is: how do you store multiple types into one list?
Thanks for a possible solution.
Edit: so as Thomas Flinkow suggested I can make an interface with some of the mutual properties.
public interface ISpeedSource {
string Manufacturer { get; set; }
}
public class Speedboat : Boat {
public virtual List<ISpeedSource> SpeedSource { get; set; }
public int Topspeed {get; set; }
}
So will the other proprties be saved if I use the interface like this?
Engineand aSailare not the same thing. So what exactly is a "SpeedSource"? Maybe your boats could have a collection of Engines and a collection of Sails as separate collections? Some boats simply have empty collections of one or the other. Unless you can define a common interface of what a "SpeedSource" is.