locked
EF repositrory IEnumerable<T> RRS feed

  • Question

  • I want to implement Repository pattern.

    I find out that many samples of that in the web contains methods which returns  IEnumerable<T> why not return IList<T> ?

    For example i Iist<T> implement IEnumerable<T> it is look like more easy to cast IList<T> to IEnumerable<T>. For example there is a lot of situations when you may need IList<T> insteard of IEnumerable<T>.

    Why IEnumerable<T> in preference ?

    Friday, August 20, 2010 10:28 AM

Answers

  • Hi,

    Well for starters, the IEnumerable<T> is an immutable interface. It does not allow you to change the underlying collection, while the IList<T> interface will allow this through methods like Add and Remove.

    It's mostly a matter of design, but it helps protect the enumerated collection against unwanted and in most cases uncontrolled changes.

    Hope this helps
    Meile Zetstra


    http://blogger.xs4all.nl/mzetstra

    Friday, August 20, 2010 10:40 AM
  • Hi,

    An IEnumerable<T> is not yet executed on the database. It contains a Linq Expression that will be executes when you call methods like "ToArray()" or "Single()" or "ToList()";

    This way your Repository can return generic queries and you can extend these queries as you need them and only hitting the database when you want to.


    Regards, Patriek
    Friday, August 20, 2010 11:20 AM
  • Hi,

    First of all, your free to return any type of object from your Repository<T>. Just return whatever is convenient for you. So if casting an IEnumerable<T> to a List<T> is convenient for you, you should do that.

    I don't know how large your collection will be, but I think you should optimize your Linq query to return a minimum result required. When this still is so big that "ToList()" doesn't perform well, you should consider paging the result.


    Regards, Patriek
    Monday, August 23, 2010 8:21 AM

All replies

  • Hi,

    Well for starters, the IEnumerable<T> is an immutable interface. It does not allow you to change the underlying collection, while the IList<T> interface will allow this through methods like Add and Remove.

    It's mostly a matter of design, but it helps protect the enumerated collection against unwanted and in most cases uncontrolled changes.

    Hope this helps
    Meile Zetstra


    http://blogger.xs4all.nl/mzetstra

    Friday, August 20, 2010 10:40 AM
  • Hi,

    An IEnumerable<T> is not yet executed on the database. It contains a Linq Expression that will be executes when you call methods like "ToArray()" or "Single()" or "ToList()";

    This way your Repository can return generic queries and you can extend these queries as you need them and only hitting the database when you want to.


    Regards, Patriek
    Friday, August 20, 2010 11:20 AM
  • I understand the difference. My question is if i need IList<T> should I have methods in Repository which returns only List<T> or i may have methods which returns IEnumarable<T> and convert result it to List<T> when i need List<T>. e.g: List<T> list = new List<T>(IEnumerable<T>); But to cast IList<T> to IEnumerable<T> not required to copy each item. e.g: IEnumerable<T> enumerable = IList<T>;

    => Performance to transform IList<T> to IEnumerable<T> is much more better rather than IEnumerable<T> to IList<T> moreover in the case to transform IList<T> to IEnumerable<T> it is only reference copy.

    Or i may have both methods List<T> and IEnumarable<T> and i n such case i may not to convert anything, when i need IEnumerable<T> i call method which returns IEnumerable<T> and when i need List<T> a call method which returns List<T>.

    There is so many different ways of doing this. What is your experience with that?

    Saturday, August 21, 2010 4:47 AM
  • Hi,

    First of all, your free to return any type of object from your Repository<T>. Just return whatever is convenient for you. So if casting an IEnumerable<T> to a List<T> is convenient for you, you should do that.

    I don't know how large your collection will be, but I think you should optimize your Linq query to return a minimum result required. When this still is so big that "ToList()" doesn't perform well, you should consider paging the result.


    Regards, Patriek
    Monday, August 23, 2010 8:21 AM