-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSchemaBuilder.cs
445 lines (418 loc) · 17.5 KB
/
SchemaBuilder.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
////////////////////////////////////////////////////////////////////////////////////////////////////////
// LLBLGen Pro LINQPad driver is (c) 2002-2012 Solutions Design. All rights reserved.
// http://www.llblgen.com
////////////////////////////////////////////////////////////////////////////////////////////////////////
// COPYRIGHTS:
// Copyright (c)2002-2012 Solutions Design. All rights reserved.
// http://www.llblgen.com
//
// The LLBLGen Pro LINQPad driver sourcecode is released under the following license (BSD2):
// ----------------------------------------------------------------------
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1) Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// 2) Redistributions in binary form must reproduce the above copyright notice, this list of
// conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY SOLUTIONS DESIGN ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOLUTIONS DESIGN OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the authors
// and should not be interpreted as representing official policies, either expressed or implied,
// of Solutions Design.
//
//////////////////////////////////////////////////////////////////////
// Contributers to the code:
// - Frans Bouma [FB]
//////////////////////////////////////////////////////////////////////
// Special thanks to:
// - Jeremy Thomas
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LINQPad.Extensibility.DataContext;
using System.Reflection;
using SD.LLBLGen.Pro.ORMSupportClasses;
using LinqExpression = System.Linq.Expressions.Expression;
using System.ComponentModel;
using SD.LLBLGen.Pro.LinqSupportClasses;
using LINQPad;
namespace SD.LLBLGen.Pro.LINQPadDriver7Plus
{
/// <summary>
/// Class which produces the list of ExplorerItem objects which represent the schema of the selected entity assembly.
/// </summary>
internal class SchemaBuilder
{
#region Class Member Declarations
private IConnectionInfo _cxInfo;
private Type _linqMetaDataType;
private Delegate _callGetEntityFactoryDelegate;
private Dictionary<string, EntityTypeSchemaData> _entityTypeSchemaDataPerEntityName;
private Dictionary<Type, EntityTypeSchemaData> _entityTypeSchemaDataPerType;
/// <summary>
/// Lock object for factory delegate creator method.
/// </summary>
private static object _semaphore = new object();
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="SchemaBuilder"/> class.
/// </summary>
/// <param name="cxInfo">The cx info.</param>
/// <param name="linqMetaDataType">Type of the linq meta data.</param>
public SchemaBuilder(IConnectionInfo cxInfo, Type linqMetaDataType)
{
_cxInfo = cxInfo;
_linqMetaDataType = linqMetaDataType;
}
/// <summary>
/// Gets the schema.
/// </summary>
/// <returns></returns>
internal List<ExplorerItem> GetSchema()
{
VerifyEntityAssemblyVersion();
// We're only interested in the DataSource(2)<TEntity> returning properties.
var queryableProperties = _linqMetaDataType.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => typeof(IQueryable).IsAssignableFrom(p.PropertyType));
_entityTypeSchemaDataPerEntityName = new Dictionary<string, EntityTypeSchemaData>();
_entityTypeSchemaDataPerType = new Dictionary<Type, EntityTypeSchemaData>();
// first we discover all entity types.
foreach(var property in queryableProperties)
{
if((!property.PropertyType.IsGenericType) && !typeof(IQueryable).IsAssignableFrom(property.PropertyType))
{
continue;
}
var entityType = property.PropertyType.GetGenericArguments()[0];
ProduceEntityFactoryDelegate(entityType);
var factory = GetFactory(entityType);
if(factory == null)
{
continue;
}
var dummyInstance = factory.Create();
var entitySchemaData = new EntityTypeSchemaData()
{
EntityType = entityType,
Factory = factory,
InheritanceInfo = dummyInstance.GetInheritanceInfo(),
LinqMetaDataProperty = property
};
_entityTypeSchemaDataPerEntityName[dummyInstance.LLBLGenProEntityName] = entitySchemaData;
_entityTypeSchemaDataPerType[entityType] = entitySchemaData;
}
// traverse all entity type schema data objects we've created and recurse over the types to build all property data
foreach(var entityName in _entityTypeSchemaDataPerEntityName.Keys)
{
AddEntityExplorerItem(entityName);
}
// second iteration, we discover all navigators. Because all entity types are known we can simply use the lookups
foreach(var entityStateData in _entityTypeSchemaDataPerEntityName.Values.Distinct())
{
DiscoverEntityNavigators(entityStateData);
}
return _entityTypeSchemaDataPerType.Values.Select(v => v.RelatedExplorerItem).ToList();
}
/// <summary>
/// Verifies the version of the entity assembly, whether it's of a version this driver can work with.
/// </summary>
private void VerifyEntityAssemblyVersion()
{
if(_linqMetaDataType == null)
{
throw new InvalidOperationException("No ILinqMetaData type found.");
}
var ormSupportClassesAssemblyName = _linqMetaDataType.Assembly.GetReferencedAssemblies()
.Where(an=>(an.Name=="SD.LLBLGen.Pro.ORMSupportClasses") &&
(an.Version.Major==Constants.MajorVersion) &&
(an.Version.Minor==Constants.MinorVersion)).FirstOrDefault();
if(ormSupportClassesAssemblyName == null)
{
throw new InvalidOperationException(string.Format("The assembly '{0}' is not compiled against the LLBLGen Pro Runtime Framework v{1}.{2}.",
CxInfoHelper.GetEntityAssemblyFilename(_cxInfo, CxInfoHelper.GetTemplateGroup(_cxInfo)), Constants.MajorVersion, Constants.MinorVersion));
}
}
/// <summary>
/// Discovers the entity navigators.
/// </summary>
/// <param name="entitySchemaData">The entity schema data.</param>
private void DiscoverEntityNavigators(EntityTypeSchemaData entitySchemaData)
{
var entityType = entitySchemaData.EntityType;
if(entitySchemaData.Factory == null)
{
throw new InvalidOperationException(string.Format("Factory is null in entitySchemaData for entity '{0}'", entitySchemaData.EntityType.FullName));
}
var dummyInstance = entitySchemaData.Factory.Create();
var relationsWithMappedFields = dummyInstance.GetAllRelations().Where(r => !string.IsNullOrEmpty(r.MappedFieldName) && !r.IsHierarchyRelation);
var relationPerMappedFieldName = relationsWithMappedFields.ToDictionary(r => r.MappedFieldName);
var properties = TypeDescriptor.GetProperties(entityType).Cast<PropertyDescriptor>();
var inheritedProperties = new HashSet<PropertyDescriptor>(DetermineInheritedProperties(entitySchemaData.EntityType, properties));
var childrenCollection = entitySchemaData.RelatedExplorerItem.Children;
foreach(PropertyDescriptor property in properties)
{
if(!(typeof(IEntityCore).IsAssignableFrom(property.PropertyType) || typeof(IEntityCollectionCore).IsAssignableFrom(property.PropertyType)))
{
// not an entity navigator,
continue;
}
string suffix = string.Empty;
bool inherited = false;
if(inheritedProperties.Contains(property))
{
suffix = string.Format(" (Inherited from '{0}')", property.ComponentType.Name.Replace("Entity", ""));
inherited=true;
}
// relationMapped can be null, in the case of a m:n navigator.
IEntityRelation relationMapped = null;
relationPerMappedFieldName.TryGetValue(property.Name, out relationMapped);
if(typeof(IEntityCore).IsAssignableFrom(property.PropertyType))
{
var relatedEntitySchemaData = GetEntitySchemaDataForType(property.PropertyType);
// single entity navigator, or property added manually.
if(relationMapped == null)
{
// property added manually, ignore.
continue;
}
var icon = relationMapped.TypeOfRelation == RelationType.ManyToOne ? ExplorerIcon.ManyToOne : ExplorerIcon.OneToOne;
if(inherited)
{
icon=ExplorerIcon.Inherited;
}
childrenCollection.Add(new ExplorerItem(property.Name + suffix, ExplorerItemKind.ReferenceLink, icon)
{
DragText = property.Name,
HyperlinkTarget = relatedEntitySchemaData == null ? null : relatedEntitySchemaData.RelatedExplorerItem
});
continue;
}
if(typeof(IEntityCollectionCore).IsAssignableFrom(property.PropertyType))
{
// collection navigator. We have to determine which entity type is returned from the collection.
// First, check if there's a TypeContainedAttribute on the property. If so, use the type in the attribute. If not,
// try to determine the type using the linq utils method.
Type containedType = null;
var typeContainedAttribute = property.Attributes[typeof(TypeContainedAttribute)] as TypeContainedAttribute;
if((typeContainedAttribute != null) && typeContainedAttribute.TypeContainedInCollection != null)
{
containedType = typeContainedAttribute.TypeContainedInCollection;
}
else
{
containedType = LinqUtils.DetermineEntityTypeFromEntityCollectionType(property.PropertyType);
}
var relatedEntitySchemaData = GetEntitySchemaDataForType(containedType);
var icon = relationMapped == null ? ExplorerIcon.ManyToMany : ExplorerIcon.OneToMany;
if(inherited)
{
icon = ExplorerIcon.Inherited;
}
childrenCollection.Add(new ExplorerItem(property.Name + suffix, ExplorerItemKind.CollectionLink, icon)
{
DragText = property.Name,
HyperlinkTarget = relatedEntitySchemaData == null ? null : relatedEntitySchemaData.RelatedExplorerItem
});
}
}
}
/// <summary>
/// Gets the type of the entity schema data for.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <returns></returns>
private EntityTypeSchemaData GetEntitySchemaDataForType(Type entityType)
{
EntityTypeSchemaData toReturn = null;
_entityTypeSchemaDataPerType.TryGetValue(entityType, out toReturn);
return toReturn;
}
/// <summary>
/// Adds the explorer item for the entity with the name specified to the entity's schemadata object. If it's already present,
/// it simply returns that explorer item.
/// </summary>
/// <param name="entityName">Name of the entity.</param>
private void AddEntityExplorerItem(string entityName)
{
EntityTypeSchemaData entitySchemaData = null;
if(!_entityTypeSchemaDataPerEntityName.TryGetValue(entityName, out entitySchemaData))
{
return;
}
if(entitySchemaData.RelatedExplorerItem != null)
{
return;
}
// not yet created.
var entityNameForElement = entityName.Replace("Entity", "");
var suffix = string.Empty;
if(!string.IsNullOrEmpty(entitySchemaData.SuperTypeName))
{
suffix = string.Format(" (Sub-type of '{0}')", entitySchemaData.SuperTypeName.Replace("Entity", ""));
}
entitySchemaData.RelatedExplorerItem = new ExplorerItem(entityNameForElement + suffix, ExplorerItemKind.QueryableObject, ExplorerIcon.Table)
{
DragText = entityNameForElement,
IsEnumerable = true,
Children = new List<ExplorerItem>()
};
var dummyInstance = entitySchemaData.Factory.Create();
DiscoverEntityFields(dummyInstance, entitySchemaData);
}
/// <summary>
/// Discovers the entity fields for the entity type specified.
/// </summary>
/// <param name="instance">The instance.</param>
/// <param name="resourceType">Type of the resource.</param>
/// <param name="entityType">Type of the entity.</param>
/// <param name="customState">State of the custom.</param>
private void DiscoverEntityFields(IEntityCore instance, EntityTypeSchemaData entitySchemaData)
{
var instanceFieldsToTravese = instance.Fields
.Where(f =>!f.IsPrimaryKey || (f.IsPrimaryKey && (f.ActualContainingObjectName==f.ContainingObjectName)))
.ToDictionary(f=>f.Name);
var allProperties = TypeDescriptor.GetProperties(entitySchemaData.EntityType).Cast<PropertyDescriptor>();
var propertiesToTraverse = allProperties.Where(p => instanceFieldsToTravese.ContainsKey(p.Name));
var childrenCollection = entitySchemaData.RelatedExplorerItem.Children;
List<ExplorerItem> toAdd = new List<ExplorerItem>();
foreach(PropertyDescriptor property in propertiesToTraverse)
{
var field = instance.Fields[property.Name];
bool isPK = false;
bool isFK = false;
int fieldIndex = -1;
if(field == null)
{
// other property
if(typeof(IEntityCore).IsAssignableFrom(property.PropertyType) || typeof(IEntityCollectionCore).IsAssignableFrom(property.PropertyType))
{
// entity navigator, done later
continue;
}
else
{
if(!property.IsBrowsable)
{
continue;
}
}
}
else
{
isPK = field.IsPrimaryKey;
isFK = field.IsForeignKey;
fieldIndex = field.FieldIndex;
}
string propertyText = property.Name;
if(isFK)
{
propertyText+=" (FK)";
}
var icon = isPK ? ExplorerIcon.Key : ExplorerIcon.Column;
if(field.ActualContainingObjectName != field.ContainingObjectName)
{
// inherited field.
propertyText += string.Format(" (Inherited from '{0}')", field.ContainingObjectName.Replace("Entity", ""));
icon = ExplorerIcon.Inherited;
}
toAdd.Add(new ExplorerItem(propertyText, ExplorerItemKind.Property, icon)
{
DragText = property.Name,
Tag = fieldIndex
});
}
childrenCollection.AddRange(toAdd.OrderBy(i => (int)i.Tag).ThenBy(i => i.Text));
}
/// <summary>
/// Determines the inherited properties. This method only marks a property as inherited if it belongs to another entity or to commonentitybase or
/// classes up in the entity hierarchy.
/// </summary>
/// <param name="entityType">Type of the entity.</param>
/// <param name="allProperties">All properties.</param>
/// <returns>enumerable of property descriptors which are truly inherited from other entities.</returns>
private IEnumerable<PropertyDescriptor> DetermineInheritedProperties(Type entityType, IEnumerable<PropertyDescriptor> allProperties)
{
Type baseType = entityType.BaseType;
bool includeBaseTypeProperties = false;
if(typeof(IEntity).IsAssignableFrom(entityType))
{
// selfservicing
// check for 2-class scenario
includeBaseTypeProperties = baseType.Name.StartsWith(entityType.Name);
}
else
{
// adapter.
// check for 2-class scenario
includeBaseTypeProperties = entityType.Name.Equals("My" + baseType.Name);
}
if(includeBaseTypeProperties)
{
return allProperties.Where(p => p.ComponentType != entityType && p.ComponentType != baseType);
}
else
{
return allProperties.Where(p => p.ComponentType != entityType);
}
}
/// <summary>
/// Gets the factory for the entity with the type specified.
/// </summary>
/// <param name="elementType">Type of the element.</param>
/// <returns>the factory for the element specified, or null if not found</returns>
private IEntityFactoryCore GetFactory(Type elementType)
{
if(_callGetEntityFactoryDelegate == null)
{
return null;
}
return (IEntityFactoryCore)_callGetEntityFactoryDelegate.DynamicInvoke(elementType);
}
/// <summary>
/// Produces the entity factory delegate and sets the
/// </summary>
/// <param name="elementType">Type of the element.</param>
private void ProduceEntityFactoryDelegate(Type elementType)
{
if(typeof(IEntityCore).IsAssignableFrom(elementType) && (_callGetEntityFactoryDelegate == null))
{
lock(_semaphore)
{
if(_callGetEntityFactoryDelegate == null)
{
// create the delegate for invoking the entity factory factory.
string rootNamespace = elementType.FullName.Substring(0, elementType.FullName.Length - (elementType.Name.Length + ".EntityClasses".Length) - 1);
string generalEntityFactoryFactoryTypeName = rootNamespace + ".FactoryClasses.EntityFactoryFactory";
Type generalEntityFactoryFactoryType = elementType.Assembly.GetType(generalEntityFactoryFactoryTypeName);
if(generalEntityFactoryFactoryType == null)
{
return;
}
var methodInfo = generalEntityFactoryFactoryType.GetMethod("GetFactory", BindingFlags.Static | BindingFlags.Public, null,
new Type[] { typeof(Type) }, null);
if(methodInfo == null)
{
return;
}
var parameter = LinqExpression.Parameter(typeof(Type), "p");
var methodCallExpression = LinqExpression.Call(methodInfo, parameter);
var lambda = LinqExpression.Lambda(methodCallExpression, parameter);
_callGetEntityFactoryDelegate = lambda.Compile();
}
}
}
}
}
}