Entity Framework 4 in action Reading Notes -- Chapter 1: Data Access overload: Entity Framework (1)

Source: Internet
Author: User

Preface

Before going into the details of the object framework, we will first discuss the convenience of data access from the traditional dataset Method to the object-based method, and how these two methods work differently to lead to the use of O/RM tools like Entity Framework.

Use dataset and datareader as data containers

Let's first look at an example. Suppose there is a database with two tables: Order and orderdetail. The table structure is as follows:

To display all the orders, you only need to create a simple page with the followingCode.

 
Using (sqlconnection conn = new sqlconnection (connstring) {using (sqldataadapter da = new sqldataadapter ("select * from order", Conn) {datatable dt = new datatable (); da. fill (DT); listview1.datasource = DT; listview1.databind ();}}

After completing the above requirements, your customers want to view the details below each order, then it becomes challenging, because you can choose to implement different methods:

1. First query all orders from the order table, and then query details for each order from the orderdetail table.

2. join queries for the order and orderdetail tables.

3. Retrieve all orders and all details in two different queries.

No matter which method you choose, your code is determined by the database structure and data retrieval method. Every change will be painful.

The following code displays the order Id 1 details.

 
Using (sqlconnection conn = new sqlconnection (connstring) {using (sqlcommand CM = new sqlcommand ("select * From Order limit where orderid = 1", Conn) {Conn. open (); Using (sqldatareader RD = cm. executereader () {Rd. read (); date. TEXT = (datetime) RD ["orderdate"]). tostring (); shippingaddress. TEXT = RD ["shippingaddress"]. tostring (); shippingcity. TEXT = RD ["shippingcity"]. tostring ();} using (sqldatareader RD = cm. executereader () {details. datasource = RD; details. databind ();}}}

The data access method is completely insecure and universal.

1. You can easily write out the common code of the above function without having to know the field name in the table.

2. specifying the field name of a table with a string loses the type security. If the field name is incorrect, an exception is thrown only during running.

3. The returned values in the preceding column are of the object type and need to be displayed and converted to the appropriate type.

Common Data container Defects

I. Strong Coupling

Datareader and datatable do not allow you to retrieve data transparently without affecting the code on the user interface. This means that your applicationProgramIt is strongly coupled with the database structure. Any changes to the database structure must be made to your program. This should be solved at the data access layer, rather than at the user interface layer.

In many cases, databases provide services for an application, so that data can be easily organized and used. However, some programs are built on the existing database, so no changes can be made to the database at this time, because other programs are using this database. In this case, your code may be more coupled with the database, or even more than you think. For example, orders may be stored in one table, and shipping addresses are stored in another table. The data access layer code may reduce this impact, but the problem persists.

What happens when the column name changes? In fact, this often happens during development. As a result, you must change the code at the user interface layer to adapt to this change.

Ii. Loose type

Obtain the values of columns stored in datareader and able. You usually need to specify columns using strings. The following code gets the value of a column in the datatable.

 
Object shippingaddress = orders. Rows [0] ["shippingaddress"];

The variable shippingaddress is of the object type. It can store any type of data. You may know that it stores a stable type of data and needs to be used like a stable type, must be displayed for type conversion.

 
String shippingaddress = (string) orders. Rows [0] ["shippingaddress"]; string shippingaddress = orders. Rows [0] ["shippingaddress"]. tostring ();

The performance and memory usage of type conversion have some losses, because the binning operation is required from the value type conversion to the reference type, and vice versa.

Datareader has an advantage over datatable. It provides a method that does not need to explicitly convert the type of access fields. The parameter of the method accepts the index value of a column in a row. It also provides a method to provide the column name to return the index. However, if the input string is incorrect, an exception is thrown.

 
String address = RD. getstring (RD. getordinal ("shippingaddress"); string address = RD. getstring (RD. getordinal ("shipingadres"); // exception

Iii. performance problems

Dataset may be the most complex structure in the. NET class library. It includes one or more able instances, and each datatable instance includes a series of datarow objects. Each datarow object consists of a datacolumn object. Datatable can be composed of one or more columns to form a primary key, foreign keys can be declared on some columns, and the column also supports version control. Although these features are often useless and ignored by developers, dataset still creates an empty set containing these objects internally. This may be a negligible performance loss for an independent application, but in a multi-user environment, there are thousands of requests, which is unacceptable.

In contrast, datareader can be used in different scenarios. Datatable reads all the data from the database and puts it in the memory. However, you do not need to store so much data in the memory. You only need to read records from the database one by one. In another case, you often query data without updating it. In this case, datareader is the best choice because it uses the read-only method to retrieve data. Although datareader improves performance, it still has performance loss caused by type conversion.

Last words

Next, we will explain how to use classes to organize data.

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.