forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPocoInstantiator.cs
121 lines (101 loc) · 3.25 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
using System;
using System.Reflection;
using System.Runtime.Serialization;
using log4net;
using NHibernate.Bytecode;
using NHibernate.Mapping;
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 ILog log = LogManager.GetLogger(typeof(PocoInstantiator));
private readonly System.Type mappedClass;
[NonSerialized]
private readonly IInstantiationOptimizer optimizer;
private readonly bool embeddedIdentifier;
[NonSerialized]
private ConstructorInfo constructor;
private readonly System.Type proxyInterface;
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(string.Format("no default (no-argument) constructor for class: {0} (class must be instantiated by Interceptor)", mappedClass.FullName));
constructor = null;
}
}
public PocoInstantiator(PersistentClass persistentClass, IInstantiationOptimizer optimizer)
{
mappedClass = persistentClass.MappedClass;
proxyInterface = persistentClass.ProxyInterface;
embeddedIdentifier = persistentClass.HasEmbeddedIdentifier;
this.optimizer = optimizer;
try
{
constructor = ReflectHelper.GetDefaultConstructor(mappedClass);
}
catch (PropertyNotFoundException)
{
log.Info(string.Format("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 (ReflectHelper.IsAbstractClass(mappedClass))
{
throw new InstantiationException("Cannot instantiate abstract class or interface: ", mappedClass);
}
else if (optimizer != null)
{
return optimizer.CreateInstance();
}
else if (constructor == null)
{
throw new InstantiationException("No default constructor for entity: ", mappedClass);
}
else
{
try
{
return constructor.Invoke(null);
}
catch (Exception e)
{
throw new InstantiationException("Could not instantiate entity: ", e, mappedClass);
}
}
}
public bool IsInstance(object obj)
{
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
}
}