public void ApplyMapping(Type entityType, BsonClassMap classMap) { //Ignore extra elements when the "IgnoreExtraElementsAttribute" is on the Entity var ignoreExtraElements = entityType.GetCustomAttribute <IgnoreExtraElementsAttribute>(); if (ignoreExtraElements != null) { classMap.SetIgnoreExtraElements(true); classMap.SetIgnoreExtraElementsIsInherited(ignoreExtraElements.IgnoreInherited); } else { //If any of the Entity's properties have the "ExtraElementsAttribute", assign that against the BsonClassMap var extraElementsProperty = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly) .Select(p => new { PropertyInfo = p, ExtraElementsAttribute = p.GetCustomAttribute <ExtraElementsAttribute>() }).Where(p => p.ExtraElementsAttribute != null).FirstOrDefault(); if (extraElementsProperty != null && typeof(IDictionary <string, object>).IsAssignableFrom(extraElementsProperty.PropertyInfo.PropertyType)) { classMap.SetExtraElementsMember(new BsonMemberMap(classMap, extraElementsProperty.PropertyInfo)); } } }
public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) { var entityType = definition.EntityType; //Ignore extra elements when the "IgnoreExtraElementsAttribute" is on the Entity var ignoreExtraElements = entityType.GetCustomAttribute <IgnoreExtraElementsAttribute>(); if (ignoreExtraElements != null) { classMap.SetIgnoreExtraElements(true); classMap.SetIgnoreExtraElementsIsInherited(ignoreExtraElements.IgnoreInherited); } else { classMap.SetIgnoreExtraElements(false); //If any of the Entity's properties have the "ExtraElementsAttribute", assign that against the BsonClassMap foreach (var property in definition.Properties) { var extraElementsAttribute = property.PropertyInfo.GetCustomAttribute <ExtraElementsAttribute>(); if (extraElementsAttribute != null && typeof(IDictionary <string, object>).IsAssignableFrom(property.PropertyType)) { foreach (var memberMap in classMap.DeclaredMemberMaps) { if (memberMap.ElementName == property.ElementName) { classMap.SetExtraElementsMember(memberMap); return; } } } } } }
public static void ConfigureExtraProperties <T>(this BsonClassMap <T> map) where T : class, IHasExtraProperties { map.SetExtraElementsMember(new BsonMemberMap( map, typeof(T).GetMember(nameof(IHasExtraProperties.ExtraProperties))[0]) ); }
/// <summary> /// Configures SetExtraElementsMember if the <see cref="BsonClassMap.ClassType"/> /// implements the <see cref="IHasExtraProperties"/> interface. /// Otherwise, does nothing /// </summary> public static void ConfigureExtraProperties(this BsonClassMap map) { if (map.ClassType.IsAssignableTo <IHasExtraProperties>()) { map.SetExtraElementsMember( new BsonMemberMap( map, map.ClassType.GetMember(nameof(IHasExtraProperties.ExtraProperties))[0] ) ); } }
private static void ConfigureDefaultsInternal(Type entityType) { var map = new BsonClassMap(entityType); map.AutoMap(); if (entityType.IsAssignableTo <IHasExtraProperties>()) { map.SetExtraElementsMember( new BsonMemberMap( map, entityType.GetMember(nameof(IHasExtraProperties.ExtraProperties))[0] ) ); } BsonClassMap.RegisterClassMap(map); }
public static bool TryConfigureExtraProperties <TEntity> (this BsonClassMap <TEntity> map) where TEntity : class, IHasExtraProperties { var property = map.ClassType.GetProperty( nameof(IHasExtraProperties.ExtraProperties), BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty ); if (property?.DeclaringType != map.ClassType) { return(false); } map.SetExtraElementsMember(new BsonMemberMap( map, typeof(TEntity).GetMember(nameof(IHasExtraProperties.ExtraProperties)) [0])); return(true); }