示例#1
0
        /// <summary>
        /// Delete document using predicate in asynchronous manner.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task <DeleteResult> DeleteAsync <T>(Expression <Func <T, bool> > predicate) where T : class, new()
        {
            VerifyActiveDatabase();
            var table      = typeof(T).TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);

            return(await collection.DeleteOneAsync(predicate));
        }
示例#2
0
        /// <summary>
        /// Insert specified document in an asynchronous manner.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task <int> InsertAsync <T>(T obj) where T : class, new()
        {
            VerifyActiveDatabase();
            var table      = typeof(T).TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);
            await collection.InsertOneAsync(obj);

            return(0);
        }
示例#3
0
        /// <summary>
        /// Insert documents.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int Insert <T>(IEnumerable <T> obj) where T : class, new()
        {
            VerifyActiveDatabase();
            var table      = typeof(T).TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);

            collection.InsertMany(obj);
            return(0);
        }
示例#4
0
        /// <summary>
        /// Read documents which match predicate.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <param name="top"></param>
        /// <returns></returns>
        public IEnumerable <T> Query <T>(Expression <Func <T, bool> > predicate, int?top = null) where T : class, new()
        {
            VerifyActiveDatabase();
            var table      = typeof(T).TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);
            var buffer     = collection.Find <T>(predicate);
            var result     = top == null?buffer.ToList() : buffer.Limit(top.Value).ToList();

            return(result);
        }
示例#5
0
        /// <summary>
        /// Read document using key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public T Query <T>(object primaryKey) where T : class, new()
        {
            VerifyActiveDatabase();
            var type       = typeof(T);
            var table      = type.TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);
            var filter     = Builders <T> .Filter.Eq(type.PrimaryKeyAttributeValidate().Name, primaryKey);

            var buffer = collection.Find <T>(filter);

            return(buffer.FirstOrDefault());
        }
示例#6
0
        /// <summary>
        /// Delete specified document in an asynchronous manner.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task <DeleteResult> DeleteAsync <T>(T obj) where T : class, new()
        {
            VerifyActiveDatabase();
            var primaryKey = typeof(T).PrimaryKeyAttributeValidate();
            var field      = primaryKey.Name;
            var value      = primaryKey.GetValue(obj);
            var table      = typeof(T).TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);
            var filter     = Builders <T> .Filter.Eq(field, value);

            return(await collection.DeleteOneAsync(filter));
        }
示例#7
0
        /// <summary>
        /// Delete document using key.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public DeleteResult Delete <T>(object primaryKey) where T : class, new()
        {
            VerifyActiveDatabase();
            var pk         = typeof(T).PrimaryKeyAttributeValidate();
            var field      = pk.Name;
            var value      = primaryKey;
            var table      = typeof(T).TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);
            var filter     = Builders <T> .Filter.Eq(field, value);

            return(collection.DeleteOne(filter));
        }
示例#8
0
        /// <summary>
        /// Update specified document in an asynchrounous manner.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public async Task <ReplaceOneResult> UpdateAsync <T>(T obj) where T : class, new()
        {
            VerifyActiveDatabase();
            var type       = typeof(T);
            var table      = type.TableNameAttributeValidate();
            var collection = ActiveDatabase.GetCollection <T>(table);
            var primaryKey = type.PrimaryKeyAttributeValidate();
            var filter     = Builders <T> .Filter.Eq(primaryKey.Name, primaryKey.GetValue(obj));

            var result = await collection.ReplaceOneAsync(filter, obj);

            return(result);
        }