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接口ide

  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
相關文章
相關標籤/搜索