ICollection <T> Vs List <T> in Entity Framework
I only watched a few webcasts before I went first to develop several Entity Framework applications. I really have not read this documentation, and I feel that I am suffering from it now.
I used List<T> in my classes and it did a great job.
Now I read some documentation and it says that I should have used ICollection<T> . I changed this, and it did not even cause a change in the model context. Is it because both List<T> and ICollection<T> inherit IEnumerable<T> , and this is what is actually required for EF?
However, if so, why is the IEF documentation not IEnumerable<T> instead of ICollection<T> ?
In any case, are there any flaws in what I did, or should I change it?
Entity Framework will use ICollection<T> because it needs to support Add operations that are not part of the IEnumerable<T> interface.
Also note that you used ICollection<T> , you just displayed it as an implementation of List<T> . List<T> combines IList<T> , ICollection<T> and IEnumerable<T> .
As for your change, demonstrating through the interface is a good choice, even though List<T> works. The interface defines the contract, but not the implementation. Implementation may change. In some cases, for example, the implementation may be a HashSet<T> , for example. (This is a way of thinking that you could use not only as an Entity Framework, by the way. A good object-oriented practice is to program the interface, not the implementation. Implementations can and will change.)
They chose the interface they made because it gives a clear abstraction of the magic queries that the Entity Framework performs when using Linq.
Here is the difference between the interfaces:
IEnumerable<T>is read-only- You can add and remove elements in
ICollection<T> - You can do random access (by index) to
List<T>
Of these, ICollection and IEnumerable well displayed for database operations, since queries and adding / removing objects are things you can do in the database.
Random access by index is also not displayed, as you will have to have an existing query result for repetition or each random access will query the database again. In addition, what will the index focus on? Line number? There are not many row queries, and this is not useful when creating large queries. Therefore, they simply do not support it.
ICollection<T> supported and will allow you to both query and modify data, so use this.
The reason List<T> begins with the fact that the implementation of EF ends with a return at the end. But this is at the end of your query chain, not at the beginning. Therefore, creating your ICollection<T> properties will make it more obvious that EF creates a bunch of SQL and returns only List<T> at the end, rather than querying for every Linq level you use.
ICollection differs from IEnumerable in that you really can add items to the collection, whereas with IEnumerable you cannot. Thus, for example, in your POCO classes you want to use ICollection if you are going to allow the collection to be added. Make the ICollection virtual machine also useful for lazy loading.
Although the question was published many years ago, it is still valid when someone is looking for the same scenario.
There is a recent article by CodeProject [2015] that explains the difference in detail and graphic representation with sample code . He does not focus directly on EF, but still hopes that he will be of great help:
List vs IEnumerable vs IQueryable vs ICollection vs IDictionary