forked from RIteshSolidity/MicrosServices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomerRepository.cs
48 lines (43 loc) · 1.73 KB
/
CustomerRepository.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CustomersLibrary;
using Microsoft.ServiceFabric.Data;
using Microsoft.ServiceFabric.Data.Collections;
namespace Customers
{
public class CustomerRepository : ICustomerRepository
{
private IReliableStateManager _manager;
public CustomerRepository(IReliableStateManager manager)
{
_manager = manager;
}
public async Task AddCustomer(CustomerEntity cust)
{
var customer = await _manager.GetOrAddAsync<IReliableDictionary<int, CustomerEntity>>("CustomerStore");
using (var tx = _manager.CreateTransaction()) {
var result = await customer.AddOrUpdateAsync(tx, cust.CustomerId, cust, (id, val) => cust);
await tx.CommitAsync();
}
}
public async Task<IEnumerable<CustomerEntity>> GetAllCustomers()
{
var customer = await _manager.GetOrAddAsync<IReliableDictionary<int, CustomerEntity>>("CustomerStore");
List<CustomerEntity> allCustomers = new List<CustomerEntity>();
using (var tx = _manager.CreateTransaction()) {
var custEnu = await customer.CreateEnumerableAsync(tx,EnumerationMode.Ordered);
using (var enumerator = custEnu.GetAsyncEnumerator()) {
while (await enumerator.MoveNextAsync(CancellationToken.None)) {
KeyValuePair<int, CustomerEntity> custObjects = enumerator.Current;
allCustomers.Add(custObjects.Value);
}
//
}
return allCustomers;
}
}
}
}