Last active
February 2, 2020 06:07
-
-
Save ankitvijay/2ed9bf03d503de5f787d2ad7a4ff529c to your computer and use it in GitHub Desktop.
API Before IAsyncenumerable changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using Dapper; | |
public async Task<IEnumerable<Item>> GetItems(int id) | |
{ | |
var reader = await _connection.QueryMultipleAsync(getItemsSql, | |
param: new | |
{ | |
Id = id | |
}); | |
var idFromDb = (await reader.ReadAsync<int?>().ConfigureAwait(false)).SingleOrDefault(); | |
if (idFromDb == null) | |
{ | |
return null; | |
} | |
var items = await reader.ReadAsync<Item>(buffered: false).ConfigureAwait(false); | |
return Stream(reader, items); | |
} | |
private IEnumerable<Item> Stream(SqlMapper.GridReader reader, IEnumerable<Item> items) | |
{ | |
using (reader) | |
{ | |
foreach (var item in items) | |
{ | |
yield return item; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<IActionResult> GetItems(int id) | |
{ | |
var items = await _query.GetItems(id); | |
return Ok(Stream(items); | |
} | |
private IEnumerable<ItemDto> Stream(IEnumerable<Item> items) | |
{ | |
foreach (var item in items) | |
{ | |
yield return GetItemDto(item); | |
} | |
} | |
private static ItemDto GetItemDto(Item item) | |
{ | |
return new ItemDto | |
{ | |
// Convert Item to ItemDto | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment