private void IgnoreMembersWithBsonIgnoreAttribute(BsonClassMap classMap) { foreach (var memberMap in classMap.DeclaredMemberMaps.ToList()) { var ignoreAttribute = (BsonIgnoreAttribute)memberMap.MemberInfo.GetCustomAttributes(typeof(BsonIgnoreAttribute), false).FirstOrDefault(); // james.wei 增加对XmlIgnoreAttribute标记的处理 2015-05-26 var ignoreAttribute2 = (XmlIgnoreAttribute)memberMap.MemberInfo.GetCustomAttributes(typeof(XmlIgnoreAttribute), false).FirstOrDefault(); if (ignoreAttribute != null || ignoreAttribute2 != null) { classMap.UnmapMember(memberMap.MemberInfo); } } #region 特殊处理EAS.Data 实体/所有虚属性都不处理 2015-05-26 System.Type T1 = System.Type.GetType("EAS.Data.ORM.BaseDataEntity,EAS.Data"); System.Type T2 = System.Type.GetType("EAS.Data.ORM.VirtualAttribute,EAS.Data"); if (T1 != null && T1.IsAssignableFrom(classMap.ClassType)) { foreach (var memberMap in classMap.DeclaredMemberMaps.ToList()) { var virtualAttribute = memberMap.MemberInfo.GetCustomAttributes(T2, false).FirstOrDefault(); if (virtualAttribute != null) { classMap.UnmapMember(memberMap.MemberInfo); } } } #endregion }
public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) { //Remove invalid maps for the class foreach (var map in classMap.DeclaredMemberMaps.ToArray()) { if (map.MemberInfo is PropertyInfo propertyInfo) { var getPropertyBaseDefinition = propertyInfo.GetMethod.GetBaseDefinition(); if (getPropertyBaseDefinition.DeclaringType != definition.EntityType) { //Remove any member map that just overrides another member classMap.UnmapMember(map.MemberInfo); continue; } } else { //Removes any member map that isn't for a property classMap.UnmapMember(map.MemberInfo); } } definition.Properties = classMap.DeclaredMemberMaps .Select(m => new EntityProperty { EntityType = definition.EntityType, IsKey = m == classMap.IdMemberMap, ElementName = m.ElementName, FullPath = m.ElementName, PropertyType = (m.MemberInfo as PropertyInfo).PropertyType, PropertyInfo = (m.MemberInfo as PropertyInfo) }); }
public static bool RegisterTrackablePocoMap(Type trackablePocoType) { if (BsonClassMap.IsClassMapRegistered(trackablePocoType)) { return(false); } var classMap = new BsonClassMap(trackablePocoType); var pocoType = TrackableResolver.GetPocoType(trackablePocoType); // start with auto map classMap.AutoMap(); // ignore extra elements for smooth schema change classMap.SetIgnoreExtraElements(true); // unmap all members which T doesn't have var propertyNames = new HashSet <string>(pocoType.GetProperties().Select(p => p.Name)); var deletingMembers = classMap.DeclaredMemberMaps.Where(m => { var propertyInfo = m.MemberInfo as PropertyInfo; return(propertyInfo == null || propertyNames.Contains(propertyInfo.Name) == false); }).ToList(); foreach (var m in deletingMembers) { classMap.UnmapMember(m.MemberInfo); } // set default ignore for saving spaces foreach (var memberMap in classMap.DeclaredMemberMaps) { var mt = memberMap.MemberType; var defaultValue = mt.IsValueType ? Activator.CreateInstance(mt) : null; memberMap.SetDefaultValue(defaultValue); memberMap.SetIgnoreIfDefault(true); } // tell customized id to mongo-db var identityColumn = pocoType.GetProperties().FirstOrDefault( p => TrackablePropertyAttribute.GetParameter(p, "mongodb.identity") != null); if (identityColumn != null) { classMap.MapIdProperty(identityColumn.Name); } try { BsonClassMap.RegisterClassMap(classMap); } catch (ArgumentException) { // if duplicate key exists return(false); } return(true); }
public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) { definition.Relationships = GetEntityRelationships(definition).ToArray(); var removeProperties = new HashSet <string>(); foreach (var relationship in definition.Relationships) { if (relationship.IsCollection) { var memberMap = classMap.MapMember(relationship.NavigationProperty.PropertyInfo); var serializerType = typeof(EntityNavigationCollectionSerializer <>).MakeGenericType(relationship.EntityType); var collectionSerializer = Activator.CreateInstance(serializerType, relationship.IdProperty) as IBsonSerializer; memberMap.SetSerializer(collectionSerializer); } else { removeProperties.Add(relationship.NavigationProperty.FullPath); classMap.UnmapMember(relationship.NavigationProperty.PropertyInfo); } } //Remove navigation properties definition.Properties = definition.Properties.Where(p => !removeProperties.Contains(p.FullPath)).ToArray(); }
public override void Map(BsonClassMap <Entity> config) { config.MapIdMember(m => m.Id) .SetIdGenerator(StringObjectIdGenerator.Instance); config.IdMemberMap .SetSerializer(new StringSerializer(BsonType.ObjectId)); config.UnmapMember(e => e.DomainEvents); }
private void IgnoreMembersWithBsonIgnoreAttribute(BsonClassMap classMap) { foreach (var memberMap in classMap.DeclaredMemberMaps.ToList()) { var ignoreAttribute = (BsonIgnoreAttribute)memberMap.MemberInfo.GetCustomAttributes(inherit: false).OfType <BsonIgnoreAttribute>().FirstOrDefault(); if (ignoreAttribute != null) { classMap.UnmapMember(memberMap.MemberInfo); } } }
public static void Register(BsonClassMap <NodeStorage <CindiClusterState> > cm) { cm.AutoMap(); cm.SetIsRootClass(true); cm.MapMember(s => s.Logs).SetSerializer(new DictionaryInterfaceImplementerSerializer <SortedList <int, LogEntry> >(DictionaryRepresentation.ArrayOfArrays)); /*AppDomain.CurrentDomain.GetAssemblies().SelectMany(assem => assem.GetTypes()).Where(type => type.IsSubclassOf(typeof(NodeStorage))).ToList() * .ForEach(type => cm.AddKnownType(type));*/ cm.UnmapMember(c => c.CommandsQueue); cm.SetIgnoreExtraElements(true); }
public static bool RegisterClassMap(Type classType) { if (BsonClassMap.IsClassMapRegistered(classType)) { return(false); } var classMap = new BsonClassMap(classType); // start with auto map classMap.AutoMap(); // ignore extra elements for smooth schema change classMap.SetIgnoreExtraElements(true); // unmap all members which has mongodb.ignore attribute var deletingMembers = classMap .DeclaredMemberMaps .Where(m => { return(TrackablePropertyAttribute.GetParameter(m.MemberInfo, "mongodb.ignore") != null); }) .ToList(); foreach (var m in deletingMembers) { classMap.UnmapMember(m.MemberInfo); } // set default ignore for saving spaces foreach (var memberMap in classMap.DeclaredMemberMaps) { var mt = memberMap.MemberType; var defaultValue = mt.IsValueType ? Activator.CreateInstance(mt) : null; memberMap.SetDefaultValue(defaultValue); memberMap.SetIgnoreIfDefault(true); } try { BsonClassMap.RegisterClassMap(classMap); } catch (ArgumentException) { // if duplicate key exists return(false); } return(true); }
public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap) { definition.Relationships = GetEntityRelationships(definition).ToArray(); foreach (var relationship in definition.Relationships) { if (relationship.IsCollection) { var memberMap = classMap.MapMember(relationship.NavigationProperty.PropertyInfo); var serializerType = typeof(EntityNavigationCollectionSerializer <>).MakeGenericType(relationship.EntityType); var collectionSerializer = Activator.CreateInstance(serializerType, relationship.IdProperty) as IBsonSerializer; memberMap.SetSerializer(collectionSerializer); } else { classMap.UnmapMember(relationship.NavigationProperty.PropertyInfo); } } }
public void ApplyMapping(Type entityType, BsonClassMap classMap) { var entityMapper = new EntityMapper(entityType); var relationships = entityMapper.GetEntityRelationships(); foreach (var relationship in relationships) { if (relationship.IsCollection) { var memberMap = classMap.MapMember(relationship.NavigationProperty); var serializerType = typeof(EntityNavigationCollectionSerializer <>).MakeGenericType(relationship.EntityType); var collectionSerializer = Activator.CreateInstance(serializerType, relationship.IdProperty.Name) as IBsonSerializer; memberMap.SetSerializer(collectionSerializer); } else { classMap.UnmapMember(relationship.NavigationProperty); } } }
public static bool RegisterClassMap(Type classType) { if (BsonClassMap.IsClassMapRegistered(classType)) return false; var classMap = new BsonClassMap(classType); // start with auto map classMap.AutoMap(); // ignore extra elements for smooth schema change classMap.SetIgnoreExtraElements(true); // unmap all members which has mongodb.ignore attribute var deletingMembers = classMap.DeclaredMemberMaps.Where(m => { return TrackablePropertyAttribute.GetParameter(m.MemberInfo, "mongodb.ignore") != null; }).ToList(); foreach (var m in deletingMembers) classMap.UnmapMember(m.MemberInfo); // set default ignore for saving spaces foreach (var memberMap in classMap.DeclaredMemberMaps) { var mt = memberMap.MemberType; var defaultValue = mt.IsValueType ? Activator.CreateInstance(mt) : null; memberMap.SetDefaultValue(defaultValue); memberMap.SetIgnoreIfDefault(true); } try { BsonClassMap.RegisterClassMap(classMap); } catch (ArgumentException) { // if duplicate key exists return false; } return true; }
private void IgnoreMembersWithBsonIgnoreAttribute(BsonClassMap classMap) { foreach (var memberMap in classMap.DeclaredMemberMaps.ToList()) { var ignoreAttribute = (BsonIgnoreAttribute)memberMap.MemberInfo.GetCustomAttributes(typeof(BsonIgnoreAttribute), false).FirstOrDefault(); if (ignoreAttribute != null) { classMap.UnmapMember(memberMap.MemberInfo); } } }
public static bool RegisterTrackablePocoMap(Type trackablePocoType) { if (BsonClassMap.IsClassMapRegistered(trackablePocoType)) return false; var classMap = new BsonClassMap(trackablePocoType); var pocoType = TrackableResolver.GetPocoType(trackablePocoType); // start with auto map classMap.AutoMap(); // ignore extra elements for smooth schema change classMap.SetIgnoreExtraElements(true); // unmap all members which T doesn't have var propertyNames = new HashSet<string>(pocoType.GetProperties().Select(p => p.Name)); var deletingMembers = classMap.DeclaredMemberMaps.Where(m => { var propertyInfo = m.MemberInfo as PropertyInfo; return propertyInfo == null || propertyNames.Contains(propertyInfo.Name) == false; }).ToList(); foreach (var m in deletingMembers) classMap.UnmapMember(m.MemberInfo); // set default ignore for saving spaces foreach (var memberMap in classMap.DeclaredMemberMaps) { var mt = memberMap.MemberType; var defaultValue = mt.IsValueType ? Activator.CreateInstance(mt) : null; memberMap.SetDefaultValue(defaultValue); memberMap.SetIgnoreIfDefault(true); } // tell customized id to mongo-db var identityColumn = pocoType.GetProperties().FirstOrDefault( p => TrackablePropertyAttribute.GetParameter(p, "mongodb.identity") != null); if (identityColumn != null) { classMap.MapIdProperty(identityColumn.Name); } try { BsonClassMap.RegisterClassMap(classMap); } catch (ArgumentException) { // if duplicate key exists return false; } return true; }