Learning Data Binding with Windows Forms 2.0(1)-The Quest for Type Safety

Problem
Take a look at the code:
Code
If you believe in writing maintainable code, seeing hard-coded column and table names and/or indices like in this code example should make you nervousif the underlying schema changes, you have to hunt down all the places where the table and column names are used. If you use indices to get at the columns in a row, as the last line of code in this snippet does, the problem is even worse. At best, you can use find and replace, which is error prone and time consuming; often you will miss something, and you won't know about the problem until runtime.
Solution
Using typed data sets solves these problems. When you create a typed data set, code is generated that wraps the data set in a type-safe way by exposing tables and columns as strongly typed properties.
Another implicit benefit is that typed data sets expose the data set's schema through strongly typed properties and methods, and that enhances productivity.
Using typed data sets lets you write code like this:
Code
the beauty of the above code is that there aren't any hard-coded schema references. There aren't any string literals for table or column names, and you don't have to index any column positions because you can more easily and clearly get to them through the typed properties exposed on the typed row object.
The other part that isn't apparent from a block of code like this is actually one of the best benefits of typed data sets from a productivity standpoint: IntelliSense!
中文简介:强类型数据集避免了硬编码,当数据库的字段非常多而且十分复杂时使用强类型数据集能够大大提高开发速度:1.提供智能感知;2.避免您在程序中使用StringBuilder中拼接冗长而且及其容易出错的SQL语句,因为你可以将这些基本的增删改查使用标准SQL语句写在DataAdapter中。
posted @ 2009-09-02 17:15  wispzone  阅读(224)  评论(0编辑  收藏  举报