用Linq To SQL 搭建底層

用Linq To SQL 搭建底層

接口

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace Project.DAL
{
    interface IBaseService<T> where T:class,new()
    {
        IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where);
        IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where);
        IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where);
        void Insert(T t);
        void Insert(IEnumerable<T> t);
        void Delete(T t);
        void Delete(IEnumerable<T> t);
        bool SaveChange();
    }
}

底層

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace Project.DAL
{
    public class BaseService<T> : IBaseService<T> where T : class, new()
    {
    //上下文對象
    Models.QuestionDBDataContext dbContext = new Models.QuestionDBDataContext();//直接複製會報錯應先在Models裏引入數據庫

    private IQueryable<T> table = null;
    private IQueryable<T> GetTable()
    {
        if (table == null)
        {
            table = (IQueryable<T>)dbContext.GetTable<T>();
        }
        return table;
    }

    /// <summary>
    /// 查詢
    /// </summary>
    /// <param name="where">查詢條件</param>
    /// <returns></returns>
    public IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where)
    {
        IQueryable<T> iq = GetTable();
        if (where != null || where.Length < 1)
        {
            foreach (var item in where)
            {
                iq = iq.Where(item);
            }
        }
        return iq;
    }

    /// <summary>
    /// 排序
    /// </summary>
    /// <typeparam name="type">排序類型</typeparam>
    /// <param name="order">排序列</param>
    /// <param name="isAsc">升序or降序</param>
    /// <param name="where"></param>
    /// <returns></returns>
    public IQueryable<T> QueryAll<type>(Expression<Func<T, type>> order, bool isAsc = true, params Expression<Func<T, bool>>[] where)
    {
        var iq = QueryAll(where);
        if (isAsc)
        {
            iq = iq.OrderBy(order);
        }
        else
        {
            iq = iq.OrderByDescending(order);
        }
        return iq;
    }

    /// <summary>
    /// 分頁查詢
    /// </summary>
    /// <typeparam name="type"></typeparam>
    /// <param name="total">總數據數</param>
    /// <param name="skip">跳過n條</param>
    /// <param name="take">取n條(一頁顯示幾條)</param>
    /// <param name="order"></param>
    /// <param name="isAsc"></param>
    /// <param name="where"></param>
    /// <returns></returns>
    public IQueryable<T> QueryAll<type>(out int total, int skip = 0, int take = 10, Expression<Func<T, type>> order = null, bool isAsc = true, params Expression<Func<T, bool>>[] where)
    {
        var iq = QueryAll<type>(order, isAsc, where);
        total = iq.Count();
        return iq.Skip(skip).Take(take);
    }

    public void Insert(T t)
    {
        dbContext.GetTable<T>().InsertOnSubmit(t);
    }

    public void Insert(IEnumerable<T> t)
    {
        dbContext.GetTable<T>().InsertAllOnSubmit(t);
    }

    public void Delete(T t)
    {
        dbContext.GetTable<T>().DeleteOnSubmit(t);
    }

    public void Delete(IEnumerable<T> t)
    {
        dbContext.GetTable<T>().DeleteAllOnSubmit(t);
    }

    /// <summary>
    /// 保存修改
    /// </summary>
    /// <returns></returns>
    public bool SaveChange()
    {
        try
        {
            dbContext.SubmitChanges();
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
    }
}

}sql

相關文章
相關標籤/搜索