IEnumerable <T> as return value, deferred execution and N-level applications
First, I do not consider this question as a duplicate of these SO questions:
Should I always return an IEnumerable <T> instead of an IList <T>? and IEnumerable <T> as return type
As we all know, the main goals of introducing several levels are to reduce grip.
We need to define some interface for data access, and our BL does not have to worry about the details of the DAL implementation. If the interface mentioned returns, IEnumerable<T>BL does not know if it is just static IEnumerableor something that is delayed. At the same time, this particular detail can significantly affect performance and requires different coding depending on the implementation.
Well, you can call .ToList()for everyone IEnumerablein situations where we are going to iterate the collection several times. But this reduces performance for static collections due to an unnecessary new instance of the list.
So, I'm trying to figure out which approach is better.
More versatile and potentially less productive and more complex. I think there is no silver bullet, but these may be other approaches that I missed.
If it is conceptually important for the caller to know what type of method is being returned Listand not IEnumerable(for example, to know that there will be no negative consequences for repeating it again), then you should return List. An interface return point instead is a way of saying, "It doesn't matter what the implementation is." If the implementation matters, then do not use the interface (in this particular situation).