public void TestAllValuesGoBackToTheirDefaults()
        {
            var classMap = new BsonClassMap <TestClass>(cm =>
            {
                cm.SetCreator(() => { throw new Exception("will get reset."); });
                cm.MapMember(x => x.String);
                cm.SetDiscriminator("blah");
                cm.SetDiscriminatorIsRequired(true);
                cm.MapExtraElementsMember(x => x.ExtraElements);
                cm.MapIdMember(x => x.OId);
                cm.SetIgnoreExtraElements(false);
                cm.SetIgnoreExtraElementsIsInherited(true);
                cm.SetIsRootClass(true);
                cm.AddKnownType(typeof(InheritedTestClass));
            });

            classMap.Reset();

            classMap.Freeze();

            Assert.DoesNotThrow(() => classMap.CreateInstance());
            Assert.AreEqual(0, classMap.DeclaredMemberMaps.Count());
            Assert.AreEqual("TestClass", classMap.Discriminator);
            Assert.IsFalse(classMap.DiscriminatorIsRequired);
            Assert.IsNull(classMap.ExtraElementsMemberMap);
            Assert.IsNull(classMap.IdMemberMap);
            Assert.IsTrue(classMap.IgnoreExtraElements);
            Assert.IsFalse(classMap.IgnoreExtraElementsIsInherited);
            Assert.IsFalse(classMap.IsRootClass);
            Assert.AreEqual(0, classMap.KnownTypes.Count());
        }
        public void SetOrder_not_called()
        {
            var cm = new BsonClassMap <C>();

            cm.AutoMap();
            cm.Freeze();

            cm.AllMemberMaps.Select(m => m.MemberName).Should().Equal("Id", "X", "Y");
        }
        public void SetOrder_Id_2_X_1_called()
        {
            var cm = new BsonClassMap <C>();

            cm.AutoMap();
            cm.GetMemberMap("Id").SetOrder(2);
            cm.GetMemberMap("X").SetOrder(1);
            cm.Freeze();

            cm.AllMemberMaps.Select(m => m.MemberName).Should().Equal("X", "Id", "Y");
        }
Ejemplo n.º 4
0
            private IBsonSerializer BuildProjectedSerializer(ProjectionMapping mapping)
            {
                // We are building a serializer specifically for a projected type based
                // on serialization information collected from other serializers.
                // We cannot cache this in the serializer registry because the compiler reuses
                // the same anonymous type definition in different contexts as long as they
                // are structurally equatable. As such, it might be that two different queries
                // projecting the same shape might need to be deserialized differently.
                var          classMapType = typeof(BsonClassMap <>).MakeGenericType(mapping.Expression.Type);
                BsonClassMap classMap     = (BsonClassMap)Activator.CreateInstance(classMapType);

                foreach (var memberMapping in mapping.Members)
                {
                    var serializationExpression = memberMapping.Expression as ISerializationExpression;
                    if (serializationExpression == null)
                    {
                        var serializer        = Build(memberMapping.Expression);
                        var serializationInfo = new BsonSerializationInfo(
                            memberMapping.Member.Name,
                            serializer,
                            TypeHelper.GetMemberType(memberMapping.Member));
                        serializationExpression = new SerializationExpression(
                            memberMapping.Expression,
                            serializationInfo);
                    }

                    var memberMap = classMap.MapMember(memberMapping.Member)
                                    .SetSerializer(serializationExpression.SerializationInfo.Serializer)
                                    .SetElementName(memberMapping.Member.Name);

                    if (classMap.IdMemberMap == null && serializationExpression is IdExpression)
                    {
                        classMap.SetIdMember(memberMap);
                    }
                }

                var mappedParameters = mapping.Members
                                       .Where(x => x.Parameter != null)
                                       .OrderBy(x => x.Parameter.Position)
                                       .Select(x => x.Member)
                                       .ToList();

                if (mappedParameters.Count > 0)
                {
                    classMap.MapConstructor(mapping.Constructor)
                    .SetArguments(mappedParameters);
                }

                var serializerType = typeof(BsonClassMapSerializer <>).MakeGenericType(mapping.Expression.Type);

                return((IBsonSerializer)Activator.CreateInstance(serializerType, classMap.Freeze()));
            }
        public void TestSetCreator()
        {
            var classMap = new BsonClassMap <B>(cm =>
            {
                cm.AutoMap();
                cm.SetCreator(() => new B(10));
            });

            classMap.Freeze();

            var instance = (B)classMap.CreateInstance();

            Assert.AreEqual(10, instance.A);
        }
            private IBsonSerializer BuildSerializerForAnonymousType(NewExpression node)
            {
                // We are building a serializer specifically for an anonymous type based
                // on serialization information collected from other serializers.
                // We cannot cache this because the compiler reuses the same anonymous type
                // definition in different contexts as long as they are structurally equatable.
                // As such, it might be that two different queries projecting the same shape
                // might need to be deserialized differently.
                var          classMapType = typeof(BsonClassMap <>).MakeGenericType(node.Type);
                BsonClassMap classMap     = (BsonClassMap)Activator.CreateInstance(classMapType);

                var properties             = node.Type.GetProperties();
                var parameterToPropertyMap = from parameter in node.Constructor.GetParameters()
                                             join property in properties on parameter.Name equals property.Name
                                             select new { Parameter = parameter, Property = property };

                foreach (var parameterToProperty in parameterToPropertyMap)
                {
                    var argument = node.Arguments[parameterToProperty.Parameter.Position];
                    var serializationExpression = argument as ISerializationExpression;
                    if (serializationExpression == null)
                    {
                        var serializer        = Build(argument);
                        var serializationInfo = new BsonSerializationInfo(parameterToProperty.Property.Name, serializer, parameterToProperty.Property.PropertyType);
                        serializationExpression = new SerializationExpression(
                            node.Arguments[parameterToProperty.Parameter.Position],
                            serializationInfo);
                    }

                    var memberMap = classMap.MapMember(parameterToProperty.Property)
                                    .SetSerializer(serializationExpression.SerializationInfo.Serializer)
                                    .SetElementName(parameterToProperty.Property.Name);

                    if (classMap.IdMemberMap == null && serializationExpression is IdExpression)
                    {
                        classMap.SetIdMember(memberMap);
                    }

                    //TODO: Need to set default value as well...
                }

                // Anonymous types are immutable and have all their values passed in via a ctor.
                classMap.MapConstructor(node.Constructor, properties.Select(x => x.Name).ToArray());
                classMap.Freeze();

                var serializerType = typeof(BsonClassMapSerializer <>).MakeGenericType(node.Type);

                return((IBsonSerializer)Activator.CreateInstance(serializerType, classMap));
            }
Ejemplo n.º 7
0
        public static List <BsonDocument> ApplyLookup <TSource>(this IFindFluent <TSource, TSource> findFluent, FilterDefinition <TSource> filterDefinition)
        {
            var bsonClassMap = new BsonClassMap(typeof(TSource));

            bsonClassMap.AutoMap();
            bsonClassMap.Freeze();
            var serializer   = new BsonClassMapSerializer <TSource>(bsonClassMap);
            var bsonDocument = filterDefinition.Render(serializer, new BsonSerializerRegistry());
            var propertyInfo = typeof(TSource).GetProperty("Lookup");

            if (propertyInfo == null)
            {
                return(new List <BsonDocument>());
            }

            var lookups       = (Dictionary <string, EntityLookup>)propertyInfo.GetValue("Lookup");
            var bsonDocuments = new List <BsonDocument>();

            foreach (var lookup in lookups)
            {
                bsonDocuments.Add(new BsonDocument
                {
                    {
                        "$lookup", new BsonDocument
                        {
                            { "from", lookup.Value.From }, { "foreignField", lookup.Value.ForeignField },
                            { "localField", lookup.Value.LocalField }, { "as", lookup.Value.As }
                        }
                    }
                });

                bsonDocuments.Add(new BsonDocument
                {
                    { "$unwind", new BsonDocument {
                          { "path", $"${lookup.Value.As}" }, { "preserveNullAndEmptyArrays", true }
                      } }
                });
            }
            bsonDocuments.Insert(0, new BsonDocument {
                { "$match", bsonDocument }
            });
            return(bsonDocuments);
        }
        public void TestUnmap()
        {
            var classMap = new BsonClassMap <C>(cm => {
                cm.AutoMap();
                cm.SetIdMember(cm.GetMemberMap("Id"));
                cm.UnmapField("Id");
                cm.UnmapField("FieldUnmappedByName");
                cm.UnmapField(c => c.FieldUnmappedByLambda);
                cm.UnmapProperty("PropertyUnmappedByName");
                cm.UnmapProperty(c => c.PropertyUnmappedByLambda);
            });

            classMap.Freeze();
            Assert.IsNull(classMap.IdMemberMap);
            Assert.AreEqual(1, classMap.MemberMaps.Count());
            var memberMap = classMap.MemberMaps.Single();

            Assert.AreEqual("X", memberMap.MemberName);
        }
        public void TestSetCreator()
        {
            var classMap = new BsonClassMap<B>(cm =>
            {
                cm.AutoMap();
                cm.SetCreator(() => new B(10));
            });

            classMap.Freeze();

            var instance = (B)classMap.CreateInstance();
            Assert.AreEqual(10, instance.A);
        }
 public void TestUnmap()
 {
     var classMap = new BsonClassMap<C>(cm =>
     {
         cm.AutoMap();
         cm.SetIdMember(cm.GetMemberMap("Id"));
         cm.UnmapField("Id");
         cm.UnmapField("FieldUnmappedByName");
         cm.UnmapField(c => c.FieldUnmappedByLambda);
         cm.UnmapProperty("PropertyUnmappedByName");
         cm.UnmapProperty(c => c.PropertyUnmappedByLambda);
     });
     classMap.Freeze();
     Assert.IsNull(classMap.IdMemberMap);
     Assert.AreEqual(1, classMap.AllMemberMaps.Count());
     var memberMap = classMap.AllMemberMaps.Single();
     Assert.AreEqual("X", memberMap.MemberName);
 }
        public void TestAllValuesGoBackToTheirDefaults()
        {
            var classMap = new BsonClassMap<TestClass>(cm =>
            {
                cm.SetCreator(() => { throw new Exception("will get reset."); });
                cm.MapMember(x => x.String);
                cm.SetDiscriminator("blah");
                cm.SetDiscriminatorIsRequired(true);
                cm.MapExtraElementsMember(x => x.ExtraElements);
                cm.MapIdMember(x => x.OId);
                cm.SetIgnoreExtraElements(false);
                cm.SetIgnoreExtraElementsIsInherited(true);
                cm.SetIsRootClass(true);
                cm.AddKnownType(typeof(InheritedTestClass));
            });

            classMap.Reset();

            classMap.Freeze();

            Assert.DoesNotThrow(() => classMap.CreateInstance());
            Assert.AreEqual(0, classMap.DeclaredMemberMaps.Count());
            Assert.AreEqual("TestClass", classMap.Discriminator);
            Assert.IsFalse(classMap.DiscriminatorIsRequired);
            Assert.IsNull(classMap.ExtraElementsMemberMap);
            Assert.IsNull(classMap.IdMemberMap);
            Assert.IsTrue(classMap.IgnoreExtraElements);
            Assert.IsFalse(classMap.IgnoreExtraElementsIsInherited);
            Assert.IsFalse(classMap.IsRootClass);
            Assert.AreEqual(0, classMap.KnownTypes.Count());
        }