public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
        {
            var entityType = definition.EntityType;

            //Find the first property with the "Key" attribute to use as the Id
            var properties = entityType.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            var idProperty = properties.Where(p => p.GetCustomAttribute <KeyAttribute>() != null).FirstOrDefault();

            if (idProperty != null)
            {
                classMap.MapIdMember(idProperty);
            }

            //If there is no Id generator, set a default based on the member type
            if (classMap.IdMemberMap != null && classMap.IdMemberMap.IdGenerator == null)
            {
                var idMemberMap = classMap.IdMemberMap;
                var memberType  = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
                if (memberType == typeof(string))
                {
                    idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
                }
                else if (memberType == typeof(Guid))
                {
                    idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
                }
                else if (memberType == typeof(ObjectId))
                {
                    idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
                }
            }
        }
Esempio n. 2
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the BsonMemberMap class.
 /// </summary>
 /// <param name="classMap">The class map this member map belongs to.</param>
 /// <param name="memberInfo">The member info.</param>
 public BsonMemberMap(BsonClassMap classMap, MemberInfo memberInfo)
 {
     _classMap     = classMap;
     _memberInfo   = memberInfo;
     _memberType   = BsonClassMap.GetMemberInfoType(memberInfo);
     _defaultValue = GetDefaultValue(_memberType);
 }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the BsonMemberMap class.
 /// </summary>
 /// <param name="memberInfo">The member info.</param>
 /// <param name="conventions">The conventions to use with this member.</param>
 public BsonMemberMap(
     MemberInfo memberInfo,
     ConventionProfile conventions
     )
 {
     this.memberInfo  = memberInfo;
     this.memberType  = BsonClassMap.GetMemberInfoType(memberInfo);
     this.conventions = conventions;
 }
        // constructors
        /// <summary>
        /// Initializes a new instance of the BsonMemberMap class.
        /// </summary>
        /// <param name="classMap">The class map this member map belongs to.</param>
        /// <param name="memberInfo">The member info.</param>
        public BsonMemberMap(BsonClassMap classMap, MemberInfo memberInfo)
        {
            _classMap              = classMap;
            _memberInfo            = memberInfo;
            _memberType            = BsonClassMap.GetMemberInfoType(memberInfo);
            _memberTypeIsBsonValue = typeof(BsonValue).IsAssignableFrom(_memberType);

            Reset();
        }
Esempio n. 5
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the BsonMemberMap class.
 /// </summary>
 /// <param name="classMap">The class map this member map belongs to.</param>
 /// <param name="memberInfo">The member info.</param>
 public BsonMemberMap(BsonClassMap classMap, MemberInfo memberInfo)
 {
     _classMap              = classMap;
     _memberInfo            = memberInfo;
     _memberType            = BsonClassMap.GetMemberInfoType(memberInfo);
     _memberTypeIsBsonValue = typeof(BsonValue).IsAssignableFrom(_memberType);
     _defaultValue          = GetDefaultValue(_memberType);
     _defaultValueSpecified = false;
 }
        public void ApplyMapping(IEntityDefinition definition, BsonClassMap classMap)
        {
            IEntityProperty idProperty = default;

            foreach (var property in definition.Properties)
            {
                if (property.PropertyInfo.GetCustomAttribute <KeyAttribute>() != null)
                {
                    idProperty = property;
                    break;
                }

                if (property.ElementName.Equals("id", StringComparison.InvariantCultureIgnoreCase))
                {
                    //We don't break here just in case another property has the KeyAttribute
                    //We preference the attribute over the name match
                    idProperty = property;
                }
            }

            if (idProperty is EntityProperty entityProperty)
            {
                classMap.MapIdMember(idProperty.PropertyInfo);
                entityProperty.IsKey = true;

                //Set an Id Generator based on the member type
                var idMemberMap = classMap.IdMemberMap;
                var memberType  = BsonClassMap.GetMemberInfoType(idMemberMap.MemberInfo);
                if (memberType == typeof(string))
                {
                    idMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
                    definition.KeyGenerator = new EntityKeyGenerator(StringObjectIdGenerator.Instance);
                }
                else if (memberType == typeof(Guid))
                {
                    idMemberMap.SetIdGenerator(CombGuidGenerator.Instance);
                    definition.KeyGenerator = new EntityKeyGenerator(CombGuidGenerator.Instance);
                }
                else if (memberType == typeof(ObjectId))
                {
                    idMemberMap.SetIdGenerator(ObjectIdGenerator.Instance);
                    definition.KeyGenerator = new EntityKeyGenerator(ObjectIdGenerator.Instance);
                }
            }
        }
Esempio n. 7
0
 public IIdGenerator GetIdGenerator(MemberInfo memberInfo)
 {
     return(BsonSerializer.LookupIdGenerator(BsonClassMap.GetMemberInfoType(memberInfo)));
 }