最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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à)值,感興趣的小伙伴們可以參考一下

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

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

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

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

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

DALFactory:數(shù)據(jù)會(huì)話層,封裝了所有數(shù)據(jù)操作類實(shí)例的創(chuàng)建,將數(shù)據(jù)訪問(wèn)層與業(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:通用層,用來(lái)存放一些工具類。

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

模型層的構(gòu)建

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

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

      DAL層中,我們首先需要一個(gè)方法來(lái)獲取單例的EF數(shù)據(jù)操縱上下文對(duì)象,以保證每個(gè)用戶訪問(wèn)時(shí)只有使用一個(gè)上下文對(duì)象對(duì)數(shù)據(jù)庫(kù)進(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)查詢、分頁(yè)查詢、增加、刪除和修改這五個(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>
    /// 查詢過(guò)濾
    /// </summary>
    /// <param name="whereLamada">過(guò)濾條件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>
    /// 分頁(yè)查詢
    /// </summary>
    /// <typeparam name="TS">排序類型</typeparam>
    /// <param name="pageIndex">查詢的頁(yè)碼</param>
    /// <param name="pageSize">每頁(yè)顯示的數(shù)目</param>
    /// <param name="totalCount">符合條件的總行數(shù)</param>
    /// <param name="whereLambda">過(guò)濾條件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ù)庫(kù)的操作效率。

考慮到每個(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>
    /// 查詢過(guò)濾
    /// </summary>
    /// <param name="whereLamada">過(guò)濾條件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>
    /// 分頁(yè)查詢
    /// </summary>
    /// <typeparam name="TS">排序類型</typeparam>
    /// <param name="pageIndex">查詢的頁(yè)碼</param>
    /// <param name="pageSize">每頁(yè)顯示的數(shù)目</param>
    /// <param name="totalCount">符合條件的總行數(shù)</param>
    /// <param name="whereLambda">過(guò)濾條件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ù)訪問(wèn)接口層的構(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>
    /// 通過(guò)反射獲得程序集中某類型的實(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>
    /// 查詢過(guò)濾
    /// </summary>
    /// <param name="whereLambda"></param>
    /// <returns></returns>
    public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
    {
      return CurrentDal.LoadEntities(whereLambda);
    }

    /// <summary>
    /// 分頁(yè)
    /// </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下ajax.ajaxMethod使用方法

    asp.net下ajax.ajaxMethod使用方法

    使用AjaxMethod可以在客戶端異步調(diào)用服務(wù)端方法,簡(jiǎn)單地說(shuō)就是在JS里調(diào)用后臺(tái).cs文件里的方法,做一些JS無(wú)法做到的操作,如查詢數(shù)據(jù)庫(kù)
    2011-10-10
  • ASP.NET MVC對(duì)URL匹配操作

    ASP.NET MVC對(duì)URL匹配操作

    這篇文章介紹了ASP.NET MVC對(duì)URL匹配操作的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • 十分鐘搞定多圖片/文件服務(wù)器

    十分鐘搞定多圖片/文件服務(wù)器

    圖片/文件服務(wù)器,顧名思義就是存文件唄,有的人用阿里云的現(xiàn)有服務(wù),有的把文件Post到文件服務(wù)器,在文件服務(wù)器一端用一個(gè)應(yīng)用程序來(lái)接收并保存,方法各不相同。下面跟著小編一起來(lái)看下吧
    2017-01-01
  • ASP.NET TreeView讀取數(shù)據(jù)庫(kù)實(shí)例

    ASP.NET TreeView讀取數(shù)據(jù)庫(kù)實(shí)例

    這篇文章主要介紹了ASP.NET TreeView讀取數(shù)據(jù)庫(kù)實(shí)例,有需要的朋友可以參考一下
    2013-11-11
  • .NET 6開(kāi)發(fā)TodoList應(yīng)用實(shí)現(xiàn)結(jié)構(gòu)搭建

    .NET 6開(kāi)發(fā)TodoList應(yīng)用實(shí)現(xiàn)結(jié)構(gòu)搭建

    這篇文章主要介紹了.NET 6開(kāi)發(fā)TodoList應(yīng)用實(shí)現(xiàn)結(jié)構(gòu)搭建,上一篇我們講解了實(shí)現(xiàn)系列背景 ,今天繼續(xù)來(lái)講講.NET 6開(kāi)發(fā)TodoList并且實(shí)現(xiàn)結(jié)構(gòu)搭建,更多詳細(xì)內(nèi)容剛興趣得小伙伴可以來(lái)參考一下下面文章得具體內(nèi)容
    2021-12-12
  • Visual Studio 2017創(chuàng)建.net standard類庫(kù)編譯出錯(cuò)原因及解決方法

    Visual Studio 2017創(chuàng)建.net standard類庫(kù)編譯出錯(cuò)原因及解決方法

    這篇文章主要為大家詳細(xì)介紹了Visual Studio 2017創(chuàng)建.net standard類庫(kù)編譯出錯(cuò)原因及解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • ADO調(diào)用分頁(yè)查詢存儲(chǔ)過(guò)程的實(shí)例講解

    ADO調(diào)用分頁(yè)查詢存儲(chǔ)過(guò)程的實(shí)例講解

    下面小編就為大家分享一篇ADO調(diào)用分頁(yè)查詢存儲(chǔ)過(guò)程的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2017-12-12
  • .NET中的異常和異常處理用法分析

    .NET中的異常和異常處理用法分析

    這篇文章主要介紹了.NET中的異常和異常處理用法,分析了.NET中的異常處理機(jī)制以及相關(guān)注意事項(xiàng),具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01
  • ASP.NET MVC+EF框架+EasyUI實(shí)現(xiàn)權(quán)限管系列

    ASP.NET MVC+EF框架+EasyUI實(shí)現(xiàn)權(quán)限管系列

    在學(xué)習(xí)MVC之前,我們有必要知道這些知識(shí)點(diǎn)(自動(dòng)屬性,隱式類型var,對(duì)象初始化器和集合初始化器,匿名類,擴(kuò)展方法,Lambda表達(dá)式),如果你還不知道的話就請(qǐng)看我下面的簡(jiǎn)單的介紹,看下面我建立的項(xiàng)目的初步圖像,然后下篇我們開(kāi)始簡(jiǎn)單的介紹。
    2014-11-11

最新評(píng)論

疏勒县| 永福县| 大化| 蓝山县| 武陟县| 张掖市| 兰溪市| 新昌县| 凉城县| 灵武市| 临泽县| 拜城县| 宣化县| 禄丰县| 四会市| 昆明市| 隆昌县| 浠水县| 攀枝花市| 南丹县| 渝中区| 咸宁市| 蒲江县| 革吉县| 宝坻区| 罗城| 弋阳县| 二连浩特市| 五莲县| 荣成市| 高陵县| 怀远县| 公主岭市| 丰原市| 墨脱县| 夹江县| 内乡县| 嘉祥县| 巍山| 客服| 会同县|