forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractEntityTuplizer.cs
424 lines (361 loc) · 12.5 KB
/
AbstractEntityTuplizer.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.Id;
using NHibernate.Intercept;
using NHibernate.Mapping;
using NHibernate.Properties;
using NHibernate.Proxy;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Tuple.Entity
{
/// <summary> Support for tuplizers relating to entities. </summary>
public abstract class AbstractEntityTuplizer : IEntityTuplizer
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(AbstractEntityTuplizer));
private readonly EntityMetamodel entityMetamodel;
protected readonly IGetter idGetter;
protected readonly ISetter idSetter;
protected int propertySpan;
protected IGetter[] getters;
protected ISetter[] setters;
protected bool hasCustomAccessors;
private readonly IProxyFactory proxyFactory;
private readonly IAbstractComponentType identifierMapperType;
/// <summary> Constructs a new AbstractEntityTuplizer instance. </summary>
/// <param name="entityMetamodel">The "interpreted" information relating to the mapped entity. </param>
/// <param name="mappingInfo">The parsed "raw" mapping data relating to the given entity. </param>
protected AbstractEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappingInfo)
{
this.entityMetamodel = entityMetamodel;
if (!entityMetamodel.IdentifierProperty.IsVirtual)
{
idGetter = BuildPropertyGetter(mappingInfo.IdentifierProperty, mappingInfo);
idSetter = BuildPropertySetter(mappingInfo.IdentifierProperty, mappingInfo);
}
else
{
idGetter = null;
idSetter = null;
}
propertySpan = entityMetamodel.PropertySpan;
getters = new IGetter[propertySpan];
setters = new ISetter[propertySpan];
bool foundCustomAccessor = false;
int i = 0;
foreach (Mapping.Property property in mappingInfo.PropertyClosureIterator)
{
getters[i] = BuildPropertyGetter(property, mappingInfo);
setters[i] = BuildPropertySetter(property, mappingInfo);
if (!property.IsBasicPropertyAccessor)
foundCustomAccessor = true;
i++;
}
if (log.IsDebugEnabled())
{
log.Debug("{0} accessors found for entity: {1}", foundCustomAccessor ? "Custom" : "No custom",
mappingInfo.EntityName);
}
hasCustomAccessors = foundCustomAccessor;
//NH-1587
//instantiator = BuildInstantiator(mappingInfo);
if (entityMetamodel.IsLazy)
{
proxyFactory = BuildProxyFactory(mappingInfo, idGetter, idSetter);
if (proxyFactory == null)
{
entityMetamodel.IsLazy = false;
}
}
else
{
proxyFactory = null;
}
Mapping.Component mapper = mappingInfo.IdentifierMapper;
identifierMapperType = mapper == null ? null : (IAbstractComponentType)mapper.Type;
}
#region IEntityTuplizer Members
public virtual bool IsLifecycleImplementor
{
get { return false; }
}
public virtual bool IsValidatableImplementor
{
get { return false; }
}
public abstract System.Type ConcreteProxyClass { get; }
public abstract bool IsInstrumented { get; }
public object Instantiate(object id)
{
object result = Instantiator.Instantiate(id);
if (id != null)
{
SetIdentifier(result, id);
}
return result;
}
public object GetIdentifier(object entity)
{
object id;
if (entityMetamodel.IdentifierProperty.IsEmbedded)
{
id = entity;
}
else
{
if (idGetter == null)
{
if (identifierMapperType == null)
{
throw new HibernateException("The class has no identifier property: " + EntityName);
}
else
{
ComponentType copier = (ComponentType)entityMetamodel.IdentifierProperty.Type;
id = copier.Instantiate();
copier.SetPropertyValues(id, identifierMapperType.GetPropertyValues(entity));
}
}
else
{
id = GetIdentifierPropertyValue(entity);
}
}
return id;
}
public void SetIdentifier(object entity, object id)
{
if (entityMetamodel.IdentifierProperty.IsEmbedded)
{
if (entity != id)
{
IAbstractComponentType copier = (IAbstractComponentType)entityMetamodel.IdentifierProperty.Type;
copier.SetPropertyValues(entity, copier.GetPropertyValues(id));
}
}
else if (idSetter != null)
{
SetIdentifierPropertyValue(entity, id);
}
}
public void ResetIdentifier(object entity, object currentId, object currentVersion)
{
if (!(entityMetamodel.IdentifierProperty.IdentifierGenerator is Assigned))
{
//reset the id
object result = entityMetamodel.IdentifierProperty.UnsavedValue.GetDefaultValue(currentId);
SetIdentifier(entity, result);
//reset the version
VersionProperty versionProperty = entityMetamodel.VersionProperty;
if (entityMetamodel.IsVersioned)
{
SetPropertyValue(entity, entityMetamodel.VersionPropertyIndex, versionProperty.UnsavedValue.GetDefaultValue(currentVersion));
}
}
}
public object GetVersion(object entity)
{
if (!entityMetamodel.IsVersioned)
return null;
return GetPropertyValue(entity, entityMetamodel.VersionPropertyIndex);
}
public virtual void SetPropertyValue(object entity, int i, object value)
{
setters[i].Set(entity, value);
}
public void SetPropertyValue(object entity, string propertyName, object value)
{
SetPropertyValue(entity, entityMetamodel.GetPropertyIndex(propertyName), value);
}
public virtual object[] GetPropertyValuesToInsert(object entity, IDictionary mergeMap, ISessionImplementor session)
{
int span = entityMetamodel.PropertySpan;
object[] result = new object[span];
for (int j = 0; j < span; j++)
{
result[j] = getters[j].GetForInsert(entity, mergeMap, session);
}
return result;
}
public object GetPropertyValue(object entity, string propertyPath)
{
int loc = propertyPath.IndexOf('.');
string basePropertyName = loc > 0 ? propertyPath.Substring(0, (loc) - (0)) : propertyPath;
int index = entityMetamodel.GetPropertyIndex(basePropertyName);
object baseValue = GetPropertyValue(entity, index);
if (loc > 0)
{
ComponentType type = (ComponentType)entityMetamodel.PropertyTypes[index];
return GetComponentValue(type, baseValue, propertyPath.Substring(loc + 1));
}
else
{
return baseValue;
}
}
// Since v5.3
[Obsolete("Use overload without lazyPropertiesAreUnfetched parameter")]
public virtual void AfterInitialize(object entity, bool lazyPropertiesAreUnfetched, ISessionImplementor session)
{
AfterInitialize(entity, session);
}
public virtual void AfterInitialize(object entity, ISessionImplementor session)
{
}
public bool HasProxy
{
get { return entityMetamodel.IsLazy; }
}
public object CreateProxy(object id, ISessionImplementor session)
{
return ProxyFactory.GetProxy(id, session);
}
public virtual bool HasUninitializedLazyProperties(object entity)
{
// the default is to simply not lazy fetch properties for now...
return false;
}
internal virtual ISet<string> GetUninitializedLazyProperties(object entity)
{
return CollectionHelper.EmptySet<string>();
}
#endregion
#region ITuplizer Members
public abstract System.Type MappedClass { get; }
public virtual object[] GetPropertyValues(object entity)
{
var uninitializedPropNames = GetUninitializedLazyProperties(entity);
int span = entityMetamodel.PropertySpan;
object[] result = new object[span];
for (int j = 0; j < span; j++)
{
StandardProperty property = entityMetamodel.Properties[j];
if (!uninitializedPropNames.Contains(property.Name) || !property.IsLazy)
{
result[j] = GetPropertyValue(entity, j);
}
else
{
result[j] = LazyPropertyInitializer.UnfetchedProperty;
}
}
return result;
}
public virtual void SetPropertyValues(object entity, object[] values)
{
bool setAll = !entityMetamodel.HasLazyProperties;
for (int j = 0; j < entityMetamodel.PropertySpan; j++)
{
if (setAll || !Equals(LazyPropertyInitializer.UnfetchedProperty, values[j]))
{
SetPropertyValue(entity, j, values[j]);
}
}
}
public virtual object GetPropertyValue(object entity, int i)
{
return getters[i].Get(entity);
}
public object Instantiate()
{
return Instantiate(null);
}
public bool IsInstance(object obj)
{
return Instantiator.IsInstance(obj);
}
#endregion
protected virtual object GetIdentifierPropertyValue(object entity)
{
return idGetter.Get(entity);
}
protected virtual void SetIdentifierPropertyValue(object entity, object value)
{
idSetter.Set(entity, value);
}
/// <summary> Return the entity-mode handled by this tuplizer instance. </summary>
public abstract EntityMode EntityMode { get; }
protected virtual IInstantiator Instantiator { get; set; }
/// <summary>Retrieves the defined entity-name for the tuplized entity. </summary>
protected virtual string EntityName
{
get { return entityMetamodel.Name; }
}
/// <summary>
/// Retrieves the defined entity-names for any subclasses defined for this entity.
/// </summary>
protected virtual ISet<string> SubclassEntityNames
{
get { return entityMetamodel.SubclassEntityNames; }
}
/// <summary> Build an appropriate Getter for the given property. </summary>
/// <param name="mappedProperty">The property to be accessed via the built Getter. </param>
/// <param name="mappedEntity">The entity information regarding the mapped entity owning this property. </param>
/// <returns> An appropriate Getter instance. </returns>
protected abstract IGetter BuildPropertyGetter(Mapping.Property mappedProperty, PersistentClass mappedEntity);
/// <summary> Build an appropriate Setter for the given property. </summary>
/// <param name="mappedProperty">The property to be accessed via the built Setter. </param>
/// <param name="mappedEntity">The entity information regarding the mapped entity owning this property. </param>
/// <returns> An appropriate Setter instance. </returns>
protected abstract ISetter BuildPropertySetter(Mapping.Property mappedProperty, PersistentClass mappedEntity);
/// <summary> Build an appropriate Instantiator for the given mapped entity. </summary>
/// <param name="mappingInfo">The mapping information regarding the mapped entity. </param>
/// <returns> An appropriate Instantiator instance. </returns>
protected abstract IInstantiator BuildInstantiator(PersistentClass mappingInfo);
/// <summary> Build an appropriate ProxyFactory for the given mapped entity. </summary>
/// <param name="mappingInfo">The mapping information regarding the mapped entity. </param>
/// <param name="idGetter">The constructed Getter relating to the entity's id property. </param>
/// <param name="idSetter">The constructed Setter relating to the entity's id property. </param>
/// <returns> An appropriate ProxyFactory instance. </returns>
protected abstract IProxyFactory BuildProxyFactory(PersistentClass mappingInfo, IGetter idGetter, ISetter idSetter);
/// <summary> Extract a component property value. </summary>
/// <param name="type">The component property types. </param>
/// <param name="component">The component instance itself. </param>
/// <param name="propertyPath">The property path for the property to be extracted. </param>
/// <returns> The property value extracted. </returns>
protected virtual object GetComponentValue(ComponentType type, object component, string propertyPath)
{
int loc = propertyPath.IndexOf('.');
string basePropertyName = loc > 0 ? propertyPath.Substring(0, (loc) - (0)) : propertyPath;
string[] propertyNames = type.PropertyNames;
int index = 0;
for (; index < propertyNames.Length; index++)
{
if (basePropertyName.Equals(propertyNames[index]))
break;
}
if (index == propertyNames.Length)
{
throw new MappingException("component property not found: " + basePropertyName);
}
object baseValue = type.GetPropertyValue(component, index);
if (loc > 0)
{
ComponentType subtype = (ComponentType)type.Subtypes[index];
return GetComponentValue(subtype, baseValue, propertyPath.Substring(loc + 1));
}
else
{
return baseValue;
}
}
protected virtual IProxyFactory ProxyFactory
{
get { return proxyFactory; }
}
protected virtual bool ShouldGetAllProperties(object entity)
{
if (!EntityMetamodel.BytecodeEnhancementMetadata.EnhancedForLazyLoading)
{
return true;
}
return !EntityMetamodel.BytecodeEnhancementMetadata.HasAnyUninitializedLazyProperties(entity);
}
protected EntityMetamodel EntityMetamodel
{
get { return entityMetamodel; }
}
}
}