1
1
using System ;
2
2
using Iesi . Collections . Generic ;
3
3
using NHibernate . Engine ;
4
+ using NHibernate . Proxy ;
4
5
5
6
namespace NHibernate . Intercept
6
7
{
@@ -12,16 +13,18 @@ public abstract class AbstractFieldInterceptor : IFieldInterceptor
12
13
[ NonSerialized ]
13
14
private ISessionImplementor session ;
14
15
private ISet < string > uninitializedFields ;
16
+ private ISet < string > uninitializedGhostFieldNames ;
15
17
private readonly string entityName ;
16
18
17
19
[ NonSerialized ]
18
20
private bool initializing ;
19
21
private bool isDirty ;
20
22
21
- protected internal AbstractFieldInterceptor ( ISessionImplementor session , ISet < string > uninitializedFields , string entityName )
23
+ protected internal AbstractFieldInterceptor ( ISessionImplementor session , ISet < string > uninitializedFields , ISet < string > uninitializedGhostFieldNames , string entityName )
22
24
{
23
25
this . session = session ;
24
26
this . uninitializedFields = uninitializedFields ;
27
+ this . uninitializedGhostFieldNames = uninitializedGhostFieldNames ;
25
28
this . entityName = entityName ;
26
29
}
27
30
@@ -74,11 +77,9 @@ public bool Initializing
74
77
get { return initializing ; }
75
78
}
76
79
77
- public object Intercept ( object target , string fieldName )
80
+ public object Intercept ( object target , string fieldName , object value )
78
81
{
79
- if ( initializing ||
80
- uninitializedFields == null ||
81
- ! uninitializedFields . Contains ( fieldName ) )
82
+ if ( initializing )
82
83
return InvokeImplementation ;
83
84
84
85
if ( session == null )
@@ -90,11 +91,38 @@ public object Intercept(object target, string fieldName)
90
91
throw new LazyInitializationException ( "session is not connected" ) ;
91
92
}
92
93
94
+ if ( uninitializedFields != null && uninitializedFields . Contains ( fieldName ) )
95
+ {
96
+ return InitializeField ( fieldName , target ) ;
97
+ }
98
+ if ( value is INHibernateProxy && uninitializedGhostFieldNames != null && uninitializedGhostFieldNames . Contains ( fieldName ) )
99
+ {
100
+ return InitializeOrGetAssociation ( ( INHibernateProxy ) value ) ;
101
+ }
102
+ return InvokeImplementation ;
103
+ }
104
+
105
+ private object InitializeOrGetAssociation ( INHibernateProxy value )
106
+ {
107
+ if ( value . HibernateLazyInitializer . IsUninitialized )
108
+ {
109
+ value . HibernateLazyInitializer . Initialize ( ) ;
110
+ var association = value . HibernateLazyInitializer . GetImplementation ( session ) ;
111
+ var narrowedProxy = session . PersistenceContext . ProxyFor ( association ) ;
112
+ // we set the narrowed impl here to be able to get it back in the future
113
+ value . HibernateLazyInitializer . SetImplementation ( narrowedProxy ) ;
114
+ }
115
+ return value . HibernateLazyInitializer . GetImplementation ( session ) ;
116
+ }
117
+
118
+ private object InitializeField ( string fieldName , object target )
119
+ {
93
120
object result ;
94
121
initializing = true ;
95
122
try
96
123
{
97
- result = ( ( ILazyPropertyInitializer ) session . Factory . GetEntityPersister ( entityName ) ) . InitializeLazyProperty ( fieldName , target , session ) ;
124
+ var lazyPropertyInitializer = ( ( ILazyPropertyInitializer ) session . Factory . GetEntityPersister ( entityName ) ) ;
125
+ result = lazyPropertyInitializer . InitializeLazyProperty ( fieldName , target , session ) ;
98
126
}
99
127
finally
100
128
{
0 commit comments