forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPocoInstantiator.cs
157 lines (130 loc) · 4.32 KB
/
PocoInstantiator.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
using System;
using System.Reflection;
using System.Runtime.Serialization;
using NHibernate.Bytecode;
using NHibernate.Mapping;
using NHibernate.Proxy;
using NHibernate.Util;
namespace NHibernate.Tuple
{
/// <summary> Defines a POCO-based instantiator for use from the tuplizers.</summary>
[Serializable]
public class PocoInstantiator : IInstantiator, IDeserializationCallback
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(PocoInstantiator));
private readonly System.Type mappedClass;
[NonSerialized]
private IInstantiationOptimizer optimizer;
private readonly IProxyFactory proxyFactory; // 6.0 TODO: remove
private readonly bool generateFieldInterceptionProxy; // 6.0 TODO: remove
private readonly bool embeddedIdentifier;
private readonly bool _isAbstract;
[NonSerialized]
private ConstructorInfo constructor;
private readonly System.Type proxyInterface; // 6.0 TODO: remove
// Since 5.3
[Obsolete("This constructor has no more usage in NHibernate and will be removed in a future version.")]
public PocoInstantiator()
{
}
public PocoInstantiator(Mapping.Component component, IInstantiationOptimizer optimizer)
{
mappedClass = component.ComponentClass;
this.optimizer = optimizer;
proxyInterface = null;
embeddedIdentifier = false;
try
{
constructor = ReflectHelper.GetDefaultConstructor(mappedClass);
}
catch (PropertyNotFoundException)
{
log.Info("no default (no-argument) constructor for class: {0} (class must be instantiated by Interceptor)", mappedClass.FullName);
constructor = null;
}
}
// Since 5.3
[Obsolete("Use PocoEntityInstantiator class instead.")]
public PocoInstantiator(PersistentClass persistentClass, IInstantiationOptimizer optimizer, IProxyFactory proxyFactory, bool generateFieldInterceptionProxy)
: this(persistentClass.MappedClass, optimizer, persistentClass.HasEmbeddedIdentifier)
{
proxyInterface = persistentClass.ProxyInterface;
this.proxyFactory = proxyFactory;
this.generateFieldInterceptionProxy = generateFieldInterceptionProxy;
}
public PocoInstantiator(System.Type mappedClass, IInstantiationOptimizer optimizer, bool embeddedIdentifier)
{
this.mappedClass = mappedClass;
this.optimizer = optimizer;
this.embeddedIdentifier = embeddedIdentifier;
_isAbstract = ReflectHelper.IsAbstractClass(mappedClass);
try
{
constructor = ReflectHelper.GetDefaultConstructor(mappedClass);
}
catch (PropertyNotFoundException)
{
log.Info("no default (no-argument) constructor for class: {0} (class must be instantiated by Interceptor)", mappedClass.FullName);
constructor = null;
}
}
#region IInstantiator Members
public object Instantiate(object id)
{
bool useEmbeddedIdentifierInstanceAsEntity = embeddedIdentifier && id != null && id.GetType().Equals(mappedClass);
return useEmbeddedIdentifierInstanceAsEntity ? id : Instantiate();
}
public object Instantiate()
{
if (_isAbstract)
{
throw new InstantiationException("Cannot instantiate abstract class or interface: ", mappedClass);
}
// 6.0 TODO: Remove if statement
if (generateFieldInterceptionProxy)
{
return proxyFactory.GetFieldInterceptionProxy(CreateInstance);
}
return CreateInstance();
}
protected virtual object CreateInstance()
{
if (optimizer != null)
{
return optimizer.CreateInstance();
}
if (mappedClass.IsValueType)
{
return Activator.CreateInstance(mappedClass, true);
}
if (constructor == null)
{
throw new InstantiationException("No default constructor for entity: ", mappedClass);
}
try
{
return constructor.Invoke(null);
}
catch (Exception e)
{
throw new InstantiationException("Could not instantiate entity: ", e, mappedClass);
}
}
public virtual bool IsInstance(object obj)
{
// 6.0 TODO: Remove the proxyInterface check
return mappedClass.IsInstanceOfType(obj) || (proxyInterface != null && proxyInterface.IsInstanceOfType(obj)); //this one needed only for guessEntityMode()
}
#endregion
#region IDeserializationCallback Members
public void OnDeserialization(object sender)
{
constructor = ReflectHelper.GetDefaultConstructor(mappedClass);
}
#endregion
public void SetOptimizer(IInstantiationOptimizer optimizer)
{
this.optimizer = optimizer;
}
}
}