DataBinding對手動查詢/添加到控件的好處

[英]Benefits of DataBinding over Manually Querying / Adding to Control


I've been a C# programmer for about 2 years total, and professionally for a little more than 1. I work at a company as a developer on an application that began before the days of .NET 2. My question is this:

我已經是一名C#程序員,總共約2年,而且專業性超過1.我作為開發人員在一家公司工作,開始於.NET 2之前的應用程序。我的問題是:

What is the benefit to use databinding from sql queries directly to a control over querying and manually adding items to the control? Is this approach generally considered cleaner, and simpler in terms of deploying? Will there still be cases where manually adding would give more fine grained control over the display / value of the data?

從sql查詢直接使用數據綁定到控件查詢和手動向控件添加項目有什么好處?這種方法通常被認為是更清潔,在部署方面更簡單嗎?是否仍會出現手動添加會對數據的顯示/值進行更精細控制的情況?

I'm asking because I've been assigned some bugs that deal with some controls that populate themselves based off query results, and would love to clean up unnecessary logic and push that off to the libraries to handle instead of me.

我問的是因為我已經分配了一些錯誤來處理一些根據查詢結果填充自己的控件,並且希望清理不必要的邏輯並將其推送到庫來處理而不是我。

Note: We are using .NET 2.0

注意:我們使用的是.NET 2.0

5 个解决方案

#1


5  

I personally find that using the

我個人覺得使用了

control.DataSource = YourSource;
control.DataBind();

process is much easier, you don't have to do the iteration, and overall reduces LOC.

過程要容易得多,您不必進行迭代,總體上減少了LOC。

If working with DropDownLists and other controls you will most likely set the DataValueField and DataTextField properties as well.

如果使用DropDownLists和其他控件,您很可能也會設置DataValueField和DataTextField屬性。

#2


1  

Data binding is much easier to set up, less error prone overall, reduces LOC significantly (as Mitchel Sellers said), and, a few minor glitches aside, works fairly reliably.

數據綁定更容易設置,總體上更不容易出錯,顯着降低LOC(如Mitchel Sellers所說),並且除了一些小故障外,工作相當可靠。

In my experience, you only actually need full manual control if you need to specify the exact update order or timing for data bound controls.

根據我的經驗,如果您需要指定數據綁定控件的確切更新順序或時間,則實際上只需要完全手動控制。

#3


1  

Information joining is much simpler to set up, less problem subject overall, lowers LOC considerably, and, a few modest mistakes aside, will work pretty easily. In my experience, you only actually need full guide control if you need to specify the actual up-date order or time for details certain handles.

信息加入設置起來要簡單得多,整體問題主題較少,大大降低了LOC,而且除了一些適度的錯誤之外,它們的工作非常簡單。根據我的經驗,如果您需要指定實際的最新訂單或時間以獲取特定句柄的詳細信息,您實際上只需要完整的指南控制。

#4


0  

It can be useful to manually bind if you've got a complex input scenario. With databound input controls you can find that you only know you've got bad data when it hits the DB and throws an exception (badly formatted date/time, integer out of correct range etc).

如果您有復雜的輸入方案,手動綁定會很有用。使用數據綁定輸入控件,您可以發現只有在遇到數據庫並且拋出異常(格式錯誤的日期/時間,整數超出正確范圍等)時才知道您有錯誤的數據。

You can obviously handle this with the various validation / pre-commit events on the data controls but it can be easier (and more obviously readable) to just manually validate your input and post it when you know it's correct.

您可以使用數據控件上的各種驗證/預提交事件來處理這一點,但只需手動驗證您的輸入並在您知道它正確時發布它就可以更容易(並且更明顯可讀)。

That's the only reason I can think of and it's only applicable for input. If you're in a read-only scenario then databinding is a no-brainer.

這是我能想到的唯一原因,它只適用於輸入。如果您處於只讀方案中,則數據綁定是一個明智的選擇。

#5


0  

My experiences were quite the opposite to my previous posters here. This blog entry (dated 2006) summarizes my feelings about databinding.

我的經歷與我此前的海報完全相反。這篇博客文章(日期為2006年)總結了我對數據綁定的感受。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2008/10/30/7207047e711fad04651b8ed330b12172.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com