List, IList, IEnumerable, IQueryable, ICollection, which is the most flexible return type?

I saw this question posted here earlier, but I am not satisfied that I understand the full consequences. The problem is what type of return should have a data layer that uses linq-to-sql return for maximum flexibility and query capability. This is what I read / found:

  • IEnumerable is limited and only allows read forward. IEnumerable is the most common. I found that IEnumerable allows query operations and extension syntax.

  • The list allows more flexibility due to insert operations.

  • Instead of a list, you can use collections to include read-only collections.

  • IQueryable should never be used, it should be "used and turned off." IQueryable does not return a list, but generates query syntax for the database.

I feel that I am better at compromises, but still not sure about some things:

  • Why do I need to choose interface options over specific types? Ie IList or ICollection vs List or Collection. What benefit will I get?

  • I see extension operations work, but will extended query syntax also work?

  • Someone suggested using AsQueryable () before. But why should I do this if I do not have a database connection? Extension methods seem to work independently.

+47
collections architecture linq-to-sql
Aug 24 '10 at 18:42
source share
4 answers

Collections are generally not very useful for DAL returns because the collection does not imply a warranty order. This is just a bucket of items. IList, on the other hand, implicitly guarantees order. So, we get to IEnumerable or IList. Next question: List object "live"? that is, it is related to data support, so when you add an element to IList, will it be displayed in the database? For LINQ-to-SQL, this is not the case. Rather, you should attach entities to tables. Therefore, if you do not provide this additional posting, the list will be redundant. Stick with IEnumerable.

+29
Aug 24 '10 at 18:51
source share

IEnumerable <> => Allows the use of 'foreach' in a collection

ICollection <> => Since IEnumerable <> + Add (), Remove (), Count, Clear (), Contains (), IsReadOnly, CopyTo ()

IList <> => Like ICollection <> + this [int], Insert (), IndexOf (), RemoveAt (). i.e. It adds an index type to list operators

• May use 'return yield ???' combined with IEnumerable <> to return only one object at a time. Here comes the real power of IEnumerable (and not just the return of a list or array).

• When returning the list, decide what can be exposed to the user and return the appropriate type.

• Perhaps it is best to return ICollection <> or IList <>, and if the code client only needs to list the list, it can pass it to IEnumerable <>. i.e. Given ICollection SomeMethod () ... The user can call it as IEnumerable widgets = SomeMethod ()

+33
Jun 19 '12 at 8:30
source share

You should always return an interface, not a specific type, this goes without saying, as it indicates acceptable behavior without binding the consumer to a specific implementation.

In terms of the interface to return, you should think about the purpose of the method and the intentions of the caller. If you return the collection, should the caller change the collection? for example add / remove items? if all they need to do is list it (do foreach), then you should just return IEnumerable.

+7
Feb 16 '12 at 19:06
source share

1) It is best to return an IList so that the results can be placed in any object that implements this interface, instead of forcing the caller to use List. For example, the caller may wish to return the results to an ArrayList; this would not be possible if the results were returned to the list. ArrayList does not inherit List, but implements IList.

+3
Aug 24 '10 at 21:07
source share



All Articles