MongoDB官网驱动仓库封装

定义IMongoRepositoryBase接口

public interface IMongoRepositoryBase
    {
        /// <summary>
        
/// 新增一条数据
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="model"></param>
        void Insert<T>(T model) where T : class;
        /// <summary>
        
/// 批量新增数据
        
/// </summary>
        
/// <typeparam name="T">泛型</typeparam>
        
/// <param name="list">泛型集合</param>
        void Insert<T>(IList<T> list) where T : class;



        /// <summary>
        
/// 更新一条数据
        
/// </summary>
        
/// <typeparam name="T">泛型</typeparam>
        
/// <param name="model">实体类</param>
        
/// <param name="where">查询条件</param>
        bool Update<T>(T model, Expression<Func<T, bool>> wherewhere T : class;

        /// <summary>
        
/// 删除数据
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="where"></param>
        bool Delete<T>(Expression<Func<T, bool>> wherewhere T : class;

        /// <summary>
        
/// 根据条件,获取一条记录
        
/// </summary>
        
/// <typeparam name="T">返回值类型</typeparam>
        
/// <param name="where"></param>
        
/// <returns></returns>
        T GetModel<T>(Expression<Func<T, bool>> wherewhere T : class;

        /// <summary>
        
/// 获取列表
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="where"></param>
        
/// <returns></returns>
        IList<T> GetList<T>(Expression<Func<T, bool>> wherewhere T : class;

        /// <summary>
        
/// 获取列表
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="orderBy"></param>
        
/// <returns></returns>
        IList<T> GetList<T>(Expression<Func<T, object>> orderBy) where T : class;
        /// <summary>
        
/// 获取带排序的列表
        
/// </summary>
        
/// <typeparam name="T">数据类型</typeparam>
        
/// <param name="where">查询条件</param>
        
/// <param name="orderBy">排序</param>
        
/// <returns></returns>
        IList<T> GetList<T>(Expression<Func<T, bool>> where, Expression<Func<T, object>> orderBy) where T : class;

        /// <summary>
        
/// 获取分页
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="pageIndex"></param>
        
/// <param name="pageSize"></param>
        
/// <param name="where"></param>
        
/// <param name="orderby"></param>
        
/// <returns></returns>
        IList<T> GetList<T>(int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> orderbywhere T : class;


        /// <summary>
        
/// 获取总记录数
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="where"></param>
        
/// <returns></returns>
        long GetTotalCount<T>(Expression<Func<T, bool>> wherewhere T : class;

    }
View Code

 

定义MongoRepositoryBase类,并实现IMongoRepositoryBase接口

 public class MongoRepositoryBase : IMongoRepositoryBase
    {
        /// <summary>
        
/// 数据库
        
/// </summary>
        private IMongoDatabase db { getset; }

        /// <summary>
        
/// 表名
        
/// </summary>
        private string collectionName { getset; }


        public MongoRepositoryBase(string collectionName)
        {
            this.db = MongoDBConnection.GetMongoDatabase();

            this.collectionName = collectionName;
        }


        /// <summary>
        
/// 新增一条数据
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="model"></param>
        public void Insert<T>(T model) where T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);
            //向链表中批量写入数据
            collection.InsertOneAsync(model);
        }



        /// <summary>
        
/// 批量新增数据
        
/// </summary>
        
/// <typeparam name="T">泛型</typeparam>
        
/// <param name="list">泛型集合</param>
        
/// <param name="collectionName">表名</param>
        public void Insert<T>(IList<T> list) where T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);
            //向链表中批量写入数据
            collection.InsertManyAsync(list);
        }


        /// <summary>
        
/// 更新一条数据
        
/// </summary>
        
/// <typeparam name="T">泛型</typeparam>
        
/// <param name="model">实体类</param>
        
/// <param name="collectionName">表名</param>
        
/// <param name="where">查询条件</param>
        public bool Update<T>(T model, Expression<Func<T, bool>> wherewhere T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);
            //要更新的字段集合
            var fieldList = new List<UpdateDefinition<T>>();

            foreach (var property in typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                if (property.Name != "Id")//更新集中不能有实体键_id
                {
                    fieldList.Add(Builders<T>.Update.Set(property.Name, property.GetValue(model)));
                }
            }

            return collection.UpdateOneAsync(where, Builders<T>.Update.Combine(fieldList)).Result.ModifiedCount > 0 ? true : false;
        }

        public bool Delete<T>(Expression<Func<T, bool>> wherewhere T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            return collection.DeleteOneAsync(where).Result.DeletedCount > 0 ? true : false;

        }

        /// <summary>
        
/// 根据条件,获取一条记录
        
/// </summary>
        
/// <typeparam name="T">返回值类型</typeparam>
        
/// <param name="collectionName">表名</param>
        
/// <param name="where"></param>
        
/// <returns></returns>
        public T GetModel<T>(Expression<Func<T, bool>> wherewhere T : class
        {

            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            return collection.Find(where).FirstOrDefaultAsync().Result;
        }

        /// <summary>
        
/// 获取列表
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <returns></returns>
        public IList<T> GetList<T>() where T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            return collection.Find(new BsonDocument()).ToListAsync().Result;
        }

        /// <summary>
        
/// 获取列表
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="orderBy"></param>
        
/// <returns></returns>
        public IList<T> GetList<T>(Expression<Func<T, object>> orderBy) where T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            return collection.Find(new BsonDocument()).SortByDescending(orderBy).ToListAsync().Result;
        }

        /// <summary>
        
/// 获取列表
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="where"></param>
        
/// <returns></returns>
        public IList<T> GetList<T>(Expression<Func<T, bool>> wherewhere T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            return collection.Find(where).ToListAsync().Result;
        }

        /// <summary>
        
/// 获取带排序的列表
        
/// </summary>
        
/// <typeparam name="T">数据类型</typeparam>
        
/// <param name="where">查询条件</param>
        
/// <param name="orderBy">排序</param>
        
/// <returns></returns>
        public IList<T> GetList<T>(Expression<Func<T, bool>> where, Expression<Func<T, object>> orderBy) where T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            return collection.Find(where).SortByDescending(orderBy).ToListAsync().Result;
        }


        /// <summary>
        
/// 获取分页
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="pageIndex"></param>
        
/// <param name="pageSize"></param>
        
/// <param name="where"></param>
        
/// <param name="orderby"></param>
        
/// <returns></returns>
        public IList<T> GetList<T>(int pageIndex, int pageSize, Expression<Func<T, bool>> where, Expression<Func<T, object>> orderbywhere T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            var skip = (pageIndex - 1) * pageSize;

            return collection.Find(where).SortByDescending(orderby).Skip(skip).Limit(pageSize).ToListAsync().Result;
        }


        /// <summary>
        
/// 获取总记录数
        
/// </summary>
        
/// <typeparam name="T"></typeparam>
        
/// <param name="where"></param>
        
/// <returns></returns>
        public long GetTotalCount<T>(Expression<Func<T, bool>> wherewhere T : class
        {
            //获取链表
            var collection = db.GetCollection<T>(collectionName);

            if (where != null)
            {
                return collection.CountAsync(where).Result;
            }
            else
            {
                return collection.CountAsync(new BsonDocument()).Result;
            }
        }
    }
View Code
posted @ 2015-09-09 11:29  ゞ追忆o0ゞ  阅读(808)  评论(0编辑  收藏  举报