在制做项目时我们都会将数据库中的字段名写成一个类,也就是将数据库中的每一张表写成一个类,我们将它称为实体类.EntitySet类能构将类的很多个实例对象封装在Entities属性中,同时可以记录查询记录的总数,对数据库分页很有用;拥有类是List<>范型功能,应用于所有实体类,能减少我们很多代码.
通常我们要为一个实体类定义四个重要的函数,当然这是最少的要求:(假设我们有一个实体类EntitySimple) 下面我们就定义四个和数据库相关联的函数:
EntitySet其实就是一个ArrayList的变形版,有了这个类在读取是就可以将对象entitySimple给Add进去了,当我们要用的时候就可以直接调用了:EntitySet set=SimpleManager.GetAllEntitySimple().Entities;如果直接使用SimpleManager.GetAllEntitySimple().Entities前显式转化:(EntitySimple)SimpleManager.GetAllEntitySimple().Entities.
通常我们要为一个实体类定义四个重要的函数,当然这是最少的要求:(假设我们有一个实体类EntitySimple) 下面我们就定义四个和数据库相关联的函数:
1
using
System;
2
using
System.Data;
3
using
System.Data.SqlClient;
4
//
其中EntitySimple类位于下面的命名空间中.
5
using
NexPlus.Entities;
6![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace
NexPlus.Data.SqlDataProvider
{
8
public class SimpleDataProvider{
9
10
public SimpleDataProvider(){}
11![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
public bool CreateUpdateEntitySimple(EntitySimple entitySimple,DataAction action){
13
//代码
.
14
}
15![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
public EntitySimple GetEntitySimple(int id){
17
//代码
.
18
}
19![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
public bool DeleteEntitySimple(int id){
21
//代码
.
22
}
23![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
public EntitySet GetAllEntitySimple(){
25
//代码
.
26
}
27
}
28
}
从上面可以很容易看到就是SQL的四大守护神:SELECT,UPDATE,DELETE,INSERT;一般创建和更新就差一个Id,都是用实体类封装实例,所以我们把他们写作同一个函数里,用枚举(
DataAction action)来区分创建还是更新.今天我们要讨论的就是
EntitySet类,我们来看一下它的代码:![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
8
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
13
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
14
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
15
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
18
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
22
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![dot.gif](https://www.cnblogs.com/Images/dot.gif)
26
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
1
using
System;
2
using
System.Collections;
3![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
namespace
NexPlus.Entities
5
{
6
public class EntitySet
7
{
8
ArrayList entities = new ArrayList();
9
int totalRecords = 0;
10
/// <summary>
11
/// 实体总纪录数。
12
/// </summary>
13
public int TotalRecords{
14
get{return totalRecords;}
15
set{totalRecords = value;}
16
}
17
/// <summary>
18
/// 实体项列表。
19
/// </summary>
20
public ArrayList Entities{
21
get{return entities;}
22
}
23
/// <summary>
24
/// 实体中是否有项。
25
/// </summary>
26
public bool HasResults{
27
get{
28
if (entities.Count > 0)
29
return true;
30
return false;
31
}
32
}
33
}
34
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
6
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
8
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
13
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
15
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
22
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
24
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
26
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
33
![ExpandedSubBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
34
![ExpandedBlockEnd.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
EntitySet其实就是一个ArrayList的变形版,有了这个类在读取是就可以将对象entitySimple给Add进去了,当我们要用的时候就可以直接调用了:EntitySet set=SimpleManager.GetAllEntitySimple().Entities;如果直接使用SimpleManager.GetAllEntitySimple().Entities前显式转化:(EntitySimple)SimpleManager.GetAllEntitySimple().Entities.
代码很简单,但能构充当十分重要的角色,对于一个网站来说需要很多个实体类集合,这里我们就可以用这个类来替代集合类.能构减少很多代码,当然你可以用List<EntitySimple>,但是有时需要TotalRecords,就不能直接从中读取,像上一篇文章中就不需要加out int totalRecords了,可以直接存在这个类里,我觉得这个类会比较好用,所以就拿出来和大家分享一下,或许你有更好的,希望批评指正!
本文转自网魂小兵博客园博客,原文链接:http://www.cnblogs.com/xdotnet/archive/2006/09/20/EntitySet_Class_So_Good.html,如需转载请自行联系原作者