-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
66 lines (52 loc) · 2.09 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
using System;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Starcounter.Database;
using Starcounter.Database.Extensions;
namespace NestedTransactions
{
[Database]
public abstract class Person
{
public abstract string Name { get; set; }
public abstract Person BestFriend { get; set; }
}
class Program
{
static void Main(string[] args)
{
using var services = new ServiceCollection()
.AddStarcounter($"Database=./.database/NestedTransactions")
.Decorate<ITransactor, NestedTransactor>()
.BuildServiceProvider();
var transactor = services.GetRequiredService<ITransactor>();
transactor.Transact(db =>
{
var jane = db.Insert<Person>();
jane.Name = "Jane Doe";
transactor.Transact(db =>
{
// Without NestedTransactor, this would execute as a new, independent
// transaction. Now, it instead join the outer one.
var john = db.Insert<Person>();
john.Name = "John Doe";
// Here, we can find "Jane", simply because the NestedTransactor
// executes the current delegate as a part of the outer transaction.
var jane = db.Sql<Person>("SELECT p FROM Person p WHERE p.Name = ?", "Jane Doe").First();
john.BestFriend = jane;
// You can use extension IsNested() if you need to check if the
// current scope is a nested transaction.
Console.WriteLine(db.IsNested());
Console.WriteLine(john.BestFriend.Name);
});
// This will return false, because we are executing a top-level
// transaction.
Console.WriteLine(db.IsNested());
});
// Output:
// True
// Jane Doe
// False
}
}
}