DBHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
using MongoDB.Linq;
using System.Web;
using Commons;
using System.Linq.Expressions;
namespace MongoTest
{
    public class DBHelper
    {
        #region 基本信息
        //public static string ConnectionString = System.Web.Configuration.WebConfigurationManager.AppSettings["MongoServer"].ToString();
        //public static string DatabaseName = System.Web.Configuration.WebConfigurationManager.AppSettings["MongoDBName"].ToString();
        public static string ConnectionString = "server=192.168.1.101:27627";
        public static string DatabaseName = "test";
        static DBHelper()
        {
            var builder = new MongoConnectionStringBuilder(ConnectionString)
            {
                Username = "",
                Password = "",
                MinimumPoolSize = 2,
                MaximumPoolSize = 120,
            };
            ConnectionString = builder.ToString();
        }
        /// <summary>
        /// 当前Mongo数据库对象
        /// </summary>
        /// <param name="mongo">Mongo对象</param>
        /// <returns>返回当前Mongo数据库对象</returns>
        public static IMongoDatabase GetCurrentDataBase(Mongo mongo)
        {
            return mongo.GetDatabase(DatabaseName);
        }
        /// <summary>
        /// 当前Mongo对象
        /// </summary>
        public static Mongo CurrentMongo
        {
            get
            {
                return new Mongo(ConnectionString);
            }
        }
        #endregion
        #region 数据查询
        private static IndexOrder GetIndexOrder(DataOrder order)
        {
            IndexOrder orderby = IndexOrder.Descending;
            if (order == DataOrder.Ascending)
            {
                orderby = IndexOrder.Ascending;
            }
            else
            {
                orderby = IndexOrder.Descending;
            }
            return orderby;
        }
        /// <summary>
        /// 根据Lamda表达式条件获取上一条或下一条数据
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">查询Lamda表达式</param>
        /// <param name="order">排序</param>
        /// <param name="orderField">排序字段</param>
        /// <returns>返回上一条或下一条数据</returns>
        public static T GetNextOrPrevBySearch<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector, DataOrder order, string orderField) where T : class
        {
            Mongo mongo = null;
            IndexOrder orderby = GetIndexOrder(order);
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                int count = collection.Find<T>(selector).Sort(orderField, orderby).Documents.Count();
                if (count > 0)
                {
                    return collection.Find<T>(selector).Sort(orderField, orderby).Limit(1).Documents.Single<T>();
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 根据条件进行查询,并进行分页
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">条件Lamda表达式</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页的显示记录数</param>
        /// <returns>返回查询的集合</returns>
        public static List<T> GetBySearch<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector, int pageIndex, int pageSize) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.Find<T>(selector).Skip((pageIndex - 1) * pageSize).Limit(pageSize).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 根据条件进行查询,并进行分页和排序
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">条件Lamda表达式</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页的显示记录数</param>
        /// <param name="order">排序规则</param>
        /// <param name="orderField">排序字段</param>
        /// <returns>返回查询的集合</returns>
        public static List<T> GetBySearch<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector, int pageIndex, int pageSize, DataOrder order, string orderField) where T : class
        {
            Mongo mongo = null;
            IndexOrder orderby = GetIndexOrder(order);
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.Find<T>(selector).Sort(orderField, orderby).Skip((pageIndex - 1) * pageSize).Limit(pageSize).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">条件Lamda表达式</param>
        /// <returns>返回查询的集合</returns>
        public static List<T> GetBySearch<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.Find<T>(selector).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 条件查询,并进行排序
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">条件Lamda表达式</param>
        /// <param name="order">排序规则</param>
        /// <param name="orderField">排序字段</param>
        /// <returns>返回查询的集合</returns>
        public static List<T> GetBySearch<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector, DataOrder order, string orderField) where T : class
        {
            Mongo mongo = null;
            IndexOrder orderby = GetIndexOrder(order);
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.Find<T>(selector).Sort(orderField, orderby).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 返回所有数据
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <returns>返回所有数据的集合</returns>
        public static List<T> GetAll<T>() where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.FindAll().Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 返回所有数据,并排序
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="order">排序规则</param>
        /// <param name="orderField">排序字段</param>
        /// <returns>返回所有数据的集合</returns>
        public static List<T> GetAll<T>(DataOrder order, string orderField) where T : class
        {
            Mongo mongo = null;
            IndexOrder orderby = GetIndexOrder(order);
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.FindAll().Sort(orderField, orderby).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 返回所有数据并支持分页
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页显示的记录数</param>
        /// <returns>返回集合</returns>
        public static List<T> GetAll<T>(int pageIndex, int pageSize) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.FindAll().Skip((pageIndex - 1) * pageSize).Limit(pageSize).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 返回所有数据并支持分页排序
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页显示的记录数</param>
        /// <param name="order">排序规则</param>
        /// <param name="orderField">排序字段名</param>
        /// <returns>返回集合</returns>
        public static List<T> GetAll<T>(int pageIndex, int pageSize, DataOrder order, string orderField) where T : class
        {
            Mongo mongo = null;
            IndexOrder orderby = GetIndexOrder(order);
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.FindAll().Sort(orderField, orderby).Skip((pageIndex - 1) * pageSize).Limit(pageSize).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 返回前几条数据
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="topNumber">返回数量</param>
        /// <param name="order">排序规则</param>
        /// <param name="orderField">排序字段</param>
        /// <returns>返回结果集合</returns>
        public static List<T> GetTopList<T>(int topNumber, DataOrder order, string orderField) where T : class
        {
            Mongo mongo = null;
            IndexOrder orderby = GetIndexOrder(order);
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.FindAll().Sort(orderField, orderby).Limit(topNumber).Documents.ToList();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 根据条件查询一个对象
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">查询条件Lamda表达式</param>
        /// <returns></returns>
        public static T GetOneBySearch<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.FindOne<T>(selector);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 统计总记录数
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <returns>返回总记录数</returns>
        public static long GetTotalCount<T>() where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.Count();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 根据条件获取总记录数
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">查询条件委托</param>
        /// <returns>返回总记录数</returns>
        public static int GetTotalCountBySearch<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                IMongoCollection<T> collection = db.GetCollection<T>();
                return collection.Count<T>(selector);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        #endregion
        #region 数据插入
        /// <summary>
        /// 插入数据
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="t">要插入的对象</param>
        public static void Insert<T>(T t) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                db.GetCollection<T>().Save(t);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        #endregion
        #region 数据更新
        /// <summary>
        /// 更新数据(带条件)
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="t">更新的对象修改</param>
        /// <param name="selector">更新的条件Lamda表达式</param>
        public static void Update<T>(T t, System.Linq.Expressions.Expression<Func<T, bool>> selector) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                db.GetCollection<T>().Update<T>(t, selector);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="t">要更新的对象</param>
        public static void Update<T>(T t) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                db.GetCollection<T>().Save(t);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        #endregion
        #region 数据删除
        /// <summary>
        /// 数据删除
        /// </summary>
        /// <typeparam name="T">类型参数</typeparam>
        /// <param name="selector">查询的条件Lamda表达式</param>
        public static void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> selector) where T : class
        {
            Mongo mongo = null;
            try
            {
                mongo = CurrentMongo;
                mongo.Connect();
                var db = GetCurrentDataBase(mongo);
                db.GetCollection<T>().Remove<T>(selector);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                mongo.Disconnect();
                mongo.Dispose();
            }
        }
        #endregion
    }
}

你可能感兴趣的:(help)