forked from nhibernate/fluent-nhibernate
/
PersistenceModel.cs
232 lines (191 loc) · 7.3 KB
/
PersistenceModel.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Xml;
using FluentNHibernate.Conventions;
using FluentNHibernate.Mapping.Providers;
using FluentNHibernate.MappingModel;
using FluentNHibernate.MappingModel.ClassBased;
using FluentNHibernate.Xml;
using NHibernate.Cfg;
namespace FluentNHibernate
{
public class PersistenceModel
{
protected readonly IList<IMappingProvider> classProviders = new List<IMappingProvider>();
protected readonly IList<IIndeterminateSubclassMappingProvider> subclassProviders = new List<IIndeterminateSubclassMappingProvider>();
private readonly IList<IMappingModelVisitor> visitors = new List<IMappingModelVisitor>();
public IConventionFinder Conventions { get; private set; }
public bool MergeMappings { get; set; }
private IEnumerable<HibernateMapping> compiledMappings;
public PersistenceModel(IConventionFinder conventionFinder)
{
Conventions = conventionFinder;
visitors.Add(new SeparateSubclassVisitor(subclassProviders));
visitors.Add(new ConventionVisitor(Conventions));
AddDefaultConventions();
}
public PersistenceModel()
: this(new DefaultConventionFinder())
{}
private void AddDefaultConventions()
{
foreach (var foundType in from type in typeof(PersistenceModel).Assembly.GetTypes()
where type.Namespace == "FluentNHibernate.Conventions.Defaults" && !type.IsAbstract
select type)
{
Conventions.Add(foundType);
}
}
protected void AddMappingsFromThisAssembly()
{
Assembly assembly = FindTheCallingAssembly();
AddMappingsFromAssembly(assembly);
}
public void AddMappingsFromAssembly(Assembly assembly)
{
foreach (var type in assembly.GetExportedTypes().Where(x => IsClassMap(x) || IsSubclassMap(x)))
Add(type);
}
private static Assembly FindTheCallingAssembly()
{
StackTrace trace = new StackTrace(Thread.CurrentThread, false);
Assembly thisAssembly = Assembly.GetExecutingAssembly();
Assembly callingAssembly = null;
for (int i = 0; i < trace.FrameCount; i++)
{
StackFrame frame = trace.GetFrame(i);
Assembly assembly = frame.GetMethod().DeclaringType.Assembly;
if (assembly != thisAssembly)
{
callingAssembly = assembly;
break;
}
}
return callingAssembly;
}
public void Add(IMappingProvider provider)
{
classProviders.Add(provider);
}
public void Add(IIndeterminateSubclassMappingProvider provider)
{
subclassProviders.Add(provider);
}
public void Add(Type type)
{
var mapping = type.InstantiateUsingParameterlessConstructor();
if (IsClassMap(type))
Add((IMappingProvider)mapping);
else if (IsSubclassMap(type))
Add((IIndeterminateSubclassMappingProvider)mapping);
else
throw new InvalidOperationException("Unsupported mapping type '" + type.FullName + "'");
}
private bool IsClassMap(Type type)
{
return !type.IsGenericType && typeof(IMappingProvider).IsAssignableFrom(type);
}
private bool IsSubclassMap(Type type)
{
return !type.IsGenericType && typeof(IIndeterminateSubclassMappingProvider).IsAssignableFrom(type);
}
public IEnumerable<HibernateMapping> BuildMappings()
{
var hbms = new List<HibernateMapping>();
if (MergeMappings)
BuildSingleMapping(hbms.Add);
else
BuildSeparateMappings(hbms.Add);
foreach (var mapping in hbms)
ApplyVisitors(mapping);
return hbms;
}
private void BuildSeparateMappings(Action<HibernateMapping> add)
{
foreach (var classMap in classProviders)
{
var hbm = classMap.GetHibernateMapping();
hbm.AddClass(classMap.GetClassMapping());
add(hbm);
}
}
private void BuildSingleMapping(Action<HibernateMapping> add)
{
var hbm = new HibernateMapping();
foreach (var classMap in classProviders)
{
hbm.AddClass(classMap.GetClassMapping());
}
if (hbm.Classes.Count() > 0)
add(hbm);
}
public void ApplyVisitors(HibernateMapping mapping)
{
foreach (var visitor in visitors)
mapping.AcceptVisitor(visitor);
}
private void EnsureMappingsBuilt()
{
if (compiledMappings != null) return;
compiledMappings = BuildMappings();
}
protected virtual string GetMappingFileName()
{
return "FluentMappings.hbm.xml";
}
public void WriteMappingsTo(string folder)
{
EnsureMappingsBuilt();
foreach (var mapping in compiledMappings)
{
var serializer = new MappingXmlSerializer();
var document = serializer.Serialize(mapping);
var filename = MergeMappings ? GetMappingFileName() : mapping.Classes.First().Type.FullName + ".hbm.xml";
using (var writer = new XmlTextWriter(Path.Combine(folder, filename), Encoding.Default))
{
writer.Formatting = Formatting.Indented;
document.WriteTo(writer);
}
}
}
public virtual void Configure(Configuration cfg)
{
EnsureMappingsBuilt();
foreach (var mapping in compiledMappings)
{
var serializer = new MappingXmlSerializer();
XmlDocument document = serializer.Serialize(mapping);
if (cfg.GetClassMapping(mapping.Classes.First().Type) == null)
cfg.AddDocument(document);
}
}
}
public interface IMappingProvider
{
ClassMapping GetClassMapping();
// HACK: In place just to keep compatibility until verdict is made
HibernateMapping GetHibernateMapping();
}
public class PassThroughMappingProvider : IMappingProvider
{
private readonly ClassMapping mapping;
public PassThroughMappingProvider(ClassMapping mapping)
{
this.mapping = mapping;
}
public ClassMapping GetClassMapping()
{
return mapping;
}
public HibernateMapping GetHibernateMapping()
{
return new HibernateMapping();
}
}
}