/// <summary>
        /// Converts a value to a BsonValue by serializing it.
        /// </summary>
        /// <param name="serializer">The serializer.</param>
        /// <param name="value">The value.</param>
        /// <returns>The serialized value.</returns>
        public static BsonValue ToBsonValue(this IBsonSerializer serializer, object value)
        {
            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                var context = BsonSerializationContext.CreateRoot(writer);
                writer.WriteStartDocument();
                writer.WriteName("x");
                serializer.Serialize(context, value);
                writer.WriteEndDocument();
            }
            return(document[0]);
        }
        /// <summary>
        /// Serializes the value.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>The serialized value.</returns>
        public BsonValue SerializeValue(object value)
        {
            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("value");
                _serializer.Serialize(context, value);
                bsonWriter.WriteEndDocument();
                return(tempDocument[0]);
            }
        }
Beispiel #3
0
        public void ReserializingStringIdEntityMaintainsStateExceptNulls()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(StringIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <StringIdModel>(foreignProperty);

            var initialCollection = new EntityNavigationCollection <StringIdModel>(foreignProperty)
            {
                new StringIdModel
                {
                    Description = "1"
                },
                new StringIdModel
                {
                    Id          = "5ac383379a5f1303784400f8",
                    Description = "2"
                }
            };
            EntityNavigationCollection <StringIdModel> deserializedCollection = null;

            initialCollection.AddForeignId("5ac383379a5f1303784400f9");

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <StringIdModel>;
            }

            Assert.AreEqual(3, initialCollection.GetForeignIds().Count());
            Assert.AreEqual(2, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(deserializedCollection.GetForeignIds().All(id => initialCollection.GetForeignIds().Contains(id)));
        }
Beispiel #4
0
        public static BsonValue SerializeValue(this ISerializationExpression field, object value)
        {
            Ensure.IsNotNull(field, nameof(field));

            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("value");
                field.Serializer.Serialize(context, value);
                bsonWriter.WriteEndDocument();
                return(tempDocument[0]);
            }
        }
        public UpdateDefinition <BsonDocument> BuildUpdatesForSave(
            UpdateDefinition <BsonDocument> update, TrackablePocoTracker <T> tracker, params object[] keyValues)
        {
            var keyNamespace = DocumentHelper.ToDotPathWithTrailer(keyValues);

            // mimic BsonClassMapSerializer.Serialize to serialize changed value correctly.
            // build bson elements containing set field values

            var setDocument   = new BsonDocument();
            var setBsonWriter = new BsonDocumentWriter(setDocument);
            var setContext    = BsonSerializationContext.CreateRoot(setBsonWriter);

            setBsonWriter.WriteStartDocument();

            foreach (var change in tracker.ChangeMap)
            {
                if (change.Value.NewValue != null)
                {
                    BsonMemberMap memberMap;
                    if (_propertyToMemberMap.TryGetValue(change.Key, out memberMap) == false)
                    {
                        continue;
                    }

                    setBsonWriter.WriteName(memberMap.ElementName);
                    memberMap.GetSerializer().Serialize(setContext, change.Value.NewValue);
                }
                else
                {
                    update = (update == null)
                        ? Builders <BsonDocument> .Update.Unset(keyNamespace + change.Key.Name)
                        : update.Unset(keyNamespace + change.Key.Name);
                }
            }

            setBsonWriter.WriteEndDocument();
            setBsonWriter.Dispose();

            foreach (var element in setDocument.Elements)
            {
                update = (update == null)
                    ? Builders <BsonDocument> .Update.Set(keyNamespace + element.Name, element.Value)
                    : update.Set(keyNamespace + element.Name, element.Value);
            }

            return(update);
        }
Beispiel #6
0
        public void ReserializingObjectIdIdEntityMaintainsState()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(ObjectIdIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <ObjectIdIdModel>(foreignProperty);

            var initialCollection = new EntityNavigationCollection <ObjectIdIdModel>(foreignProperty)
            {
                new ObjectIdIdModel
                {
                    Id          = ObjectId.GenerateNewId(),
                    Description = "1"
                }
            };
            EntityNavigationCollection <ObjectIdIdModel> deserializedCollection = null;

            initialCollection.AddForeignId(ObjectId.GenerateNewId());

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <ObjectIdIdModel>;
            }

            Assert.AreEqual(2, initialCollection.GetForeignIds().Count());
            Assert.AreEqual(2, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(initialCollection.GetForeignIds().All(id => deserializedCollection.GetForeignIds().Contains(id)));
        }
Beispiel #7
0
        public static BsonArray SerializeValues(IBsonSerializer itemSerializer, IEnumerable values)
        {
            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("_v");
                writer.WriteStartArray();
                var context = BsonSerializationContext.CreateRoot(writer);
                foreach (var value in values)
                {
                    itemSerializer.Serialize(context, value);
                }
                writer.WriteEndArray();
                writer.WriteEndDocument();
            }
            return(document["_v"].AsBsonArray);
        }
Beispiel #8
0
        public void SerializeICollectionCompatibleButIsntEntityNavigationCollection()
        {
            var serializer = new EntityNavigationCollectionSerializer <ObjectIdIdModel>("Id");

            var initialCollection = new List <ObjectIdIdModel>
            {
                new ObjectIdIdModel
                {
                    Id          = ObjectId.GenerateNewId(),
                    Description = "1"
                }
            };
            EntityNavigationCollection <ObjectIdIdModel> deserializedCollection = null;

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }

            using (var reader = new BsonDocumentReader(document))
            {
                reader.ReadBsonType();
                reader.ReadStartDocument();
                reader.ReadBsonType();
                reader.SkipName();

                var context = BsonDeserializationContext.CreateRoot(reader);
                deserializedCollection = serializer.Deserialize(context) as EntityNavigationCollection <ObjectIdIdModel>;
            }

            Assert.AreEqual(1, initialCollection.Count());
            Assert.AreEqual(1, deserializedCollection.GetForeignIds().Count());
            Assert.IsTrue(initialCollection.Select(e => e.Id).All(id => deserializedCollection.GetForeignIds().Contains(id)));
        }
Beispiel #9
0
        public static BsonArray SerializeValues(this ISerializationExpression field, IEnumerable values)
        {
            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("values");
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    field.Serializer.Serialize(context, ConvertIfNecessary(field.Serializer.ValueType, value));
                }
                bsonWriter.WriteEndArray();
                bsonWriter.WriteEndDocument();

                return((BsonArray)tempDocument[0]);
            }
        }
        /// <summary>
        /// Serializes the values.
        /// </summary>
        /// <param name="values">The values.</param>
        /// <returns>The serialized values.</returns>
        public BsonArray SerializeValues(IEnumerable values)
        {
            var tempDocument = new BsonDocument();

            using (var bsonWriter = new BsonDocumentWriter(tempDocument))
            {
                var context = BsonSerializationContext.CreateRoot <BsonDocument>(bsonWriter);
                bsonWriter.WriteStartDocument();
                bsonWriter.WriteName("values");
                bsonWriter.WriteStartArray();
                foreach (var value in values)
                {
                    context.SerializeWithChildContext(_serializer, value);
                }
                bsonWriter.WriteEndArray();
                bsonWriter.WriteEndDocument();

                return(tempDocument[0].AsBsonArray);
            }
        }
Beispiel #11
0
        public void DeleteResource(object targetResource)
        {
            var resourceType = GetResourceType(targetResource);
            var collection   = GetCollection(resourceType);
            var annotation   = (TypedResourceTypeAnnotation)resourceType.CustomState;

            var idValue      = annotation.ClassMap.IdMemberMap.Getter(targetResource);
            var idSerializer = annotation.ClassMap.IdMemberMap.GetSerializer();
            var doc          = new BsonDocument();

            using (var writer = new BsonDocumentWriter(doc))
            {
                writer.WriteStartDocument();
                writer.WriteName("_id");
                idSerializer.Serialize(BsonSerializationContext.CreateRoot(writer), idValue);
                writer.WriteEndDocument();
            }

            _rememberedInstances.Add(targetResource);
            _actions.Add(() => collection.Remove(Query.EQ("_id", doc[0])));
        }
        private string SerializeKeyString(object key)
        {
            var keyDocument = new BsonDocument();

            using (var keyWriter = new BsonDocumentWriter(keyDocument))
            {
                var context = BsonSerializationContext.CreateRoot(keyWriter);
                keyWriter.WriteStartDocument();
                keyWriter.WriteName("k");
                _keySerializer.Serialize(context, key);
                keyWriter.WriteEndDocument();
            }

            var keyValue = keyDocument["k"];

            if (keyValue.BsonType != BsonType.String)
            {
                throw new BsonSerializationException("When using DictionaryRepresentation.Document key values must serialize as strings.");
            }

            return((string)keyValue);
        }
Beispiel #13
0
        public void SerializeUnsupportedType()
        {
            var serializer = new EntityNavigationCollectionSerializer <ObjectIdIdModel>("Id");

            var initialCollection = new List <DateTime>
            {
                DateTime.Now
            };

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }
        }
Beispiel #14
0
        public static BsonDocument Concat(BsonDocument doc1, BsonDocument doc2)
        {
            BsonDocument       dest   = new BsonDocument();
            BsonDocumentWriter writer = new BsonDocumentWriter(dest);
            var context = BsonSerializationContext.CreateRoot(writer);

            writer.WriteStartDocument();

            foreach (var field in doc1)
            {
                writer.WriteName(field.Name);
                BsonValueSerializer.Instance.Serialize(context, field.Value);
            }

            foreach (var field in doc2)
            {
                writer.WriteName(field.Name);
                BsonValueSerializer.Instance.Serialize(context, field.Value);
            }

            writer.WriteEndDocument();
            return(writer.Document);
        }
Beispiel #15
0
        public void SerializeUnsupportedType()
        {
            var foreignProperty = EntityMapping.GetOrCreateDefinition(typeof(ObjectIdIdModel)).GetIdProperty();
            var serializer      = new EntityNavigationCollectionSerializer <ObjectIdIdModel>(foreignProperty);

            var initialCollection = new List <DateTime>
            {
                DateTime.Now
            };

            var document = new BsonDocument();

            using (var writer = new BsonDocumentWriter(document))
            {
                writer.WriteStartDocument();
                writer.WriteName("Items");

                var context = BsonSerializationContext.CreateRoot(writer);
                serializer.Serialize(context, initialCollection);

                writer.WriteEndDocument();
            }
        }