// public methods
        /// <summary>
        /// Applies a modification to the class map.
        /// </summary>
        /// <param name="classMap">The class map.</param>
        public void Apply(BsonClassMap classMap)
        {
            foreach (var name in _names)
            {
                var member = classMap.ClassType.GetMember(name, _memberTypes, _bindingFlags).SingleOrDefault();

                if (member != null)
                {
                    Type memberType = null;
                    var  fieldInfo  = member as FieldInfo;
                    if (fieldInfo != null)
                    {
                        memberType = fieldInfo.FieldType;
                    }
                    else
                    {
                        var propertyInfo = member as PropertyInfo;
                        if (propertyInfo != null)
                        {
                            memberType = propertyInfo.PropertyType;
                        }
                    }

                    if (memberType != null && (memberType == typeof(BsonDocument) || typeof(IDictionary <string, object>).IsAssignableFrom(memberType)))
                    {
                        classMap.MapExtraElementsMember(member);
                        return;
                    }
                }
            }
        }
Beispiel #2
0
            public void PostProcess(BsonClassMap classMap)
            {
                var memberName = _convention.FindExtraElementsMember(classMap.ClassType);

                if (string.IsNullOrEmpty(memberName))
                {
                    return;
                }

                var memberInfo = classMap.ClassType.GetMember(memberName, MemberTypes.Field | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).SingleOrDefault();

                if (memberInfo == null)
                {
                    return;
                }

                classMap.MapExtraElementsMember(memberInfo);
            }
        public static bool TryConfigureExtraProperties(this BsonClassMap map)
        {
            if (!map.ClassType.IsAssignableTo <IHasExtraProperties>())
            {
                return(false);
            }

            var property = map.ClassType.GetProperty(
                nameof(IHasExtraProperties.ExtraProperties),
                BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty
                );

            if (property?.DeclaringType != map.ClassType)
            {
                return(false);
            }

            map.MapExtraElementsMember(property);

            return(true);
        }
            public void Apply(BsonClassMap classMap)
            {
                if (classMap.ClassType.Namespace.Contains("AppStoreService.Core"))
                {
                    foreach (var map in classMap.DeclaredMemberMaps.Where(x => x != null))
                    {
                        if (map.MemberType == typeof(object) &&
                            (map.MemberName == "Id" || map.MemberName == "ObjectId"))
                        {
                            classMap.MapIdMember(map.MemberInfo);
                            map.SetIdGenerator(new ObjectIdGenerator());
                        }

                        if (map.MemberType.IsGenericParameter && map.MemberType.GetGenericTypeDefinition() ==
                            typeof(NullValueDictionary <,>))
                        {
                            classMap.MapExtraElementsMember(map.MemberInfo);
                        }
                    }
                }
            }
Beispiel #5
0
        static void RegisterBsonClassMaps(Dictionary <Type, Type> readModelHasCustomClassMap)
        {
            foreach (var pair in readModelHasCustomClassMap)
            {
                if (pair.Value == null)
                {
                    if (pair.Key.HasInterface <IHaveExtraElements>())
                    {
                        if (BsonClassMap.IsClassMapRegistered(pair.Key))
                        {
                            continue;
                        }
                        var classMap = new BsonClassMap(pair.Key);

                        MemberInfo extraElementsMemberInfo = pair.Key.GetMember(nameof(IHaveExtraElements.ExtraElements)).FirstOrDefault();
                        classMap.AutoMap();
                        classMap.MapExtraElementsMember(extraElementsMemberInfo);
                        BsonClassMap.RegisterClassMap(classMap);
                    }
                    else
                    {
                        BsonClassMap.LookupClassMap(pair.Key);
                    }
                }
                else
                {
                    if (!(Activator.CreateInstance(pair.Value) is ICanRegisterBsonClassMap customClassMap))
                    {
                        throw new CustomBsonClassMapRegistratorNotProvided(
                                  "There isn't provided a custom BsonClassMap registrator class that implements " + typeof(ICanRegisterBsonClassMap).FullName
                                  + " for " + pair.Key.FullName);
                    }
                    if (!customClassMap.IsRegistered())
                    {
                        customClassMap.Register();
                    }
                }
            }
        }
            public void PostProcess(BsonClassMap classMap)
            {
                var memberName = _convention.FindExtraElementsMember(classMap.ClassType);
                if (string.IsNullOrEmpty(memberName))
                {
                    return;
                }

                var memberInfo = classMap.ClassType.GetMember(memberName, MemberTypes.Field | MemberTypes.Property, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).SingleOrDefault();
                if (memberInfo == null)
                {
                    return;
                }

                classMap.MapExtraElementsMember(memberInfo);
            }
        // public methods
        /// <summary>
        /// Applies a modification to the class map.
        /// </summary>
        /// <param name="classMap">The class map.</param>
        public void Apply(BsonClassMap classMap)
        {
            foreach (var name in _names)
            {
                var member = classMap.ClassType.GetMember(name, _memberTypes, _bindingFlags).SingleOrDefault();

                if (member != null)
                {
                    Type memberType = null;
                    var fieldInfo = member as FieldInfo;
                    if (fieldInfo != null)
                    {
                        memberType = fieldInfo.FieldType;
                    }
                    else
                    {
                        var propertyInfo = member as PropertyInfo;
                        if (propertyInfo != null)
                        {
                            memberType = propertyInfo.PropertyType;
                        }
                    }

                    if (memberType != null && (memberType == typeof(BsonDocument) || typeof(IDictionary<string, object>).IsAssignableFrom(memberType)))
                    {
                        classMap.MapExtraElementsMember(member);
                        return;
                    }
                }
            }
        }