The ObjectDataSource in asp.net2.0 can enable data display controls such as GridView to be bound for display and editing. It also supports built-in paging, sorting, etc. After using the ORM, you can also use ObjectDataSource.
The paging here no longer takes out all the pages from the database and then selectively binds them, but directly takes out the pages in the database and then binds them. The difference is still huge, and the efficiency is greatly improved.
Editing, creation, and sorting are all provided directly by ObjectDataSource, and there is no need to write any code in GridView.
In this way, the Object design can contain a lot of logic, at least for database operations, and the UI will appear relatively simple. If you peel it off and open it up a little, it will be more beneficial to transplant it to win in the future, or make it into a SmartClient.
Here is a blog that talks better http://www.evosoftworks.com/Articles/wormods.aspx .
I happened to be using WilsonORM, so I made one accordingly.
The basic structure is like this:
UI (GridView and other controls--ObjectDataSource control)----〉ObjectDataSource class (Object, write CRUD paging and other logic)---〉(ORM implements CRUD)---〉DB
has several main steps
1: Add properties and methods to Object to complete CRUD, paging and other logic
2: Configure UI controls such as GridView to connect to the ObjectDataSource control.
Let’s look at the first one first
1: Add properties and methods to Object to complete CRUD, paging and other logic. The Object class is generated by the tool based on the DB structure, and the Mapping file is also generated.
First, add an identification attribute DataObject() to the Object, in the System.ComponentModel namespace [DataObject()]
public class ProductDescription
{Second, add CRUD methods to this object class.
Let’s look at an Insert method first
[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);
}
} This method needs to be preceded by a [DataObjectMethod(DataObjectMethodType.Insert)] attribute, indicating that this is the Insert method;
This method is a static public method;
The parameter is an instance of the Object itself. This is better, because the logic is easy to understand and is all operating on Object.
The rest of the Delete and Update methods are also written in this way.
Then take a look at the Select method, which is quite special.
Select method
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)
twenty two {
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 } The first method is public Collection<ProductDescription> Retrieve(string query, int maxRows, int startRowIndex, string sortClause), which is a method that can implement built-in paging and sorting. It should be noted that this code _numRecs = ((IObjectPage)cs).TotalCount; Here, after paging, the total number of pages is taken out immediately. This is used to display the page number; correspondingly, the method public int RecCount( string query, int maxRows, int startRowIndex, string sortClause) are used to retrieve the number of records; note that these two methods must correspond, and the parameters are the same.
The second method, static public ObjectSet Retrieve(string Key, string Value), simply retrieves a record. Can be used in the display of DetailView/FormView.
Although the code seems to be a lot, it is actually very patterned, so you can use CodeSmith or directly modify the ORMHelper tool to generate it dynamically without writing code manually.
With these four methods, CRUD, paging, and sorting are complete. Such Object has nothing to do with UI, but only data logic.
2: UI configuration. The UI configuration is also divided into two layers: GridView and other display controls; ObjectDataSource control
Now configure the Object data source for controls such as GridView and directly connect to the Object to implement functions such as display and editing. In fact, it is to set a property connected to the ObjectDataSource.
<asp:GridView ID="gv_data" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="ods_list"
This is the configuration of the ObjectDataSource control
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>
Take a look at the properties inside, which are the parameters for configuring the CRUD method and the corresponding method name. These are exactly what we implement in the class. For example, the Delete method is configured here: DeleteMethod="Delete"; and here is the attribute of the number of records just mentioned: SelectCountMethod="RecCount"; and sorting and so on.
How to pass the parameters here? System-related attributes are passed by the system, for example, maxRows, startRowIndex, etc.; they can also be passed by code: this.ods_list.SelectParameters["query"].DefaultValue = query;
http://dlwang2002.cnblogs.com/archive /2006/06/11/422991.html