forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InterAop.cs
185 lines (173 loc) · 6.07 KB
/
InterAop.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using CYQ.Data.Cache;
using System;
using CYQ.Data.Table;
namespace CYQ.Data.Aop
{
/// <summary>
/// 内部预先实现CacheAop
/// </summary>
internal class InterAop
{
private CacheManage _Cache = CacheManage.LocalInstance;//Cache操作
// private AutoCache cacheAop = new AutoCache();
private static readonly object lockObj = new object();
internal bool isHasCache = false;
public AopOp aopOp = AopOp.OpenAll;
internal bool IsLoadAop
{
get
{
return aopOp != AopOp.CloseAll && (AppConfig.Cache.IsAutoCache || outerAop != null);
}
}
internal bool IsTxtDataBase
{
get
{
return Para.DalType == DalType.Txt || Para.DalType == DalType.Xml;
}
}
private AopInfo _AopInfo;
/// <summary>
/// Aop参数
/// </summary>
public AopInfo Para
{
get
{
if (_AopInfo == null)
{
_AopInfo = new AopInfo();
}
return _AopInfo;
}
}
private IAop outerAop;
public InterAop()
{
outerAop = GetFromConfig();
}
#region IAop 成员
public AopResult Begin(AopEnum action)
{
AopResult ar = AopResult.Continue;
if (outerAop != null && (aopOp == AopOp.OpenAll || aopOp == AopOp.OnlyOuter))
{
ar = outerAop.Begin(action, Para);
if (ar == AopResult.Return)
{
return ar;
}
}
if (aopOp == AopOp.OpenAll || aopOp == AopOp.OnlyInner)
{
if (AppConfig.Cache.IsAutoCache && !IsTxtDataBase) // 只要不是直接返回
{
isHasCache = AutoCache.GetCache(action, Para); //找看有没有Cache
}
if (isHasCache) //找到Cache
{
if (outerAop == null || ar == AopResult.Default)//不执行End
{
return AopResult.Return;
}
return AopResult.Break;//外部Aop说:还需要执行End
}
}
return ar;// 没有Cache,默认返回
}
public void End(AopEnum action)
{
if (outerAop != null && (aopOp == AopOp.OpenAll || aopOp == AopOp.OnlyOuter))
{
outerAop.End(action, Para);
}
if (aopOp == AopOp.OpenAll || aopOp == AopOp.OnlyInner)
{
if (!isHasCache && !IsTxtDataBase && Para.IsSuccess)//Select内部调用了GetCount,GetCount的内部isHasCache为true影响了
{
AutoCache.SetCache(action, Para); //找看有没有Cache
}
}
}
public void OnError(string msg)
{
if (outerAop != null)
{
outerAop.OnError(msg);
}
}
#endregion
static bool _IsLoadCompleted = false;
private IAop GetFromConfig()
{
IAop aop = null;
string aopApp = AppConfig.Aop;
if (!string.IsNullOrEmpty(aopApp))
{
string key = "OuterAop_Instance";
if (_Cache.Contains(key))
{
aop = _Cache.Get(key) as IAop;
}
else
{
#region AOP加载
string[] aopItem = aopApp.Split(',');
if (aopItem.Length == 2)//完整类名,程序集(dll)名称
{
if (!_IsLoadCompleted)
{
try
{
lock (lockObj)
{
if (_IsLoadCompleted)
{
return GetFromConfig();//重新去缓存里拿。
}
_IsLoadCompleted = true;
System.Reflection.Assembly ass = System.Reflection.Assembly.Load(aopItem[1]);
if (ass != null)
{
object instance = ass.CreateInstance(aopItem[0]);
if (instance != null)
{
_Cache.Set(key, instance, 1440, AppConst.RunFolderPath + aopItem[1].Replace(".dll", "") + ".dll");
aop = instance as IAop;
aop.OnLoad();
}
}
}
}
catch (Exception err)
{
string errMsg = err.Message + "--Web.config need add a config item,for example:<add key=\"Aop\" value=\"Web.Aop.AopAction,Aop\" />(value format:namespace.Classname,Assembly name) ";
Error.Throw(errMsg);
}
}
}
#endregion
}
}
if (aop != null)
{
return aop.Clone();
}
return null;
}
#region 内部单例
//public static InterAop Instance
//{
// get
// {
// return Shell.instance;
// }
//}
//class Shell
//{
// internal static readonly InterAop instance = new InterAop();
//}
#endregion
}
}