MVC项目结构搭建及单个类的实现学习笔记1(3)

using System.Data.Entity; using PMS.IDAL; using PMS.DAL; namespace PMS.DALFactory { public partial class DbSession:IDbSession { public DbContext Db { get { return DbContextFactory.CreateContext(); } } private IUserDal _userDal; public IUserDal UserDal { get { return _userDal ?? (_userDal = AbstractFactory.CreateUserInfoDal()); } set { _userDal = value; } } /// <summary> /// 工作单元模式,统一保存数据 /// </summary> /// <returns></returns> public bool SaveChanges() { return Db.SaveChanges() > 0; } } }

业务逻辑层的构建

业务类基类BaseService

using System; using System.Linq; using System.Linq.Expressions; using PMS.DALFactory; using PMS.IDAL; namespace PMS.BLL { public abstract class BaseService<T> where T:class,new() { public IDbSession CurrentDbSession { get { return new DbSession(); } } public IBaseDal<T> CurrentDal { get; set; } public abstract void SetCurrentDal(); public BaseService() { SetCurrentDal();//子类一定要实现抽象方法,以指明当前类的子类类型。 } /// <summary> /// 查询过滤 /// </summary> /// <param></param> /// <returns></returns> public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda) { return CurrentDal.LoadEntities(whereLambda); } /// <summary> /// 分页 /// </summary> /// <typeparam></typeparam> /// <param></param> /// <param></param> /// <param></param> /// <param></param> /// <param></param> /// <param></param> /// <returns></returns> public IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderbyLambda, bool isAsc) { return CurrentDal.LoadPageEntities<s>(pageIndex, pageSize, out totalCount, whereLambda, orderbyLambda, isAsc); } /// <summary> /// 删除 /// </summary> /// <param></param> /// <returns></returns> public bool DeleteEntity(T entity) { CurrentDal.DeleteEntity(entity); return CurrentDbSession.SaveChanges(); } /// <summary> /// 编辑 /// </summary> /// <param></param> /// <returns></returns> public bool EditEntity(T entity) { CurrentDal.EditEntity(entity); return CurrentDbSession.SaveChanges(); } /// <summary> /// 添加数据 /// </summary> /// <param></param> /// <returns></returns> public T AddEntity(T entity) { CurrentDal.AddEntity(entity); CurrentDbSession.SaveChanges(); return entity; } } }

UserService类:

using PMS.IBLL; using PMS.Model; namespace PMS.BLL { public partial class UserService : BaseService<User> { public override void SetCurrentDal() { CurrentDal = CurrentDbSession.UserDal; } } }  

业务逻辑接口层的构建

直接建立对应的接口并使用UserService类实现IUserService接口

IBaseService接口:

using System; using System.Linq; using System.Linq.Expressions; using PMS.IDAL; namespace PMS.IBLL { public interface IBaseService<T> where T : class,new() { IDbSession CurrentDbSession { get; } IBaseDal<T> CurrentDal { get; set; } void SetCurrentDal(); IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda); IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderbyLambda, bool isAsc); bool DeleteEntity(T entity); bool EditEntity(T entity); T AddEntity(T entity); } }

IUserService接口:

using PMS.Model; namespace PMS.IBLL { public partial interface IUserService:IBaseService<User> { } }

使用UserService类实现IUserService接口:

public partial class UserService : BaseService<User>, IUserService  

以上我们就完成了整个框架中关于User类的各层次的实现。

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

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