asp.Netmvcでは工場モデルを実現するspringを独自に構築した.Net+三層アーキテクチャ(一)
89059 ワード
まず本人が管理しているGithubの項目を添付しますhttps://github.com/lichaojacobs/Tomato
オンラインアドレス:www.wanhb.cn
次の3つの階層構造には、親インタフェース、子インタフェース、親クラス、子クラスの部分が含まれています.
1、データアクセス層の作成
データ・アクセス・レイヤは、データベースの削除を変更する操作です.まず、データ・アクセス・レイヤの親インタフェースで、データベースの統合処理に一般的な方法を宣言します.
オンラインアドレス:www.wanhb.cn
次の3つの階層構造には、親インタフェース、子インタフェース、親クラス、子クラスの部分が含まれています.
1、データアクセス層の作成
データ・アクセス・レイヤは、データベースの削除を変更する操作です.まず、データ・アクセス・レイヤの親インタフェースで、データベースの統合処理に一般的な方法を宣言します.
1 #region 1.0 +int Add(T model) 2 ///
3 /// 4 /// 5 /// 6 ///
7 int Add(T model); 8 #endregion 9 10 #region 2.0 id +int Del(T model) 11 /// 12 /// id 13 /// 14 /// id 15 ///
16 int Del(T model); 17 #endregion 18 19 #region 3.0 +int DelBy(Expression > delWhere) 20 /// 21 /// 3.0 22 /// 23 /// 24 ///
25 int DelBy(Expression bool>> delWhere); 26 #endregion 27 28 #region 4.0 +int Modify(T model, params string[] proNames) 29 /// 30 /// 4.0 , : 31 /// T u = new T() { uId = 1, uLoginName = "asdfasdf" }; 32 /// this.Modify(u, "uLoginName"); 33 /// 34 /// 35 /// 36 ///
37 int Modify(T model, params string[] proNames); 38 #endregion 39 40 #region 4.0 +int Modify(T model, Expression > whereLambda, params string[] modifiedProNames) 41 /// 42 /// 4.0 43 /// 44 /// 45 /// 46 /// 47 ///
48 int ModifyBy(T model, Expression bool>> whereLambda, params string[] modifiedProNames); 49 #endregion 50 51 #region 5.0 +List GetListBy(Expression > whereLambda) 52 /// 53 /// 5.0 +List
GetListBy(Expression > whereLambda) 54 /// 55 /// 56 /// 57 List GetListBy(Expression bool>> whereLambda); 58 #endregion 59 60 #region 5.1 + List GetListBy 61 /// 62 /// 5.1 63 /// 64 ///
65 /// lambda 66 /// lambda 67 /// 68 List GetListBy (Expression bool>> whereLambda, Expression > orderLambda); 69 #endregion 70 71 #region 6.0 + List GetPagedList 72 /// 73 /// 6.0 + List
GetPagedList 74 /// 75 /// 76 /// 77 /// lambda 78 /// lambda 79 /// 80 List GetPagedList (int pageIndex, int pageSize, Expression bool>> whereLambda, Expression > orderBy); 81 #endregion
( Model, Model, ),
1
2
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7
8 namespace IDAL
9 {
10 public partial interface IsysdiagramsDAL : IBaseDAL
11 {
12
13
14 }
15
16 public partial interface IT001 DAL : IBaseDAL
17 {
18
19
20 }
21
22 public partial interface IT002 DAL : IBaseDAL
23 {
24
25
26 }
27
28 public partial interface IT003 DAL : IBaseDAL
29 {
30
31
32 }
33
34 public partial interface IT004 DAL : IBaseDAL
35 {
36
37
38 }
39
40 public partial interface IT005 DAL : IBaseDAL
41 {
42
43
44 }
45
46 public partial interface IT006 DAL : IBaseDAL
47 {
48
49
50 }
51
52 public partial interface IT007 DAL : IBaseDAL
53 {
54
55
56 }
57
58 public partial interface IT008 DAL : IBaseDAL
59 {
60
61
62 }
63
64 public partial interface IT009 DAL : IBaseDAL
65 {
66
67
68 }
69
70
71 }
インタフェースを き え、 に インタフェースの BaseDALを する.cs、 クラスでデータベースを するためのデータコンテキストクラスを 1 using System;
2 using System.Collections.Generic;
3 using System.Data.Entity.Infrastructure;
4 using System.Linq;
5 using System.Linq.Expressions;
6 using System.Reflection;
7 using System.Text;
8 using System.Threading.Tasks;
9 using System.Data.Entity;
10
11 namespace DAL
12 {
13 public class BaseDAL:IDAL.IBaseDAL where T:class,new()
14 {
15 //1,
16 // Model.TomatoEntities db = new Model.TomatoEntities();
17 protected DbContext db = new DBContextFactory().GetDbContext();
18
19 //2,
20 #region 1.0 +int Add(T model)
21 ///
22 ///
23 ///
24 ///
25 ///
26 public int Add(T model)
27 {
28 db.Set().Add(model);
29 return db.SaveChanges();// , id model ,
30 }
31 #endregion
32
33 #region 2.0 id +int Del(T model)
34 ///
35 /// id
36 ///
37 /// id
38 ///
39 public int Del(T model)
40 {
41 db.Set().Attach(model);
42 db.Set().Remove(model);
43 return db.SaveChanges();
44 }
45 #endregion
46
47 #region 3.0 +int DelBy(Expression> delWhere)
48 ///
49 /// 3.0
50 ///
51 ///
52 ///
53 public int DelBy(Expressionbool>> delWhere)
54 {
55 //3.1
56 List listDeleting = db.Set().Where(delWhere).ToList();
57 //3.2 EF
58 listDeleting.ForEach(u =>
59 {
60 db.Set().Attach(u);// EF
61 db.Set().Remove(u);//
62 });
63 //3.3 sql
64 return db.SaveChanges();
65 }
66 #endregion
67
68 #region 4.0 +int Modify(T model, params string[] proNames)
69 ///
70 /// 4.0 , :
71 /// T u = new T() { uId = 1, uLoginName = "asdfasdf" };
72 /// this.Modify(u, "uLoginName");
73 ///
74 ///
75 ///
76 ///
77 public int Modify(T model,params string[] proNames)
78 {
79 //int idreal = (int)id;
80 //var ent = db.Set().Find(idreal);
81 //if (ent != null)
82 //{
83 // db.Entry(model).State = System.Data.EntityState.Detached;
84 //}
85 //4.1 EF
86 DbEntityEntry entry = db.Entry(model);
87
88 ///
89 db.Set().Attach(model);
90 //4.2 Unchanged
91 entry.State = System.Data.EntityState.Unchanged;
92 //4.3
93 foreach (string proName in proNames)
94 {
95 //4.4 ; update ,
96 entry.Property(proName).IsModified = true;
97 }
98 //4.4 sql
99 return db.SaveChanges();
100 }
101 #endregion
102
103 #region 4.0 +int Modify(T model, Expression> whereLambda, params string[] modifiedProNames)
104 ///
105 /// 4.0
106 ///
107 ///
108 ///
109 ///
110 ///
111 public int ModifyBy(T model, Expressionbool>> whereLambda, params string[] modifiedProNames)
112 {
113 //4.1
114 List listModifing = db.Set().Where(whereLambda).ToList();
115
116 //
117 Type t = typeof(T); // model.GetType();
118 //
119 List proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList();
120 //
121 Dictionary<string, PropertyInfo> dictPros = new Dictionary<string, PropertyInfo>();
122 // : :
123 proInfos.ForEach(p =>
124 {
125 if (modifiedProNames.Contains(p.Name))
126 {
127 dictPros.Add(p.Name, p);
128 }
129 });
130
131 //4.3
132 foreach (string proName in modifiedProNames)
133 {
134 //
135 if (dictPros.ContainsKey(proName))
136 {
137 // ,
138 PropertyInfo proInfo = dictPros[proName];
139 //
140 object newValue = proInfo.GetValue(model, null); //object newValue = model.uName;
141
142 //4.4
143 foreach (T usrO in listModifing)
144 {
145 //
146 proInfo.SetValue(usrO, newValue, null); //usrO.uName = newValue;
147 }
148 }
149 }
150 //4.4 sql
151 return db.SaveChanges();
152 }
153 #endregion
154
155 #region 5.0 +List GetListBy(Expression> whereLambda)
156 ///
157 /// 5.0 +List GetListBy(Expression > whereLambda)
158 ///
159 ///
160 ///
161 public List GetListBy(Expressionbool>> whereLambda)
162 {
163 return db.Set().Where(whereLambda).ToList();
164 }
165 #endregion
166
167 #region 5.1 + List GetListBy
168 ///
169 /// 5.1
170 ///
171 ///
172 /// lambda
173 /// lambda
174 ///
175 public List GetListBy(Expressionbool>> whereLambda, Expression> orderLambda)
176 {
177 return db.Set().Where(whereLambda).OrderBy(orderLambda).ToList();
178 }
179 #endregion
180
181 #region 6.0 + List GetPagedList
182 ///
183 /// 6.0 + List GetPagedList
184 ///
185 ///
186 ///
187 /// lambda
188 /// lambda
189 ///
190 public List GetPagedList(int pageIndex, int pageSize, Expressionbool>> whereLambda, Expression> orderBy)
191 {
192 // : Skip OrderBy
193 return db.Set().Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
194 }
195 #endregion
196
197 }
198 }
, (DBContextFactory) , ,
1 using System;
2 using System.Collections.Generic;
3 using System.Data.Entity;
4 using System.Linq;
5 using System.Runtime.Remoting.Messaging;
6 using System.Text;
7
8 namespace DAL
9 {
10 public class DBContextFactory:IDAL.IDBContextFactory
11 {
12 #region EF , + DbContext GetDbContext()
13 ///
14 /// EF ,
15 ///
16 ///
17 public DbContext GetDbContext()
18 {
19 // EF
20 DbContext dbContext = CallContext.GetData(typeof(DBContextFactory).Name) as DbContext;
21 if (dbContext == null)
22 {
23 dbContext = new Model.TomatoEntities();
24 // ef
25 CallContext.SetData(typeof(DBContextFactory).Name, dbContext);
26 }
27 return dbContext;
28 }
29 #endregion
30 }
31 }
2、
( ), ,
( ):
( , ):
1
2
3 using System;
4 using System.Collections.Generic;
5 using System.Linq;
6 using System.Text;
7
8 namespace IBLL
9 {
10 public partial interface IsysdiagramsBLL : IBaseBLL
11 {
12
13 }
14
15 public partial interface IT001 BLL : IBaseBLL
16 {
17
18 }
19
20 public partial interface IT002 BLL : IBaseBLL
21 {
22
23 }
24
25 public partial interface IT003 BLL : IBaseBLL
26 {
27
28 }
29
30 public partial interface IT004 BLL : IBaseBLL
31 {
32
33 }
34
35 public partial interface IT005 BLL : IBaseBLL
36 {
37
38 }
39
40 public partial interface IT006 BLL : IBaseBLL
41 {
42
43 }
44
45 public partial interface IT007 BLL : IBaseBLL
46 {
47
48 }
49
50 public partial interface IT008 BLL : IBaseBLL
51 {
52
53 }
54
55 public partial interface IT009 BLL : IBaseBLL
56 {
57
58 }
59
60
61 }
および の : 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Linq.Expressions;
5 using System.Text;
6 using System.Threading.Tasks;
7
8
9 namespace BLL
10 {
11 public abstract class BaseBLL : IBLL.IBaseBLL where T : class,new()
12 {
13 public BaseBLL()
14 {
15 SetDAL();
16 }
17
18
19
20 //1,
21 protected DAL.BaseDAL idal= new DAL.BaseDAL();
22 ///
23 /// , !
24 ///
25 public abstract void SetDAL();
26
27
28 //2,
29 //2.
30 #region 1.0 +int Add(T model)
31 ///
32 ///
33 ///
34 ///
35 ///
36 public int Add(T model)
37 {
38 return idal.Add(model);
39 }
40 #endregion
41
42 #region 2。0 id +int Del(int uId)
43 ///
44 /// id
45 ///
46 ///
47 ///
48 public int Del(T model)
49 {
50 return idal.Del(model);
51 }
52 #endregion
53
54 #region 3.0 +int DelBy(Expression> delWhere)
55 ///
56 /// 3.0
57 ///
58 ///
59 ///
60 public int DelBy(Expressionbool>> delWhere)
61 {
62 return idal.DelBy(delWhere);
63 }
64 #endregion
65
66 #region 4.0 +int Modify(T model, params string[] proNames)
67 ///
68 /// 4.0 , :
69 ///
70 ///
71 ///
72 ///
73 public int Modify(T model, params string[] proNames)
74 {
75 return idal.Modify(model, proNames);
76 }
77 #endregion
78
79 #region 4.0 +int Modify(T model, Expression> whereLambda, params string[] modifiedProNames)
80 ///
81 /// 4.0 +int Modify(T model, Expression > whereLambda, params string[] modifiedProNames)
82 ///
83 ///
84 ///
85 ///
86 ///
87 public int ModifyBy(T model, Expressionbool>> whereLambda, params string[] modifiedProNames)
88 {
89 return idal.ModifyBy(model, whereLambda, modifiedProNames);
90 }
91 #endregion
92
93 #region 5.0 +List GetListBy(Expression> whereLambda)
94 ///
95 /// 5.0 +List GetListBy(Expression > whereLambda)
96 ///
97 ///
98 ///
99 public List GetListBy(Expressionbool>> whereLambda)
100 {
101 return idal.GetListBy(whereLambda);
102 }
103 #endregion
104
105 #region 5.1 + List GetListBy
106 ///
107 /// 5.1
108 ///
109 ///
110 /// lambda
111 /// lambda
112 ///
113 public List GetListBy(Expressionbool>> whereLambda, Expression> orderLambda)
114 {
115 return idal.GetListBy(whereLambda, orderLambda);
116 }
117 #endregion
118
119 #region 6.0 + List GetPagedList
120 ///
121 /// 6.0 + List GetPagedList
122 ///
123 ///
124 ///
125 /// lambda
126 /// lambda
127 ///
128 public List GetPagedList(int pageIndex, int pageSize, Expressionbool>> whereLambda, Expression> orderBy)
129 {
130 return idal.GetPagedList(pageIndex, pageSize, whereLambda, orderBy);
131 }
132 #endregion
133
134 }
135 }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using IBLL;
using IDAL;
namespace BLL
{
public partial class sysdiagramsBLL: BaseBLL,IsysdiagramsBLL
{
public override void SetDAL()
{
}
}
public partial class T001 BLL: BaseBLL,IT001 BLL
{
public override void SetDAL()
{
idal = DAL.BaseDAL();
}
}
public partial class T002 BLL: BaseBLL,IT002 BLL
{
public override void SetDAL()
{
idal = DAL.BaseDAL();
}
}
public partial class T003 BLL: BaseBLL,IT003 BLL
{
public override void SetDAL()
{
idal =DAL.BaseDAL();
}
}
public partial class T004 BLL: BaseBLL,IT004 BLL
{
public override void SetDAL()
{
idal = DAL.BaseDAL();;
}
}
public partial class T005 BLL: BaseBLL,IT005 BLL
{
public override void SetDAL()
{
idal = DAL.BaseDAL();
}
}
public partial class T006 BLL: BaseBLL,IT006 BLL
{
public override void SetDAL()
{
idal = DAL.BaseDAL();
}
}
public partial class T007 BLL: BaseBLL,IT007 BLL
{
public override void SetDAL()
{
idal =DAL.BaseDAL();
}
}
public partial class T008 BLL: BaseBLL,IT008 BLL
{
public override void SetDAL()
{
idal =DAL.BaseDAL();
}
}
public partial class T009 BLL: BaseBLL,IT009 BLL
{
public override void SetDAL()
{
idal = DAL.BaseDAL();
}
}
}
3、 の な び し:
IBLL.IT 001アカウントテーブルBLL user=new BLL.T 001アカウントテーブルBLL();
user.GetListBy(m => m.Email =="");//Lambda
: と に られ、 springから れています.Netと モードはまだ いので、これからも を けます.githubのプロジェクトを て、 の と モードを することに があります.