-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataContext.cs
85 lines (73 loc) · 3.23 KB
/
DataContext.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DxSample.Service.Models {
public static class DataContext {
private static readonly List<Customer> ListCustomers;
private static readonly List<Order> ListOrders;
private static int CustomersCounter;
private static int OrdersCounter;
private static readonly object Sync = new object();
static DataContext() {
DataContext.ListCustomers = new List<Customer>();
DataContext.ListOrders = new List<Order>();
DataContext.CustomersCounter = 1;
DataContext.OrdersCounter = 1;
DataContext.AddCustomer("John");
DataContext.AddCustomer("Bob");
DataContext.AddOrder("Chai", 1);
DataContext.AddOrder("Chang", 1);
DataContext.AddOrder("Queso Caprale", 2);
}
public static IQueryable<Customer> Customers {
get { return DataContext.ListCustomers.AsQueryable(); }
}
public static IQueryable<Order> Orders {
get { return DataContext.ListOrders.AsQueryable(); }
}
public static Task<Customer> AddCustomer(string name) {
return Task.Run<Customer>(() => {
lock (DataContext.Sync) {
Customer result = new Customer() { ID = DataContext.CustomersCounter++, Name = name };
DataContext.ListCustomers.Add(result);
return result;
}
});
}
public static Task<Order> AddOrder(string name, int customerID) {
return Task.Run<Order>(() => {
lock (DataContext.Sync) {
Order result = new Order() { ID = DataContext.OrdersCounter++, Name = name, CustomerID = customerID };
DataContext.ListOrders.Add(result);
return result;
}
});
}
public static Task<Customer> FindCustomerAsync(int key) {
return Task.Run<Customer>(() => DataContext.Customers.SingleOrDefault(c => c.ID == key));
}
public static Task<Order> FindOrderAsync(int key) {
return Task.Run<Order>(() => DataContext.Orders.SingleOrDefault(o => o.ID == key));
}
public static Task UpdateCustomer(Customer customer) {
return Task.Run(() => {
Customer original = DataContext.Customers.Single(c => c.ID == customer.ID);
int index = DataContext.ListCustomers.IndexOf(original);
DataContext.ListCustomers[index] = customer;
});
}
public static Task UpdateOrder(Order order) {
return Task.Run(() => {
Order original = DataContext.Orders.Single(o => o.ID == order.ID);
int index = DataContext.ListOrders.IndexOf(original);
DataContext.ListOrders[index] = order;
});
}
public static Task RemoveCustomer(Customer customer) {
return Task.Run(() => DataContext.ListCustomers.Remove(customer));
}
public static Task RemoveOrder(Order order) {
return Task.Run(() => DataContext.ListOrders.Remove(order));
}
}
}