Asp.net2.0:如何使用ObjectDataSource
生活随笔
收集整理的這篇文章主要介紹了
Asp.net2.0:如何使用ObjectDataSource
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
asp.net2.0里面的ObjectDataSource可以使數(shù)據(jù)顯示控件GridView等進(jìn)行綁定顯示,編輯。還可以支持內(nèi)置的分頁(yè),排序等。使用了ORM之后,一樣可以使用ObjectDataSource。
這里的分頁(yè)不再是從數(shù)據(jù)庫(kù)取出所有,然后選擇性綁定,而是直接在數(shù)據(jù)庫(kù)取出第幾頁(yè),然后綁定。這個(gè)差別還是十分巨大的,效率大大提高。
編輯,創(chuàng)建,排序也都是,直接由ObjectDataSource提供,不需要再GridView中寫什么代碼。
這樣,可以把Object設(shè)計(jì)的包含有不少邏輯,至少是對(duì)數(shù)據(jù)庫(kù)操作的,而UI就顯得比較簡(jiǎn)單,剝離的再開一點(diǎn),對(duì)以后移植到win上,或者做成SmartClient都比較有益。
這里有一片blog,講的比較好http://www.evosoftworks.com/Articles/wormods.aspx。
我用的正好也是WilsonORM,所以照此也作了一個(gè)。
基本的結(jié)構(gòu)是這樣的:
UI(GridView等控件--ObjectDataSource控件)----〉ObjectDataSource類(Object,寫CRUD分頁(yè)等邏輯)---〉(ORM實(shí)現(xiàn)CRUD)---〉DB
主要有幾步
1:給Object增加屬性和方法,來完成CRUD,分頁(yè)等邏輯
2:配置GridView等UI控件連接到ObjectDataSource控件。
先看第一個(gè)
1:給Object增加屬性和方法,來完成CRUD,分頁(yè)等邏輯。該Object類由工具根據(jù)DB結(jié)構(gòu)生成,同時(shí)生成的還有Mapping文件。
????? 首先,給該Object增加一個(gè)標(biāo)示屬性DataObject(),在System.ComponentModel命名空間里面 [DataObject()]
public?class?ProductDescription?
?{ ???? 第二,給這個(gè)object類增加CRUD的方法。
???????先看一個(gè)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);
????????????}
????????} ?????? 這個(gè)方法前面需要加一個(gè)[DataObjectMethod(DataObjectMethodType.Insert)]屬性,表示這是Insert方法;
?????? 這個(gè)方法是靜態(tài)公開的方法;
?????? 參數(shù),就是這個(gè)Object本身的一個(gè)實(shí)例。這樣比較好,因?yàn)樵谶壿嫼煤芎美斫?#xff0c;都是在對(duì)Object進(jìn)行操作。
?????? 剩下的,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????????} ?????????? 第一個(gè)方法public Collection<ProductDescription> Retrieve(string query, int maxRows, int startRowIndex, string sortClause),這是可以實(shí)現(xiàn)內(nèi)置分頁(yè),和排序的方法。需要注意的是這句代碼_numRecs = ((IObjectPage)cs).TotalCount; 在這里,分頁(yè)之后,立即取出總頁(yè)數(shù),這個(gè)是用來供顯示頁(yè)號(hào)的;于此對(duì)應(yīng),方法 public int RecCount(string query, int maxRows, int startRowIndex, string sortClause)就是用來取出記錄條數(shù)的;注意,這兩個(gè)方法一定要對(duì)應(yīng),參數(shù)也一樣。
????????? 第二個(gè)方法 static public ObjectSet Retrieve(string Key, string Value)只是普通的取出一條紀(jì)錄。可以用在DetailView/FormView的顯示。
?????????? 代碼看上去雖然很多,但是其實(shí)很模式化,所以可以使用CodeSmith或者直接修改一下ORMHelper工具來動(dòng)態(tài)生成,不需要手工寫代碼。
?????????? 有了這四個(gè)方法,CRUD,分頁(yè),排序就已經(jīng)完成了。這樣的Object,和UI無關(guān),只是數(shù)據(jù)邏輯。
2:UI的配置。UI配置也分兩層:GridView等顯示控件;ObjectDataSource控件
?????? 現(xiàn)在給GridView等控件配置Object數(shù)據(jù)源,直接連接到Object上,實(shí)現(xiàn)顯示編輯等功能。其實(shí)就是設(shè)置一個(gè)連接到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方法的參數(shù),和對(duì)應(yīng)的方法名。這些正是我們?cè)陬愔袑?shí)現(xiàn)的。比方說這里配置Delete方法:DeleteMethod="Delete";而這里就是剛才說的記錄個(gè)數(shù)的屬性:SelectCountMethod="RecCount";還有排序等等。
???????? 這里的參數(shù)怎么傳遞?系統(tǒng)相關(guān)的屬性由系統(tǒng)傳遞,比方說,maxRows,startRowIndex什么的;也可以用代碼來傳遞: ?this.ods_list.SelectParameters["query"].DefaultValue?=?query;
這里的分頁(yè)不再是從數(shù)據(jù)庫(kù)取出所有,然后選擇性綁定,而是直接在數(shù)據(jù)庫(kù)取出第幾頁(yè),然后綁定。這個(gè)差別還是十分巨大的,效率大大提高。
編輯,創(chuàng)建,排序也都是,直接由ObjectDataSource提供,不需要再GridView中寫什么代碼。
這樣,可以把Object設(shè)計(jì)的包含有不少邏輯,至少是對(duì)數(shù)據(jù)庫(kù)操作的,而UI就顯得比較簡(jiǎn)單,剝離的再開一點(diǎn),對(duì)以后移植到win上,或者做成SmartClient都比較有益。
這里有一片blog,講的比較好http://www.evosoftworks.com/Articles/wormods.aspx。
我用的正好也是WilsonORM,所以照此也作了一個(gè)。
基本的結(jié)構(gòu)是這樣的:
UI(GridView等控件--ObjectDataSource控件)----〉ObjectDataSource類(Object,寫CRUD分頁(yè)等邏輯)---〉(ORM實(shí)現(xiàn)CRUD)---〉DB
主要有幾步
1:給Object增加屬性和方法,來完成CRUD,分頁(yè)等邏輯
2:配置GridView等UI控件連接到ObjectDataSource控件。
先看第一個(gè)
1:給Object增加屬性和方法,來完成CRUD,分頁(yè)等邏輯。該Object類由工具根據(jù)DB結(jié)構(gòu)生成,同時(shí)生成的還有Mapping文件。
????? 首先,給該Object增加一個(gè)標(biāo)示屬性DataObject(),在System.ComponentModel命名空間里面 [DataObject()]
public?class?ProductDescription?
?{ ???? 第二,給這個(gè)object類增加CRUD的方法。
???????先看一個(gè)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);
????????????}
????????} ?????? 這個(gè)方法前面需要加一個(gè)[DataObjectMethod(DataObjectMethodType.Insert)]屬性,表示這是Insert方法;
?????? 這個(gè)方法是靜態(tài)公開的方法;
?????? 參數(shù),就是這個(gè)Object本身的一個(gè)實(shí)例。這樣比較好,因?yàn)樵谶壿嫼煤芎美斫?#xff0c;都是在對(duì)Object進(jìn)行操作。
?????? 剩下的,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????????} ?????????? 第一個(gè)方法public Collection<ProductDescription> Retrieve(string query, int maxRows, int startRowIndex, string sortClause),這是可以實(shí)現(xiàn)內(nèi)置分頁(yè),和排序的方法。需要注意的是這句代碼_numRecs = ((IObjectPage)cs).TotalCount; 在這里,分頁(yè)之后,立即取出總頁(yè)數(shù),這個(gè)是用來供顯示頁(yè)號(hào)的;于此對(duì)應(yīng),方法 public int RecCount(string query, int maxRows, int startRowIndex, string sortClause)就是用來取出記錄條數(shù)的;注意,這兩個(gè)方法一定要對(duì)應(yīng),參數(shù)也一樣。
????????? 第二個(gè)方法 static public ObjectSet Retrieve(string Key, string Value)只是普通的取出一條紀(jì)錄。可以用在DetailView/FormView的顯示。
?????????? 代碼看上去雖然很多,但是其實(shí)很模式化,所以可以使用CodeSmith或者直接修改一下ORMHelper工具來動(dòng)態(tài)生成,不需要手工寫代碼。
?????????? 有了這四個(gè)方法,CRUD,分頁(yè),排序就已經(jīng)完成了。這樣的Object,和UI無關(guān),只是數(shù)據(jù)邏輯。
2:UI的配置。UI配置也分兩層:GridView等顯示控件;ObjectDataSource控件
?????? 現(xiàn)在給GridView等控件配置Object數(shù)據(jù)源,直接連接到Object上,實(shí)現(xiàn)顯示編輯等功能。其實(shí)就是設(shè)置一個(gè)連接到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方法的參數(shù),和對(duì)應(yīng)的方法名。這些正是我們?cè)陬愔袑?shí)現(xiàn)的。比方說這里配置Delete方法:DeleteMethod="Delete";而這里就是剛才說的記錄個(gè)數(shù)的屬性:SelectCountMethod="RecCount";還有排序等等。
???????? 這里的參數(shù)怎么傳遞?系統(tǒng)相關(guān)的屬性由系統(tǒng)傳遞,比方說,maxRows,startRowIndex什么的;也可以用代碼來傳遞: ?this.ods_list.SelectParameters["query"].DefaultValue?=?query;
總結(jié)
以上是生活随笔為你收集整理的Asp.net2.0:如何使用ObjectDataSource的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新年/新春祝福标语文案28句
- 下一篇: 怎样填写个人简历较有效