forked from uma075/ServiceFabricDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductRepository.cs
49 lines (47 loc) · 1.8 KB
/
ProductRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECommerce.ProductCatalog.Models;
using Microsoft.ServiceFabric.Data;
using Microsoft.ServiceFabric.Data.Collections;
using System.Threading;
namespace ECommerce.ProductCatalog
{
class ProductRepository : IProductRepository
{
private IReliableStateManager _stateManager;
public ProductRepository(IReliableStateManager stateManager)
{
_stateManager = stateManager;
}
async Task IProductRepository.AddProduct(Product product)
{
var products = await this._stateManager.GetOrAddAsync<IReliableDictionary<Guid, Product>>("products");
using (var tx = _stateManager.CreateTransaction())
{
await products.AddOrUpdateAsync(tx,product.Id,product,(id,value)=> product);
await tx.CommitAsync();
}
}
async Task<IEnumerable<Product>> IProductRepository.GetAllProducts()
{
var products = await _stateManager.GetOrAddAsync<IReliableDictionary<Guid, Product>>("products");
var result = new List<Product>();
using (var tx = _stateManager.CreateTransaction())
{
var allProducts = await products.CreateEnumerableAsync(tx,EnumerationMode.Unordered);
using (var enumerator = allProducts.GetAsyncEnumerator())
{
while(await enumerator.MoveNextAsync(cancellationToken: CancellationToken.None))
{
KeyValuePair<Guid, Product> current = enumerator.Current;
result.Add(current.Value);
}
}
}
return result;
}
}
}