新人剛開始學習ASP.NET MVC,若有不足之處希望能得到您的指點,不勝感激!
先來一張項目的層級結構圖:
Model:模型層,主要是各種類型、枚舉以及ORM框架,框架完成數(shù)據(jù)庫和實體類的映射。項目中選用了微軟的開源ORM框架 EntityFramework 6.0 (以下簡稱EF),數(shù)據(jù)庫則選擇了微軟的輕量級數(shù)據(jù)庫SQL Server Compact 4.0本地數(shù)據(jù)庫(簡稱Compact),Compact對EF支持比較完美,又屬于文檔型數(shù)據(jù)庫,部署起來比較簡潔。
DAL:數(shù)據(jù)訪問層,主要是對數(shù)據(jù)庫的操作層,為業(yè)務邏輯層或表示層提供數(shù)據(jù)服務。
IDAL:數(shù)據(jù)訪問接口層,是數(shù)據(jù)訪問層的接口,降低耦合。
DALFactory:數(shù)據(jù)會話層,封裝了所有數(shù)據(jù)操作類實例的創(chuàng)建,將數(shù)據(jù)訪問層與業(yè)務邏輯層解耦。
BLL:業(yè)務邏輯層,主要負責對數(shù)據(jù)層的操作,把一些數(shù)據(jù)層的操作進行組合以完成業(yè)務的需要。
IBLL:業(yè)務邏輯接口層,業(yè)務邏輯層的接口,降低耦合。
WebApp:表現(xiàn)層,是一個ASP.NET MVC項目,完成具體網(wǎng)站的實現(xiàn)。
Common:通用層,用來存放一些工具類。
下面是各個層級之間具體的實現(xiàn),首先創(chuàng)建以 項目名.層級名 命名的各個層次,除WebApp層為ASP.NET MVC項目外,其余均創(chuàng)建為類庫項目。
模型層的構建
先建立模型層,新建ASP.NET 實體數(shù)據(jù)模型,關聯(lián)到已經(jīng)設計好的數(shù)據(jù)庫,EF自動完成模型類的創(chuàng)建。
數(shù)據(jù)訪問層的構建
DAL層中,我們首先需要一個方法來獲取單例的EF數(shù)據(jù)操縱上下文對象,以保證每個用戶訪問時只有使用一個上下文對象對數(shù)據(jù)庫進行操作。
DbContextFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
using System.Data.Entity; using System.Runtime.Remoting.Messaging; using PMS.Model; namespace PMS.DAL { public class DbContextFactory { /// <summary> /// 負責創(chuàng)建EF數(shù)據(jù)操作上下文實例,必須保證線程內(nèi)唯一 /// </summary> public static DbContext CreateContext() { DbContext dbContext = (DbContext)CallContext.GetData( "dbContext" ); if (dbContext != null ) return dbContext; dbContext = new PMSEntities(); CallContext.SetData( "dbContext" , dbContext); return dbContext; } } } |
為User類創(chuàng)建DAL層,實現(xiàn)查詢、分頁查詢、增加、刪除和修改這五個基本的方法:
UserDAL.cs
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
|
using System; using System.Data.Entity; using System.Linq; using PMS.IDAL; namespace PMS.DAL { public partial class UserDal { public DbContext DbEntities = DbContextFactory.CreateContext(); /// <summary> /// 查詢過濾 /// </summary> /// <param name="whereLamada">過濾條件Lambda表達式</param> /// <returns>實體集合</returns> public IQueryable<UserDal> LoadEntities(System.Linq.Expressions.Expression<Func<UserDal, bool >> whereLamada) { return DbEntities.Set<UserDal>().Where(whereLamada); } /// <summary> /// 分頁查詢 /// </summary> /// <typeparam name="TS">排序類型</typeparam> /// <param name="pageIndex">查詢的頁碼</param> /// <param name="pageSize">每頁顯示的數(shù)目</param> /// <param name="totalCount">符合條件的總行數(shù)</param> /// <param name="whereLambda">過濾條件Lambda表達式</param> /// <param name="orderbyLambda">排序Lambda表達式</param> /// <param name="isAsc">排序方向</param> /// <returns>實體集合</returns> public IQueryable<UserDal> LoadPageEntities<TS>( int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<UserDal, bool >> whereLambda, System.Linq.Expressions.Expression<Func<UserDal, TS>> orderbyLambda, bool isAsc) { var temp = DbEntities.Set<UserDal>().Where(whereLambda); totalCount = temp.Count(); temp = isAsc ? temp.OrderBy(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize) : temp.OrderByDescending(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize); return temp; } /// <summary> /// 刪除數(shù)據(jù) /// </summary> /// <param name="entity">待刪數(shù)據(jù)</param> /// <returns>刪除結果</returns> public bool DeleteEntity(UserDal entity) { DbEntities.Entry(entity).State = EntityState.Deleted; return true ; } /// <summary> /// 編輯數(shù)據(jù) /// </summary> /// <param name="entity">待編輯數(shù)據(jù)</param> /// <returns>編輯結果</returns> public bool EditEntity(UserDal entity) { DbEntities.Entry(entity).State = EntityState.Modified; return true ; } /// <summary> /// 添加數(shù)據(jù) /// </summary> /// <param name="entity">待添加數(shù)據(jù)</param> /// <returns>已添加數(shù)據(jù)</returns> public UserDal AddEntity(UserDal entity) { entity = DbEntities.Set<UserDal>().Add(entity); return entity; } } } |
注:這里的增刪改操作并不即時進行,而是在封裝在數(shù)據(jù)會話層中,以實現(xiàn)工作單元模式,提高數(shù)據(jù)庫的操作效率。
考慮到每個類都需要實現(xiàn)相同的數(shù)據(jù)操作,我們可以將以上方法封裝到一個泛型基類中,各類型只需要繼承泛型基類就可以實現(xiàn)以上方法:
BaseDal.cs
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
|
using System; using System.Data.Entity; using System.Linq; namespace PMS.DAL { public class BaseDal<T> where T: class , new () { public DbContext DbEntities = DbContextFactory.CreateContext(); /// <summary> /// 查詢過濾 /// </summary> /// <param name="whereLamada">過濾條件Lambda表達式</param> /// <returns>實體集合</returns> public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool >> whereLamada) { return DbEntities.Set<T>().Where(whereLamada); } /// <summary> /// 分頁查詢 /// </summary> /// <typeparam name="TS">排序類型</typeparam> /// <param name="pageIndex">查詢的頁碼</param> /// <param name="pageSize">每頁顯示的數(shù)目</param> /// <param name="totalCount">符合條件的總行數(shù)</param> /// <param name="whereLambda">過濾條件Lambda表達式</param> /// <param name="orderbyLambda">排序Lambda表達式</param> /// <param name="isAsc">排序方向</param> /// <returns>實體集合</returns> public IQueryable<T> LoadPageEntities<TS>( int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool >> whereLambda, System.Linq.Expressions.Expression<Func<T, TS>> orderbyLambda, bool isAsc) { var temp = DbEntities.Set<T>().Where(whereLambda); totalCount = temp.Count(); temp = isAsc ? temp.OrderBy(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize) : temp.OrderByDescending(orderbyLambda).Skip((pageIndex - 1) * pageSize).Take(pageSize); return temp; } /// <summary> /// 刪除數(shù)據(jù) /// </summary> /// <param name="entity">待刪數(shù)據(jù)</param> /// <returns>刪除結果</returns> public bool DeleteEntity(T entity) { DbEntities.Entry(entity).State = EntityState.Deleted; return true ; } /// <summary> /// 編輯數(shù)據(jù) /// </summary> /// <param name="entity">待編輯數(shù)據(jù)</param> /// <returns>編輯結果</returns> public bool EditEntity(T entity) { DbEntities.Entry(entity).State = EntityState.Modified; return true ; } /// <summary> /// 添加數(shù)據(jù) /// </summary> /// <param name="entity">待添加數(shù)據(jù)</param> /// <returns>已添加數(shù)據(jù)</returns> public T AddEntity(T entity) { entity = DbEntities.Set<T>().Add(entity); //DbEntities.SaveChanges(); return entity; } } } |
UserDal繼承BaseDal
1
2
3
4
5
6
7
8
9
10
|
using PMS.IDAL; using PMS.Model; namespace PMS.DAL { public partial class UserDal : BaseDal<User> { } } |
數(shù)據(jù)訪問接口層的構建
然后我們建立相應的IbaseDal接口和IUserDal接口,并且使UserDal類實現(xiàn)IUserDal接口
IBaseDal:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
using System; using System.Linq; namespace PMS.IDAL { public interface IBaseDal<T> where T: class , new () { IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool >> whereLamada); IQueryable<T> LoadPageEntities<s>( int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool >> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderbyLambda, bool isAsc); bool DeleteEntity(T entity); bool EditEntity(T entity); T AddEntity(T entity); } } |
IUserDal:
1
2
3
4
5
6
7
8
|
using PMS.Model; namespace PMS.IDAL { public partial interface IUserDal:IBaseDal<User> { } } |
UserDal實現(xiàn)IUserDal接口:
public partial class UserDal : BaseDal<User>,IUserDal
數(shù)據(jù)會話層的構建
抽象工廠類AbstractFactory:
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
|
using System.Configuration; using System.Reflection; using PMS.IDAL; namespace PMS.DALFactory { public partial class AbstractFactory { //讀取保存在配置文件中的程序集名稱與命名空間名 private static readonly string AssemblyPath = ConfigurationManager.AppSettings[ "AssemblyPath" ]; private static readonly string NameSpace = ConfigurationManager.AppSettings[ "NameSpace" ]; /// <summary> /// 獲取UserDal的實例 /// </summary> /// <returns></returns> public static IUserDal CreateUserInfoDal() { var fullClassName = NameSpace + ".UserInfoDal" ; return CreateInstance(fullClassName) as IUserDal; } /// <summary> /// 通過反射獲得程序集中某類型的實例 /// </summary> /// <param name="className"></param> /// <returns></returns> private static object CreateInstance( string className) { var assembly = Assembly.Load(AssemblyPath); return assembly.CreateInstance(className); } } } |
數(shù)據(jù)會話類DbSession:
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
|
using System.Data.Entity; using PMS.IDAL; using PMS.DAL; namespace PMS.DALFactory { public partial class DbSession:IDbSession { public DbContext Db { get { return DbContextFactory.CreateContext(); } } private IUserDal _userDal; public IUserDal UserDal { get { return _userDal ?? (_userDal = AbstractFactory.CreateUserInfoDal()); } set { _userDal = value; } } /// <summary> /// 工作單元模式,統(tǒng)一保存數(shù)據(jù) /// </summary> /// <returns></returns> public bool SaveChanges() { return Db.SaveChanges() > 0; } } } |
業(yè)務邏輯層的構建
業(yè)務類基類BaseService
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
|
using System; using System.Linq; using System.Linq.Expressions; using PMS.DALFactory; using PMS.IDAL; namespace PMS.BLL { public abstract class BaseService<T> where T: class , new () { public IDbSession CurrentDbSession { get { return new DbSession(); } } public IBaseDal<T> CurrentDal { get ; set ; } public abstract void SetCurrentDal(); public BaseService() { SetCurrentDal(); //子類一定要實現(xiàn)抽象方法,以指明當前類的子類類型。 } /// <summary> /// 查詢過濾 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public IQueryable<T> LoadEntities(Expression<Func<T, bool >> whereLambda) { return CurrentDal.LoadEntities(whereLambda); } /// <summary> /// 分頁 /// </summary> /// <typeparam name="s"></typeparam> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="totalCount"></param> /// <param name="whereLambda"></param> /// <param name="orderbyLambda"></param> /// <param name="isAsc"></param> /// <returns></returns> public IQueryable<T> LoadPageEntities<s>( int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool >> whereLambda, Expression<Func<T, s>> orderbyLambda, bool isAsc) { return CurrentDal.LoadPageEntities<s>(pageIndex, pageSize, out totalCount, whereLambda, orderbyLambda, isAsc); } /// <summary> /// 刪除 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(T entity) { CurrentDal.DeleteEntity(entity); return CurrentDbSession.SaveChanges(); } /// <summary> /// 編輯 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool EditEntity(T entity) { CurrentDal.EditEntity(entity); return CurrentDbSession.SaveChanges(); } /// <summary> /// 添加數(shù)據(jù) /// </summary> /// <param name="entity"></param> /// <returns></returns> public T AddEntity(T entity) { CurrentDal.AddEntity(entity); CurrentDbSession.SaveChanges(); return entity; } } } |
UserService類:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
using PMS.IBLL; using PMS.Model; namespace PMS.BLL { public partial class UserService : BaseService<User> { public override void SetCurrentDal() { CurrentDal = CurrentDbSession.UserDal; } } } |
業(yè)務邏輯接口層的構建
直接建立對應的接口并使用UserService類實現(xiàn)IUserService接口
IBaseService接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
using System; using System.Linq; using System.Linq.Expressions; using PMS.IDAL; namespace PMS.IBLL { public interface IBaseService<T> where T : class , new () { IDbSession CurrentDbSession { get ; } IBaseDal<T> CurrentDal { get ; set ; } void SetCurrentDal(); IQueryable<T> LoadEntities(Expression<Func<T, bool >> whereLambda); IQueryable<T> LoadPageEntities<s>( int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool >> whereLambda, Expression<Func<T, s>> orderbyLambda, bool isAsc); bool DeleteEntity(T entity); bool EditEntity(T entity); T AddEntity(T entity); } } |
IUserService接口:
1
2
3
4
5
6
7
8
9
|
using PMS.Model; namespace PMS.IBLL { public partial interface IUserService:IBaseService<User> { } } |
使用UserService類實現(xiàn)IUserService接口:
public partial class UserService : BaseService<User>, IUserService
以上我們就完成了整個框架中關于User類的各層次的實現(xiàn)。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/JiaoWoWeiZai/p/5876880.html