views:

31

answers:

3

When using repository based persistence in a .net application, which collection type should normally be used?

IList, IEnumerable?

For example, the FindAll() method.

+1  A: 

For a read-only collection you could use ReadOnlyCollection<T>. The functionality FindAll provides for Lists can be gained on other types by using LINQ.

Mark Byers
+2  A: 

It depends.

  • IEnumerable<T> minimises the coupling between the calling code and the repository implementation. But all further processing (e.g. sorting) has to be done in process.
  • IQueryable<T> allows further operations to be added, which may (depending on the repository implementation) be performed on the underlying data store.

Since you can convert easily between either of these (there is an AsQueryable extension method that allows an IEnumerable<T> to be converted to a IQueryable<T> for typing, but of course processing is still done in memory).

Personally I like the potential benefits that IQueryable<T> gives, even if a non-LINQ based repository may not benefit from it now. On the other hand YAGTNI.

Richard
+1  A: 

I always use IEnumerable, cuz it's the smallest possible type to return, so you don't give the client stuff that he doesn't need

Omu