I’m trying to create an interface for a model class in ASP.NET MVC2 and I wonder if I can use a List<interface> within another interface. It’s better if I give a code example.
I have two interfaces, a terminal can have multiple bays. So I code my interfaces like the following.
Bay Interface:
public interface IBay
{
// Properties
int id {get; set;}
string name {get;set;}
// ... other properties
}
Terminal Interface:
public interface ITerminal
{
// Properties
int id {get;set;}
string name {get;set;}
// ... other properties
List<IBay> bays {get;set;}
}
My question is when I implement my class based on these interfaces how to I set up the list of bays. Am I going to have to do the list of bays outside the ITerminal interface and inside the concrete implementation?
My goal is to be able to do the following:
Concrete implementation:
Bay Class:
class Bay : IBay
{
// Constructor
public Bay()
{
// ... constructor
}
}
Terminal Class:
class Terminal : ITerminal
{
// Constructor
public Terminal()
{
// ... constructor
}
}
And then be able to access the list of bays like this
Terminal.Bays
Any help/suggestions would be greatly appreciated.
This should work fine. Just realize that your Terminal class will still contain a
List<IBay>, which can be populated withBayinstances as needed. (Note that I would recommend usingIList<IBay>instead, however.)If you want terminal to return a concrete
Baytypes, then you would need to redesign your Terminal interface, and modify this as:However, there may be little value in adding this complexity.