業(yè)務(wù)邏輯層在Ninesky.Core中實(shí)現(xiàn),主要功能封裝一些方法通過(guò)調(diào)用數(shù)據(jù)存儲(chǔ)層,向界面層提供服務(wù)。
一、業(yè)務(wù)邏輯層的架構(gòu)
Ninesky.Core包含三個(gè)命名空間Ninesky.Core、Ninesky.Core.Types、Ninesky.Core.General.
Ninesky.Core包含模型和功能實(shí)現(xiàn),Ninesky.Core.Types是項(xiàng)目用到的一些類型的定義,Ninesky.Core.General是項(xiàng)目用到的一些方法的定義。
1、Ninesky.Core命名空間的結(jié)構(gòu)
NineskyContext-數(shù)據(jù)上下文
ContextFactory- 獲取數(shù)據(jù)上下文的工廠類
BaseManager-基礎(chǔ)類,實(shí)現(xiàn)了一些常用數(shù)據(jù)訪問(wèn)方法,提供其他管理類繼承。
Category-欄目模型。
CategoryManager-欄目管理類。
Content-內(nèi)容模型。
ContentManager-內(nèi)容管理類。
User-用戶模型
UserManager-用戶管理類
Administrator-管理員類
AdministratorManager-管理員管理類
2、Ninesky.Core.Types命名空間的結(jié)構(gòu)
Response 響應(yīng)返回類。
Paging<T> 分頁(yè)數(shù)據(jù)類。
二、基礎(chǔ)功能的實(shí)現(xiàn)
1、添加引用
(1)、添加EntityFramewok 引用
Ninesky.Core項(xiàng)目->引用【右鍵】 –>管理NuGet程序包
在NuGet包管理對(duì)器話框中選擇 EntityFramewok 并安裝。
(2)、添加Ninesky.DataLibrary項(xiàng)目的引用
Ninesky.Core項(xiàng)目->引用【右鍵】 –>添加引用
在引用管理器中選擇 項(xiàng)目->解決方案->Ninesky.DataLibrary,點(diǎn)擊確定。
2、NineskyContext類
NineskyContext類是項(xiàng)目的數(shù)據(jù)數(shù)據(jù)上下文,使模型和數(shù)據(jù)庫(kù)的表進(jìn)行對(duì)應(yīng)。
Ninesky.Core項(xiàng)目【右鍵】->添加->類, 輸入類名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是一個(gè)簡(jiǎn)單工廠類,CurrentContext()是一個(gè)靜態(tài)函數(shù),用來(lái)獲取當(dāng)前線程DbContext。
Ninesky.Core項(xiàng)目【右鍵】->添加->類, 輸入類名ContextFactory。
在類中添加對(duì)System.Runtime.Remoting.Messaging的引用。在類中實(shí)現(xiàn)CurrentContext()靜態(tài)方法返回?cái)?shù)據(jù)上下文NineskyContext。方法中通過(guò)CallContext類在線程中存儲(chǔ)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> /// 數(shù)據(jù)上下文工廠 /// </summary> public class ContextFactory { /// <summary> /// 獲取當(dāng)前線程的數(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類是一個(gè)常用的方法返回?cái)?shù)據(jù)類型,包含返回代碼、返回消息和返回?cái)?shù)據(jù)3個(gè)屬性。
在Ninesky.Core項(xiàng)目[右鍵]新建文件夾,輸入名稱Types。
在Types文件夾[右鍵]->添加->類,在彈出的添加新項(xiàng)對(duì)話框中輸入類名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-成功,其他-具體見方法返回值說(shuō)明 /// </summary> public int Code { get ; set ; } /// <summary> /// 返回消息 /// </summary> public string Message { get ; set ; } /// <summary> /// 返回?cái)?shù)據(jù) /// </summary> public dynamic Data { get ; set ; } public Response() { Code = 0; } } } |
5、Paging<T>類
Paging<T>類是一個(gè)查詢分頁(yè)數(shù)據(jù)時(shí)使用的類,包含當(dāng)前頁(yè)、每頁(yè)記錄數(shù)、總記錄數(shù)、和當(dāng)前頁(yè)數(shù)據(jù)列表等幾個(gè)屬性。
在Types文件夾[右鍵]->添加->類,在彈出的添加新項(xiàng)對(duì)話框中輸入類名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> /// 當(dāng)前頁(yè)。從1計(jì)數(shù) /// </summary> public int PageIndex { get ; set ; } /// <summary> /// 每頁(yè)記錄數(shù)。默認(rèn)20 /// </summary> public int PageSize { get ; set ; } /// <summary> /// 總記錄數(shù) /// </summary> public int TotalNumber; /// <summary> /// 當(dāng)前頁(yè)記錄列表 /// </summary> public List<T> Items { get ; set ; } public Paging() { PageIndex = 1; PageSize = 20; } } } |
6、BaseManager類
BaseManager類是所有管理類的基類,此類包含了管理類的常用方法。
將Ninesky.Core項(xiàng)目的Class1.cs重命名為BaseManager.cs
引入命名空間System.Data.Entity和Ninesky.Core.Types,實(shí)現(xiàn)共有方法。
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> /// 數(shù)據(jù)倉(cāng)儲(chǔ)類 /// </summary> protected Repository<T> Repository; /// <summary> /// 默認(rèn)構(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í)體數(shù)據(jù)</param> /// <returns>成功時(shí)屬性【Data】為添加后的數(shù)據(jù)實(shí)體</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í)體數(shù)據(jù)</param> /// <returns>成功時(shí)屬性【Data】為更新后的數(shù)據(jù)實(shí)體</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> /// 查找實(shí)體 /// </summary> /// <param name="ID">主鍵</param> /// <returns>實(shí)體</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> /// 查找分頁(yè)數(shù)據(jù) /// </summary> /// <param name="paging">分頁(yè)數(shù)據(jù)</param> /// <returns>分頁(yè)數(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/SourceControl/latest
代碼下載:https://ninesky.codeplex.com 點(diǎn)擊SOURCE CODE 點(diǎn)擊Download下載源文件。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/mzwhj/p/5197028.html