.net通用框架 (四)--DAL数据层以及数据接口

数据层以及数据接口设计如下图(以g_orga组织机构和g_role角色)为例,这几个类可以通过.tt模版生成

设计参考学习http://www.cnblogs.com/hanyinglong/archive/2013/04/08/3008896.html

1.IBaseRepository

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Data.Entity;
 6 using System.Linq.Expressions;
 7
 8 namespace MISMODEL.DAL
 9 {
10     public interface IBaseRepository<T> where T : class, new()
11     {
12         void SetLazyLoading(bool flag);
13
14         // 1实现对数据库的添加功能,添加实现EF框架的引用
15          bool AddEntity(T entity);
16          bool AddEntity(T entity,bool isSave);
17         // 2实现对数据库的修改功能
18         bool UpdateEntity(T entity);
19         // 3实现对数据库的删除功能
20         bool DeleteEntity(T entity);
21         bool DeleteEntity(List<T> entity, bool isSave);
22         // 4实现对数据库的查询  --主键查询
23         T FindByID(Expression<Func<T, bool>> whereLambda);
24         // 5实现对数据库的查询  --条件查询
25         IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda);
26         // 6实现对数据库的查询  --无条件查询
27         IQueryable<T> LoadEntities();
28         // 7查询返回dbset
29         DbSet<T> LoadDbSetEntities();
30         // 8实现对数据库的查询  --首行查询
31         T FirstOrDefaultEntities(Expression<Func<T, bool>> whereLambda);
32         // 9实现对数据库的查询   --动态条件查询
33         IQueryable<T> SqlQuery(string sqlstring, params object[] paramertes);
34         // 10实现对数据的分页查询
35         IQueryable<T> LoadPageEntities<S>(int pageIndex, int pageSize, out int total, Expression<Func<T, bool>> whereLambda, bool isAsc, Func<T, S> orderByLambda);
36         //10事物提交
37         bool Commit();
38     }
39 }

2. BaseRepository<T>

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Data.Entity;
  6 using System.Data.Entity.Infrastructure;
  7 using System.Data;
  8 using System.Linq.Expressions;
  9
 10
 11 namespace MISMODEL.DAL
 12 {
 13     public class BaseRepository<T> : IBaseRepository<T> where T : class ,new()
 14     {
 15         public MISDBEntities db = ContextFactory.GetCurrentContext();
 16         public void SetLazyLoading(bool flag)
 17         {
 18             db.Configuration.LazyLoadingEnabled = flag;
 19         }
 20        //1实现对数据库的增加功能
 21         public bool AddEntity(T entity)
 22         {
 23             db.Set<T>().Add(entity);
 24             return db.SaveChanges() > 0;
 25         }
 26         public bool AddEntity(T entity,bool isSave)
 27         {
 28             db.Set<T>().Add(entity);
 29             if (isSave)
 30             {
 31                 return db.SaveChanges() > 0;
 32             }
 33             else {
 34                 return false;
 35             }
 36          //   return isSave ? db.SaveChanges() > 0: false;
 37         }
 38        //2实现对数据库的编辑功能
 39        public bool UpdateEntity(T entity)
 40         {
 41                 db.Set<T>().Attach(entity);
 42                 db.Entry<T>(entity).State = EntityState.Modified;
 43
 44                return db.SaveChanges() > 0;
 45          }
 46        //3实现对数据库的删除功能
 47        public bool DeleteEntity(T entity)
 48        {
 49            db.Set<T>().Attach(entity);
 50            db.Entry<T>(entity).State = EntityState.Deleted;
 51            return db.SaveChanges() > 0;
 52        }
 53        public bool DeleteEntity(List<T> entitys, bool isSave)
 54        {
 55            foreach (var T in entitys) {
 56                db.Set<T>().Attach(T);
 57                db.Entry<T>(T).State = EntityState.Deleted;
 58            }
 59            if (isSave)
 60            {
 61                return db.SaveChanges() > 0;
 62            }
 63            else
 64            {
 65                return false;
 66            }
 67        }
 68        public bool Commit() {
 69            return db.SaveChanges() > 0;
 70        }
 71        //4实现对数据库的查询  --主键查询
 72        public T FindByID(Expression<Func<T, bool>> whereLambda)
 73        {
 74            return db.Set<T>().AsNoTracking().FirstOrDefault(whereLambda);
 75        }
 76        //5实现对数据库的查询  --条件查询
 77        public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
 78        {
 79            return db.Set<T>().AsNoTracking().Where<T>(whereLambda);
 80        }
 81        public DbSet<T> LoadDbSetEntities()
 82        {
 83            return db.Set<T>();
 84        }
 85        //6实现对数据库的查询  --无条件查询
 86        public IQueryable<T> LoadEntities()
 87        {
 88            return db.Set<T>().AsNoTracking().AsQueryable();
 89        }
 90        //7实现对数据库的查询  --首行查询
 91        public T FirstOrDefaultEntities(Expression<Func<T, bool>> whereLambda)
 92        {
 93            return db.Set<T>().FirstOrDefault<T>(whereLambda);
 94        }
 95        //8实现对数据库的查询   --动态条件查询
 96        public IQueryable<T> SqlQuery(string sqlstring, params object[] paramertes)
 97        {
 98            return db.Database.SqlQuery<T>(sqlstring, paramertes).AsQueryable();
 99        }
100         //9实现对数据库的查询 --分页
101        public IQueryable<T> LoadPageEntities<S>(int pageIndex, int pageSize, out  int total, Expression<Func<T, bool>> whereLambda, bool isAsc, Func<T, S> orderByLambda)
102        {
103            var temp = db.Set<T>().Where<T>(whereLambda);
104            total = temp.Count(); //得到总的条数
105            //排序,获取当前页的数据
106            if (isAsc)
107            {
108                temp = temp.OrderBy<T, S>(orderByLambda)
109                     .Skip<T>(pageSize * (pageIndex - 1)) //越过多少条
110                     .Take<T>(pageSize).AsQueryable(); //取出多少条
111            }
112            else
113            {
114                temp = temp.OrderByDescending<T, S>(orderByLambda)
115                    .Skip<T>(pageSize * (pageIndex - 1)) //越过多少条
116                    .Take<T>(pageSize).AsQueryable(); //取出多少条
117            }
118            return temp.AsQueryable();
119        }
120       }
121 }

3.角色和组织机构数据接口

1     public interface IG_orgaRepository:IBaseRepository<G_orga> //生成接口
2     {
3     }
4
5     public interface IG_roleRepository:IBaseRepository<G_role> //生成接口
6     {
7     }

4.角色和组织机构数据层实现

    public class G_orgaRepository:BaseRepository<G_orga>,IG_orgaRepository //生成实体对象
    {
    }

    public class G_roleRepository:BaseRepository<G_role>,IG_roleRepository //生成实体对象

5.为实现数据线程唯一,新建类ContextFactory

BaseRepository<T>调用该类

public MISDBEntities db = ContextFactory.GetCurrentContext();

    public class ContextFactory
    {
        /// <summary>
        /// 获取当前数据上下文
        /// </summary>
        /// <returns></returns>
        public static MISDBEntities GetCurrentContext()
        {
            MISDBEntities _nContext = CallContext.GetData("MISDB") as MISDBEntities;
            if (_nContext == null)
            {
                _nContext = new MISDBEntities();
                CallContext.SetData("MISDB", _nContext);
            }
            _nContext.Configuration.LazyLoadingEnabled = false;
            return _nContext;
        }
    }

6.为降低系统耦合建立工厂类RepositoryFactory

public static class RepositoryFactory
    {

        public static  IG_orgaRepository  G_orgaRepository
        {
            get { return new G_orgaRepository(); }
        }
        public static  IG_rolemenuRepository  G_rolemenuRepository
        {
            get { return new G_rolemenuRepository(); }
        }

    }

时间: 2024-08-24 14:19:49

.net通用框架 (四)--DAL数据层以及数据接口的相关文章

业务层和数据层

业务层和数据层父类及接口-T4模板 在上一篇中,我们已经把项目的基本框架搭起来了,这一篇我们就来实现业务层和数据层的父接口及父类. 1.我们先来定义一个业务层父接口IBaseBLL.cs using System;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Text;using System.Threading.Tasks; namespace IBLL

如何设计一个坚如磐石的数据层?

在现代软件工程的开发应用和系统设计中,我们一般把软件系统的逻辑层次分为三层:展示层,处理层和数据层.数据层又可细分为缓存层,数据库层和文件存储层,如图: 鉴于创建高性能程序的关键是多花时间在系统设计上,本文主要对软件系统的数据层优化设计做一些阐述和归纳总结. 缓存 在互联网系统中,缓存技术对高并发,高性能的帮助起着功不可没的作用.以目前使用最为广泛的是redis3.0版本为例,其有三种工作模式: 以上3种模式都支持级联部署.针对不同数据量大小,一般可采用两种方案: 如果数据量在10G以内,单ma

EF通用数据层封装类(支持读写分离,一主多从)

浅谈orm 记得四年前在学校第一次接触到 Ling to Sql,那时候瞬间发现不用手写sql语句是多么的方便,后面慢慢的接触了许多orm框架,像 EF,Dapper,Hibernate,ServiceStack.OrmLite 等.当然每种orm都有各自的优势,也有不足的地方.园子里也有很多大神开源了他们写的orm,如SqlSugar,Chloe.ORM,CYQ.Data 等.先不说这些开源的orm使用度怎么样,我觉得起码从开源的精神上就很可嘉了,我也曾下载过这几位大神的源码进行学习. 所有o

【开源】OSharp框架解说系列(5.2):EntityFramework数据层实现

〇.前言 上篇 的数据层设计中,我们主要设计了数据对对外开放的 实体基类EntityBase<TKey>,单元操作接口IUnitOfWork 和 数据仓储接口IRepository<TEntity, TKey>,下面我们来解说怎样来使用 EntityFramework 对这些数据访问需求进行实现.EntityFramework 的实现中,我们不仅要实现以上设计的两个接口,还要做以下几件事: 设计一个与 业务实体解耦的 EntityFramework数据上下文类 设计 实体加载方案,

EF 通用数据层类

EF 通用数据层父类方法小结 转载:http://www.cnblogs.com/yq-Hua/p/4165344.html MSSql 数据库 数据层 父类 增删改查: using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Linq; using Syste

IFC数据模式架构的四个概念层详解说明

IFC模型体系结构由四个层次构成,从下到上依次是 资源层(Resource Layer).核心层(Core Layer).交互层(Interoperability Layer).领域层(Domain Layer).每层中都包含一系列的信息描述模块,并且遵守一个规则:每个层次只能引用同层次和下层的信息资源,而不能引用上层的资源,当上层资源发生变动时,下层是不会受到影响的. ①资源层IFC体系架构中的最低层,能为其他层所引用.主要是描述标准中用到的基本信息,不针对具体的行业本身,是无整体结构的分散信

国产InitPHP框架系列 - InitPHP框架搭建高可用WEB应用05:数据层Dao使用

InitPHP框架是一款轻量级PHP开源框架,框架文档和下载地址:http://initphp.com Dao层说明 Dao层通俗的讲就是数据层.再简单的讲,Dao层主要是用于写sql语句的.可能没有搞过Java的同学会对DAO层比较陌生,甚至不能接受. 但是引入DAO层有非常大的好处: 1. 将业务和数据操作进行剥离.例如将原来的MVC中的module层分割成Service和Dao层.Service主要用来负责业务操作,而Dao主要用来负责数据的操作. 2. 原来的MVC模式,项目开发越久,时

EF 通用数据层父类方法小结

MSSql 数据库 数据层 父类 增删改查: using System;using System.Collections.Generic;using System.Data;using System.Data.Entity;using System.Data.Entity.Infrastructure;using System.Linq;using System.Linq.Expressions;using System.Reflection;using System.Text; namespa

存储过程通用DAL类查询分页数据返回datatable

DAL #region 查询分页数据 public DataTable SelectPageing(string feids, int page, int pagesize, int paixu, string where, string paixufeids, string tablename) { SqlParameter[] parms = new SqlParameter[] { new SqlParameter("@FEILDS",SqlDbType.NVarChar,100