Is AsList() better than ToList() with IDbConnectio

2020-02-28 06:10发布

问题:

I read this answer from Marc Gravell (@MarcGravell): https://stackoverflow.com/a/47790712/5779732

The last line says:

As a minor optimization to your code: prefer AsList() to ToList() to avoid creating a copy.

That statement is about QueryMultiple() which returns GridReader.

In my understanding, System.Linq provides an extension method IEnumerable.ToList(). Following is from Microsoft about ToList().

The ToList(IEnumerable) method forces immediate query evaluation and returns a List that contains the query results. You can append this method to your query in order to obtain a cached copy of the query results.

IDbConnection.Query() will ALWAYS return IEnumerable or null. Null-check could be easily done in calling code. What difference does AsList makes then?

If my understanding is correct, AsList will always internally call ToList which will create a copy.

Considering this, is AsList() better than ToList() with IDbConnection.Query() which returns IEnumerable? If yes; why?

What is that AsList() does internally that makes it a better choice in this case?

回答1:

AsList is a custom Dapper extension method. All it does is checks if IEnumerable<T> you pass to it is really List<T>. If it is - it returns it back, just casts to List<T>. If it's not - it calls regular ToList. The point is - ToList() always creates a copy, even if what you pass to it is already a list. AsList() method avoids doing this copy, and so is useful if such copy is unnecessary.

In this specific scenario, you have the following code:

multipleresult.Read<MerchantProduct>()

where multipleresult is GridReader. Read has buffered argument which is true by default. When its true - Read will really return List<T>, so by calling ToList you will copy that list again without much reason.

The same is true for IDbConnection.Query() - is also has buffered parameter, which is true by default, so it will also by default return List<T>.

If you prefer to use ToList() - you can pass buffered: false to Query() or Read() to avoid creating that additional copy.



回答2:

This extension is a custom dapper extension which does an additional check before calling ToList. Source:

public static List<T> AsList<T>(this IEnumerable<T> source) 
    => (source == null || source is List<T>) ? (List<T>)source : source.ToList();
  • ToList always creates a new List<T> instance and fills it with the given items
  • AsList checks if the sequence is already a List<T>, then it will just cast it

Of course this approach can be more efficient because casting something is much less work than creating and filling something new. So it's completely different.

This is kind of opinion based, but i find this dangerous. Someone might overlook the AsList and reads ToList or just don't know the difference. It's dangerous if someone changes the code later.

So for example a method that takes IEnumerable<T> which uses AsList:

public static List<T> GetResult<T>(IEnumerable<T> seq)
{
    if(some condition here)
    {
        seq = seq.Where(some predicate here);
    }
    return seq.AsList()
}

Now code called this method with a list:

IEnumerable<string> sequence = (gets a list from somewhere)
List<string> userList = GetResult(sequence);

Later someone decides that an array is more appropriate here:

IEnumerable<string> sequence = (gets an array from somewhere)
List<string> userList = GetResult(sequence);

This doesn't really hurt until now. Now a new List is initialized and filled because the source is not a list and can't be casted. So it's just less efficient. But if the logic also relied on the list being the same reference, this won't work anymore.

if(userList == seq)
{
    // do something
}

This is always false once the array is used . So the code was broken silently.

To cut a long story short: i don't like the AsList method. You can always check the type yourself.