MVC5 網站開發之四 業務邏輯層的架構和基本功能

業務邏輯層在Ninesky.Core中實現,主要功能封裝一些方法經過調用數據存儲層,向界面層提供服務。html

 

目錄數據庫

奔跑吧,代碼小哥!架構

MVC5網站開發之一 整體概述函數

MVC5 網站開發之二 建立項目網站

MVC5 網站開發之三 數據存儲層功能實現this

MVC5 網站開發之四 業務邏輯層的架構和基本功能spa

MVC5 網站開發之五 展現層架構線程

MVC5 網站開發之六 管理員 一、登陸、驗證和註銷3d

MVC5 網站開發之六 管理員 二、添加、刪除、重置密碼、修改密碼、列表瀏覽code

MVC5 網站開發之七 用戶功能 一、角色的後臺管理

 

1、業務邏輯層的架構

image

Ninesky.Core包含三個命名空間Ninesky.Core、Ninesky.Core.Types、Ninesky.Core.General.

Ninesky.Core包含模型和功能實現,Ninesky.Core.Types是項目用到的一些類型的定義,Ninesky.Core.General是項目用到的一些方法的定義。

一、Ninesky.Core命名空間的結構

image

 

NineskyContext-數據上下文

ContextFactory- 獲取數據上下文的工廠類 

BaseManager-基礎類,實現了一些經常使用數據訪問方法,提供其餘管理類繼承。

Category-欄目模型。

CategoryManager-欄目管理類。

Content-內容模型。

ContentManager-內容管理類。

User-用戶模型

UserManager-用戶管理類

Administrator-管理員類

AdministratorManager-管理員管理類

 

二、Ninesky.Core.Types命名空間的結構

image

Response 響應返回類。

Paging<T> 分頁數據類。

2、基礎功能的實現

一、添加引用

(1)、添加EntityFramewok 引用

 

 

 

 

 

image

Ninesky.Core項目->引用【右鍵】 –>管理NuGet程序包

image

NuGet包管理對器話框中選擇 EntityFramewok 並安裝。

(2)、添加Ninesky.DataLibrary項目的引用

image

Ninesky.Core項目->引用【右鍵】 –>添加引用

image

在引用管理器中選擇 項目->解決方案->Ninesky.DataLibrary,點擊肯定。

二、NineskyContext類

NineskyContext類是項目的數據數據上下文,使模型和數據庫的表進行對應。

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

三、ContextFactory類

ContextFactory是一個簡單工廠類,CurrentContext()是一個靜態函數,用來獲取當前線程DbContext。

Ninesky.Core項目【右鍵】->添加->, 輸入類名ContextFactory

在類中添加對System.Runtime.Remoting.Messaging的引用。在類中實現CurrentContext()靜態方法返回數據上下文NineskyContext。方法中經過CallContext類在線程中存儲NineskyContext。

using System.Runtime.Remoting.Messaging;

namespace Ninesky.Core
{
    /// <summary>
    /// 數據上下文工廠
    /// </summary>
    public class ContextFactory
    {
        /// <summary>
        /// 獲取當前線程的數據上下文
        /// </summary>
        /// <returns>數據上下文</returns>
        public static NineskyContext CurrentContext()
        {
            NineskyContext _nContext = CallContext.GetData("NineskyContext") as NineskyContext;
            if (_nContext == null)
            {
                _nContext = new NineskyContext();
                CallContext.SetData("NineskyContext", _nContext);
            }
            return _nContext;
        }
    }
}

四、Response類

Response類是一個經常使用的方法返回數據類型,包含返回代碼、返回消息和返回數據3個屬性。

image

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>
        /// 返回數據
        /// </summary>
        public dynamic Data { get; set; }

        public Response()
        {
            Code = 0;
        }
    }
}

五、Paging<T>類

Paging<T>類是一個查詢分頁數據時使用的類,包含當前頁、每頁記錄數、總記錄數、和當前頁數據列表等幾個屬性。

image

Types文件夾[右鍵]->添加->,在彈出的添加新項對話框中輸入類名Paging。代碼以下:

using System.Collections.Generic;

namespace Ninesky.Core.Types
{
    public class Paging<T>
    {
        /// <summary>
        /// 當前頁。從1計數
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 每頁記錄數。默認20
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 總記錄數
        /// </summary>
        public int TotalNumber;/// <summary>
        /// 當前頁記錄列表
        /// </summary>
        public List<T> Items { get; set; }
        
        public Paging()
        {
            PageIndex = 1;
            PageSize = 20;
        }
    }
}

六、BaseManager

BaseManager類是全部管理類的基類,此類包含了管理類的經常使用方法。

image

Ninesky.Core項目的Class1.cs重命名爲BaseManager.cs

引入命名空間System.Data.EntityNinesky.Core.Types,實現共有方法。

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>
        /// 數據倉儲類
        /// </summary>
        protected Repository<T> Repository;

        /// <summary>
        /// 默認構造函數
        /// </summary>
        public BaseManager():this(ContextFactory.CurrentContext())
        {
        }
        /// <summary>
        /// 構造函數
        /// </summary>
        /// <param name="dbContext">數據上下文</param>
        public BaseManager(DbContext dbContext){
            Repository = new Repository<T>(dbContext);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity">實體數據</param>
        /// <returns>成功時屬性【Data】爲添加後的數據實體</returns>
        public virtual Response Add(T entity)
        {
            Response _response = new Response();
            if(Repository.Add(entity)>0)
            {
                _response.Code = 1;
                _response.Message = "添加數據成功!";
                _response.Data = entity;
            }
            else
            {
                _response.Code = 0;
                _response.Message = "添加數據失敗!";
            }

            return _response;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">實體數據</param>
        /// <returns>成功時屬性【Data】爲更新後的數據實體</returns>
        public virtual Response Update(T entity)
        {
            Response _response = new Response();
            if (Repository.Update(entity) > 0)
            {
                _response.Code = 1;
                _response.Message = "更新數據成功!";
                _response.Data = entity;
            }
            else
            {
                _response.Code = 0;
                _response.Message = "更新數據失敗!";
            }

            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 = "刪除數據成功!";
                }
                else
                {
                    _response.Code = 0;
                    _response.Message = "刪除數據失敗!";
                }
            }
            

            return _response;
        }

        /// <summary>
        /// 查找實體
        /// </summary>
        /// <param name="ID">主鍵</param>
        /// <returns>實體</returns>
        public virtual T Find(int ID)
        {
            return Repository.Find(ID);
        }

        /// <summary>
        /// 查找數據列表-【全部數據】
        /// </summary>
        /// <returns>全部數據</returns>
        public IQueryable<T> FindList()
        {
            return Repository.FindList();
        }

        /// <summary>
        /// 查找分頁數據
        /// </summary>
        /// <param name="paging">分頁數據</param>
        /// <returns>分頁數據</returns>
        public Paging<T> FindPageList(Paging<T> paging)
        {
            paging.Items = Repository.FindPageList(paging.PageSize, paging.PageIndex, out paging.TotalNumber).ToList();
            return paging;
        }

        /// <summary>
        /// 總記錄數
        /// </summary>
        /// <returns>總記錄數</returns>
        public virtual int Count()
        {
            return Repository.Count();
        }
    }
}

=====================================

代碼見:https://ninesky.codeplex.com/SourceControl/latest

代碼下載:https://ninesky.codeplex.com 點擊SOURCE CODE 點擊Download下載源文件。

相關文章
相關標籤/搜索