C# 轻量级ORM 编写思维
时间:2014-04-30 21:27:46
收藏:0
阅读:508
目标:
降低研发人员门槛,提高效率,去除重复引用DLL的工作,基础配置由抽象工厂处理。
基础扩展
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 |
/// <summary> /// DataTable 转换为List 集合 /// </summary> /// <typeparam name="TResult">类型</typeparam> /// <param name="dt">DataTable</param> /// <returns></returns> public
static List<TResult> ToList<TResult>( this
DataTable dt) where
TResult : class , new () /// <summary> /// 转换为一个DataTable /// </summary> /// <typeparam name="TResult"></typeparam> /// <param name="value"></param> /// <returns></returns> public
static DataTable ToDataTable<TResult>( this
IEnumerable<TResult> value) where
TResult : class |
业务逻辑接口
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 |
//name:wujc 2013-06-20 namespace
Yc.BLL { public
class DataDomain : IDataService { private
readonly IDataService dal = DataAccess.CreateService(); public
DataDomain() { } #region 成员方法 /// <summary> /// 获取表行数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="strWhere"></param> /// <returns></returns> public
int GetRowsCount<T>( string
strWhere) where
T : BaseModel, new () { return
dal.GetRowsCount<T>(strWhere); } /// <summary> /// 获取表行数 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="sql"></param> /// <returns></returns> public
int GetRowsCount( string
sql) { return
dal.GetRowsCount(sql); } /// <summary> /// 是否存在该记录 /// </summary> public
bool Exists<T>( string
id) where
T : BaseModel, new () { return
dal.Exists<T>(id); } /// <summary> /// 增加一条数据 /// </summary> public
bool Add<T>(T model) where
T : BaseModel, new () { return
dal.Add<T>(model); } /// <summary> /// 增加多条数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="modelList"></param> /// <returns></returns> public
bool Add<T>(List<T> modelList) where
T : BaseModel, new () { return
dal.Add<T>(modelList); } /// <summary> /// 更新一条数据 /// </summary> public
bool Update<T>(T model) where
T : BaseModel, new () { return
dal.Update<T>(model); } /// <summary> /// 更新多条数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public
bool Update<T>(List<T> modelList) where
T : BaseModel, new () { return
dal.Update<T>(modelList); } /// <summary> /// 删除一条数据 /// </summary> public
bool Delete<T>( string
id) where
T : BaseModel, new () { return
dal.Delete<T>(id); } /// <summary> /// 删除一条数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="id"></param> /// <returns></returns> public
bool Delete<T>(T model) where
T : BaseModel, new () { return
dal.Delete<T>(model); } /// <summary> /// 删除多条数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public
bool Delete<T>(List<T> modelList) where
T : BaseModel, new () { return
dal.Delete<T>(modelList); } /// <summary> /// 批量删除数据 /// </summary> /// <param name="idList">逗号分隔</param> /// <returns></returns> public
bool DeleteList<T>( string
idList) where
T : BaseModel, new () { return
dal.DeleteList<T>(idList); } /// <summary> /// 得到一个对象实体 /// </summary> public
T GetModel<T>( string
id) where
T : BaseModel, new () { return
dal.GetModel<T>(id); } /// <summary> /// 得到全部对象实体 /// </summary> public
List<T> GetModelList<T>( string
strWhere) where
T : BaseModel, new () { return
dal.GetModelList<T>(strWhere); } /// <summary> /// 获得数据列表 /// </summary> public
DataSet GetList<T>( string
strWhere) where
T : BaseModel, new () { return
dal.GetList<T>(strWhere); } /// <summary> /// 获得数据表 /// </summary> public
DataTable GetTable<T>( string
strWhere) where
T : BaseModel, new () { return
dal.GetTable<T>(strWhere); } /// <summary> /// 根据分页获得数据列表 /// </summary> public
DataSet GetList<T>( string
strWhere, string
orderby , int
startIndex, int
endIndex) where
T : BaseModel, new () { return
dal.GetList<T>(strWhere, orderby , startIndex, endIndex); } /// <summary> /// 根据分页获得数据列表 /// </summary> public
DataSet GetListBySql( string
sql, string
orderby , int
startIndex, int
endIndex) { return
dal.GetListBySql(sql, orderby , startIndex, endIndex); } /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public
int ExecuteSql( string
SQLString) { return
dal.ExecuteSql(SQLString); } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public
DataSet Query( string
SQLString) { return
dal.Query(SQLString); } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param> public
void ExecuteSqlTran(Hashtable SQLStringList) { dal.ExecuteSqlTran(SQLStringList); } #endregion 成员方法 |
因为这还是个公司框架,还不能开源,不过看完以上接口定义之后,已经知道个大概了吧。
数据逻辑抽象工厂,由抽象接口层,根据配置创建抽象实例,还有缓冲池等。
数据逻辑基础方法和接口
这些很基础的,网上也一大把,我只是为了配合框架做了一些小修改。
接着,每创立一个新项目,都不需要重新再去重做一套orm 了, 引入以上所说的。
接着,新增一个Model层,值得主意的是,Model要继承Yc.Base的BaseModel。
示例:
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 |
/// <summary> /// 用户表 /// </summary> [System.Serializable()] public
class UserInfo : Yc.Base.BaseModel { /// <summary> /// 初始化 /// </summary> public
UserInfo() { this .TableUser = "test" ; } private
System.String _userInfoId; /// <summary> /// 用户Id,NVARCHAR2,128 /// </summary> public
System.String UserInfoId { get { return
_userInfoId == null ? "" :_userInfoId; } set { _userInfoId = value; } } private
System.String _name; /// <summary> /// 用户名,NVARCHAR2,500 /// </summary> public
System.String Name { get { return
_name == null ? "" :_name; } set { _name = value; } } private
System.String _pwd; /// <summary> /// 用户密码,NVARCHAR2,500 /// </summary> public
System.String Pwd { get { return
_pwd == null ? "" :_pwd; } set { _pwd = value; } } } |
再引用你该项目的Model层
一切就这样简单,希望对大家了解orm有所帮助!
深圳元创
技术经理:五加乘
评论(0)