2

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?

1
  • 1
    It sounds like the conceptual modeling is broken. An Engine and a Sail are 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. Commented Apr 20, 2018 at 12:38

1 Answer 1

1

You can define all your "speed sources" like this:

public class Engine : ISpeedSource { ... }

public class Sail : ISpeedSource { ... }

with ISpeedSource being a simple (maybe empty, but not necessarily) interface

public interface ISpeedSource { ... }

And then have your Speedboat class like this:

public class Speedboat : Boat 
{
    public virtual List<ISpeedSource> SpeedSource { get; set; }
    public int Topspeed { get; set; }
}

You might want to overthink your design as David suggested, because empty interfaces should be avoided.

Sign up to request clarification or add additional context in comments.

10 Comments

As you state, empty interfaces should be avoided. More specifically in this case, when posting a form from the client, I doubt the model binder would know what to do with an empty interface. If all the model binder knows is that it's a collection of ISpeedSource and an ISpeedSource has no properties, it probably wouldn't store anything useful in that collection.
@Thomas Flinkow I like the suggestion of the interface because there a a few common properties, so I edited my question. But I also have a common class that has a List that will be of a different size by the type of "speedSource", would it be enough to limit this List size on the front-end of my application?
@Kitesaint1309 If I understand this correctly, you want the interface to have a List<SpeedSize>? If so, you could just do this: interface { IList<SpeedSource> MyList { get; } if it isn't absolutely necessary to restrict the size.
@ThomasFlinkow yes something like that, I my case it would be something like interface { IList<string> CrewNames { get; set; } which is dependent on the type of boat (sailboats need more crew then engine boats).
@Kitesaint1309 alright, that would be perfectly fine and imo should work.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.