zoukankan      html  css  js  c++  java
  • 用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<T> where T:class,new()
        {
            T Find(params object[] keys);
            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 Update(T t);
            void Update(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;
    using System.Data.Entity;
    using System.Runtime.Remoting.Messaging;
    
    namespace Project.DAL
    {
        public class BaseService<T> : IBaseService<T> where T : class, new()
        {
    
            Models.MyAll1817Entities dbContext = DbContextFactory.Create() as Models.MyAll1817Entities;
    
            public T Find(params object[] keys)
            {
                return dbContext.Set<T>().Find(keys);
            }
            public IQueryable<T> QueryAll(params Expression<Func<T, bool>>[] where) 
            {
                IQueryable<T> iq = dbContext.Set<T>();
                if (where != null || where.Length < 1)
                {
                    foreach (var item in where)
                    {
                        iq = iq.Where(item);
                    }
                }
                return iq;
            }
    
            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;
            }
    
            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.Set<T>().Add(t);
            }
    
            public void Insert(IEnumerable<T> t)
            {
                dbContext.Set<T>().AddRange(t);
            }
            public void Update(T t)
            {
                dbContext.Entry<T>(t).State = EntityState.Modified;
            }
    
            public void Update(IEnumerable<T> t)
            {
                foreach (var item in t)
                {
                    dbContext.Entry<T>(item).State = EntityState.Modified;
                }
            }
    
            public void Delete(T t)
            {
                dbContext.Set<T>().Remove(t);
            }
    
            public void Delete(IEnumerable<T> t)
            {
                dbContext.Set<T>().RemoveRange(t);
            }
    
            /// <summary>
            /// 保存修改
            /// </summary>
            /// <returns></returns>
            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
        {
            /// <summary>
            /// 创建EF上下文对象,已存在就直接取,不存在就创建,保证线程内是唯一。
            /// </summary>
            public static DbContext Create()
            {
                DbContext dbContext = CallContext.GetData("DbContext") as DbContext;
                if (dbContext == null)
                {
                    dbContext = new MyAll1817Entities ();
                    CallContext.SetData("DbContext", dbContext);
                }
                return dbContext;
            }
        }
    }
    
    
  • 相关阅读:
    417 Pacific Atlantic Water Flow 太平洋大西洋水流
    416 Partition Equal Subset Sum 分割相同子集和
    415 Add Strings 字符串相加
    414 Third Maximum Number 第三大的数
    413 Arithmetic Slices 等差数列划分
    412 Fizz Buzz
    410 Split Array Largest Sum 分割数组的最大值
    409 Longest Palindrome 最长回文串
    day22 collection 模块 (顺便对比queue也学习了一下队列)
    day21 计算器作业
  • 原文地址:https://www.cnblogs.com/setsuna-cn/p/12256196.html
Copyright © 2011-2022 走看看