1

Could I return List from my DAL ?, In internet I read somewhere that it's not good. What is the problem if return List ?

LOZ
  • 1,169
  • 2
  • 16
  • 43
kayak
  • 1,805
  • 5
  • 18
  • 22

2 Answers2

9

You can return a List<T> object, but the property should be typed IList<T> instead. This will allow you to change the list implementation that you use at a later time, without breaking binary compatibility with existing assemblies compiled against your library.

This example probably won't match your constructor pattern, but it demonstrates what I mean (returning a List<T> through an IList<T> property):

public class Foo
{
    public IList<string> AListOfStrings { get; private set; }

    public Foo()
    {
        AListOfStrings = new List<string>();
    }
}
cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • 2
    How about just returning `IEnumerable`? That's even more flexible. – John Saunders Dec 14 '10 at 21:01
  • 3
    @John: Perhaps, but I would favor `ICollection` over `IEnumerable`. If this is a DAL class, modification may be necessary, and modifying IEnumerables is non-trivial, while `ICollection` exposes mutating methods. – cdhowie Dec 14 '10 at 21:02
  • 1
    If you don't need indexing or count, you can use `IEnumerable`, and then you would have more flexibility, like the option to return a lazy enumeration. – configurator Dec 14 '10 at 21:02
  • @cdhowie: `ICollection` is making assumptions about how the collection will be used. I'd rather not do that. – John Saunders Dec 14 '10 at 21:05
  • @John: Same with `IEnumerable`. Only the developer will be able to effectively choose which is appropriate. – cdhowie Dec 14 '10 at 21:07
  • @cdhowie: nonsense. `IEnumerable` simply assumes it will be enumerated! An `ICollection` is something that can be modified. – John Saunders Dec 14 '10 at 21:09
  • what about (sorry could not insert line breaks) , public ICollection GetSchool() { ICollection schoolList = new List(); ..... return schoolList.ToList(); } – kayak Dec 14 '10 at 21:11
  • @John: That's exactly my point. If the point is to expose a modifiable collection to the caller, `IEnumerable` is not going to cut it. That's why the developer will have to choose which type makes the most sense for their application. – cdhowie Dec 14 '10 at 21:11
  • @kayak: There is no need to type `schoolList` as `ICollection` -- you can use `List` there. And the `ToList()` call is unnecessary. Just return `schoolList`; `List` implements `ICollection`, so no conversion is necessary. – cdhowie Dec 14 '10 at 21:12
  • @cdhowie, Thanks i found some more links related to it http://stackoverflow.com/questions/400135/c-listt-or-ilistt – kayak Dec 14 '10 at 21:19
  • @cdhowie: it depends on what the DAL is trying to expose. I don't believe I've ever seen a DAL return a modifiable collection, unless it was planning to ignore any modifications, in which case I'd prefer it return `IEnumerable` – John Saunders Dec 14 '10 at 22:35
0

In principle, there's no problem with returning List<T>, however a common problem is when classes return a member List<T> variable which violates encapsulation e.g.

public class SomeClass<T>
{
    public List<T> Items { get; private set; }
    public void DoSomething()
    {
        T first = Items[0];
        ...
    }
}

In this case, any clients of this class can modify its state which could lead to bugs since the class can no longer protect its invariants. Note that returning IList<T> in this case will not help.

For a data access object however, it will (hopefully) not retain the results of any queries, so a new copy of the list will be returned each time and it will not be affected by the modifications of any clients.

Lee
  • 142,018
  • 20
  • 234
  • 287