
正文
.net通用权限框架B/S (四)--DAL数据层以及数据接口
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
数据层以及数据接口设计如下图(以g_orga组织机构和g_role角色)为例,这几个类可以通过.tt模版生成
设计参考学习http://www.cnblogs.com/hanyinglong/archive/2013/04/08/3008896.html

1.IBaseRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Linq.Expressions; namespace MISMODEL.DAL
{
public interface IBaseRepository<T> where T : class, new()
{
void SetLazyLoading(bool flag); // 1实现对数据库的添加功能,添加实现EF框架的引用
bool AddEntity(T entity);
bool AddEntity(T entity,bool isSave);
// 2实现对数据库的修改功能
bool UpdateEntity(T entity);
// 3实现对数据库的删除功能
bool DeleteEntity(T entity);
bool DeleteEntity(List<T> entity, bool isSave);
// 4实现对数据库的查询 --主键查询
T FindByID(Expression<Func<T, bool>> whereLambda);
// 5实现对数据库的查询 --条件查询
IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda);
// 6实现对数据库的查询 --无条件查询
IQueryable<T> LoadEntities();
// 7查询返回dbset
DbSet<T> LoadDbSetEntities();
// 8实现对数据库的查询 --首行查询
T FirstOrDefaultEntities(Expression<Func<T, bool>> whereLambda);
// 9实现对数据库的查询 --动态条件查询
IQueryable<T> SqlQuery(string sqlstring, params object[] paramertes);
// 10实现对数据的分页查询
IQueryable<T> LoadPageEntities<S>(int pageIndex, int pageSize, out int total, Expression<Func<T, bool>> whereLambda, bool isAsc, Func<T, S> orderByLambda);
//10事物提交
bool Commit();
}
}
2. BaseRepository<T>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data;
using System.Linq.Expressions; namespace MISMODEL.DAL
{
public class BaseRepository<T> : IBaseRepository<T> where T : class ,new()
{
public MISDBEntities db = ContextFactory.GetCurrentContext();
public void SetLazyLoading(bool flag)
{
db.Configuration.LazyLoadingEnabled = flag;
}
//1实现对数据库的增加功能
public bool AddEntity(T entity)
{
db.Set<T>().Add(entity);
return db.SaveChanges() > ;
}
public bool AddEntity(T entity,bool isSave)
{
db.Set<T>().Add(entity);
if (isSave)
{
return db.SaveChanges() > ;
}
else {
return false;
}
// return isSave ? db.SaveChanges() > 0: false;
}
//2实现对数据库的编辑功能
public bool UpdateEntity(T entity)
{
db.Set<T>().Attach(entity);
db.Entry<T>(entity).State = EntityState.Modified; return db.SaveChanges() > ;
}
//3实现对数据库的删除功能
public bool DeleteEntity(T entity)
{
db.Set<T>().Attach(entity);
db.Entry<T>(entity).State = EntityState.Deleted;
return db.SaveChanges() > ;
}
public bool DeleteEntity(List<T> entitys, bool isSave)
{
foreach (var T in entitys) {
db.Set<T>().Attach(T);
db.Entry<T>(T).State = EntityState.Deleted;
}
if (isSave)
{
return db.SaveChanges() > ;
}
else
{
return false;
}
}
public bool Commit() {
return db.SaveChanges() > ;
}
//4实现对数据库的查询 --主键查询
public T FindByID(Expression<Func<T, bool>> whereLambda)
{
return db.Set<T>().AsNoTracking().FirstOrDefault(whereLambda);
}
//5实现对数据库的查询 --条件查询
public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
{
return db.Set<T>().AsNoTracking().Where<T>(whereLambda);
}
public DbSet<T> LoadDbSetEntities()
{
return db.Set<T>();
}
//6实现对数据库的查询 --无条件查询
public IQueryable<T> LoadEntities()
{
return db.Set<T>().AsNoTracking().AsQueryable();
}
//7实现对数据库的查询 --首行查询
public T FirstOrDefaultEntities(Expression<Func<T, bool>> whereLambda)
{
return db.Set<T>().FirstOrDefault<T>(whereLambda);
}
//8实现对数据库的查询 --动态条件查询
public IQueryable<T> SqlQuery(string sqlstring, params object[] paramertes)
{
return db.Database.SqlQuery<T>(sqlstring, paramertes).AsQueryable();
}
//9实现对数据库的查询 --分页
public IQueryable<T> LoadPageEntities<S>(int pageIndex, int pageSize, out int total, Expression<Func<T, bool>> whereLambda, bool isAsc, Func<T, S> orderByLambda)
{
var temp = db.Set<T>().Where<T>(whereLambda);
total = temp.Count(); //得到总的条数
//排序,获取当前页的数据
if (isAsc)
{
temp = temp.OrderBy<T, S>(orderByLambda)
.Skip<T>(pageSize * (pageIndex - )) //越过多少条
.Take<T>(pageSize).AsQueryable(); //取出多少条
}
else
{
temp = temp.OrderByDescending<T, S>(orderByLambda)
.Skip<T>(pageSize * (pageIndex - )) //越过多少条
.Take<T>(pageSize).AsQueryable(); //取出多少条
}
return temp.AsQueryable();
}
}
}
3.角色和组织机构数据接口
public interface IG_orgaRepository:IBaseRepository<G_orga> //生成接口
{
} public interface IG_roleRepository:IBaseRepository<G_role> //生成接口
{
}
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(); }
} }








