1
1
using System ;
2
2
using NHibernate . Engine ;
3
- using NHibernate . Persister . Entity ;
4
3
5
4
namespace NHibernate . Proxy
6
5
{
@@ -30,7 +29,6 @@ public abstract partial class AbstractLazyInitializer : ILazyInitializer
30
29
private bool unwrap ;
31
30
private readonly string _entityName ;
32
31
private bool readOnly ;
33
- private bool ? readOnlyBeforeAttachedToSession ;
34
32
35
33
/// <summary>
36
34
/// Create a LazyInitializer to handle all of the Methods/Properties that are called
@@ -43,54 +41,40 @@ protected internal AbstractLazyInitializer(string entityName, object id, ISessio
43
41
{
44
42
_id = id ;
45
43
_entityName = entityName ;
46
-
47
- if ( session == null )
48
- UnsetSession ( ) ;
49
- else
50
- SetSession ( session ) ;
44
+
45
+ SetSession ( session ) ;
51
46
}
52
47
53
48
/// <inheritdoc />
54
49
public void SetSession ( ISessionImplementor s )
55
50
{
56
- if ( s != _session )
51
+ if ( s == _session )
52
+ return ;
53
+
54
+ // check for s == null first, since it is least expensive
55
+ if ( s == null )
57
56
{
58
- // check for s == null first, since it is least expensive
59
- if ( s == null )
60
- {
61
- UnsetSession ( ) ;
62
- }
63
- else if ( IsConnectedToSession )
64
- {
65
- //TODO: perhaps this should be some other RuntimeException...
66
- throw new HibernateException ( "illegally attempted to associate a proxy with two open Sessions" ) ;
67
- }
68
- else
69
- {
70
- // s != null
71
- _session = s ;
72
- if ( readOnlyBeforeAttachedToSession == null )
73
- {
74
- // use the default read-only/modifiable setting
75
- IEntityPersister persister = s . Factory . GetEntityPersister ( _entityName ) ;
76
- SetReadOnly ( s . PersistenceContext . DefaultReadOnly || ! persister . IsMutable ) ;
77
- }
78
- else
79
- {
80
- // use the read-only/modifiable setting indicated during deserialization
81
- SetReadOnly ( readOnlyBeforeAttachedToSession . Value ) ;
82
- readOnlyBeforeAttachedToSession = null ;
83
- }
84
- }
57
+ UnsetSession ( ) ;
58
+ return ;
85
59
}
60
+
61
+ if ( IsConnectedToSession )
62
+ {
63
+ //TODO: perhaps this should be some other RuntimeException...
64
+ throw new HibernateException ( "illegally attempted to associate a proxy with two open Sessions" ) ;
65
+ }
66
+
67
+ _session = s ;
68
+
69
+ // use the default read-only/modifiable setting
70
+ SetReadOnly ( s . PersistenceContext . DefaultReadOnly || ! s . Factory . GetEntityPersister ( _entityName ) . IsMutable ) ;
86
71
}
87
72
88
73
/// <inheritdoc />
89
74
public void UnsetSession ( )
90
75
{
91
76
_session = null ;
92
77
readOnly = false ;
93
- readOnlyBeforeAttachedToSession = null ;
94
78
}
95
79
96
80
protected internal bool IsConnectedToSession
@@ -275,9 +259,7 @@ private object GetProxyOrNull()
275
259
276
260
private void SetReadOnly ( bool readOnly )
277
261
{
278
- IEntityPersister persister = _session . Factory . GetEntityPersister ( _entityName ) ;
279
-
280
- if ( ! persister . IsMutable && ! readOnly )
262
+ if ( ! readOnly && ! _session . Factory . GetEntityPersister ( _entityName ) . IsMutable )
281
263
{
282
264
throw new InvalidOperationException ( "cannot make proxies for immutable entities modifiable" ) ;
283
265
}
0 commit comments