forked from Halifa/TonyBlogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DbTransactionContext.cs
125 lines (111 loc) · 3.93 KB
/
DbTransactionContext.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
namespace TonyBlogs.Framework.Data
{
public class DbTransactionContext
{
private static string CONTEXT_TRANSACTIONS_COLLECTION = "CONTEXT_TRANSACTIONS_COLLECTION";
/// <summary>
/// 添加事物
/// </summary>
/// <param name="key"></param>
/// <param name="transaction"></param>
public static void AddTransaction(string key, MyTransaction transaction)
{
var obj = CallContext.LogicalGetData(CONTEXT_TRANSACTIONS_COLLECTION);
List<MyTransaction> MyTransactions = null;
if (obj == null)
{
MyTransactions = new List<MyTransaction>();
MyTransactions.Add(transaction);
}
else
{
if (obj is List<MyTransaction>)
{
MyTransactions = obj as List<MyTransaction>;
if (MyTransactions.SingleOrDefault(t => t.DataBase == key) == null)
{
MyTransactions.Add(transaction);
}
}
else
{
throw new InvalidCastException("字典转换失败");
}
}
CallContext.LogicalSetData(CONTEXT_TRANSACTIONS_COLLECTION, MyTransactions);
}
/// <summary>
/// 是否包含事物
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool HasTransaction(string key)
{
var obj = CallContext.LogicalGetData(CONTEXT_TRANSACTIONS_COLLECTION);
if (obj == null)
return false;
if (obj is List<MyTransaction>)
{
if ((obj as List<MyTransaction>).SingleOrDefault(t => t.DataBase == key) != null)
return true;
return false;
}
throw new InvalidCastException("类型转换失败");
}
/// <summary>
/// 获取事物
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static MyTransaction GetTransaction(string key)
{
var obj = CallContext.LogicalGetData(CONTEXT_TRANSACTIONS_COLLECTION);
if (obj == null)
return null;
if (obj is List<MyTransaction>)
{
var trans = (obj as List<MyTransaction>).SingleOrDefault(t => t.DataBase == key);
return trans;
}
throw new InvalidCastException("类型转换失败");
}
/// <summary>
/// 移除事物
/// </summary>
/// <param name="key"></param>
/// <param name="distinct"></param>
/// <returns></returns>
public static bool RemoveTransaction(string key)
{
var obj = CallContext.LogicalGetData(CONTEXT_TRANSACTIONS_COLLECTION);
if (obj == null)
{
throw new ArgumentException("字典不能为空");
}
if (obj is List<MyTransaction>)
{
var transList = obj as List<MyTransaction>;
var trans = transList.SingleOrDefault(t => t.DataBase == key);
if (trans != null)
{
transList.Remove(trans);
if (transList.Count > 0)
CallContext.LogicalSetData(CONTEXT_TRANSACTIONS_COLLECTION, transList);
else
CallContext.LogicalSetData(CONTEXT_TRANSACTIONS_COLLECTION, null);
return true;
}
return false;
}
else
{
throw new InvalidCastException("字典转换失败");
}
}
}
}