zoukankan      html  css  js  c++  java
  • MVC三层BLL

    DAL层的封装并不是很好,有些地方还要修改,往后面我们就会发现问题。

    下面就是对BLL层的封装了,同样以UserInfo要有一个BLL类UserInfoService,其他的实例也有一个Service类同样是对IDAL层的调用,又要抽象出一个基类BaseService来

     1 using DALFactory;
     2 using IDAL;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 using System.Linq.Expressions;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 
    10 namespace BLL
    11 {
    12     public abstract class BaseService<T>where T:class,new()
    13     {
    14         //??怎样调用IDAL实例,基类怎么知道调用哪个DAL实例啊?
    15 
    16         //公用的CRUD方法
    17         public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda)
    18         {
    19             //return ?.LoadEntities(whereLamda);
    20         }
    21         public IQueryable<T> LoadPageEntities<s>(int pageSize, int pageIndex, out int totalCount, Expression<Func<T, bool>> whereLamda, Expression<Func<T, s>> orderByLamda, bool IsAsc)
    22         {
    23            // return ?.LoadPageEntities<s>(pageSize, pageIndex, out totalCount, whereLamda, orderByLamda, IsAsc);
    24         }
    25         public bool DeleteEntity(T entity)
    26         {
    27            //return  ?.DeleteEntity(entity);
    28         }
    29         public bool EditEntity(T entity)
    30         {
    31             //return  ?.EditEntity(entity);
    32         }
    33         public T AddEntity(T entity)
    34         {
    35             return  ?.AddEntity(entity);
    36         }
    37     }
    38 }
    View Code

    代码中这个基类不知道要调用哪个DAL实例,这样就实现不了对基类的封装BaseService,我们在BaseService类中定义一个属性CurrentDAL,这个属性里面放的就是IBaseDAL<>类型,这样就可以知道是哪个DAL了,因为这个类的类型是IBaseDAL类型封装了对所有实例的方法,但是只定义没有赋值,我们要在BaseService对CurrentDAL赋值,但是BaseService确定不了到底要用哪个DAL,所以在BaseService定义一个抽象方法,把这个方法给子类实现,子类UserInfoService知道自己是对UserInfo表实现的数据操作,再把父类改为抽象类,在BaseService的构造函数里面调用一下这个方法

     1 using DALFactory;
     2 using IDAL;
     3 using System;
     4 using System.Collections.Generic;
     5 using System.Linq;
     6 using System.Linq.Expressions;
     7 using System.Text;
     8 using System.Threading.Tasks;
     9 
    10 namespace BLL
    11 {
    12     public abstract class BaseService<T>where T:class,new()
    13     {
    14         /// <summary>
    15         /// 当前DAL实例字段,存储当前DAL
    16         /// </summary>
    17         public IBaseDAL<T> CurrentDAL{get;set;}
    18         /// <summary>
    19         /// 设置当前DAL实例,由子类实现
    20         /// </summary>
    21         /// <returns></returns>
    22         public abstract void SetCurrentDAL();
    23         /// <summary>
    24         /// 构造方法内调用子类的SetCurrentDAL()方法,初始化CurrentDAL
    25         /// </summary>
    26         public BaseService()
    27         {
    28             SetCurrentDAL();
    29         }
    30         //公用的CRUD方法
    31         public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda)
    32         {
    33             return CurrentDAL.LoadEntities(whereLamda);
    34         }
    35         public IQueryable<T> LoadPageEntities<s>(int pageSize, int pageIndex, out int totalCount, Expression<Func<T, bool>> whereLamda, Expression<Func<T, s>> orderByLamda, bool IsAsc)
    36         {
    37             return CurrentDAL.LoadPageEntities<s>(pageSize, pageIndex, out totalCount, whereLamda, orderByLamda, IsAsc);
    38         }
    39         public bool DeleteEntity(T entity)
    40         {
    41             CurrentDAL.DeleteEntity(entity);
    42             return this.GetCurrentDbSession.SaveChanges();
    43         }
    44         public bool EditEntity(T entity)
    45         {
    46             CurrentDAL.EditEntity(entity);
    47             return this.GetCurrentDbSession.SaveChanges();
    48         }
    49         public T AddEntity(T entity)
    50         {
    51             CurrentDAL.AddEntity(entity);
    52             this.GetCurrentDbSession.SaveChanges();
    53             return entity;
    54         }
    55     }
    56 }
    View Code

    让其他要实现这些方法的类继承这个基类,实现这个基类抽象方法,BLL层就封装好了,接下来就是开放一个IBLLService的接口了,同样IUserInfoService和IUserStuService都要实现BaseService的方法,所以封装一个IBaseService接口,让IUserInfoService和其他这样的类继承这个基接口

     1 using IDAL;
     2 using System;
     3 using System.Collections.Generic;
     4 using System.Linq;
     5 using System.Linq.Expressions;
     6 using System.Text;
     7 using System.Threading.Tasks;
     8 
     9 namespace IBLL
    10 {
    11     public interface IBaseService<T>where T:class,new()
    12     {
    13         IBaseDAL<T> CurrentDAL { get; set; }
    14         IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLamda);
    15         IQueryable<T> LoadPageEntities<s>(int pageSize, int pageIndex, out int totalCount, Expression<Func<T, bool>> whereLamda, Expression<Func<T, s>> orderByLamda, bool IsAsc);
    16         bool DeleteEntity(T entity);
    17         bool EditEntity(T entity);
    18         T AddEntity(T entity);
    19     }
    20 }
    View Code

    这样最外层的控制器里就使用IUserInfoService接口,让BLL层和控制器层解耦……

    BLL层也带了个爹IBLL层……

  • 相关阅读:
    1.2基础--对象属性操作检测遍历
    C语言概览
    测试博客园字体
    c语言指针理解,指针的概念和演示指针的简单操作2
    c语言指针理解代码示例_函数返回指针变量_解除运算符获取指针的值
    c语言指针理解,指针的概念和演示指针的简单操作
    c语言指针概念解释_初步
    项目对文件分目录的感想
    visual studio快捷键
    java开源项目RuoYi若依管理系统
  • 原文地址:https://www.cnblogs.com/lvshijian-1994/p/4876886.html
Copyright © 2011-2022 走看看