【Design】业务逻辑层封装

与数据访问层一样,业务逻辑层也有增删改查,所以也定义一个基类

public abstract class BaseService<T> where T : class, new()
{
    public IBaseDal<T> CurrentDal { get; set; }

    public IDbSession DbSession { get { return DbSessionFactory.GetCurrentDbSession(); } }

    public abstract void SetCurrentDal();//子类必须实现抽象方法

    public BaseService()
    {
        SetCurrentDal();
    }

    /// <summary>
    /// 添加
    /// </summary>
    public T Add(T entity)
    {
        CurrentDal.Add(entity);
        DbSession.SaveChanges();
        return entity;
    }

    /// <summary>
    /// 修改
    /// </summary>
    public bool Update(T entity)
    {
        CurrentDal.Update(entity);
        return DbSession.SaveChanges() > 0;
    }

    /// <summary>
    /// 删除
    /// </summary>
    public bool Delete(T entity)
    {
        CurrentDal.Delete(entity);
        return DbSession.SaveChanges() > 0;
    }

    /// <summary>
    /// 通过参数获取信息
    /// </summary>
    public IQueryable<T> GetEntities(Expression<Func<T, bool>> whereLambda)
    {
        return CurrentDal.GetEntities(whereLambda);
    }

    /// <summary>
    /// 分页
    /// </summary>
    /// <typeparam name="S">排序对象</typeparam>
    /// <param name="pageSize">页数据个数</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="total">数据总量</param>
    /// <param name="whereLambda">条件参数</param>
    /// <param name="orderByLambda">排序参数</param>
    /// <param name="isAsc">是否升序</param>
    /// <returns></returns>
    public IQueryable<T> GetPageEntities<S>(int pageSize, int pageIndex, out int total,
       Expression<Func<T, bool>> whereLambda,
       Expression<Func<T, S>> orderByLambda,
       bool isAsc)
    {
        return CurrentDal.GetPageEntities(pageSize, pageIndex, out total, whereLambda, orderByLambda, isAsc);
    }
}

UserInfoService

public class UserInfoService : BaseService<UserInfo>
{
    public override void SetCurrentDal()
    {
        CurrentDal = this.DbSession.UserInfoDal;
    }
}

OrderInfoService 

public class OrderInfoService : BaseService<OrderInfo>
{
    public override void SetCurrentDal()
    {
        CurrentDal = this.DbSession.OrderInfoDal;
    }
}

 

相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页