文章出處

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

 

目錄

奔跑吧,代碼小哥!

MVC5網站開發之一 總體概述

MVC5 網站開發之二 創建項目

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

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

MVC5 網站開發之五 展示層架構

MVC5 網站開發之六 管理員 1、登錄、驗證和注銷

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

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

 

一、業務邏輯層的架構

image

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

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

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

image

 

NineskyContext-數據上下文

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

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

Category-欄目模型。

CategoryManager-欄目管理類。

Content-內容模型。

ContentManager-內容管理類。

User-用戶模型

UserManager-用戶管理類

Administrator-管理員類

AdministratorManager-管理員管理類

 

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

image

Response 響應返回類。

Paging<T> 分頁數據類。

二、基礎功能的實現

1、添加引用

(1)、添加EntityFramewok 引用

 

 

 

 

 

image

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

image

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

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

image

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

image

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

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

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

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

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

6、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下載源文件。


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()