forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPersisterFactory.cs
160 lines (151 loc) · 4.85 KB
/
PersisterFactory.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
using System;
using System.Reflection;
using System.Text;
using NHibernate.Cache;
using NHibernate.Engine;
using NHibernate.Mapping;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
using NHibernate.Util;
namespace NHibernate.Persister
{
/// <summary>
/// Factory for <c>IEntityPersister</c> and <c>ICollectionPersister</c> instances.
/// </summary>
public static class PersisterFactory
{
//TODO: make ClassPersisters *not* depend on ISessionFactoryImplementor
// interface, if possible
static readonly System.Type[] PersisterConstructorArgs = {
typeof(PersistentClass),
typeof(ICacheConcurrencyStrategy),
typeof(ISessionFactoryImplementor),
typeof(IMapping)
};
static readonly System.Type[] CollectionPersisterConstructorArgs =
{
typeof(Mapping.Collection),
typeof(ICacheConcurrencyStrategy),
typeof(ISessionFactoryImplementor)
};
/// <summary>
/// Creates a built in Entity Persister or a custom Persister.
/// </summary>
public static IEntityPersister CreateClassPersister(PersistentClass model, ICacheConcurrencyStrategy cache,
ISessionFactoryImplementor factory, IMapping cfg)
{
System.Type persisterClass = model.EntityPersisterClass;
if (persisterClass == null || persisterClass == typeof(SingleTableEntityPersister))
{
return new SingleTableEntityPersister(model, cache, factory, cfg);
}
else if (persisterClass == typeof(JoinedSubclassEntityPersister))
{
return new JoinedSubclassEntityPersister(model, cache, factory, cfg);
}
else if (persisterClass == typeof(UnionSubclassEntityPersister))
{
return new UnionSubclassEntityPersister(model, cache, factory, cfg);
}
else
{
return Create(persisterClass, model, cache, factory, cfg);
}
}
public static ICollectionPersister CreateCollectionPersister(Mapping.Collection model, ICacheConcurrencyStrategy cache,
ISessionFactoryImplementor factory)
{
System.Type persisterClass = model.CollectionPersisterClass;
if (persisterClass == null)
{
// default behaviour
return
model.IsOneToMany
? (ICollectionPersister) new OneToManyPersister(model, cache, factory)
: (ICollectionPersister) new BasicCollectionPersister(model, cache, factory);
}
else
{
return Create(persisterClass, model, cache, factory);
}
}
/// <summary>
/// Creates a specific Persister - could be a built in or custom persister.
/// </summary>
public static IEntityPersister Create(System.Type persisterClass, PersistentClass model,
ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory,
IMapping cfg)
{
ConstructorInfo pc;
try
{
pc = persisterClass.GetConstructor(PersisterConstructorArgs);
}
catch (Exception e)
{
throw new MappingException("Could not get constructor for " + persisterClass.Name, e);
}
try
{
return (IEntityPersister) pc.Invoke(new object[] {model, cache, factory, cfg});
}
catch (TargetInvocationException tie)
{
Exception e = tie.InnerException;
if (e is HibernateException)
{
throw ReflectHelper.UnwrapTargetInvocationException(tie);
}
else
{
throw new MappingException("Could not instantiate persister " + persisterClass.Name, e);
}
}
catch (Exception e)
{
throw new MappingException("Could not instantiate persister " + persisterClass.Name, e);
}
}
public static ICollectionPersister Create(System.Type persisterClass, Mapping.Collection model,
ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory)
{
ConstructorInfo pc;
try
{
pc = persisterClass.GetConstructor(CollectionPersisterConstructorArgs);
}
catch (Exception e)
{
throw new MappingException("Could not get constructor for " + persisterClass.Name, e);
}
if(pc == null)
{
var messageBuilder = new StringBuilder();
messageBuilder.Append("Could not find a public constructor for ").Append(persisterClass.Name).AppendLine(";");
messageBuilder.Append("- The ctor may have ").Append(CollectionPersisterConstructorArgs.Length).AppendLine(" parameters of types (in order):");
System.Array.ForEach(CollectionPersisterConstructorArgs, t=> messageBuilder.AppendLine(t.FullName));
throw new MappingException(messageBuilder.ToString());
}
try
{
return (ICollectionPersister) pc.Invoke(new object[] {model, cache, factory});
}
catch (TargetInvocationException tie)
{
Exception e = tie.InnerException;
if (e is HibernateException)
{
throw ReflectHelper.UnwrapTargetInvocationException(tie);
}
else
{
throw new MappingException("Could not instantiate collection persister " + persisterClass.Name, e);
}
}
catch (Exception e)
{
throw new MappingException("Could not instantiate collection persister " + persisterClass.Name, e);
}
}
}
}