asp.net 2.0 ObjectDataSource Control

Source: Internet
Author: User

The ObjectDataSource control is similar to the SqlDataSource control's object model. ObjectDataSource has no ConnectionString property, exposing the TypeName property to specify the object type (class name) to perform the data operation that needs to be instantiated. The ObjectDataSource control is similar to the SqlDataSource Command property, and also supports properties such as SelectMethod, UpdateMethod, InsertMethod, and DeleteMethod. A method that indicates the type of association to perform these data operations. This article explains the techniques for building the data access layer and business logic layer components and displaying the ASP.net 2.0 data components through ObjectDataSource objects.

Binding to the data access layer

The data access layer component encapsulates the Ado.net code that queries and modifies the database using SQL commands. In a typical case, it abstracts the details of establishing ado.net connections and commands, exposing methods that can be invoked through the appropriate parameters. A typical data access layer component might expose some of the following methods:

public class MyDataLayer {
 public DataView GetRecords();
 public DataView GetRecordsByCategory(String categoryName);
 public DataView GetRecordByID(int recordID);

 public int UpdateRecord(int recordID, String recordData);
 public int DeleteRecord(int recordID);
 public int InsertRecord(int recordID, String recordData);
}

ObjectDataSource can be associated with this type in the following ways:

<asp:ObjectDataSource TypeName="MyDataLayer" SelectMethod="GetRecords" UpdateMethod="UpdateRecord"
DeleteMethod="DeleteRecord" InsertMethod="InsertRecord" runat="server"/>

ObjectDataSource requires objects to have a very special design pattern. These constraints are caused by a stateless (stateless) environment where Web application requests are made. Because in typical cases, objects are created and destroyed to serve a request, objects bound by an object data source are stateless. By default, ObjectDataSource uses the default constructor of the type specified by the TypeName property (without parameters), although a custom object instance is established by handling the ObjectCreating event. and assign it to the ObjectInstance attribute of the event parameter, so it is feasible to implement the instantiation. The object method associated with the SelectMethod property can return any object, IEnumerable list, collection, or array. In the above example of the data access layer, the DataView object implements the IEnumerable interface. As we'll discuss in the next section, these methods can also return a collection or object of the hardening type.

GetProducts() -> ProductCollection
GetProductsDataSet() -> DataSet
GetProduct (int productId) -> Product

Update, insert, and delete generally take a separate data item field as a parameter, or a collection class object with a public property of a data item field as a parameter.

UpdateProduct (int id, String name, double price, bool inStock)
UpdateProduct (Product p) // p.ID, p.Name, p.Price, p.InStock
DeleteProduct (int id)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.