ASP.NET?MVC5網(wǎng)站開發(fā)之業(yè)務(wù)邏輯層的架構(gòu)和基本功能(四)
業(yè)務(wù)邏輯層在Ninesky.Core中實現(xiàn),主要功能封裝一些方法通過調(diào)用數(shù)據(jù)存儲層,向界面層提供服務(wù)。
一、業(yè)務(wù)邏輯層的架構(gòu)
Ninesky.Core包含三個命名空間Ninesky.Core、Ninesky.Core.Types、Ninesky.Core.General.
Ninesky.Core包含模型和功能實現(xiàn),Ninesky.Core.Types是項目用到的一些類型的定義,Ninesky.Core.General是項目用到的一些方法的定義。
1、Ninesky.Core命名空間的結(jié)構(gòu)
NineskyContext-數(shù)據(jù)上下文
ContextFactory- 獲取數(shù)據(jù)上下文的工廠類
BaseManager-基礎(chǔ)類,實現(xiàn)了一些常用數(shù)據(jù)訪問方法,提供其他管理類繼承。
Category-欄目模型。
CategoryManager-欄目管理類。
Content-內(nèi)容模型。
ContentManager-內(nèi)容管理類。
User-用戶模型
UserManager-用戶管理類
Administrator-管理員類
AdministratorManager-管理員管理類
2、Ninesky.Core.Types命名空間的結(jié)構(gòu)
Response 響應(yīng)返回類。
Paging<T> 分頁數(shù)據(jù)類。
二、基礎(chǔ)功能的實現(xiàn)
1、添加引用
(1)、添加EntityFramewok 引用
Ninesky.Core項目->引用【右鍵】 –>管理NuGet程序包
在NuGet包管理對器話框中選擇 EntityFramewok 并安裝。
(2)、添加Ninesky.DataLibrary項目的引用
Ninesky.Core項目->引用【右鍵】 –>添加引用
在引用管理器中選擇 項目->解決方案->Ninesky.DataLibrary,點擊確定。
2、NineskyContext類
NineskyContext類是項目的數(shù)據(jù)數(shù)據(jù)上下文,使模型和數(shù)據(jù)庫的表進行對應(yīng)。
Ninesky.Core項目【右鍵】->添加->類, 輸入類名NineskyContext。
在類中引入命名空間System.Data.Entity;
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Entity; namespace Ninesky.Core { public class NineskyContext:DbContext { public NineskyContext():base("DefaultConnection") { Database.SetInitializer<NineskyContext>(new CreateDatabaseIfNotExists<NineskyContext>()); } } }
3、ContextFactory類
ContextFactory是一個簡單工廠類,CurrentContext()是一個靜態(tài)函數(shù),用來獲取當前線程DbContext。
Ninesky.Core項目【右鍵】->添加->類, 輸入類名ContextFactory。
在類中添加對System.Runtime.Remoting.Messaging的引用。在類中實現(xiàn)CurrentContext()靜態(tài)方法返回數(shù)據(jù)上下文NineskyContext。方法中通過CallContext類在線程中存儲NineskyContext。
using System.Runtime.Remoting.Messaging; namespace Ninesky.Core { /// <summary> /// 數(shù)據(jù)上下文工廠 /// </summary> public class ContextFactory { /// <summary> /// 獲取當前線程的數(shù)據(jù)上下文 /// </summary> /// <returns>數(shù)據(jù)上下文</returns> public static NineskyContext CurrentContext() { NineskyContext _nContext = CallContext.GetData("NineskyContext") as NineskyContext; if (_nContext == null) { _nContext = new NineskyContext(); CallContext.SetData("NineskyContext", _nContext); } return _nContext; } } }
4、Response類
Response類是一個常用的方法返回數(shù)據(jù)類型,包含返回代碼、返回消息和返回數(shù)據(jù)3個屬性。
在Ninesky.Core項目[右鍵]新建文件夾,輸入名稱Types。
在Types文件夾[右鍵]->添加->類,在彈出的添加新項對話框中輸入類名Response。代碼如下:
namespace Ninesky.Core.Types { /// <summary> /// /// </summary> public class Response { /// <summary> /// 返回代碼. 0-失敗,1-成功,其他-具體見方法返回值說明 /// </summary> public int Code { get; set; } /// <summary> /// 返回消息 /// </summary> public string Message { get; set; } /// <summary> /// 返回數(shù)據(jù) /// </summary> public dynamic Data { get; set; } public Response() { Code = 0; } } }
5、Paging<T>類
Paging<T>類是一個查詢分頁數(shù)據(jù)時使用的類,包含當前頁、每頁記錄數(shù)、總記錄數(shù)、和當前頁數(shù)據(jù)列表等幾個屬性。
在Types文件夾[右鍵]->添加->類,在彈出的添加新項對話框中輸入類名Paging。代碼如下:
using System.Collections.Generic; namespace Ninesky.Core.Types { public class Paging<T> { /// <summary> /// 當前頁。從1計數(shù) /// </summary> public int PageIndex { get; set; } /// <summary> /// 每頁記錄數(shù)。默認20 /// </summary> public int PageSize { get; set; } /// <summary> /// 總記錄數(shù) /// </summary> public int TotalNumber;/// <summary> /// 當前頁記錄列表 /// </summary> public List<T> Items { get; set; } public Paging() { PageIndex = 1; PageSize = 20; } } }
6、BaseManager類
BaseManager類是所有管理類的基類,此類包含了管理類的常用方法。
將Ninesky.Core項目的Class1.cs重命名為BaseManager.cs
引入命名空間System.Data.Entity和Ninesky.Core.Types,實現(xiàn)共有方法。
using Ninesky.Core.Types; using Ninesky.DataLibrary; using System.Data.Entity; using System.Linq; namespace Ninesky.Core { /// <summary> /// 管理類的基類 /// </summary> /// <typeparam name="T">模型類</typeparam> public abstract class BaseManager<T> where T :class { /// <summary> /// 數(shù)據(jù)倉儲類 /// </summary> protected Repository<T> Repository; /// <summary> /// 默認構(gòu)造函數(shù) /// </summary> public BaseManager():this(ContextFactory.CurrentContext()) { } /// <summary> /// 構(gòu)造函數(shù) /// </summary> /// <param name="dbContext">數(shù)據(jù)上下文</param> public BaseManager(DbContext dbContext){ Repository = new Repository<T>(dbContext); } /// <summary> /// 添加 /// </summary> /// <param name="entity">實體數(shù)據(jù)</param> /// <returns>成功時屬性【Data】為添加后的數(shù)據(jù)實體</returns> public virtual Response Add(T entity) { Response _response = new Response(); if(Repository.Add(entity)>0) { _response.Code = 1; _response.Message = "添加數(shù)據(jù)成功!"; _response.Data = entity; } else { _response.Code = 0; _response.Message = "添加數(shù)據(jù)失?。?; } return _response; } /// <summary> /// 更新 /// </summary> /// <param name="entity">實體數(shù)據(jù)</param> /// <returns>成功時屬性【Data】為更新后的數(shù)據(jù)實體</returns> public virtual Response Update(T entity) { Response _response = new Response(); if (Repository.Update(entity) > 0) { _response.Code = 1; _response.Message = "更新數(shù)據(jù)成功!"; _response.Data = entity; } else { _response.Code = 0; _response.Message = "更新數(shù)據(jù)失??!"; } return _response; } /// <summary> /// 刪除 /// </summary> /// <param name="ID">主鍵</param> /// <returns>Code:0-刪除失敗;1-刪除陳功;10-記錄不存在</returns> public virtual Response Delete(int ID) { Response _response = new Response(); var _entity = Find(ID); if (_entity == null) { _response.Code = 10; _response.Message = "記錄不存在!"; } else { if (Repository.Delete(_entity) > 0) { _response.Code = 1; _response.Message = "刪除數(shù)據(jù)成功!"; } else { _response.Code = 0; _response.Message = "刪除數(shù)據(jù)失??!"; } } return _response; } /// <summary> /// 查找實體 /// </summary> /// <param name="ID">主鍵</param> /// <returns>實體</returns> public virtual T Find(int ID) { return Repository.Find(ID); } /// <summary> /// 查找數(shù)據(jù)列表-【所有數(shù)據(jù)】 /// </summary> /// <returns>所有數(shù)據(jù)</returns> public IQueryable<T> FindList() { return Repository.FindList(); } /// <summary> /// 查找分頁數(shù)據(jù) /// </summary> /// <param name="paging">分頁數(shù)據(jù)</param> /// <returns>分頁數(shù)據(jù)</returns> public Paging<T> FindPageList(Paging<T> paging) { paging.Items = Repository.FindPageList(paging.PageSize, paging.PageIndex, out paging.TotalNumber).ToList(); return paging; } /// <summary> /// 總記錄數(shù) /// </summary> /// <returns>總記錄數(shù)</returns> public virtual int Count() { return Repository.Count(); } } }
代碼下載:https://ninesky.codeplex.com 點擊SOURCE CODE 點擊Download下載源文件。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- java學生信息管理系統(tǒng)MVC架構(gòu)詳解
- SpringMVC架構(gòu)的項目 js,css等靜態(tài)文件導(dǎo)入有問題的解決方法
- thinkPHP5.0框架整體架構(gòu)總覽【應(yīng)用,模塊,MVC,驅(qū)動,行為,命名空間等】
- SpringMVC互聯(lián)網(wǎng)軟件架構(gòu)REST使用詳解
- mvc架構(gòu)實現(xiàn)商品的購買(二)
- ASP.NET MVC5網(wǎng)站開發(fā)之展示層架構(gòu)(五)
- ssi框架學習總結(jié)(mvc三層架構(gòu))
- PHP MVC模式在網(wǎng)站架構(gòu)中的實現(xiàn)分析
- MayFish PHP的MVC架構(gòu)的開發(fā)框架
- SpringMVC MVC架構(gòu)原理及實現(xiàn)方法詳解
相關(guān)文章
在asp.net(C#)中采用自定義標簽和XML、XSL顯示數(shù)據(jù)
在asp.net(C#)中采用自定義標簽和XML、XSL顯示數(shù)據(jù)的實現(xiàn)代碼。2009-06-06詳解.net core webapi 前后端開發(fā)分離后的配置和部署
這篇文章主要介紹了.net core webapi 前后端開發(fā)分離后的配置和部署,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04this connector is disabled錯誤的解決方法
打開editor/filemanager/connectors/aspx/config.ascx修改CheckAuthentication()方法,返回true2008-11-11如何創(chuàng)建一個AJAXControlToolKit的擴展控件
相信熟悉Microsoft提供的AJAXControlToolKit的朋友已經(jīng)感覺到它的強大了。但是如果我們需要其它一些控件,或者是我們碰到一些很好的javascript然后需要把它們整合到ajaxcontroltoolkit中,如何來做。???2009-08-08詳解.Net Core + Angular2 環(huán)境搭建
這篇文章主要介紹了詳解.Net Core + Angular2 環(huán)境搭建,具有一定的參考價值,有興趣的可以了解一下。2016-12-12asp.net下Oracle,SQL Server,Access萬能數(shù)據(jù)庫通用類
Oracle,SQL Server,Access萬能數(shù)據(jù)庫通用類!,使用asp.net開發(fā)多數(shù)據(jù)庫系統(tǒng)的朋友可以參考下。2010-10-10進度條在.net導(dǎo)入Excel時的應(yīng)用實例
這篇文章主要介紹了進度條在.net導(dǎo)入Excel時的應(yīng)用,以實例形式講述了.net導(dǎo)入Excel時根據(jù)頁面情況顯示進度條的實現(xiàn)方法,非常具有實用價值,需要的朋友可以參考下2014-10-10Visual Studio 2017開發(fā)環(huán)境的安裝圖文教程
Visual Studio 2017是微軟于2017年3月8日正式推出的新版本,是迄今為止 最具生產(chǎn)力 的 Visual Studio 版本。這篇文章主要介紹了Visual Studio 2017開發(fā)環(huán)境的安裝,需要的朋友可以參考下2017-11-11