Parent / child relationships in interfaces

If I had interfaces like this:

public interface IAlbum
{
    string Title { get; set; }
}

public interface ITrack
{
    string Title { get; set; }
    float Duration { get; set; }
}

      

What would be the best way to add a track list to the IAlbum interface? What if I wanted individual tracks to be properties and not have an open array of tracks?

+2


source to share


3 answers


The track list can be:

IList<ITrack> Tracks {get;}

      

or

IEnumerable<ITrack> Tracks {get;}

      

If you wanted a specific type to represent a more specific API, you could implement the above with an explicit implementation.



You could theoretically do:

interface IAlbum : IEnumerable<ITrack> {...}

      

but I don't think this is a good idea.

Re-displaying tracks as properties; if I understand correctly, you can make this "view" at runtime by implementing ICustomTypeDescriptor

in a specific type of album. But this is non-trivial and a little messy - and it won't help unless you are using data binding.

+2


source


Use indexer . In particular:



    ITrack this[int trackIndex]
    {
        get;
    }

      

+5


source


You can also specify an indexer that works with the track name. It is [the kind] between properties for tracks and having a collection.

ITrack this [string trackName]
{
    get {return getTraxckUsingName (trackName); }
}
0


source







All Articles