欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MVC項(xiàng)目結(jié)構(gòu)搭建及單個(gè)類的實(shí)現(xiàn)學(xué)習(xí)筆記1

 更新時(shí)間:2016年09月21日 10:59:17   作者:叫我瑋仔  
這篇文章主要介紹了MVC項(xiàng)目結(jié)構(gòu)搭建及單個(gè)類在各個(gè)層次中的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

新人剛開始學(xué)習(xí)ASP.NET MVC,若有不足之處希望能得到您的指點(diǎn),不勝感激!

先來一張項(xiàng)目的層級(jí)結(jié)構(gòu)圖:

Model:模型層,主要是各種類型、枚舉以及ORM框架,框架完成數(shù)據(jù)庫和實(shí)體類的映射。項(xiàng)目中選用了微軟的開源ORM框架 EntityFramework 6.0 (以下簡(jiǎn)稱EF),數(shù)據(jù)庫則選擇了微軟的輕量級(jí)數(shù)據(jù)庫SQL Server Compact 4.0本地?cái)?shù)據(jù)庫(簡(jiǎn)稱Compact),Compact對(duì)EF支持比較完美,又屬于文檔型數(shù)據(jù)庫,部署起來比較簡(jiǎn)潔。

DAL:數(shù)據(jù)訪問層,主要是對(duì)數(shù)據(jù)庫的操作層,為業(yè)務(wù)邏輯層或表示層提供數(shù)據(jù)服務(wù)。

IDAL:數(shù)據(jù)訪問接口層,是數(shù)據(jù)訪問層的接口,降低耦合。

DALFactory:數(shù)據(jù)會(huì)話層,封裝了所有數(shù)據(jù)操作類實(shí)例的創(chuàng)建,將數(shù)據(jù)訪問層與業(yè)務(wù)邏輯層解耦。

BLL:業(yè)務(wù)邏輯層,主要負(fù)責(zé)對(duì)數(shù)據(jù)層的操作,把一些數(shù)據(jù)層的操作進(jìn)行組合以完成業(yè)務(wù)的需要。

IBLL:業(yè)務(wù)邏輯接口層,業(yè)務(wù)邏輯層的接口,降低耦合。

WebApp:表現(xiàn)層,是一個(gè)ASP.NET MVC項(xiàng)目,完成具體網(wǎng)站的實(shí)現(xiàn)。

Common:通用層,用來存放一些工具類。

下面是各個(gè)層級(jí)之間具體的實(shí)現(xiàn),首先創(chuàng)建以 項(xiàng)目名.層級(jí)名 命名的各個(gè)層次,除WebApp層為ASP.NET MVC項(xiàng)目外,其余均創(chuàng)建為類庫項(xiàng)目。

模型層的構(gòu)建

先建立模型層,新建ASP.NET 實(shí)體數(shù)據(jù)模型,關(guān)聯(lián)到已經(jīng)設(shè)計(jì)好的數(shù)據(jù)庫,EF自動(dòng)完成模型類的創(chuàng)建。

數(shù)據(jù)訪問層的構(gòu)建

      DAL層中,我們首先需要一個(gè)方法來獲取單例的EF數(shù)據(jù)操縱上下文對(duì)象,以保證每個(gè)用戶訪問時(shí)只有使用一個(gè)上下文對(duì)象對(duì)數(shù)據(jù)庫進(jìn)行操作。

DbContextFactory.cs

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

namespace PMS.DAL
{
  public class DbContextFactory
  {
    /// <summary>
    /// 負(fù)責(zé)創(chuàng)建EF數(shù)據(jù)操作上下文實(shí)例,必須保證線程內(nèi)唯一
    /// </summary>
    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類創(chuàng)建DAL層,實(shí)現(xiàn)查詢、分頁查詢、增加、刪除和修改這五個(gè)基本的方法:

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();

    /// <summary>
    /// 查詢過濾
    /// </summary>
    /// <param name="whereLamada">過濾條件Lambda表達(dá)式</param>
    /// <returns>實(shí)體集合</returns>
    public IQueryable<UserDal> LoadEntities(System.Linq.Expressions.Expression<Func<UserDal, bool>> whereLamada)
    {
      return DbEntities.Set<UserDal>().Where(whereLamada);
    }

    /// <summary>
    /// 分頁查詢
    /// </summary>
    /// <typeparam name="TS">排序類型</typeparam>
    /// <param name="pageIndex">查詢的頁碼</param>
    /// <param name="pageSize">每頁顯示的數(shù)目</param>
    /// <param name="totalCount">符合條件的總行數(shù)</param>
    /// <param name="whereLambda">過濾條件Lambda表達(dá)式</param>
    /// <param name="orderbyLambda">排序Lambda表達(dá)式</param>
    /// <param name="isAsc">排序方向</param>
    /// <returns>實(shí)體集合</returns>
    public IQueryable<UserDal> LoadPageEntities<TS>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<UserDal, bool>> whereLambda, System.Linq.Expressions.Expression<Func<UserDal, TS>> orderbyLambda, bool isAsc)
    {
      var temp = DbEntities.Set<UserDal>().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;
    }

    /// <summary>
    /// 刪除數(shù)據(jù)
    /// </summary>
    /// <param name="entity">待刪數(shù)據(jù)</param>
    /// <returns>刪除結(jié)果</returns>
    public bool DeleteEntity(UserDal entity)
    {
      DbEntities.Entry(entity).State = EntityState.Deleted;
      return true;
    }

    /// <summary>
    /// 編輯數(shù)據(jù)
    /// </summary>
    /// <param name="entity">待編輯數(shù)據(jù)</param>
    /// <returns>編輯結(jié)果</returns>
    public bool EditEntity(UserDal entity)
    {
      DbEntities.Entry(entity).State = EntityState.Modified;
      return true;
    }

    /// <summary>
    /// 添加數(shù)據(jù)
    /// </summary>
    /// <param name="entity">待添加數(shù)據(jù)</param>
    /// <returns>已添加數(shù)據(jù)</returns>
    public UserDal AddEntity(UserDal entity)
    {
      entity = DbEntities.Set<UserDal>().Add(entity);
      return entity;
    }    
  }
}

注:這里的增刪改操作并不即時(shí)進(jìn)行,而是在封裝在數(shù)據(jù)會(huì)話層中,以實(shí)現(xiàn)工作單元模式,提高數(shù)據(jù)庫的操作效率。

考慮到每個(gè)類都需要實(shí)現(xiàn)相同的數(shù)據(jù)操作,我們可以將以上方法封裝到一個(gè)泛型基類中,各類型只需要繼承泛型基類就可以實(shí)現(xiàn)以上方法:

BaseDal.cs

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

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

    /// <summary>
    /// 查詢過濾
    /// </summary>
    /// <param name="whereLamada">過濾條件Lambda表達(dá)式</param>
    /// <returns>實(shí)體集合</returns>
    public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLamada)
    {
      return DbEntities.Set<T>().Where(whereLamada);
    }

    /// <summary>
    /// 分頁查詢
    /// </summary>
    /// <typeparam name="TS">排序類型</typeparam>
    /// <param name="pageIndex">查詢的頁碼</param>
    /// <param name="pageSize">每頁顯示的數(shù)目</param>
    /// <param name="totalCount">符合條件的總行數(shù)</param>
    /// <param name="whereLambda">過濾條件Lambda表達(dá)式</param>
    /// <param name="orderbyLambda">排序Lambda表達(dá)式</param>
    /// <param name="isAsc">排序方向</param>
    /// <returns>實(shí)體集合</returns>
    public IQueryable<T> LoadPageEntities<TS>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, TS>> orderbyLambda, bool isAsc)
    {
      var temp = DbEntities.Set<T>().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;
    }

    /// <summary>
    /// 刪除數(shù)據(jù)
    /// </summary>
    /// <param name="entity">待刪數(shù)據(jù)</param>
    /// <returns>刪除結(jié)果</returns>
    public bool DeleteEntity(T entity)
    {
      DbEntities.Entry(entity).State = EntityState.Deleted;
      return true;
    }

    /// <summary>
    /// 編輯數(shù)據(jù)
    /// </summary>
    /// <param name="entity">待編輯數(shù)據(jù)</param>
    /// <returns>編輯結(jié)果</returns>
    public bool EditEntity(T entity)
    {
      DbEntities.Entry(entity).State = EntityState.Modified;
      return true;
    }

    /// <summary>
    /// 添加數(shù)據(jù)
    /// </summary>
    /// <param name="entity">待添加數(shù)據(jù)</param>
    /// <returns>已添加數(shù)據(jù)</returns>
    public T AddEntity(T entity)
    {
      entity = DbEntities.Set<T>().Add(entity);
      //DbEntities.SaveChanges();
      return entity;
    }
  }
}

UserDal繼承BaseDal

using PMS.IDAL;
using PMS.Model;

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

數(shù)據(jù)訪問接口層的構(gòu)建

然后我們建立相應(yīng)的IbaseDal接口和IUserDal接口,并且使UserDal類實(shí)現(xiàn)IUserDal接口

IBaseDal:

using System;
using System.Linq;

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

    IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount,
      System.Linq.Expressions.Expression<Func<T, bool>> whereLambda,
      System.Linq.Expressions.Expression<Func<T, s>> 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<User>
  {

  }
}

UserDal實(shí)現(xiàn)IUserDal接口:

public partial class UserDal : BaseDal<User>,IUserDal

數(shù)據(jù)會(huì)話層的構(gòu)建

抽象工廠類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"];
    /// <summary>
    /// 獲取UserDal的實(shí)例
    /// </summary>
    /// <returns></returns>
    public static IUserDal CreateUserInfoDal()
    {
      var fullClassName = NameSpace + ".UserInfoDal";
      return CreateInstance(fullClassName) as IUserDal;
    }
    /// <summary>
    /// 通過反射獲得程序集中某類型的實(shí)例
    /// </summary>
    /// <param name="className"></param>
    /// <returns></returns>
    private static object CreateInstance(string className)
    {
      var assembly = Assembly.Load(AssemblyPath);
      return assembly.CreateInstance(className);
    }
  }
}

數(shù)據(jù)會(huì)話類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; }
    }

    /// <summary>
    /// 工作單元模式,統(tǒng)一保存數(shù)據(jù)
    /// </summary>
    /// <returns></returns>
    public bool SaveChanges()
    {
      return Db.SaveChanges() > 0;
    }
  }
}

業(yè)務(wù)邏輯層的構(gòu)建

業(yè)務(wù)類基類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();//子類一定要實(shí)現(xiàn)抽象方法,以指明當(dāng)前類的子類類型。
    }

    /// <summary>
    /// 查詢過濾
    /// </summary>
    /// <param name="whereLambda"></param>
    /// <returns></returns>
    public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
    {
      return CurrentDal.LoadEntities(whereLambda);
    }

    /// <summary>
    /// 分頁
    /// </summary>
    /// <typeparam name="s"></typeparam>
    /// <param name="pageIndex"></param>
    /// <param name="pageSize"></param>
    /// <param name="totalCount"></param>
    /// <param name="whereLambda"></param>
    /// <param name="orderbyLambda"></param>
    /// <param name="isAsc"></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 name="entity"></param>
    /// <returns></returns>
    public bool DeleteEntity(T entity)
    {
      CurrentDal.DeleteEntity(entity);
      return CurrentDbSession.SaveChanges();
    }

    /// <summary>
    /// 編輯
    /// </summary>
    /// <param name="entity"></param>
    /// <returns></returns>
    public bool EditEntity(T entity)
    {
      CurrentDal.EditEntity(entity);
      return CurrentDbSession.SaveChanges();
    }

    /// <summary>
    /// 添加數(shù)據(jù)
    /// </summary>
    /// <param name="entity"></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;
    }
  }
}

 

業(yè)務(wù)邏輯接口層的構(gòu)建

直接建立對(duì)應(yīng)的接口并使用UserService類實(shí)現(xiàn)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類實(shí)現(xiàn)IUserService接口:

public partial class UserService : BaseService<User>, IUserService  

以上我們就完成了整個(gè)框架中關(guān)于User類的各層次的實(shí)現(xiàn)。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Asp.Net MVC中配置Serilog的方法

    Asp.Net MVC中配置Serilog的方法

    Serilog是一款比較優(yōu)秀的logging framework,Serilog只支持.NET 4.5以上的版本。下面這篇文章將會(huì)通過圖文及示例代碼的形式給大家介紹Asp.Net MVC中配置Serilog的方法,有需要的朋友們可以參考借鑒,下面來跟著小編一起學(xué)習(xí)學(xué)習(xí)吧。
    2016-12-12
  • C# javaScript函數(shù)的相互調(diào)用

    C# javaScript函數(shù)的相互調(diào)用

    如何在JavaScript訪問C#函數(shù),如何在C#中訪問JavaScript的已有變量等實(shí)現(xiàn)方法
    2008-12-12
  • 詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理

    詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理

    這篇文章主要介紹了詳解ASP.NET MVC下的異步Action的定義和執(zhí)行原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-12-12
  • asp.net core分塊上傳文件示例

    asp.net core分塊上傳文件示例

    這篇文章主要介紹了asp.net core分塊上傳文件示例,這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02
  • ASP.NET也像WinForm程序一樣運(yùn)行的實(shí)現(xiàn)方法

    ASP.NET也像WinForm程序一樣運(yùn)行的實(shí)現(xiàn)方法

    我們今天要談到的是讓ASP.NET的程序也像WinForm一樣的運(yùn)行,這樣就不需要安裝IIS或者Visual Studio這樣的特定環(huán)境了
    2012-01-01
  • c#.NET 寫txt文件小例子

    c#.NET 寫txt文件小例子

    在.NET里,有時(shí)要往TXT文件里寫內(nèi)容,其實(shí)很簡(jiǎn)單。
    2013-06-06
  • asp.net 防止SQL注入攻擊

    asp.net 防止SQL注入攻擊

    asp.net網(wǎng)站防止SQL注入攻擊,通常的辦法是每個(gè)文件都修改加入過濾代碼,這樣很麻煩,下面介紹一種辦法,可以從整個(gè)網(wǎng)站防止注入。
    2009-06-06
  • ASP.NET MVC中使用jQuery時(shí)的瀏覽器緩存問題詳解

    ASP.NET MVC中使用jQuery時(shí)的瀏覽器緩存問題詳解

    這篇文章主要介紹了ASP.NET MVC中使用jQuery時(shí)的瀏覽器緩存問題詳解,需要的朋友可以參考下。
    2016-06-06
  • Entity Framework Core更新時(shí)間映射

    Entity Framework Core更新時(shí)間映射

    這篇文章介紹了Entity Framework Core更新時(shí)間映射的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • .Net Core官方JWT授權(quán)驗(yàn)證的全過程

    .Net Core官方JWT授權(quán)驗(yàn)證的全過程

    這篇文章主要給大家介紹了關(guān)于.Net Core官方JWT授權(quán)驗(yàn)證的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12

最新評(píng)論