-
Notifications
You must be signed in to change notification settings - Fork 936
/
Copy pathAbstractFieldInterceptor.cs
209 lines (176 loc) · 5.56 KB
/
AbstractFieldInterceptor.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
using System;
using System.Collections.Generic;
using Iesi.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Persister.Entity;
using NHibernate.Proxy;
using NHibernate.Util;
namespace NHibernate.Intercept
{
[Serializable]
public abstract class AbstractFieldInterceptor : IFieldInterceptor
{
public static readonly object InvokeImplementation = new object();
[NonSerialized]
private ISessionImplementor session;
private ISet<string> uninitializedFields;
private ISet<string> uninitializedFieldsReadOnly;
private readonly ISet<string> unwrapProxyFieldNames;
private readonly HashSet<string> loadedUnwrapProxyFieldNames = new HashSet<string>();
private readonly string entityName;
private readonly System.Type mappedClass;
[NonSerialized]
private bool initializing;
private bool isDirty;
protected internal AbstractFieldInterceptor(ISessionImplementor session, ISet<string> uninitializedFields, ISet<string> unwrapProxyFieldNames, string entityName, System.Type mappedClass)
{
this.session = session;
this.uninitializedFields = uninitializedFields;
this.unwrapProxyFieldNames = unwrapProxyFieldNames ?? new HashSet<string>();
this.entityName = entityName;
this.mappedClass = mappedClass;
this.uninitializedFieldsReadOnly = uninitializedFields != null ? new ReadOnlySet<string>(uninitializedFields) : null;
}
#region IFieldInterceptor Members
public bool IsDirty
{
get { return isDirty; }
}
public ISessionImplementor Session
{
get { return session; }
set { session = value; }
}
public bool IsInitialized
{
get { return uninitializedFields == null || uninitializedFields.Count == 0; }
}
public bool IsInitializedField(string field)
{
return !IsUninitializedProperty(field) && !IsUninitializedAssociation(field);
}
public void MarkDirty()
{
isDirty = true;
}
public void ClearDirty()
{
isDirty = false;
}
public string EntityName
{
get { return entityName; }
}
public System.Type MappedClass
{
get { return mappedClass; }
}
#endregion
// Since v5.3
[Obsolete("Please use GetUninitializedFields extension method instead")]
public ISet<string> UninitializedFields
{
get { return uninitializedFields; }
}
public bool Initializing
{
get { return initializing; }
}
public object Intercept(object target, string fieldName, object value)
{
return Intercept(target, fieldName, value, false);
}
public object Intercept(object target, string fieldName, object value, bool setter)
{
if (setter)
{
if (IsUninitializedProperty(fieldName))
{
uninitializedFields.Remove(fieldName);
}
if (!unwrapProxyFieldNames.Contains(fieldName))
{
return value;
}
// When a proxy is set by the user which we know when the session is set, we should not unwrap it
if (session != null || !value.IsProxy())
{
loadedUnwrapProxyFieldNames.Add(fieldName);
}
return value;
}
// NH Specific: Hibernate only deals with lazy properties here, we deal with
// both lazy properties and with no-proxy.
if (initializing)
{
return InvokeImplementation;
}
if (IsInitializedField(fieldName))
{
return value;
}
if (session == null)
{
throw new LazyInitializationException(EntityName, null, string.Format("entity with lazy properties is not associated with a session. entity-name:'{0}' property:'{1}'", EntityName, fieldName));
}
if (!session.IsOpen || !session.IsConnected)
{
throw new LazyInitializationException(EntityName, null, string.Format("session is not connected. entity-name:'{0}' property:'{1}'", EntityName, fieldName));
}
if (IsUninitializedProperty(fieldName))
{
return InitializeField(fieldName, target);
}
if (value.IsProxy() && IsUninitializedAssociation(fieldName))
{
var nhproxy = value as INHibernateProxy;
value = InitializeOrGetAssociation(nhproxy, fieldName);
// Set the property value in order to be accessible when the session is closed
var persister = session.Factory.GetEntityPersister(entityName);
persister.SetPropertyValue(
target,
persister.EntityMetamodel.BytecodeEnhancementMetadata.UnwrapProxyPropertiesMetadata.GetUnwrapProxyPropertyIndex(fieldName),
value);
return value;
}
return InvokeImplementation;
}
private bool IsUninitializedAssociation(string fieldName)
{
return unwrapProxyFieldNames.Contains(fieldName) && !loadedUnwrapProxyFieldNames.Contains(fieldName);
}
private bool IsUninitializedProperty(string fieldName)
{
return uninitializedFields != null && uninitializedFields.Contains(fieldName);
}
private object InitializeOrGetAssociation(INHibernateProxy value, string fieldName)
{
if(value.HibernateLazyInitializer.IsUninitialized)
{
value.HibernateLazyInitializer.Initialize();
value.HibernateLazyInitializer.Unwrap = true; // means that future Load/Get from the session will get the implementation
loadedUnwrapProxyFieldNames.Add(fieldName);
}
return value.HibernateLazyInitializer.GetImplementation(session);
}
private object InitializeField(string fieldName, object target)
{
object result;
initializing = true;
try
{
var lazyPropertyInitializer = ((ILazyPropertyInitializer) session.Factory.GetEntityPersister(entityName));
result = lazyPropertyInitializer.InitializeLazyProperty(fieldName, target, session);
}
finally
{
initializing = false;
}
return result;
}
public ISet<string> GetUninitializedFields()
{
return uninitializedFieldsReadOnly ?? CollectionHelper.EmptySet<string>();
}
}
}