资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

MVC项目结构搭建及单个类如何实现-创新互联

这篇文章主要介绍MVC项目结构搭建及单个类如何实现,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为天峨企业提供专业的网站设计、成都做网站,天峨网站改版等技术服务。拥有10多年丰富建站经验和众多成功案例,为您定制开发。

先来一张项目的层级结构图:

MVC项目结构搭建及单个类如何实现

Model:模型层,主要是各种类型、枚举以及ORM框架,框架完成数据库和实体类的映射。项目中选用了微软的开源ORM框架 EntityFramework 6.0 (以下简称EF),数据库则选择了微软的轻量级数据库SQL Server Compact 4.0本地数据库(简称Compact),Compact对EF支持比较完美,又属于文档型数据库,部署起来比较简洁。

DAL:数据访问层,主要是对数据库的操作层,为业务逻辑层或表示层提供数据服务。

IDAL:数据访问接口层,是数据访问层的接口,降低耦合。

DALFactory:数据会话层,封装了所有数据操作类实例的创建,将数据访问层与业务逻辑层解耦。

BLL:业务逻辑层,主要负责对数据层的操作,把一些数据层的操作进行组合以完成业务的需要。

IBLL:业务逻辑接口层,业务逻辑层的接口,降低耦合。

WebApp:表现层,是一个ASP.NET MVC项目,完成具体网站的实现。

Common:通用层,用来存放一些工具类。

下面是各个层级之间具体的实现,首先创建以 项目名.层级名 命名的各个层次,除WebApp层为ASP.NET MVC项目外,其余均创建为类库项目。

MVC项目结构搭建及单个类如何实现

模型层的构建

先建立模型层,新建ASP.NET 实体数据模型,关联到已经设计好的数据库,EF自动完成模型类的创建。

MVC项目结构搭建及单个类如何实现

数据访问层的构建

      DAL层中,我们首先需要一个方法来获取单例的EF数据操纵上下文对象,以保证每个用户访问时只有使用一个上下文对象对数据库进行操作。

DbContextFactory.cs

using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
using PMS.Model;

namespace PMS.DAL
{
  public class DbContextFactory
  {
    /// 
    /// 负责创建EF数据操作上下文实例,必须保证线程内
    /// 
    public static DbContext CreateContext()
    {
      DbContext dbContext = (DbContext)CallContext.GetData("dbContext");
      if (dbContext != null) return dbContext;
      dbContext = new PMSEntities();
      CallContext.SetData("dbContext", dbContext);
      return dbContext;
    }
  }
}

为User类创建DAL层,实现查询、分页查询、增加、删除和修改这五个基本的方法:

UserDAL.cs

using System;
using System.Data.Entity;
using System.Linq;
using PMS.IDAL;

namespace PMS.DAL
{
  public partial class UserDal   {
    public DbContext DbEntities = DbContextFactory.CreateContext();

    /// 
    /// 查询过滤
    /// 
    /// 过滤条件Lambda表达式
    /// 实体集合
    public IQueryable LoadEntities(System.Linq.Expressions.Expression> whereLamada)
    {
      return DbEntities.Set().Where(whereLamada);
    }

    /// 
    /// 分页查询
    /// 
    /// 排序类型
    /// 查询的页码
    /// 每页显示的数目
    /// 符合条件的总行数
    /// 过滤条件Lambda表达式
    /// 排序Lambda表达式
    /// 排序方向
    /// 实体集合
    public IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> whereLambda, System.Linq.Expressions.Expression> orderbyLambda, bool isAsc)
    {
      var temp = DbEntities.Set().Where(whereLambda);
      totalCount = temp.Count();
      temp = isAsc ? temp.OrderBy(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize) : temp.OrderByDescending(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize);
      return temp;
    }

    /// 
    /// 删除数据
    /// 
    /// 待删数据
    /// 删除结果
    public bool DeleteEntity(UserDal entity)
    {
      DbEntities.Entry(entity).State = EntityState.Deleted;
      return true;
    }

    /// 
    /// 编辑数据
    /// 
    /// 待编辑数据
    /// 编辑结果
    public bool EditEntity(UserDal entity)
    {
      DbEntities.Entry(entity).State = EntityState.Modified;
      return true;
    }

    /// 
    /// 添加数据
    /// 
    /// 待添加数据
    /// 已添加数据
    public UserDal AddEntity(UserDal entity)
    {
      entity = DbEntities.Set().Add(entity);
      return entity;
    }    
  }
}

注:这里的增删改操作并不即时进行,而是在封装在数据会话层中,以实现工作单元模式,提高数据库的操作效率。

考虑到每个类都需要实现相同的数据操作,我们可以将以上方法封装到一个泛型基类中,各类型只需要继承泛型基类就可以实现以上方法:

BaseDal.cs

using System;
using System.Data.Entity;
using System.Linq;

namespace PMS.DAL
{
  public class BaseDal where T:class ,new()
  {
    public DbContext DbEntities = DbContextFactory.CreateContext();

    /// 
    /// 查询过滤
    /// 
    /// 过滤条件Lambda表达式
    /// 实体集合
    public IQueryable LoadEntities(System.Linq.Expressions.Expression> whereLamada)
    {
      return DbEntities.Set().Where(whereLamada);
    }

    /// 
    /// 分页查询
    /// 
    /// 排序类型
    /// 查询的页码
    /// 每页显示的数目
    /// 符合条件的总行数
    /// 过滤条件Lambda表达式
    /// 排序Lambda表达式
    /// 排序方向
    /// 实体集合
    public IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression> whereLambda, System.Linq.Expressions.Expression> orderbyLambda, bool isAsc)
    {
      var temp = DbEntities.Set().Where(whereLambda);
      totalCount = temp.Count();
      temp = isAsc ? temp.OrderBy(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize) : temp.OrderByDescending(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize);
      return temp;
    }

    /// 
    /// 删除数据
    /// 
    /// 待删数据
    /// 删除结果
    public bool DeleteEntity(T entity)
    {
      DbEntities.Entry(entity).State = EntityState.Deleted;
      return true;
    }

    /// 
    /// 编辑数据
    /// 
    /// 待编辑数据
    /// 编辑结果
    public bool EditEntity(T entity)
    {
      DbEntities.Entry(entity).State = EntityState.Modified;
      return true;
    }

    /// 
    /// 添加数据
    /// 
    /// 待添加数据
    /// 已添加数据
    public T AddEntity(T entity)
    {
      entity = DbEntities.Set().Add(entity);
      //DbEntities.SaveChanges();
      return entity;
    }
  }
}

UserDal继承BaseDal

using PMS.IDAL;
using PMS.Model;

namespace PMS.DAL
{
  public partial class UserDal : BaseDal
  {
    
  }
}

数据访问接口层的构建

然后我们建立相应的IbaseDal接口和IUserDal接口,并且使UserDal类实现IUserDal接口

IBaseDal:

using System;
using System.Linq;

namespace PMS.IDAL
{
  public interface IBaseDal where T:class,new()
  {
    IQueryable LoadEntities(System.Linq.Expressions.Expression> whereLamada);

    IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount,
      System.Linq.Expressions.Expression> whereLambda,
      System.Linq.Expressions.Expression> orderbyLambda, bool isAsc);

    bool DeleteEntity(T entity);
    
    bool EditEntity(T entity);

    T AddEntity(T entity);
  }
}

IUserDal:

using PMS.Model;
namespace PMS.IDAL
{
  public partial interface IUserDal:IBaseDal
  {

  }
}

UserDal实现IUserDal接口:

public partial class UserDal : BaseDal,IUserDal

数据会话层的构建

抽象工厂类AbstractFactory:

using System.Configuration;
using System.Reflection;
using PMS.IDAL;

namespace PMS.DALFactory
{
  public partial class AbstractFactory
  {
    //读取保存在配置文件中的程序集名称与命名空间名
    private static readonly string AssemblyPath = ConfigurationManager.AppSettings["AssemblyPath"];
    private static readonly string NameSpace = ConfigurationManager.AppSettings["NameSpace"];
    /// 
    /// 获取UserDal的实例
    /// 
    /// 
    public static IUserDal CreateUserInfoDal()
    {
      var fullClassName = NameSpace + ".UserInfoDal";
      return CreateInstance(fullClassName) as IUserDal;
    }
    /// 
    /// 通过反射获得程序集中某类型的实例
    /// 
    /// 
    /// 
    private static object CreateInstance(string className)
    {
      var assembly = Assembly.Load(AssemblyPath);
      return assembly.CreateInstance(className);
    }
  }
}

数据会话类DbSession:

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; }
    }

    /// 
    /// 工作单元模式,统一保存数据
    /// 
    /// 
    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 where T:class,new()
  {
    public IDbSession CurrentDbSession
    {
      get
      {
        return new DbSession();
      }
    }
    public IBaseDal CurrentDal { get; set; }
    public abstract void SetCurrentDal();
    public BaseService()
    {
      SetCurrentDal();//子类一定要实现抽象方法,以指明当前类的子类类型。
    }

    /// 
    /// 查询过滤
    /// 
    /// 
    /// 
    public IQueryable LoadEntities(Expression> whereLambda)
    {
      return CurrentDal.LoadEntities(whereLambda);
    }

    /// 
    /// 分页
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    /// 
    public IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount, Expression> whereLambda,
      Expression> orderbyLambda, bool isAsc)
    {
      return CurrentDal.LoadPageEntities(pageIndex, pageSize, out totalCount, whereLambda, orderbyLambda, isAsc);
    }

    /// 
    /// 删除
    /// 
    /// 
    /// 
    public bool DeleteEntity(T entity)
    {
      CurrentDal.DeleteEntity(entity);
      return CurrentDbSession.SaveChanges();
    }

    /// 
    /// 编辑
    /// 
    /// 
    /// 
    public bool EditEntity(T entity)
    {
      CurrentDal.EditEntity(entity);
      return CurrentDbSession.SaveChanges();
    }

    /// 
    /// 添加数据
    /// 
    /// 
    /// 
    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
  {
    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 where T : class,new()
  {
    IDbSession CurrentDbSession { get; }
    IBaseDal CurrentDal { get; set; }
    void SetCurrentDal();
    IQueryable LoadEntities(Expression> whereLambda);

    IQueryable LoadPageEntities(int pageIndex, int pageSize, out int totalCount,
      Expression> whereLambda,
      Expression> 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
  {

  }
}

使用UserService类实现IUserService接口:

public partial class UserService : BaseService, IUserService

以上是“MVC项目结构搭建及单个类如何实现”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


文章标题:MVC项目结构搭建及单个类如何实现-创新互联
浏览地址:http://cdkjz.cn/article/ceogpo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220