-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathFinanceService.cs
40 lines (36 loc) · 1.09 KB
/
FinanceService.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
using PostSharp.Samples.Transactions.Data;
using System;
using System.Linq;
namespace PostSharp.Samples.Transactions
{
public static class FinanceService
{
[RequiresTransaction]
public static void Transfer(string source, string destination, decimal amount, string description)
{
AddOperation(destination, amount, description);
AddOperation(source, -amount, description);
}
[RequiresTransaction]
private static void AddOperation(string accountNumber, decimal amount, string description)
{
using (var financeDb = new FinanceDb())
{
var account = financeDb.Accounts.Single(a => a.Number == accountNumber);
account.Balance += amount;
if (account.Balance < account.MinimalBalance)
{
throw new InvalidOperationException("Account balance insufficient.");
}
financeDb.Operations.Add(new Operation
{
Account = account,
Amount = amount,
Description = description,
Time = DateTime.Now
});
financeDb.SaveChanges();
}
}
}
}