forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractLazyInitializer.cs
279 lines (245 loc) · 7.07 KB
/
AbstractLazyInitializer.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
using System;
using NHibernate.Engine;
namespace NHibernate.Proxy
{
/// <summary>
/// Provides the base functionality to Handle Member calls into a dynamically
/// generated NHibernate Proxy.
/// </summary>
/// <remarks>
/// This could be an extension point later if the .net framework ever gets a Proxy
/// class that is similar to the java.lang.reflect.Proxy or if a library similar
/// to cglib was made in .net.
/// </remarks>
[Serializable]
public abstract partial class AbstractLazyInitializer : ILazyInitializer
{
/// <summary>
/// If this is returned by Invoke then the subclass needs to Invoke the
/// method call against the object that is being proxied.
/// </summary>
protected static readonly object InvokeImplementation = new object();
private object _target = null;
private bool initialized;
private object _id;
[NonSerialized]
private ISessionImplementor _session;
private bool unwrap;
private readonly string _entityName;
private bool readOnly;
/// <summary>
/// Create a LazyInitializer to handle all of the Methods/Properties that are called
/// on the Proxy.
/// </summary>
/// <param name="entityName">The entityName</param>
/// <param name="id">The Id of the Object we are Proxying.</param>
/// <param name="session">The ISession this Proxy is in.</param>
protected internal AbstractLazyInitializer(string entityName, object id, ISessionImplementor session)
{
_id = id;
_entityName = entityName;
SetSession(session);
}
/// <inheritdoc />
public void SetSession(ISessionImplementor s)
{
if (s == _session)
return;
// check for s == null first, since it is least expensive
if (s == null)
{
UnsetSession();
return;
}
if (IsConnectedToSession)
{
//TODO: perhaps this should be some other RuntimeException...
throw new HibernateException("illegally attempted to associate a proxy with two open Sessions");
}
_session = s;
// use the default read-only/modifiable setting
SetReadOnly(s.PersistenceContext.DefaultReadOnly || !s.Factory.GetEntityPersister(_entityName).IsMutable);
}
/// <inheritdoc />
public void UnsetSession()
{
_session = null;
readOnly = false;
}
protected internal bool IsConnectedToSession
{
get { return GetProxyOrNull() != null; }
}
/// <summary>
/// Perform an ImmediateLoad of the actual object for the Proxy.
/// </summary>
/// <exception cref="HibernateException">
/// Thrown when the Proxy has no Session or the Session is closed or disconnected.
/// </exception>
public virtual void Initialize()
{
if (!initialized)
{
if (_session == null)
{
throw new LazyInitializationException(_entityName, _id, "Could not initialize proxy - no Session.");
}
else if (!_session.IsOpen)
{
throw new LazyInitializationException(_entityName, _id, "Could not initialize proxy - the owning Session was closed.");
}
else if (!_session.IsConnected)
{
throw new LazyInitializationException(_entityName, _id, "Could not initialize proxy - the owning Session is disconnected.");
}
else
{
_target = _session.ImmediateLoad(_entityName, _id);
initialized = true;
CheckTargetState();
}
}
else
{
CheckTargetState();
}
}
public object Identifier
{
get { return _id; }
set { _id = value; }
}
public abstract System.Type PersistentClass { get; }
public bool IsUninitialized
{
get { return !initialized; }
}
public bool Unwrap
{
get { return unwrap; }
set { unwrap = value; }
}
public ISessionImplementor Session
{
get { return _session; }
set
{
if (value != _session)
{
if (value != null && IsConnectedToSession)
{
//TODO: perhaps this should be some other RuntimeException...
throw new LazyInitializationException(_entityName, _id, "Illegally attempted to associate a proxy with two open Sessions");
}
else
{
_session = value;
}
}
}
}
public string EntityName
{
get { return _entityName; }
}
protected internal object Target
{
get { return _target; }
}
/// <summary>
/// Return the Underlying Persistent Object, initializing if necessary.
/// </summary>
/// <returns>The Persistent Object this proxy is Proxying.</returns>
public object GetImplementation()
{
Initialize();
return _target;
}
/// <summary>
/// Return the Underlying Persistent Object in a given <see cref="ISession"/>, or null.
/// </summary>
/// <param name="s">The Session to get the object from.</param>
/// <returns>The Persistent Object this proxy is Proxying, or <see langword="null" />.</returns>
public object GetImplementation(ISessionImplementor s)
{
EntityKey key = s.GenerateEntityKey(Identifier, s.Factory.GetEntityPersister(EntityName));
return s.PersistenceContext.GetEntity(key);
}
public void SetImplementation(object target)
{
_target = target;
initialized = true;
}
/// <inheritdoc />
public bool IsReadOnlySettingAvailable
{
get { return (_session != null && !_session.IsClosed); }
}
/// <inheritdoc />
public bool ReadOnly
{
get
{
ErrorIfReadOnlySettingNotAvailable();
return readOnly;
}
set
{
ErrorIfReadOnlySettingNotAvailable();
// only update if setting is different from current setting
if (this.readOnly != value)
{
this.SetReadOnly(value);
}
}
}
private void ErrorIfReadOnlySettingNotAvailable()
{
if (_session == null)
throw new TransientObjectException("Proxy is detached (i.e, session is null). The read-only/modifiable setting is only accessible when the proxy is associated with an open session.");
if (_session.IsClosed)
throw new SessionException("Session is closed. The read-only/modifiable setting is only accessible when the proxy is associated with an open session.");
}
private static EntityKey GenerateEntityKeyOrNull(object id, ISessionImplementor s, string entityName)
{
if (id == null || s == null || entityName == null)
return null;
return s.GenerateEntityKey(id, s.Factory.GetEntityPersister(entityName));
}
private void CheckTargetState()
{
if (!unwrap)
{
if (_target == null)
{
Session.Factory.EntityNotFoundDelegate.HandleEntityNotFound(_entityName, _id);
}
}
}
private object GetProxyOrNull()
{
EntityKey entityKey = GenerateEntityKeyOrNull(_id, _session, _entityName);
if (entityKey != null && _session != null && _session.IsOpen)
{
return _session.PersistenceContext.GetProxy(entityKey);
}
return null;
}
private void SetReadOnly(bool readOnly)
{
if (!readOnly && !_session.Factory.GetEntityPersister(_entityName).IsMutable)
{
throw new InvalidOperationException("cannot make proxies for immutable entities modifiable");
}
this.readOnly = readOnly;
if (initialized)
{
EntityKey key = GenerateEntityKeyOrNull(_id, _session, _entityName);
if (key != null && _session.PersistenceContext.ContainsEntity(key))
{
_session.PersistenceContext.SetReadOnly(_target, readOnly);
}
}
}
}
}