用EF 搭建底层

用EF 搭建底层

接口

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 where T:class,new()
    {
        T Find(params object[] keys);
        IQueryable QueryAll(params Expression>[] where);
        IQueryable QueryAll(Expression> order, bool isAsc = true, params Expression>[] where);
        IQueryable QueryAll(out int total, int skip = 0, int take = 10, Expression> order = null, bool isAsc = true, params Expression>[] where);
        void Insert(T t);
        void Insert(IEnumerable t);
        void Update(T t);
        void Update(IEnumerable t);
        void Delete(T t);
        void Delete(IEnumerable t);
        bool SaveChange();
    }
}

底层

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;

namespace Project.DAL
{
    public class BaseService : IBaseService where T : class, new()
    {

        Models.MyAll1817Entities dbContext = DbContextFactory.Create() as Models.MyAll1817Entities;

        public T Find(params object[] keys)
        {
            return dbContext.Set().Find(keys);
        }
        public IQueryable QueryAll(params Expression>[] where) 
        {
            IQueryable iq = dbContext.Set();
            if (where != null || where.Length < 1)
            {
                foreach (var item in where)
                {
                    iq = iq.Where(item);
                }
            }
            return iq;
        }

        public IQueryable QueryAll(Expression> order, bool isAsc = true, params Expression>[] where)
        {
            var iq = QueryAll(where);
            if (isAsc)
            {
                iq = iq.OrderBy(order);
            }
            else
            {
                iq = iq.OrderByDescending(order);
            }
            return iq;
        }

        public IQueryable QueryAll(out int total, int skip = 0, int take = 10, Expression> order = null, bool isAsc = true, params Expression>[] where)
        {
            var iq = QueryAll(order, isAsc, where);
            total = iq.Count();
            return iq.Skip(skip).Take(take);
        }

        public void Insert(T t)
        {
            dbContext.Set().Add(t);
        }

        public void Insert(IEnumerable t)
        {
            dbContext.Set().AddRange(t);
        }
        public void Update(T t)
        {
            dbContext.Entry(t).State = EntityState.Modified;
        }

        public void Update(IEnumerable t)
        {
            foreach (var item in t)
            {
                dbContext.Entry(item).State = EntityState.Modified;
            }
        }

        public void Delete(T t)
        {
            dbContext.Set().Remove(t);
        }

        public void Delete(IEnumerable t)
        {
            dbContext.Set().RemoveRange(t);
        }

        /// 
        /// 保存修改
        /// 
        /// 
        public bool SaveChange()
        {
            //以下try注释删掉的话Debug时不会报错
            //请正式上线时删掉注释
            //try
            //{
                dbContext.SaveChanges();
                return true;
            //}
            //catch (Exception e)
            //{
            //    return false;
            //}
        }
    }
}

保证EF上下文对象在线程内是唯一的

这里比linq要多一个类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
using Project.Models;


namespace Project.DAL
{
    public class DbContextFactory
    {
        /// 
        /// 创建EF上下文对象,已存在就直接取,不存在就创建,保证线程内是唯一。
        /// 
        public static DbContext Create()
        {
            DbContext dbContext = CallContext.GetData("DbContext") as DbContext;
            if (dbContext == null)
            {
                dbContext = new MyAll1817Entities ();
                CallContext.SetData("DbContext", dbContext);
            }
            return dbContext;
        }
    }
}

你可能感兴趣的:(用EF 搭建底层)