-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathProgram.cs
229 lines (172 loc) · 7.06 KB
/
Program.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Microsoft.EntityFrameworkCore;
public class Program
{
public static void Main()
{
var timestamps = Seed(new TimeSpan(0, 0, 1));
LookupCurrentPrice("DeLorean");
LookupPrices("DeLorean", timestamps[1], timestamps[3]);
FindOrder("Arthur", timestamps[3]);
DeleteCustomer("Arthur");
QueryCustomerAndOrderSnapshots();
RestoreCustomer("Arthur");
QueryCustomerAndOrderSnapshots();
}
private static void LookupCurrentPrice(string productName)
{
using var context = new OrdersContext();
var product = context.Products.Single(product => product.Name == productName);
Console.WriteLine($"The '{product.Name}' with PK {product.Id} is currently ${product.Price}.");
Console.WriteLine();
}
private static void LookupPrices(string productName, DateTime from, DateTime to)
{
using var context = new OrdersContext();
Console.WriteLine($"Historical prices for {productName} from {from} to {to}:");
var productSnapshots = context.Products
.TemporalFromTo(from, to)
.OrderBy(product => EF.Property<DateTime>(product, "PeriodStart"))
.Where(product => product.Name == productName)
.Select(product =>
new
{
Product = product,
PeriodStart = EF.Property<DateTime>(product, "PeriodStart"),
PeriodEnd = EF.Property<DateTime>(product, "PeriodEnd")
})
.ToList();
foreach (var snapshot in productSnapshots)
{
Console.WriteLine(
$" The '{snapshot.Product.Name}' with PK {snapshot.Product.Id} was ${snapshot.Product.Price} from {snapshot.PeriodStart} until {snapshot.PeriodEnd}.");
}
Console.WriteLine();
}
private static void FindOrder(string customerName, DateTime on)
{
using var context = new OrdersContext(log: true);
var order = context.Orders
.TemporalAsOf(on)
.Include(e => e.Product)
.Include(e => e.Customer)
.Single(order =>
order.Customer.Name == customerName
&& order.OrderDate > on.Date
&& order.OrderDate < on.Date.AddDays(1));
Console.WriteLine();
Console.WriteLine(
$"{order.Customer.Name} ordered a {order.Product.Name} for ${order.Product.Price} on {order.OrderDate}");
Console.WriteLine();
}
private static void DeleteCustomer(string customerName)
{
using var context = new OrdersContext();
var customer = context.Customers
.Include(e => e.Orders)
.Single(customer => customer.Name == customerName);
context.RemoveRange(customer.Orders);
context.Remove(customer);
context.SaveChanges();
}
private static void RestoreCustomer(string customerName)
{
using var context = new OrdersContext(log: true);
var customerDeletedOn = context.Customers
.TemporalAll()
.Where(customer => customer.Name == customerName)
.OrderBy(customer => EF.Property<DateTime>(customer, "PeriodEnd"))
.Select(customer => EF.Property<DateTime>(customer, "PeriodEnd"))
.Last();
Console.WriteLine();
var customerAndOrders = context.Customers
.TemporalAsOf(customerDeletedOn.AddMilliseconds(-1))
.Include(e => e.Orders)
.Single();
Console.WriteLine();
context.Add(customerAndOrders);
context.SaveChanges();
Console.WriteLine();
}
private static void QueryCustomerAndOrderSnapshots()
{
using var context = new OrdersContext(log: true);
var customerSnapshots = context.Customers
.TemporalAll()
.OrderBy(customer => EF.Property<DateTime>(customer, "PeriodStart"))
.Select(customer =>
new
{
Customer = customer,
PeriodStart = EF.Property<DateTime>(customer, "PeriodStart"),
PeriodEnd = EF.Property<DateTime>(customer, "PeriodEnd")
})
.ToList();
foreach (var snapshot in customerSnapshots)
{
Console.WriteLine(
$"The customer '{snapshot.Customer.Name}' existed from {snapshot.PeriodStart} until {snapshot.PeriodEnd}.");
}
Console.WriteLine();
var orderSnapshots = context.Orders
.TemporalAll()
.OrderBy(order => EF.Property<DateTime>(order, "PeriodStart"))
.Select(order =>
new
{
Order = order,
PeriodStart = EF.Property<DateTime>(order, "PeriodStart"),
PeriodEnd = EF.Property<DateTime>(order, "PeriodEnd")
})
.ToList();
foreach (var snapshot in orderSnapshots)
{
Console.WriteLine(
$"The order with ID '{snapshot.Order.Id}' existed from {snapshot.PeriodStart} until {snapshot.PeriodEnd}.");
}
Console.WriteLine();
}
private static List<DateTime> Seed(TimeSpan sleep)
{
using var context = new OrdersContext();
context.Database.EnsureDeleted();
context.Database.Migrate();
var timestamps = new List<DateTime>();
var customer = new Customer { Name = "Arthur" };
context.Customers.Add(customer);
var products = new List<Product>
{
new() { Name = "DeLorean", Price = 1_000_000.00m },
new() { Name = "Flux Capacitor", Price = 666.00m },
new() { Name = "Hoverboard", Price = 59_000.00m }
};
context.AddRange(products);
context.SaveChanges();
timestamps.Add(DateTime.UtcNow);
Thread.Sleep(sleep);
products[0].Price = 2_000_000.00m;
context.SaveChanges();
timestamps.Add(DateTime.UtcNow);
Thread.Sleep(sleep);
products[0].Price = 2_500_000.00m;
context.SaveChanges();
timestamps.Add(DateTime.UtcNow);
Thread.Sleep(sleep);
context.Add(new Order { Customer = customer, Product = products[0], OrderDate = timestamps.Last() });
context.SaveChanges();
timestamps.Add(DateTime.UtcNow);
Thread.Sleep(sleep);
products[0].Price = 75_000.00m;
context.SaveChanges();
timestamps.Add(DateTime.UtcNow);
Thread.Sleep(sleep);
products[0].Price = 150_000.00m;
context.SaveChanges();
timestamps.Add(DateTime.UtcNow);
Thread.Sleep(sleep);
return timestamps;
}
}