Weak and strong Datasets

Source: Internet
Author: User
Weak and strong Datasets

Navigation:

  • Weak and strong Datasets

    • 1 dataset disadvantages

      • 1.1 after dataset is used, is the system still like layer n?
      • 1.2 weak Dataset
    • 2. Strong Dataset
      • 2.1 how to generate a strongly Typed Dataset
      • 2.2 how to access strong-type dataset attributes
      • 2.3 comparison between strong and weak Dataset
    • 3. Conclusion
1 disadvantages of dataset 1.1 is the system still like N layers after dataset is used?

The simplest example is to access data in dataset in this way.
DS. Tables (0). Rows (I) ("userid ")
Such statements may spread across our systems. If I change the database architecture for some reason, this may affect the column name "userid". This will affect the layers of our system, or even the U layer, which may be the simplest change. Database changes involve various layers of the system. Such a design would be very dangerous.
Another drawback of dataset is that developers must understand the basic architecture.
We are not talking about basic knowledge, but about all the column names, types, and relationships.
Ctype (Ds. Tables (0). Rows (I). ("userid"), integer)

This is not only difficult to read, but also very familiar with column names and their types. Ideally, our business layer does not need to know anything about the basic database, database architecture, or SQL.

1.2 weak Dataset

Dataset is of a weak type, so it is prone to errors and may affect development. This means that the value retrieved from dataset is returned in the form of system. object at any time. You need to convert the value. However, you may face the risk of failure. Unfortunately, this error occurs at runtime rather than during compilation.
When dealing with weak types, tools such as vs cannot provide you with much help. You need to understand the architecture knowledge.

  Dim userId As Integer = CInt(ds.Tables(0).Rows(0)("UserId"))  Dim userId As Integer = CInt(ds.Tables(0).Rows(0)(0))

This code shows how to retrieve values from dataset. Such code will produce a large number of errors at runtime?

  1. Conversion may fail due to the following reasons:

    • It may be null.
    • A developer may incorrectly judge the data type.
    • If you use the serial number, who knows what the serial number represents.
  2. DS. Tables (0) may return an empty index.
  3. "Userid" may not be an invalid column name for the following reasons:
    • The name may have been changed.
    • It may not be returned by the stored procedure.
    • It may contain typos.

We can modify the code and write it in a safer way, that is, add check for null/nothing and add try/catch for the conversion. But the effect is not that good. Weak dataset transfers errors from design or compilation to runtime, which is obviously highly risky.
One solution is to create a custom object class and a set of custom objects (
User-Defined Object class ). The disadvantage of weak dataset can be compensated by strong dataset.

2. Strong Dataset

A strongly typed dataset is a class generated by Visual Studio Based on the Data Warehouse schema. The type of its members is determined by this schema. A strongly typed dataset itself is composed of dataset, able, and child classes of the datarow class inherited from ADO. net. In addition to the strongly typed able, the strongly Typed Dataset now also includes the tableadapter class, which contains various methods for filling the datatable in the dataset and returning the datatable changes to the database.

2.1 how to generate a strongly Typed Dataset
  • Project-add new item-Select data from the pop-up window-select a dataset, name it, and click OK. The design view is displayed.
  • The next step is to open the server resource manager, find the data table to be created in the data set, drag it to the design view, and finally SHIFT + Ctrl + B to generate, A typed dataset is created.
2.2 how to access strong-type dataset attributes

A strong-type dataset is derived from dataset. It generates a dataset Based on the predefined data schema and imposes a strong type constraint on fields in the dataset. You can see through the CS file it generates many methods to encapsulate the datatable operation, so that you can use mydataset. mytable. field is used to access fields, rather than mydataset. tables ("tablename") ("field ").

  • Typeddataset. typeddatatable [N]. typedproperty

    • Typeddataset: A strongly typed dataset, which returns a strongly Typed Dataset.
    • Typeddataset. typeddatatable: a data table of the strong type. A data table of the strong type is returned.
    • Typeddatatable [N]: strongly typed index, which returns strongly typed data rows.
    • Typedproperty: attribute of a strongly typed column. Columns are strongly accessed and return column-type instances.
  • Dataset. Tables [N]. Rows [m] [datacolumn | int | string columnname]
    • Dataset: DataSet is returned.
    • Tables [N]: returns the datatable.
    • Rows [m]: returns datarow.
    • Rows [m] [datacolumn | int | string columnname]: returns an object-type reference.

The strong type class uses the partial class feature. The partial class allows a class definition to exist in multiple files of a project. Classes allow you to add code to a strong dataset in a separate project file to implement verification logic or business logic. If a strongly typed dataset is regenerated, the code of this separate file will not be affected. For more information about the partial class, see

2.3 comparison between strong and weak Dataset
  • Performance Considerations: Although Typed Dataset requires more time and space to create an object instance than untypede dataset, It is faster than untyped dataset in data filling, this is because dataadapter already knows how to fill a Typed Dataset. In contrast, dataset needs to read the database twice. For the first time, the structure information of the table in the database is obtained, and the second time the data is fill.
  • Defects of typed dataset compared with Dataset: in addition to the overhead of creation, typed dataset is not as flexible as dataset, because once the typed dataset is determined, the structure of the data table is fixed. to modify the data table, it must be regenerated.
  • You can operate dataset at any time as needed (such as adding or deleting fields ).
3. Conclusion
  • Although the weak type dataset is flexible, it is obvious that there are many disadvantages. The strong type makes up for the shortcomings of the weak type dataset. The specific usage depends on the situation.
  • Both weak dataset and strong dataset maintain a connection with data, so that they can operate in relational databases conveniently, however, this means that all the advantages of object-oriented are lost.
  • There is a saying that is particularly good:
    • "Dataset is an object, right? But it is not a domain object, it is not an 'apple' or 'orange', but an object of the 'dataset' type. Dataset is a bowl (it knows it supports data storage ). Dataset is an object that knows how to save rows and columns. It knows databases very well. However, I do not want to return a bowl. I want to return a domain object, such as 'apple '.
  • For the disadvantages of dataset, using custom object classes and user-defined sets may be the best solution, but it is not the only solution, and this solution is not necessarily suitable for you. Refer to this article
  • Finally, we recommend that you take a look at the technical articles on msdn when you are free. Although it seems a little difficult, you still feel a lot of GAINS.

Msdn technical article
(This blog is written using Vim's vimwiki plug-in, and it feels good)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.