What are the framework design guidelines for exposing a custom collection vs generic one? e.g
public class ImageCollection : Collection<Image>
{
...
}
public class Product
{
public ImageCollection {get; set;}
}
VS
public class Product
{
public Collection<Image> Images{get; set;}
}
In general, it's best to expose one of the interfaces, such as IEnumerable<T>
, ICollection<T>
or IList<T>
instead of a concrete class.
This provides you with much more flexibility in terms of changing your internal API. IEnumerable<T>
, in particular, lets you potentially modify your internals later to allow streaming of results, so is the most flexible.
If you know the expected usage patterns of your "collections", you should expose the appropriate API that provides the least constraints on you in the future. If, for example, you know that people just need to iterate your results, expose IEnumerable<T>
, so you can change to ANY collection later, or even switch to just using yield return directly.