Exemplo n.º 1
0
        /// <summary>
        /// Inserts an object into the database.
        /// </summary>
        /// <param name="Object">Object to insert.</param>
        public async Task Insert(object Object)
        {
            ObjectSerializer Serializer     = this.GetObjectSerializer(Object);
            string           CollectionName = Serializer.CollectionName;
            IMongoCollection <BsonDocument> Collection;

            if (string.IsNullOrEmpty(CollectionName))
            {
                Collection = this.defaultCollection;
            }
            else
            {
                Collection = this.GetCollection(CollectionName);
            }

            if (Serializer.HasObjectIdField)
            {
                if (Serializer.HasObjectId(Object))
                {
                    throw new Exception("Object already has an Object ID. If updating an object, use the Update method.");
                }
                else
                {
                    await Serializer.GetObjectId(Object, true);
                }
            }
            else
            {
                BsonDocument Doc = Object.ToBsonDocument(Object.GetType(), Serializer);
                await Collection.InsertOneAsync(Doc);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Deletes an object in the database.
        /// </summary>
        /// <param name="Object">Object to insert.</param>
        public async Task Delete(object Object)
        {
            ObjectSerializer Serializer = this.GetObjectSerializer(Object);
            ObjectId         ObjectId   = await Serializer.GetObjectId(Object, false);

            string CollectionName = Serializer.CollectionName;
            IMongoCollection <BsonDocument> Collection;

            if (string.IsNullOrEmpty(CollectionName))
            {
                Collection = this.defaultCollection;
            }
            else
            {
                Collection = this.GetCollection(CollectionName);
            }

            await Collection.DeleteOneAsync(Builders <BsonDocument> .Filter.Eq <ObjectId>("_id", ObjectId));
        }
Exemplo n.º 3
0
        private readonly Cache <string, object> loadCache = new Cache <string, object>(10000, new TimeSpan(0, 0, 10), new TimeSpan(0, 0, 5));        // TODO: Make parameters configurable.

        /// <summary>
        /// Updates an object in the database.
        /// </summary>
        /// <param name="Object">Object to insert.</param>
        public async Task Update(object Object)
        {
            ObjectSerializer Serializer = this.GetObjectSerializer(Object);
            ObjectId         ObjectId   = await Serializer.GetObjectId(Object, false);

            string CollectionName = Serializer.CollectionName;
            IMongoCollection <BsonDocument> Collection;

            if (string.IsNullOrEmpty(CollectionName))
            {
                Collection = this.defaultCollection;
            }
            else
            {
                Collection = this.GetCollection(CollectionName);
            }

            BsonDocument Doc = Object.ToBsonDocument(Object.GetType(), Serializer);
            await Collection.ReplaceOneAsync(Builders <BsonDocument> .Filter.Eq <ObjectId>("_id", ObjectId), Doc);
        }