MongoDB官网驱动仓库封装

定义IMongoRepositoryBase接口

ExpandedBlockStart.gif
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接口

ExpandedBlockStart.gif
  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

转载于:https://www.cnblogs.com/zhuiyi/p/4794136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值