forked from MarcelRaschke/PostSharp.Samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
122 lines (109 loc) · 3.64 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
using PostSharp.Samples.Transactions.Data;
using System;
using System.Diagnostics;
using System.IO;
namespace PostSharp.Samples.Transactions
{
internal static class Program
{
private const string itDepartment = "00-001";
private const string hrDepartment = "00-002";
private static void Main(string[] args)
{
// Initialization.
CheckDtcRunning();
CreateDatabases();
// Perform a first transaction. It will be successful.
Console.WriteLine("Placing and paying one order...");
Console.WriteLine();
IntegrationService.PlaceOrderAndPay(hrDepartment, itDepartment, "Employee Benefit System", 450000);
DumpDatabases();
Console.WriteLine();
Console.WriteLine();
// Perform a second transaction. It will fail.
Console.WriteLine("Placing and paying a second order...");
try
{
IntegrationService.PlaceOrderAndPay(hrDepartment, itDepartment, "Employee Benefit System Rev 1", 75000);
}
catch (Exception e)
{
Console.WriteLine("ERROR! " + e.Message);
Console.WriteLine(
"The transaction should be rolled back and the database state identical as before the transaction.");
}
Console.WriteLine();
DumpDatabases();
}
private static void CheckDtcRunning()
{
while (Process.GetProcessesByName("msdtc").Length == 0)
{
Console.WriteLine("Distributed Transaction Coordinator (MSDTC) service is not running.");
Console.WriteLine(
"Execute 'net start msdtc' from an administrative command prompt and press Enter.");
Console.ReadLine();
}
}
private static void DumpDatabases()
{
using (var financeDb = new FinanceDb())
{
foreach (var account in financeDb.Accounts)
{
Console.WriteLine("Account {0} ({1}) - Balance ${2}", account.Number, account.Name,
account.Balance);
Console.WriteLine(
"------------------------------------------------------------------------------------------");
foreach (var operation in account.Operations)
{
Console.WriteLine("{0} - {1} - ${2}", operation.Time, operation.Description, operation.Amount);
}
Console.WriteLine();
}
}
using (var orderDb = new OrderDb())
{
Console.WriteLine("Orders");
Console.WriteLine("----------------------------------------------------------------------------");
foreach (var order in orderDb.Orders)
{
Console.WriteLine("{0} - {1}", order.Description, order.TotalAmount);
}
}
}
private static void CreateDatabases()
{
var dataDirectory = Path.Combine(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]));
AppDomain.CurrentDomain.SetData("DataDirectory", dataDirectory);
using (var financeDb = new FinanceDb())
{
if (!financeDb.Database.CreateIfNotExists())
{
financeDb.Operations.RemoveRange(financeDb.Operations);
financeDb.Accounts.RemoveRange(financeDb.Accounts);
}
financeDb.Accounts.Add(new Account
{
Name = "IT Department",
Number = itDepartment
});
financeDb.Accounts.Add(new Account
{
Name = "HR Department",
Number = hrDepartment,
Balance = 500000
});
financeDb.SaveChanges();
}
using (var orderDb = new OrderDb())
{
if (!orderDb.Database.CreateIfNotExists())
{
orderDb.Orders.RemoveRange(orderDb.Orders);
orderDb.SaveChanges();
}
}
}
}
}