Skip to content

Commit 6522ca7

Browse files
committed
Use IAsyncEnumerable instead of Task<IEnumerable>
1 parent b241b71 commit 6522ca7

File tree

5 files changed

+10
-8
lines changed

5 files changed

+10
-8
lines changed

src/Controller/UserController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public async Task FindAndDeleteProductAsync()
5050
public async Task DisplayProductsAsync()
5151
{
5252
var number = 1;
53-
foreach (var product in await _repository.GetAllProductsAsync())
53+
await foreach (var product in _repository.GetAllProductsAsync())
5454
{
5555
Console.WriteLine($"[{number++}] {product}");
5656
}

src/DAO/MemoryProductDao.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ public class MemoryProductDao : IProductDao
77
{
88
private readonly List<Product> _products = [];
99

10-
public Task<IEnumerable<Product>> GetAllProductsAsync()
10+
public async IAsyncEnumerable<Product> GetAllProductsAsync()
1111
{
12-
return Task.FromResult(_products.AsEnumerable());
12+
foreach (var product in _products)
13+
{
14+
yield return product;
15+
}
1316
}
1417

1518
public Task<Product?> GetProductByNameAsync(string productName)
1619
{
1720
return Task.FromResult(_products.FirstOrDefault(p => p.Name == productName));
1821
}
1922

20-
2123
public Task DeleteProductByNameAsync(string productName)
2224
{
2325
_products.RemoveAll(p => p.Name == 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-
Task<IEnumerable<Product>> GetAllProductsAsync();
7+
IAsyncEnumerable<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-
Task<IEnumerable<Product>> GetAllProductsAsync();
7+
IAsyncEnumerable<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 async Task<IEnumerable<Product>> GetAllProductsAsync() =>
12-
await _productDao.GetAllProductsAsync();
11+
public IAsyncEnumerable<Product> GetAllProductsAsync() =>
12+
_productDao.GetAllProductsAsync();
1313

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

0 commit comments

Comments
 (0)