業務邏輯層在Ninesky.Core中實現,主要功能封裝一些方法通過調用數據存儲層,向界面層提供服務。
一、業務邏輯層的架構
Ninesky.Core包含三個命名空間Ninesky.Core、Ninesky.Core.Types、Ninesky.Core.General.
Ninesky.Core包含模型和功能實現,Ninesky.Core.Types是項目用到的一些類型的定義,Ninesky.Core.General是項目用到的一些方法的定義。
1、Ninesky.Core命名空間的結構
NineskyContext-數據上下文
ContextFactory- 獲取數據上下文的工廠類
BaseManager-基礎類,實現了一些常用數據訪問方法,提供其他管理類繼承。
Category-欄目模型。
CategoryManager-欄目管理類。
Content-內容模型。
ContentManager-內容管理類。
User-用戶模型
UserManager-用戶管理類
Administrator-管理員類
AdministratorManager-管理員管理類
2、Ninesky.Core.Types命名空間的結構
Response 響應返回類。
Paging<T> 分頁數據類。
二、基礎功能的實現
1、添加引用
(1)、添加EntityFramewok 引用
Ninesky.Core項目->引用【右鍵】 –>管理NuGet程序包
在NuGet包管理對器話框中選擇 EntityFramewok 并安裝。
(2)、添加Ninesky.DataLibrary項目的引用
Ninesky.Core項目->引用【右鍵】 –>添加引用
在引用管理器中選擇 項目->解決方案->Ninesky.DataLibrary,點擊確定。
2、NineskyContext類
NineskyContext類是項目的數據數據上下文,使模型和數據庫的表進行對應。
Ninesky.Core項目【右鍵】->添加->類, 輸入類名NineskyContext。
在類中引入命名空間System.Data.Entity;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
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個屬性。
在Ninesky.Core項目[右鍵]新建文件夾,輸入名稱Types。
在Types文件夾[右鍵]->添加->類,在彈出的添加新項對話框中輸入類名Response。代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
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>類是一個查詢分頁數據時使用的類,包含當前頁、每頁記錄數、總記錄數、和當前頁數據列表等幾個屬性。
在Types文件夾[右鍵]->添加->類,在彈出的添加新項對話框中輸入類名Paging。代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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類是所有管理類的基類,此類包含了管理類的常用方法。
將Ninesky.Core項目的Class1.cs重命名為BaseManager.cs
引入命名空間System.Data.Entity和Ninesky.Core.Types,實現共有方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
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下載源文件。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/mzwhj/p/5197028.html