Skip to content

Commit a929aff

Browse files
authored
Merge pull request #107 from TurnerSoftware/drop-bsonclassmap-automap
Drop BsonClassMap AutoMap
2 parents d9639b6 + 7910bd2 commit a929aff

18 files changed

+169
-185
lines changed

src/MongoFramework/Infrastructure/Mapping/DefaultMappingPack.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@ private DefaultMappingPack()
1313
{
1414
new CollectionNameProcessor(),
1515
new HierarchyProcessor(),
16+
new PropertyMappingProcessor(),
1617
new EntityIdProcessor(),
17-
new MappedPropertiesProcessor(),
1818
new NestedTypeProcessor(),
1919
new ExtraElementsProcessor(),
2020
new TypeDiscoveryProcessor(),
2121
new BsonKnownTypesProcessor(),
22-
new ClassMapPropertiesProcessor(),
2322
new IndexProcessor(),
2423
new EntityRelationshipProcessor()
2524
};

src/MongoFramework/Infrastructure/Mapping/EntityMapping.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public static IEntityDefinition RegisterType(Type entityType)
6767
try
6868
{
6969
var classMap = new BsonClassMap(entityType);
70-
7170
BsonClassMap.RegisterClassMap(classMap);
72-
classMap.AutoMap();
7371

7472
foreach (var processor in MappingProcessors)
7573
{

src/MongoFramework/Infrastructure/Mapping/Processors/ClassMapPropertiesProcessor.cs

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/MongoFramework/Infrastructure/Mapping/Processors/EntityIdProcessor.cs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,37 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
1515
var entityType = definition.EntityType;
1616

1717
//Find the first property with the "Key" attribute to use as the Id
18-
var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
19-
var idProperty = properties.Where(p => p.GetCustomAttribute<KeyAttribute>() != null).FirstOrDefault();
20-
if (idProperty != null)
18+
var properties = definition.Properties;
19+
var idProperty = properties.Where(p => p.PropertyInfo.GetCustomAttribute<KeyAttribute>() != null).FirstOrDefault();
20+
if (idProperty == null)
2121
{
22-
classMap.MapIdMember(idProperty);
22+
idProperty = properties
23+
.Where(p => p.ElementName.Equals("id", StringComparison.InvariantCultureIgnoreCase))
24+
.FirstOrDefault();
2325
}
2426

25-
//If there is no Id generator, set a default based on the member type
26-
if (classMap.IdMemberMap != null && classMap.IdMemberMap.IdGenerator == null)
27+
if (idProperty is EntityProperty entityProperty)
2728
{
28-
var idMemberMap = classMap.IdMemberMap;
29-
var memberType = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
30-
if (memberType == typeof(string))
31-
{
32-
idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
33-
}
34-
else if (memberType == typeof(Guid))
35-
{
36-
idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
37-
}
38-
else if (memberType == typeof(ObjectId))
29+
classMap.MapIdMember(idProperty.PropertyInfo);
30+
entityProperty.IsKey = true;
31+
32+
//If there is no Id generator, set a default based on the member type
33+
if (classMap.IdMemberMap.IdGenerator == null)
3934
{
40-
idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
35+
var idMemberMap = classMap.IdMemberMap;
36+
var memberType = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
37+
if (memberType == typeof(string))
38+
{
39+
idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
40+
}
41+
else if (memberType == typeof(Guid))
42+
{
43+
idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
44+
}
45+
else if (memberType == typeof(ObjectId))
46+
{
47+
idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
48+
}
4149
}
4250
}
4351
}

src/MongoFramework/Infrastructure/Mapping/Processors/EntityRelationshipProcessor.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
1616
{
1717
definition.Relationships = GetEntityRelationships(definition).ToArray();
1818

19+
var removeProperties = new HashSet<string>();
20+
1921
foreach (var relationship in definition.Relationships)
2022
{
2123
if (relationship.IsCollection)
@@ -27,9 +29,13 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
2729
}
2830
else
2931
{
32+
removeProperties.Add(relationship.NavigationProperty.FullPath);
3033
classMap.UnmapMember(relationship.NavigationProperty.PropertyInfo);
3134
}
3235
}
36+
37+
//Remove navigation properties
38+
definition.Properties = definition.Properties.Where(p => !removeProperties.Contains(p.FullPath)).ToArray();
3339
}
3440
private IEnumerable<IEntityRelationship> GetEntityRelationships(IEntityDefinition definition)
3541
{

src/MongoFramework/Infrastructure/Mapping/Processors/ExtraElementsProcessor.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
2222
}
2323
else
2424
{
25+
classMap.SetIgnoreExtraElements(false);
26+
2527
//If any of the Entity's properties have the "ExtraElementsAttribute", assign that against the BsonClassMap
26-
var extraElementsProperty = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
28+
var extraElementsProperty = definition.Properties
2729
.Select(p => new
2830
{
29-
PropertyInfo = p,
30-
ExtraElementsAttribute = p.GetCustomAttribute<ExtraElementsAttribute>()
31+
Property = p,
32+
ExtraElementsAttribute = p.PropertyInfo.GetCustomAttribute<ExtraElementsAttribute>()
3133
}).Where(p => p.ExtraElementsAttribute != null).FirstOrDefault();
3234

33-
if (extraElementsProperty != null && typeof(IDictionary<string, object>).IsAssignableFrom(extraElementsProperty.PropertyInfo.PropertyType))
35+
if (extraElementsProperty != null && typeof(IDictionary<string, object>).IsAssignableFrom(extraElementsProperty.Property.PropertyType))
3436
{
3537
var memberMap = classMap.DeclaredMemberMaps
36-
.Where(m => m.MemberInfo == extraElementsProperty.PropertyInfo)
38+
.Where(m => m.ElementName == extraElementsProperty.Property.ElementName)
3739
.FirstOrDefault();
3840
classMap.SetExtraElementsMember(memberMap);
3941
}

src/MongoFramework/Infrastructure/Mapping/Processors/MappedPropertiesProcessor.cs

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/MongoFramework/Infrastructure/Mapping/Processors/NestedTypeProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class NestedTypeProcessor : IMappingProcessor
1212
public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
1313
{
1414
var entityType = definition.EntityType;
15-
var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
15+
var properties = definition.Properties;
1616

1717
foreach (var property in properties)
1818
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using MongoDB.Bson.Serialization;
2+
using System;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
using System.Linq;
5+
using System.Reflection;
6+
7+
namespace MongoFramework.Infrastructure.Mapping.Processors
8+
{
9+
public class PropertyMappingProcessor : IMappingProcessor
10+
{
11+
public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
12+
{
13+
var entityType = definition.EntityType;
14+
var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
15+
16+
foreach (var property in properties)
17+
{
18+
if (!property.CanRead || !property.CanWrite)
19+
{
20+
continue;
21+
}
22+
23+
//Skip overridden properties
24+
var getMethod = property.GetMethod;
25+
if (property.GetMethod.IsVirtual && getMethod.GetBaseDefinition().DeclaringType != entityType)
26+
{
27+
continue;
28+
}
29+
30+
//Skip indexer properties (eg. "this[int index]")
31+
if (property.GetIndexParameters().Length > 0)
32+
{
33+
continue;
34+
}
35+
36+
//Skip properties with the "NotMappedAttribute"
37+
var notMappedAttribute = property.GetCustomAttribute<NotMappedAttribute>();
38+
if (notMappedAttribute != null)
39+
{
40+
continue;
41+
}
42+
43+
//Do the mapping
44+
var memberMap = classMap.MapMember(property);
45+
46+
//Set custom element name with the "ColumnAttribute"
47+
var columnAttribute = property.GetCustomAttribute<ColumnAttribute>();
48+
if (columnAttribute != null)
49+
{
50+
var mappedName = columnAttribute.Name;
51+
memberMap.SetElementName(mappedName);
52+
}
53+
}
54+
55+
definition.Properties = classMap.DeclaredMemberMaps
56+
.Select(m => new EntityProperty
57+
{
58+
EntityType = definition.EntityType,
59+
IsKey = m == classMap.IdMemberMap,
60+
ElementName = m.ElementName,
61+
FullPath = m.ElementName,
62+
PropertyType = (m.MemberInfo as PropertyInfo).PropertyType,
63+
PropertyInfo = (m.MemberInfo as PropertyInfo)
64+
}).ToArray();
65+
}
66+
}
67+
}

tests/MongoFramework.Benchmarks/MongoFramework.Benchmarks.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,4 @@
1414
<ProjectReference Include="..\..\src\MongoFramework\MongoFramework.csproj" />
1515
</ItemGroup>
1616

17-
<ItemGroup>
18-
<Folder Include="docs\" />
19-
</ItemGroup>
20-
2117
</Project>

0 commit comments

Comments
 (0)