1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Using
|
28
|
|
using System;
|
29
|
|
using System.Collections;
|
30
|
|
using System.Reflection;
|
31
|
|
using System.Xml.Serialization;
|
32
|
|
|
33
|
|
using Castle.DynamicProxy;
|
34
|
|
|
35
|
|
using IBatisNet.Common;
|
36
|
|
using IBatisNet.Common.Utilities.Objects;
|
37
|
|
|
38
|
|
using IBatisNet.DataMapper.Exceptions;
|
39
|
|
using IBatisNet.DataMapper.MappedStatements;
|
40
|
|
|
41
|
|
using log4net;
|
42
|
|
#endregion
|
43
|
|
|
44
|
|
namespace IBatisNet.DataMapper
|
45
|
|
{
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
[Serializable]
|
50
|
|
internal class LazyLoadList : IInterceptor
|
51
|
|
{
|
52
|
|
#region Fields
|
53
|
|
private object _param = null;
|
54
|
|
private object _target = null;
|
55
|
|
private string _propertyName= string.Empty;
|
56
|
|
private DataSource _dataSource;
|
57
|
|
private MappedStatement _mappedSatement;
|
58
|
|
private bool _loaded = false;
|
59
|
|
private IList _innerList = null;
|
60
|
|
private object _loadLock = new object();
|
61
|
|
private static ArrayList _passthroughMethods = new ArrayList();
|
62
|
|
|
63
|
|
private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
|
64
|
|
#endregion
|
65
|
|
|
66
|
|
#region Constructor (s) / Destructor
|
67
|
|
|
68
|
|
|
69
|
|
|
70
|
|
|
71
|
1
|
static LazyLoadList()
|
72
|
|
{
|
73
|
1
|
_passthroughMethods.Add("GetType");
|
74
|
1
|
_passthroughMethods.Add("ToString");
|
75
|
|
}
|
76
|
|
|
77
|
|
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
13
|
internal LazyLoadList(DataSource dataSource, MappedStatement mappedSatement, object param, object target,string propertyName)
|
86
|
|
{
|
87
|
13
|
_param = param;
|
88
|
13
|
_mappedSatement = mappedSatement;
|
89
|
13
|
_dataSource = dataSource;
|
90
|
13
|
_target = target;
|
91
|
13
|
_propertyName = propertyName;
|
92
|
|
}
|
93
|
|
#endregion
|
94
|
|
|
95
|
|
#region Methods
|
96
|
|
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
13
|
internal static IList NewInstance(DataSource dataSource, MappedStatement mappedSatement, object param, object target,string propertyName)
|
106
|
|
{
|
107
|
13
|
object proxList = null;
|
108
|
13
|
IInterceptor handler = new LazyLoadList(dataSource, mappedSatement, param, target, propertyName);
|
109
|
|
|
110
|
13
|
ProxyGenerator proxyGenerator = new ProxyGenerator();
|
111
|
|
|
112
|
13
|
proxList = proxyGenerator.CreateProxy(typeof(IList), handler, new ArrayList());
|
113
|
|
|
114
|
13
|
return (IList) proxList;
|
115
|
|
}
|
116
|
|
|
117
|
|
#region IInterceptor members
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
|
123
|
|
|
124
|
|
|
125
|
4
|
public object Intercept(IInvocation invocation, params object[] arguments)
|
126
|
|
{
|
127
|
4
|
if (_logger.IsDebugEnabled)
|
128
|
|
{
|
129
|
4
|
_logger.Debug("Proxyfying call to " + invocation.Method.Name);
|
130
|
|
}
|
131
|
|
|
132
|
4
|
lock(_loadLock)
|
133
|
|
{
|
134
|
4
|
if ((_loaded == false) && (!_passthroughMethods.Contains(invocation.Method.Name)))
|
135
|
|
{
|
136
|
4
|
IDalSession session = new SqlMapSession(_dataSource);
|
137
|
|
|
138
|
4
|
if (_logger.IsDebugEnabled)
|
139
|
|
{
|
140
|
4
|
_logger.Debug("Proxyfying call, query statement " + _mappedSatement.Name);
|
141
|
|
}
|
142
|
|
|
143
|
4
|
session.OpenConnection();
|
144
|
4
|
_innerList = _mappedSatement.ExecuteQueryForList(session, _param);
|
145
|
4
|
session.CloseConnection();
|
146
|
|
|
147
|
4
|
_loaded = true;
|
148
|
|
|
149
|
|
}
|
150
|
|
}
|
151
|
|
|
152
|
4
|
object returnValue = invocation.Method.Invoke( _innerList, arguments);
|
153
|
|
|
154
|
4
|
ObjectProbe.SetPropertyValue( _target, _propertyName, _innerList);
|
155
|
|
|
156
|
4
|
if (_logger.IsDebugEnabled)
|
157
|
|
{
|
158
|
4
|
_logger.Debug("End of proxyfied call to " + invocation.Method.Name);
|
159
|
|
}
|
160
|
|
|
161
|
4
|
return returnValue;
|
162
|
|
}
|
163
|
|
|
164
|
|
#endregion
|
165
|
|
|
166
|
|
#endregion
|
167
|
|
|
168
|
|
}
|
169
|
|
}
|
170
|
|
|