简体   繁体   中英

ASP.NET Databinding on Page or in each UserControl?

Is it better to perform data access operations on the page and then populate the properties of each usercontrol with the retrieved data.

--OR--

Is it better if each UC calls the business layer and pulls its own data independently without having to depend on the page.

Existing Conditions: 1. Any information (Querystring, URL, etc) that the page would use for data access is also available to the individual UC's 2. Both solutions utilize caching.

My coworker insists that the first option is better since Data access only happens once on the page.

The second option seems more logical to me since it will allow me to place the UC on another page without the need for any additional wiring on the page itself. The page and the UC are truly decoupled.

Please advise on why one would be better than the other.

The first option is the better in most cases.

A UserControl is not the controller but the page. It should display data not access it. The page normally sets the UserControl's properties.

One of it's purpose is reusability . This will be decreased if it's hardlinked with datasources. It should work with any kind of DAL, it shouldn't bother with them at all.

But if it's only purpose is encapsulation (fe as container control used in a TabContainer's TabPanel) a UserControl is a page replacement, hence does also the data access and event handling. You wouldn't reuse this kind of UserControl often.

Both of you are correct.

If you have any plans to reuse the user control, then you should encapsulate data access in that control.

However, if you have more than one user control on a page, it will certainly be more efficient to access the data once.

What we often do in situations like is is implement a hybrid solution: either the user controls are hosted on pages that implement a data retrieval interface (they pull from the page) or the user controls implement an interface that indicates the kind of data that they need (the page then pushes the data to the user control).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM