AutoMapper/AutoMapper — pull request #254 by jeffbarnes (2012-09-27 05:41:15+00:00)
Title: Support for yielding records from DataReaderMapper rather than using a list

For the most part, the DataReaderMapper has always worked well for me. The use of a List rather than yielding records was never that big of a deal for the majority of my typical scenarios. However, I finally have a legitimate need for yielding the records due to some rather large result sets.

You might decide against merging this, but I wanted to offer it since others have asked for this ability. All of the existing unit tests pass and I also tested the changes in my own projects. Depending on how AutoMapper is being applied to data readers, there are some scenarios that could be problematic due to deferred execution.

For example, code written such as this would now fail because the reader will already be closed when iteration of the enumerable begins. The reader will not actually be processed until deferred execution is triggered.

```c#
public IEnumerable GetAllPersonRecords()
{
using (var connection = CreateConnection())
using (var command = CreateCommand())
using (var reader = command.ExecuteReader())
return Mapper.Map>(reader);
}
```

Instead, the code would have to be changed to something like this:

```c#
public IEnumerable GetAllPersonRecords()
{
var connection = CreateConnection();
var command = CreateCommand();
var reader = command.ExecuteReader(CommandBehavior.CloseConnection);
return DisposeAfterLoading(reader);
}

private IEnumerable
DisposeAfterLoading
(IDataReader reader)
{
foreach (var item in Mapper.Map>(reader))
yield return item;

reader.Dispose();
}

```

At any rate, I wanted to point this out as a potential issue so you can make the best decision about whether to merge the changes. Either way, I will definitely be putting it to use for my projects, but it would be nice to not maintain my own version of AutoMapper. /nudge :)