Objectdatasoruce binds the businessobject Control

Source: Internet
Author: User
Summary
In ASP. NET, The objectdatasoruce control is the key to implementing a three-tier approach. We can use the objectdatasoruce control to customize the Middle-Tier Business Objects. However, the examples found are directly tied to the middle-layer business objects. Although the examples are usually quite object-oriented, there are the following problems in practical use.

Problem 1: Poor maintainability
For example, the update method of the employee business object may be written in the following two ways:
[Statement 1] In the update method, each field is an independent variable.
Public Function updateemployee (employeeid as integer, lastname as string, firstname as string ,_
Address as string, city as string, region as string ,_
Postalcode as string) as integer
See: http://msdn.microsoft.com/zh-tw/library/ms178538 (vs.80). aspx
 
[Statement 2] The update method uses a strong type. Each field corresponds to a category attribute.
Public Function updateemployee (employee as northwindemployee) as integer
See: http://msdn.microsoft.com/zh-tw/library/ms227562 (vs.80). aspx
 
But does it actually work like this? Take [method 1] as an example. If there are many change fields, the independent variables of the update method will not be too many to scare people. [method 2] using a strong type can solve the problem of multiple fields, however, it is not difficult to maintain the northwindemployee category at the same time when adding or removing fields? When your system is very large, for example, there are thousands of forms. If you want to add a field to these forms at the same time, you need to update thousands of corresponding categories at the same time, it may drive you crazy.
 
Question 2: Batch Change Problems
When the gridview binds the objectdatasource and stores it when editing a record, sometimes we do not want the data to be changed back to the database immediately, that is, the objectdatasoruce update method is executed, but the data should not be actually written back to the database; instead, all edited data is saved for temporary storage, and a method (such as Save) is executed to write the changes to the data at the same time. Objectdatasource is difficult to solve this problem.
 
Question 3: Mater-Detail
Some forms have a mater-Detail relationship (such as an order), but objectdatasource is difficult to describe this relationship, because the role of objectdatasource is equivalent to a able, while Mater-detal is a set of multiple datatable, it is equivalent to dataset. That is to say, you need to have multiple objectdatasource to describe the master-detal relationship. However, changes to these objectdatasource cannot be written back to the database immediately, and the form editing actions must be saved, you can write the entire dataset change back to the database only when you press save.
 
Tbbusinessobject and tbobjectdatasource controls
To solve the preceding problems, we can use another method to tie the objectdatasource to the businessobject control. The businessobject control provides the dataset attribute to temporarily store changes generated by the objectdatasource. When calling a real middle-layer business object, the dataset attribute of the businessobject control is passed in, and the middle-layer business object is used to change the database.
In the following example, the objectdatasource is closed by the gridview. When the gridview modifies or deletes the data, the objectdatasource is used to call the businessobject control for the change, which is not written back to the database, the dataset attribute of the businessobject control is changed.
Generally
Gridview --> objectdatasoruce --> middle layer businessobject
The practice here is:
Gridview --> objectdatasoruce --> businessobject control --> middle layer businessobject

Place the tbbusinessobject control and the tbobjectdatasource control on the page. In the tbobjectdatasource control, set businessobjectid = "tbbusinessobject1" to end the tbbusinessobject control.
<Bee: tbbusinessobject id = "tbbusinessobject1" runat = "server">
</Bee: tbbusinessobject>
<Bee: tbobjectdatasource id = "tbobjectperformance1" runat = "server"
Oldvaluesparameterformatstring = "original _ {0}" rowindex = "-1"
Typename = "Bee. Web. webcontrols. tbbusinessobject"
Businessobjectid = "tbbusinessobject1"
Tablename = "employee">
</Bee: tbobjectdatasource>

A employable generated during page load, and added the datatable to tbbusinessobject. dataset. Tables. When performing update and delete operations on the gridview, you must set the rowindex of tbobjectdatasource and change it to the corresponding datarow.

Partial class _ defaultclass _ default
Inherits system. Web. UI. Page

Protected sub page_load () sub page_load (byval sender as object, byval e as system. eventargs) handles me. Load
If not me. ispostback then
Dim otable as data. datatable
Dim ocolumn as data. datacolumn
Dim orow as data. datarow

Otable = new data. datatable ("employee ")

Ocolumn = new data. datacolumn ("ID", GetType (string ))
Otable. Columns. Add (ocolumn)
Ocolumn = new data. datacolumn ("name", GetType (string ))
Otable. Columns. Add (ocolumn)
Ocolumn = new data. datacolumn ("tel", GetType (string ))
Otable. Columns. Add (ocolumn)

Orow = otable. newrow ()
Orow ("ID") = "001"
Orow ("name") = "Release 3"
Orow ("tel") = "02-11111111"
Otable. Rows. Add (orow)

Orow = otable. newrow ()
Orow ("ID") = "002"
Orow ("name") = "Li Si"
Orow ("tel") = "02-22222222"
Otable. Rows. Add (orow)

Orow = otable. newrow ()
Orow ("ID") = "003"
Orow ("name") = "Wang Wu"
Orow ("tel") = "02-33333333"
Otable. Rows. Add (orow)

Tbbusinessobject1.dataset. Tables. Add (otable)
Gridview1.databind ()
End if
End sub

Protected sub gridview1_rowcommand () sub gridview1_rowcommand (byval sender as object, byval e as system. Web. UI. webcontrols. gridviewcommandeventargs) handles gridview1.rowcommand
Select case e. commandname
Case "Update"
Tbobjectperformance1.rowindex = CINT (E. commandargument)
Case "delete"
Tbobjectperformance1.rowindex = CINT (E. commandargument)
End select
End sub
End Class

Execute the program. The gridview retrieves the employee able in tbbusinessobject through tbobjectdatasource and displays it in the gridview.

Press the edit button to edit the data column.


Press the update button to write the change back to the employee able in tbbusinessobject.

When the delete button is pressed, The datarow of the employee able in tbbusinessobject will also be deleted.



Example program: businessobjectcontrol.rar

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.