Skip to content

Commit 7796be5

Browse files
committed
Use Task<IEnumerable> instead of IAsyncEnumerable
1 parent f36208e commit 7796be5

File tree

5 files changed

+15
-8
lines changed

5 files changed

+15
-8
lines changed

src/Controller/UserController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ public async Task FindAndDeleteProductAsync()
5050
public async Task DisplayProductsAsync()
5151
{
5252
var number = 1;
53-
await foreach (var product in _repository.GetAllProductsAsync())
53+
var products = await _repository.GetAllProductsAsync();
54+
foreach (var product in products)
5455
{
5556
Console.WriteLine($"[{number++}] {product}");
5657
}

src/DAO/SqlProductDao.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
using System.Data;
12
using System.Data.SqlClient;
3+
using Dapper;
24
using SimpleInventoryManagementSystem.Domain;
35
using SimpleInventoryManagementSystem.Interfaces;
46

@@ -15,14 +17,18 @@ public SqlProductDao(ISqlDataSource dataSource, ISqlProductMapper mapper)
1517
_mapper = mapper;
1618
}
1719

18-
public async IAsyncEnumerable<Product> GetAllProductsAsync()
20+
public async Task<IEnumerable<Product>> GetAllProductsAsync()
1921
{
2022
const string sql = "SELECT * FROM Products";
2123
await using var reader = await _dataSource.ExecuteQueryAsync(sql, new List<SqlParameter>());
22-
while (reader.Read())
24+
25+
var products = new List<Product>();
26+
while (await reader.ReadAsync())
2327
{
24-
yield return _mapper.MapToDomain(reader);
28+
products.Add(_mapper.MapToDomain(reader));
2529
}
30+
31+
return products;
2632
}
2733

2834
public async Task<Product?> GetProductByNameAsync(string productName)

src/Interfaces/IProductDao.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace SimpleInventoryManagementSystem.Interfaces;
44

55
public interface IProductDao
66
{
7-
IAsyncEnumerable<Product> GetAllProductsAsync();
7+
Task<IEnumerable<Product>> GetAllProductsAsync();
88
Task<Product?> GetProductByNameAsync(string productName);
99
Task DeleteProductByNameAsync(string productName);
1010
Task AddProductAsync(Product product);

src/Interfaces/IProductRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace SimpleInventoryManagementSystem.Interfaces;
44

55
public interface IProductRepository
66
{
7-
IAsyncEnumerable<Product> GetAllProductsAsync();
7+
Task<IEnumerable<Product>> GetAllProductsAsync();
88
Task<Product?> GetProductByNameAsync(string productName);
99
Task DeleteProductByNameAsync(string productName);
1010
Task AddProductAsync(Product product);

src/Repository/ProductRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ public class ProductRepository : IProductRepository
88
private readonly IProductDao _productDao;
99
public ProductRepository(IProductDao productDao) => _productDao = productDao;
1010

11-
public IAsyncEnumerable<Product> GetAllProductsAsync() =>
12-
_productDao.GetAllProductsAsync();
11+
public async Task<IEnumerable<Product>> GetAllProductsAsync() =>
12+
await _productDao.GetAllProductsAsync();
1313

1414
public async Task<Product?> GetProductByNameAsync(string productName) =>
1515
await _productDao.GetProductByNameAsync(productName);

0 commit comments

Comments
 (0)