asp.net 学习之路 项目整体框架简单的搭建(2)


public abstract class BaseService<T> :IBLL.IBaseService<T> where T:class, new ()
{
public BaseService()
{
GetInstance();
}
protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();
protected IDAL.IBaseRepository<T> CurrentRepository { get; set; }
public abstract void GetInstance();
public IQueryable<T> GetEntities(Func<T, bool> lambdaWhere)
{
//_DbSession.SavaChanges();
return CurrentRepository.GetEntities(lambdaWhere);
}
public bool DeleteEntity(T entity)
{
CurrentRepository.DeleteEntity(entity);
return _DbSession.SaveChanges() > 0;
}
public bool UpdateEntity(T entity)
{
CurrentRepository.UpdateEntity(entity);
return _DbSession.SaveChanges() > 0;
}
public T AddEntity(T entity)
{
var en = CurrentRepository.AddEntity(entity);
_DbSession.SaveChanges();
return en;
}
public IQueryable<T> GetEntitiesByPageIndex<TS>(int pageIndex, int pageSize, out int totalCount, Func<T, bool> lambdaWhere, Func<T, TS> orderByRole, bool descending)
{
return CurrentRepository.GetEntitiesByPageIndex(pageIndex, pageSize, out totalCount, lambdaWhere, orderByRole,
descending);
}
}
}


其他的业务层也需要接口抽象出一层出来来作为约束,这样ui层也不需要关心你业务层怎么实现...
另外一种实现数据库入口的方试DBSession

我们先看一个类,dbsession里面有属性,为接口,对应的该接口所对应的实现类,两个方法SaveChanges(),与exesql(EF用的5.0+),里面返回的是当前EF线程类上下文的savechange()与执行sql语句的放回值,怎么才能确保当前进程内EF上下文只有一个了,我们看另外一个类.

复制代码 代码如下:


public partial class DbSession:IDAL.IDbSession
{
#region 代码生成器生成
//public IDAL.IRoleRepository RoleRepository
//{
// get { return new RoleRepository();}
//}
//public IDAL.IUserInfoRepository UserInfoRepository
//{
// get { return new UserInfoRepository();}
//}
#endregion
public int SaveChanges()
{
return EFContentFactory.GetCurrentContext().SaveChanges();
}
public int ExcuteSql(string strSql, System.Data.Objects.ObjectParameter[] parameters)
{
return EFContentFactory.GetCurrentContext().Database.ExecuteSqlCommand(strSql, parameters);
}
}
public class EFContentFactory
{
public static DbContext GetCurrentContext()
{
DbContext obj = CallContext.GetData("DbContext") as DbContext;
if (obj==null)
{
obj = new Model.DataContainer();
CallContext.SetData("DbContext",obj);
}
return obj;
}
}


CallContext 是类似于方法调用的线程本地存储区的专用集合对象,并提供对每个逻辑执行线程都唯一的数据槽。数据槽不在其他逻辑线程上的调用上下文之间共享,这是从msdn上截取的一段话,它有几个方法,这里面我们用到setdata跟getdata,来确保上下文线程内唯一,同样的我们让他接口化,与工厂内实现下--

复制代码 代码如下:


public class DbSeesionFactory
{
/// <summary>
/// 保证线程内dbsession唯一
/// </summary>
/// <returns></returns>
public static IDAL.IDbSession GetSession()
{
IDAL.IDbSession _dbSession = CallContext.GetData("DbSession") as IDbSession;
if (_dbSession == null)
{
_dbSession = new DbSession();
CallContext.SetData("DbSession", _dbSession);
}
return _dbSession;
}
}


业务层的子类重写方法时这么来实现,同样基类加个: protected IDAL.IDbSession _DbSession = DbSeesionFactory.GetSession();

复制代码 代码如下:


public partial class ActionInfoService:BaseService<ActionInfo>,IBLL.IActionInfoService
{
public override void GetInstance()
{
CurrentRepository = _DbSession.ActionInfoRepository;
}
}

public partial class R_UserInfo_ActionInfoService:BaseService<R_UserInfo_ActionInfo>,IBLL.IR_UserInfo_ActionInfoService
{
public override void GetInstance()
{
CurrentRepository = _DbSession.R_UserInfo_ActionInfoRepository;
}
}

public partial class RoleService:BaseService<Role>,IBLL.IRoleService
{
public override void GetInstance()
{
CurrentRepository = _DbSession.RoleRepository;
}
}


为什么要这么做了?当我们用EF的时候比如一个方法里面要操作多个表,就不断的需要用到上下文,这样可以帮我们剩不少事最后直接来个_dbsession.savechange().可以达到批量删除修改等等操作.具体看我,今天做了个批量删除的

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjdfpz.html