Beispiel #1
0
        /// <summary>
        /// Register a property as a DbRef - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        private static void RegisterDbRefItem(BsonMapper mapper, MemberMapper member, string collectionName)
        {
            // get entity
            var entity = mapper.GetEntityMapper(member.DataType);

            member.Serialize = (obj, m) =>
            {
                var idField = entity.Id;

                var id = idField.Getter(obj);

                return(new BsonDocument
                {
                    { "$id", new BsonValue(id) },
                    { "$ref", collectionName }
                });
            };

            member.Deserialize = (bson, m) =>
            {
                var idRef = bson.AsDocument["$id"];

                return(m.Deserialize(entity.ForType,
                                     idRef.IsNull ?
                                     bson : // if has no $id object was full loaded (via Include) - so deserialize using normal function
                                     new BsonDocument {
                    { "_id", idRef }
                }));                                        // if has $id, deserialize object using only _id object
            };
        }
Beispiel #2
0
        /// <summary>
        /// Register a property as a DbRefList - implement a custom Serialize/Deserialize actions to convert entity to $id, $ref only
        /// </summary>
        private static void RegisterDbRefList(BsonMapper mapper, MemberMapper member, string collectionName)
        {
            // get entity from list item type
            var entity = mapper.GetEntityMapper(member.UnderlyingType);

            member.Serialize = (list, m) =>
            {
                var result  = new BsonArray();
                var idField = entity.Id;

                foreach (var item in (IEnumerable)list)
                {
                    result.Add(new BsonDocument
                    {
                        { "$id", new BsonValue(idField.Getter(item)) },
                        { "$ref", collectionName }
                    });
                }

                return(result);
            };

            member.Deserialize = (bson, m) =>
            {
                var array = bson.AsArray;

                if (array.Count == 0)
                {
                    return(m.Deserialize(member.DataType, array));
                }

                var hasIdRef = array[0].AsDocument["$id"].IsNull;

                if (hasIdRef)
                {
                    // if no $id, deserialize as full (was loaded via Include)
                    return(m.Deserialize(member.DataType, array));
                }
                else
                {
                    // copy array changing $id to _id
                    var arr = new BsonArray();

                    foreach (var item in array)
                    {
                        arr.Add(new BsonDocument {
                            { "_id", item.AsDocument["$id"] }
                        });
                    }

                    return(m.Deserialize(member.DataType, arr));
                }
            };
        }
Beispiel #3
0
 internal EntityBuilder(BsonMapper mapper)
 {
     _mapper = mapper;
     _entity = mapper.GetEntityMapper(typeof(T));
 }