I've seen this question posted here previously but I'm not satisfied that I understand the complete ramifications. The problem is what return type should a data layer that uses linq-to-sql return for maximum flexibility and query ability. This is what I've read/found:
IEnumerable is limited and only allows for read forward operation. IEnumerable is the most generic. What I've found is that IEnumerable does allow query operations vs the extension syntax.
List allows for most flexibility because of insert operations.
Collections should be used instead of list to enable read only collections.
IQueryable should never be used, it should be "used and turned off". IQueryable doesn't return a list but generates a query syntax for database.
I feel I have a better feel for the trade offs but still not sure about a few things:
Why would I choose the interface variants over the concrete types? I.e IList or ICollection vs List or Collection. What benefit would I get?
I see that the extension operations work but will the expanded query syntax work as well?
Someone suggested I use AsQueryable() before. But, why would I do this if I don't have connection to the database? It seems the extension methods work regardless.
If you do not counting in your external code it is always better to return IEnumerable, because later you can change your implementation (without external code impact), for example, for yield iterator logic and conserve memory resources (very good language feature by the way).
IQueryable,IList,IDictionary,ICollection inherits IEnumerable Interface. All the interfaces of type collection will inherits IEnumerable Interface. Differences Between IEnumerable and IQueryable IEnumerable:-when you are running a query which returns of type IEnumerable.
IList doesn't support further filtering. IEnumerable exists in System. Collections Namespace. IEnumerable is a forward only collection, it can't move backward and between the items.
Collections are not generally very useful for DAL returns, because a collection does not implicitly guarantee order. It's just a bucket of items. An IList, on the other hand, does implicitly guarantee order. So we're down to IEnumerable or IList. The next question would be: is the List object "live"? i.e., is it connected to the data backing so that when you add an item to the IList, it will be reflected in the DB? For LINQ-to-SQL, this is not the case. Rather, you're supposed to attach entities to the tables. So unless you supply this additional wiring, a List is superfluous. Stick with IEnumerable.
You should always return an interface rather than a concrete type, this goes without saying as it specifies the behaviour allowed without tying the consumer to a specific implementation.
In terms of which interface to return, you should think about the purpose of the method and the intentions of the caller. If you return a collection, should the caller be able to alter the collection? e.g. add/remove items? if all they need to do is enumerate it (do a foreach) then you should just return an IEnumerable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With