依赖接口编程

//分层的思想:模块内部之间高内聚,模块外部之间低耦合
//当遇到跨数据库时候,数据库访问驱动层就会产生变化,就会直接影响采用高耦合使用该层的的代码块
//解决模块间高耦合的状况思路:利用接口或者抽象类来进行隔离,不再依赖具体的类的实现(new xxx)

依赖接口编程:
//UserInfoDal userInfoDal = new UserInfoDal(); //这种直接new出DAL层的内容是一种高耦合,只要DAL层的类发生变化这里也会受到很多影响
//所以不应该依赖【UserInfoDal userInfoDal = new UserInfoDal()】这种方法,而是依赖【UserInfo的接口:IUserInfo】
//依赖接口的做法:IUserInfoDal userInfoDal = new UserInfoDal(),这样字不管实例怎么变化,接口是不会变化的,而UserInfoDal是实现了接口IUserInfoDal
//IUserInfoDal userInfoDal =new UserInfoDal();//依赖接口编程,所有实现接口的类都遵从接口的规范约束

(1)基类接口:
namespace My.OA.IDal
{
//只要跨层跨模块,就一定要用接口进行隔离,哪怕这个接口没有意义,层与层之间必须依赖接口不是依赖具体的实现
//职责:数据库访问层接口基类
public interface IBaseDal<T> where T:class ,new()
{
bool AddEntityInfo(T TModel); //接口的方法默认是Ppublic的,不需要方法实体
//删
bool DeleteEntityInfo(T TModel);//实现了接口的类,必须实现它的方法,这样就形成一种约束规范,只要实现了这个接口的类都有这个接口的同名方法。实现接口方法:public bool DeleteEntityInfo(T TModel){}
//改
bool UpEntityInfo(T TModel);
//查
IQueryable<T> GetEntityInfo(Expression<Func<T,bool>> whereLambda);
//分页
IQueryable<T> GetPagingEntityInfo<s>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderLambda, bool isDesc);
}
}

(2)实现基类接口:
public interface IUserInfoDal:IBaseDal<UserInfo>//实现公共方法接口
{
}
(3)基类;
/// <summary>
/// 职责:封装所有DAL公共的方法
/// 类的职责要单一
/// </summary>
public class BaseDal<T> where T:class ,new ()
{
//Entities myEntities = new Entities();
//这是一种依赖抽象编程,不是依赖具体的实例,能用抽象编程的地方就尽量用,做到维护性最好
public DbContext myEntities
{
get { return DataBaseContextFactory.GetDataTabelContext(); }
}
//增
public bool AddEntityInfo(T TModel)
{
myEntities.Set<T>().Add(TModel);
//if (myEntities.SaveChanges() > 0)
//{
// return true;
//}
//else
//{
// return false;
//}
return true;
}
//删
public bool DeleteEntityInfo(T TModel)
{
myEntities.Entry<T>(TModel).State = EntityState.Deleted;
//if (myEntities.SaveChanges() > 0)
//{
// return true;
//}
//else
//{
// return false;
//}
return true;
}
//改
public bool UpEntityInfo(T TModel)
{
myEntities.Entry<T>(TModel).State = EntityState.Modified;
//if (myEntities.SaveChanges() > 0)
//{
// return true;
//}
//else
//{
// return false;
//}
return true;
}
//查
public IQueryable<T> GetEntityInfo(Expression<Func<T,bool>> whereLambda)
{
//Set<T>()根据泛型T在上下文返回一个DBset实例
return myEntities.Set<T>().Where(whereLambda).AsQueryable();
}
//分页
public IQueryable<T> GetPagingEntityInfo<s>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderLambda,bool isDesc)
{
//s是方法泛型,约束排序的类型
total = myEntities.Set<T>().Where(whereLambda).Count();
if (isDesc)
{
var temp = myEntities.Set<T>().Where(whereLambda).OrderByDescending<T, s>(orderLambda).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
return temp;
}
else
{
var temp = myEntities.Set<T>().Where(whereLambda).OrderBy<T, s>(orderLambda).Skip(pageSize * (pageIndex - 1)).Take(pageSize);
return temp;
}
}
}

实现基类和接口:
public class UserInfoDal:BaseDal<UserInfo>,IUserInfoDal------这样BaseDal<UserInfo>就对接口的方法进行隐式重写了
{
}
面向接口编程
IUserInfoDal userInfoDal = new UserInfoDal()

posted @ 2016-08-07 15:45  黑色鼠标  阅读(657)  评论(0编辑  收藏  举报