国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Asp.net2.0:如何使用ObjectDataSource(配合ORM)

2019-11-18 17:09:48
字體:
來源:轉載
供稿:網友

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;

http://dlwang2002.VEVb.com/archive/2006/06/11/422991.html


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 马公市| 电白县| 叙永县| 公安县| 汶川县| 六盘水市| 荥经县| 九寨沟县| 大田县| 和硕县| 任丘市| 丰镇市| 舞阳县| 潼关县| 玉门市| 平和县| 锡林郭勒盟| 即墨市| 凤凰县| 弥勒县| 嘉荫县| 施秉县| 高要市| 若羌县| 亳州市| 富顺县| 舟曲县| 图们市| 高密市| 得荣县| 马公市| 安顺市| 恩平市| 即墨市| 霍山县| 合山市| 玉门市| 汉阴县| 太湖县| 湖北省| 磐石市|