有人在.NET中使用類型化的DataSet嗎?

[英]Does anyone use typed DataSets in .NET?


I have once tried to use typed DateSets in a relatively small production application. Initially it looked like a pretty good idea, but turned out to be different. It was pretty fine for some basic tasks, but as soon as something more advanced was required, it's limitations kicked in and it failed miserably. Luckily the project got cancelled, and from now on I try to stick to a proper ORM like NHibernate.

我曾經嘗試在相對較小的生產應用程序中使用類型化的DateSet。最初它看起來是一個非常好的主意,但事實證明是不同的。對於一些基本任務來說這是相當不錯的,但是只要需要更先進的東西,它就會受到限制並且失敗了。幸運的是,該項目被取消了,從現在開始,我嘗試堅持使用像NHibernate這樣的ORM。

But I still wonder - they were created for a reason. Perhaps I just didn't understand how to use them properly? Is anyone out there successfully using them in production systems?

但我仍然想知道 - 他們是出於某種原因而創建的。也許我只是不明白如何正確使用它們?是否有人在生產系統中成功使用它們?

Added:

Could you also quickly explain how you are using them?

你能快速解釋一下你是如何使用它們的嗎?

I tried to use them as my DAL - it was a Windows Forms applications, and it would fetch data from tables into the DataSet and then manipulate with the data, before calling the TableManager's hierarchial update thing (don't remember the exact name). The DataSet had one table for each of the DB's physical tables. The problems started when I had to do something like a master/details relationship where I had to insert a bunch of records at once (one master record and several details records) into several tables while also keeping foreign keys correct.

我嘗試將它們用作我的DAL - 它是一個Windows窗體應用程序,它會將表中的數據提取到DataSet中,然后在調用TableManager的層次更新之前對數據進行操作(不記得確切的名稱)。 DataSet為每個DB的物理表都有一個表。當我不得不做一些像主/細節關系這樣的事情時我就不得不一次插入一堆記錄(一個主記錄和幾個細節記錄)到幾個表中,同時保持外鍵正確。

Added 2:

Oh, and if you are using them, where do you put your business logic then? (Validations, calculations, etc.)

哦,如果你正在使用它們,那么你在哪里放置你的業務邏輯呢? (驗證,計算等)

7 个解决方案

#1


We use typed data sets all the time, in fact we use them as a matter of best practice. The reason we do this is to catch errors at compile time rather than using string based lookups for column names that could introduce errors at run time.

我們一直使用類型化數據集,實際上我們將它們用作最佳實踐。我們這樣做的原因是在編譯時捕獲錯誤,而不是對可能在運行時引入錯誤的列名使用基於字符串的查找。

I guess, as always, it depends on the scenario and whether you gain any advantage for using them or not.

我想,一如既往,這取決於場景以及你是否獲得使用它們的任何優勢。

Normal Row Reference:

正常行參考:

oRow["row_pk"]

In a typed data set it now becomes:

在類型化數據集中,它現在變為:

oRow.row_pk

Our data sets usually match the database scema, we use DataAdapters to update the changes to the database using stored procedures. Output parameters update the data set with keys generated from the database.

我們的數據集通常與數據庫scema匹配,我們使用DataAdapters使用存儲過程更新對數據庫的更改。輸出參數使用從數據庫生成的密鑰更新數據集。

Obviously you need to run the adapter updates in the right order for all the keys to generate in the right order. You also have to be careful when deleting parent / child rows, and ensure these deletes take place in the right order to prevent database exceptions.

顯然,您需要以正確的順序運行適配器更新,以便按正確的順序生成所有密鑰。刪除父/子行時也必須小心,並確保以正確的順序進行這些刪除以防止數據庫異常。

Back when .NET was still 1.1 I read a book on ADO.NET by David Sceppa, this opened my eyes to what can actually be acheived, and simplified my DAL a lot (using typed data sets). There are a lot of techniques in there that can really help improve your code, I'd highly recommend it.

回到.NET還是1.1時,我讀了David Sceppa的一本關於ADO.NET的書,這讓我看到了實際可以實現的東西,並簡化了我的DAL(使用類型數據集)。有很多技術可以真正幫助改進你的代碼,我強烈推薦它。

#2


They were useful for me to create XML documents to send to a web service. The client defined the expected data as typed DataSets and that made it very simple to create a DataTable in memory and get the XML representation.

它們對我來說非常有用,可以創建XML文檔以發送到Web服務。客戶端將預期數據定義為類型化DataSet,這使得在內存中創建DataTable並獲取XML表示非常簡單。

Now, for real database access... I use my own DAL, I agree with you - DataSets are incredibly limited.

現在,對於真正的數據庫訪問...我使用自己的DAL,我同意你的看法 - DataSet非常有限。

#3


I use them when: 1) Writing a "1-offs" that are ditched once the project we are working on is complete. 2) Using XML files to swap data with other vendors. 3) Prototyping and testing,

我在以下情況下使用它們:1)在我們正在進行的項目完成后,編寫一個“1-offs”。 2)使用XML文件與其他供應商交換數據。 3)原型設計和測試,

I have had trouble with typed dataset when: 1) Complex queries are required to filter and save the data. (at least for me) 2) Not knowing enough of what the Visual Studio designer writes on my behalf so that I can extend the derived class that is created for typed datasets.

在以下情況下我遇到類型化數據集的問題:1)需要復雜查詢來過濾和保存數據。 (至少對我而言)2)不了解Visual Studio設計器為我寫的內容,以便我可以擴展為類型化數據集創建的派生類。

I've never had a speed issue with typed datasets and the users seem happy with the applications built with them. To be honest, I have not benchmarked the different methods that could replace typed datasets. Lastly, I have been saved more than once by the design time type checking that would have been missed by a novice programmer like myself by using typed datasets.

我從未遇到類型化數據集的速度問題,用戶似乎對使用它們構建的應用程序感到滿意。說實話,我沒有對可以替換類型化數據集的不同方法進行基准測試。最后,通過使用類型化數據集,像我這樣的新手程序員錯過了設計時間類型檢查,我不止一次保存了。

Regards,

DeBug

#4


I'm using them. Perhaps only because I haven't tried NHibernate, but I can't use LINQ, because I'm limited to Windows 2000, which doesn't run .NET 3. At first they seemed ideal, but I've discovered enough weird quirks about them to start turning me off.

我正在使用它們。也許只是因為我沒有嘗試過NHibernate,但是我不能使用LINQ,因為我只限於運行.NET 3的Windows 2000.起初他們看起來很理想,但我發現了足夠奇怪的怪癖關於他們開始讓我失望。

#5


They are exactly what's required if you believe in the benefit of software writing software, and static typing - software catching software errors - at the cost of eomplexity (for sure), development overhead (probably), and efficiency (possible but arguable) - which is the basic premise of languages like C# and java.

如果您相信軟件編寫軟件的好處,以及靜態類型 - 軟件捕獲軟件錯誤 - 以復雜性(肯定),開發開銷(可能)和效率(可能但可論證)為代價,它們正是必需的 - 是C#和java等語言的基本前提。

That, however, isn't a battle that's been completely won yet.

然而,這並不是一場完全贏得的戰斗。

EDIT (requested explication):

編輯(請求解釋):

Complexity.

Using either C# or java, you end up with a lot of code for configuration, type declaration, casting, type-checking and verification. Some of it you write yourself, some the IDE writes for you. But it's all code the developer is responsible for. The primary benefits are for the IDE/compiler - pointing out possible software errors, and autocompletion. I can safely say without taking either side that there's an industry discussion whether the sheer volume in lines of code is worth it. It's at least a clear violation of YAGNI. In VS, I commonly come across whole files of code written by the IDE, that has bugs in it that I end up needing to figure out. I don't like figuring out code I didn't write.

使用C#或java,最終會得到大量用於配置,類型聲明,轉換,類型檢查和驗證的代碼。你自己寫的一些,IDE為你寫的一些。但這是開發人員負責的所有代碼。主要的好處是IDE /編譯器 - 指出可能的軟件錯誤和自動完成。我可以有把握地說,沒有任何一方,業界討論代碼行中的絕對數量是否值得。它至少明顯違反了YAGNI。在VS中,我經常遇到由IDE編寫的整個代碼文件,其中包含我最終需要弄清楚的錯誤。我不喜歡弄清楚我沒寫的代碼。

Development overhead.

Ditto the above. And java is well-known for all the various XML configuration files you need to write and maintain to make an application fit together. A lot of the appeal of RoR is "configuration by convention", just to avoid this.

同上以上。 java是眾所周知的,您需要編寫和維護所有各種XML配置文件,以使應用程序能夠組合在一起。 RoR的很多吸引力是“按慣例配置”,只是為了避免這種情況。

Efficiency.

The assertion that interpreted or JIT-compiled languages are horribly inefficient compared to compiled languages has long been accepted as self-evident. For more reasons than I've got time for here, that assumption is being questioned; for example by some of the newer, faster javascript engines.

與編譯語言相比,解釋或JIT編譯的語言非常低效的斷言長期以來被認為是不言而喻的。由於更多原因而不是我有時間在這里,這個假設受到質疑;例如,一些更新,更快的JavaScript引擎。

The obvious things to search for on this site, and google for, are "late binding", "dynamic typing", "JIT compiler".

在這個網站上搜索的明顯的東西,以及google for,是“后期綁定”,“動態類型”,“JIT編譯器”。

#6


I use them, but not in the usual sense. It's akin to ocdecio's answer, although in my case it's as a presentation model (without table adapters) in asp.net

我使用它們,但不是通常意義上的。它類似於ocdecio的答案,雖然在我的例子中它是asp.net中的表示模型(沒有表適配器)

The "client services" layer uses them (as part of the contract) for any method that takes or returns data. The underlying business services use more traditional poco approach and the client services just do a bit of mapping to produce a strongly typed dataset for the given ui.

“客戶服務”層使用它們(作為合同的一部分)用於獲取或返回數據的任何方法。底層業務服務使用更傳統的poco方法,客戶端服務只需進行一些映射即可為給定的ui生成強類型數據集。

The tooling support makes it much easier for newer developers (who can follow the asp.net/learn videos to get up to speed) to use the services they require. The UI builders can also quickly "design" a new set of data that they'd like to retrieve and have the relevant service's team go from there.

工具支持使新開發人員(可以跟隨asp.net /學習視頻加快速度)更容易使用他們需要的服務。 UI構建器還可以快速“設計”他們想要檢索的新數據集,並讓相關服務團隊從那里開始。

#7


I still use them more than I wish I did... They are definitely better than untyped datasets, but you have to be careful of nullable value type fields. If you have an Int field which can contain a null value, you cannot try to access the field when it has a null or it will throw an exception. You have to call IsMyIntFieldNull() first to check if it is/is not null.. This means ANYWHERE your code references one of these fields, you need to do this check first... which easily adds up to a huge amount of extra code. It would be better if typed datasets supports nullable value fields for these columns, but unfortunately they do not.

我仍然使用它們比我希望的更多...它們肯定比無類型數據集更好,但你必須小心可以為空的值類型字段。如果您有一個可以包含空值的Int字段,則當它具有null值時它不能嘗試訪問該字段,否則它將引發異常。你必須首先調用IsMyIntFieldNull()來檢查它是否為空。這意味着你的代碼無論何時引用其中一個字段,你需要先進行檢查...這很容易增加額外的額外數量碼。如果類型化數據集支持這些列的可空值字段會更好,但不幸的是它們不支持。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/01/04/72fe3a45da0235f6371bfa441a57a160.html



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