forked from yevhen/Streamstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S06_Include_additional_entities.cs
71 lines (60 loc) · 2.33 KB
/
S06_Include_additional_entities.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
using System;
using System.Threading.Tasks;
using Azure;
using Azure.Data.Tables;
using Newtonsoft.Json;
using Streamstone;
namespace Example.Scenarios
{
public class S06_Include_additional_entities : Scenario
{
public override async Task RunAsync()
{
var existent = await Stream.TryOpenAsync(Partition, default);
var stream = existent.Found ? existent.Stream : new Stream(Partition);
Console.WriteLine("Writing to new stream along with making snapshot in partition '{0}'",
stream.Partition);
var snapshot = Include.Insert(new InventoryItemShapshot
{
RowKey = "SNAPSHOT",
Name = "iPhone7",
Count = 100 - 50 - 40,
Version = 5
});
var events = new[]
{
Event(new InventoryItemCreated(Id, "iPhone6")),
Event(new InventoryItemCheckedIn(Id, 100)),
Event(new InventoryItemCheckedOut(Id, 50)),
Event(new InventoryItemRenamed(Id, "iPhone6", "iPhone7")),
Event(new InventoryItemCheckedOut(Id, 40), snapshot)
};
var result = await Stream.WriteAsync(stream, default, events);
Console.WriteLine("Successfully written to new stream.\r\nEtag: {0}, Version: {1}",
result.Stream.ETag, result.Stream.Version);
}
static EventData Event(object @event, params Include[] includes)
{
var id = Guid.NewGuid();
var properties = new
{
Type = @event.GetType().Name,
Data = JsonConvert.SerializeObject(@event)
};
return new EventData(
EventId.From(id),
EventProperties.From(properties),
EventIncludes.From(includes));
}
class InventoryItemShapshot : ITableEntity
{
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTimeOffset? Timestamp { get; set; }
public ETag ETag { get; set; }
public string Name { get; set; }
public int Count { get; set; }
public long Version { get; set; }
}
}
}