简体   繁体   English

页面或每个UserControl中的ASP.NET数据绑定?

[英]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. 如果每个UC都调用业务层并独立提取自己的数据而不必依赖页面,那会更好吗?

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. 现有条件: 1.该页面用于数据访问的任何信息(查询字符串,URL等)也可用于各个UC。2.两种解决方案均使用缓存。

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. 第二种选择对我来说似乎更合乎逻辑,因为它使我可以将UC放置在另一页上,而无需在页面本身上进行任何附加布线。 The page and the UC are truly decoupled. 页面和UC真正分离了。

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. UserControl不是控制器,而是页面。 It should display data not access it. 它应该显示无法访问的数据。 The page normally sets the UserControl's properties. 该页面通常设置UserControl的属性。

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. 它应该与任何类型的DAL一起使用,完全不应该打扰他们。

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. 但是,如果它的唯一目的是封装 (例如,作为TabContainer的TabPanel中使用的容器控件),则UserControl可以替换页面,因此也可以进行数据访问和事件处理。 You wouldn't reuse this kind of UserControl often. 您不会经常重复使用这种UserControl。

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). 在类似情况下,我们经常做的是实现一个混合解决方案:用户控件托管在实现数据检索界面(它们从页面中拉出)的页面上,或者用户控件实现指示其数据类型的界面需要(然后页面将数据推送到用户控件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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