asp.net2.0里面的ObjectDataSource可以使数据显示控件GridView等进行绑定显示,编辑。还可以支持内置的分页,排序等。使用了ORM之后,一样可以使用ObjectDataSource。
这里的分页不再是从数据库取出所有,然后选择性绑定,而是直接在数据库取出第几页,然后绑定。这个差别还是十分巨大的,效率大大提高。
编辑,创建,排序也都是,直接由ObjectDataSource提供,不需要再GridView中写什么代码。
这样,可以把Object设计的包含有不少逻辑,至少是对数据库操作的,而UI就显得比较简单,剥离的再开一点,对以后移植到win上,或者做成SmartClient都比较有益。
这里有一片blog,讲的比较好 http://www.evosoftworks.com/Articles/wormods.aspx。
我用的正好也是WilsonORM,所以照此也作了一个。
基本的结构是这样的:
UI(GridView等控件--ObjectDataSource控件)----〉ObjectDataSource类(Object,写CRUD分页等逻辑)---〉(ORM实现CRUD)---〉DB
主要有几步
1:给Object增加属性和方法,来完成CRUD,分页等逻辑
2:配置GridView等UI控件连接到ObjectDataSource控件。
先看第一个
1:给Object增加属性和方法,来完成CRUD,分页等逻辑。该Object类由工具根据DB结构生成,同时生成的还有Mapping文件。
首先,给该Object增加一个标示属性DataObject(),在System.ComponentModel命名空间里面
[DataObject()]
public
class
ProductDescription

{
第二,给这个object类增加CRUD的方法。
先看一个Insert方法
[DataObjectMethod(DataObjectMethodType.Insert)]
public
static
void
Insert(ProductDescription productDescription)

{
try

{
Manager.DataManager.StartTracking(productDescription, InitialState.Inserted);
Manager.DataManager.PersistChanges(productDescription);
}
catch (Exception ex)

{
log.Error(ex);
}
}
这个方法前面需要加一个[DataObjectMethod(DataObjectMethodType.Insert)]属性,表示这是Insert方法;
这个方法是静态公开的方法;
参数,就是这个Object本身的一个实例。这样比较好,因为在逻辑好很好理解,都是在对Object进行操作。
剩下的,Delete,Update方法也是这样写。
然后看看Select方法,比较特殊。
Select方法
1
[DataObjectMethod(DataObjectMethodType.Select)]
2
public Collection<ProductDescription> Retrieve(string query, int maxRows, int startRowIndex, string sortClause)
3
{
4
try
5
{
6
int numPages = 0;
7
if (sortClause == null || sortClause == "")
8
sortClause = "ModifiedDate Desc";
9
Collection<ProductDescription> cs;
10
cs = RetrievePage(query, sortClause, maxRows, (int)Math.Ceiling((double)startRowIndex / maxRows) + 1, out numPages);
11
_numRecs = ((IObjectPage)cs).TotalCount;
12
return cs;
13
}
14
catch (Exception ex)
15
{
16
log.Error(ex);
17
return null;
18
}
19
}
20
[DataObjectMethod(DataObjectMethodType.Select)]
21
static public ObjectSet Retrieve(string Key, string Value)
22
{
23
if (Value == null || Value == "")
24
return null;
25
try
26
{
27
QueryHelper helper = Manager.DataManager.QueryHelper;
28
Key = helper.GetFieldName(typeof(ProductDescription).ToString() + "." + Key);
29
ObjectQuery query = new ObjectQuery(typeof(ProductDescription), String.Format("{0}='{1}'", Key, Value), "");
30
ObjectSet obj = Manager.DataManager.GetObjectSet(query);
31
return obj;
32
}
33
catch (Exception ex)
34
{
35
log.Error(ex);
36
return null;
37
}
38
}
39
40
public int RecCount(string query, int maxRows, int startRowIndex, string sortClause)
41
{
42
return _numRecs;
43
}
44
45
public static Collection<ProductDescription> RetrievePage(string whereClause, string sortClause, int pageSize, int pageIndex, out int pageCount)
46
{
47
ObjectQuery<ProductDescription> query = new ObjectQuery<ProductDescription>(whereClause, sortClause, pageSize, pageIndex);
48
ObjectSet<ProductDescription> pageSet = Manager.DataManager.GetObjectSet<ProductDescription>(query);
49
pageCount = pageSet.PageCount;
50
return pageSet;
51
}
第一个方法public Collection<ProductDescription> Retrieve(string query, int maxRows, int startRowIndex, string sortClause),这是可以实现内置分页,和排序的方法。需要注意的是这句代码_numRecs = ((IObjectPage)cs).TotalCount; 在这里,分页之后,立即取出总页数,这个是用来供显示页号的;于此对应,方法 public int RecCount(string query, int maxRows, int startRowIndex, string sortClause)就是用来取出记录条数的;注意,这两个方法一定要对应,参数也一样。
第二个方法 static public ObjectSet Retrieve(string Key, string Value)只是普通的取出一条纪录。可以用在DetailView/FormView的显示。
代码看上去虽然很多,但是其实很模式化,所以可以使用CodeSmith或者直接修改一下ORMHelper工具来动态生成,不需要手工写代码。
有了这四个方法,CRUD,分页,排序就已经完成了。这样的Object,和UI无关,只是数据逻辑。
2:UI的配置。UI配置也分两层:GridView等显示控件;ObjectDataSource控件
现在给GridView等控件配置Object数据源,直接连接到Object上,实现显示编辑等功能。其实就是设置一个连接到ObjectDataSource的属性。
<
asp:GridView ID
=
"
gv_data
"
runat
=
"
server
"
AllowPaging
=
"
True
"
AllowSorting
=
"
True
"
DataSourceID
=
"
ods_list
"
这是ObjectDataSource控件的配置
ObjectDataSource
1
<asp:ObjectDataSource ID="ods_list" runat="server" DataObjectTypeName="BusinessModel.ProductDescription"
2
DeleteMethod="Delete" OldValuesParameterFormatString="original_{0}" SelectMethod="Retrieve"
3
TypeName="BusinessModel.ProductDescription" UpdateMethod="Update" SortParameterName="sortClause"
4
MaximumRowsParameterName="maxRows" SelectCountMethod="RecCount" EnablePaging="true"
5
ConflictDetection="OverwriteChanges" ConvertNullToDBNull="false">
6
<SelectParameters>
7
<asp:Parameter Name="query" Type="String" />
8
<asp:Parameter Name="maxRows" Type="Int32" />
9
<asp:Parameter Name="startRowIndex" Type="Int32" />
10
<asp:Parameter Name="sortClause" Type="String" />
11
</SelectParameters>
12
</asp:ObjectDataSource>
看看里面的属性,就是配置CRUD方法的参数,和对应的方法名。这些正是我们在类中实现的。比方说这里配置Delete方法:DeleteMethod = " Delete ";而这里就是刚才说的记录个数的属性:SelectCountMethod="RecCount";还有排序等等。
这里的参数怎么传递?系统相关的属性由系统传递,比方说,maxRows,startRowIndex什么的;也可以用代码来传递:
this
.ods_list.SelectParameters[
"
query
"
].DefaultValue
=
query;
这里的分页不再是从数据库取出所有,然后选择性绑定,而是直接在数据库取出第几页,然后绑定。这个差别还是十分巨大的,效率大大提高。
编辑,创建,排序也都是,直接由ObjectDataSource提供,不需要再GridView中写什么代码。
这样,可以把Object设计的包含有不少逻辑,至少是对数据库操作的,而UI就显得比较简单,剥离的再开一点,对以后移植到win上,或者做成SmartClient都比较有益。
这里有一片blog,讲的比较好 http://www.evosoftworks.com/Articles/wormods.aspx。
我用的正好也是WilsonORM,所以照此也作了一个。
基本的结构是这样的:
UI(GridView等控件--ObjectDataSource控件)----〉ObjectDataSource类(Object,写CRUD分页等逻辑)---〉(ORM实现CRUD)---〉DB
主要有几步
1:给Object增加属性和方法,来完成CRUD,分页等逻辑
2:配置GridView等UI控件连接到ObjectDataSource控件。
先看第一个
1:给Object增加属性和方法,来完成CRUD,分页等逻辑。该Object类由工具根据DB结构生成,同时生成的还有Mapping文件。
首先,给该Object增加一个标示属性DataObject(),在System.ComponentModel命名空间里面





先看一个Insert方法



















这个方法是静态公开的方法;
参数,就是这个Object本身的一个实例。这样比较好,因为在逻辑好很好理解,都是在对Object进行操作。
剩下的,Delete,Update方法也是这样写。
然后看看Select方法,比较特殊。


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

第二个方法 static public ObjectSet Retrieve(string Key, string Value)只是普通的取出一条纪录。可以用在DetailView/FormView的显示。
代码看上去虽然很多,但是其实很模式化,所以可以使用CodeSmith或者直接修改一下ORMHelper工具来动态生成,不需要手工写代码。
有了这四个方法,CRUD,分页,排序就已经完成了。这样的Object,和UI无关,只是数据逻辑。
2:UI的配置。UI配置也分两层:GridView等显示控件;ObjectDataSource控件
现在给GridView等控件配置Object数据源,直接连接到Object上,实现显示编辑等功能。其实就是设置一个连接到ObjectDataSource的属性。

这是ObjectDataSource控件的配置


1

2

3

4

5

6

7

8

9

10

11

12

看看里面的属性,就是配置CRUD方法的参数,和对应的方法名。这些正是我们在类中实现的。比方说这里配置Delete方法:DeleteMethod = " Delete ";而这里就是刚才说的记录个数的属性:SelectCountMethod="RecCount";还有排序等等。
这里的参数怎么传递?系统相关的属性由系统传递,比方说,maxRows,startRowIndex什么的;也可以用代码来传递:
