LINQ IN ACTION (翻译)

LINQ IN ACTION

1.2 Why do we need LINQ?

We have just provided you with an overview of LINQ. The big questions at this

point are: Why do we want a tool like LINQ? What makes the previous tools inconvenient?

Was LINQ created only to make working with programming languages,

relational data, and XML at the same time more convenient?

1.2 我们为什么要使用LINQ?

我们刚刚之前已经为你提供一个有关LINQ的概述。在这个观点上的一个比较大的问题是: 为什么我们需要一个类似LINQ的工具。什么原因造成之前的工具如此不方便?LINQ的产生仅仅是为了让编程语言,关系数据,XML同时工作得更加方便?

At the origin of the LINQ project is a simple fact: The vast majority of applications

that are developed access data or talk to a relational database. Consequently,

in order to program applications, learning a language such as C# is not enough.

You also have to learn another language such as SQL, and the APIs that tie it

together with C# to form your full application.

       LINQ Project 的起源是一个非常自然的事实:有非常广泛的成熟的访问数据或者是与相关数据库交互的运用程序。因此,为了编写程序,仅仅学习某种语言,例如C#,是不足够的。你不得不也去学习其它语言,例如,SQL和同C#有关的API去完善你的整个程序。

We’ll start by taking a look at a piece of data-access code that uses the standard

.NET APIs. This will allow us to point out the common problems that are encountered

in this kind of code. We will then extend our analysis by showing how these

problems exist with other kinds of data such as XML. You’ll see that LINQ

addresses a general impedance mismatch between data sources and programming

languages. Finally, a short code sample will give you a glimpse at how LINQ is a

solution to the problem.

       我们将研究一段使用标准的.NET API访问数据库的代码开始我们的学习。在这一段代码中,将使我们能够指出所遇到共同问题。通过显示这些同其它类型的数据库如XML存在的问题,然后展开我们的分析。你会看到LINQ解决了在数据与程序语言之间的一般的阻抗失配。最后,一段短小简单的代码将会演示给你,LINQ是怎样解决这个问题的。

1.2.1 Common problems

1.2.1 普遍的问题

The frequent use of databases in applications requires that the .NET Framework

address the need for APIs that can access the data stored within. Of course, this

has been the case since the first appearance of .NET. The .NET Framework Class

Library (FCL) includes ADO.NET, which provides an API to access relational databases

and to represent relational data in memory. This API consists of classes such

as SqlConnection, SqlCommand, SqlReader, DataSet, and DataTable, to name a

few. The problem with these classes is that they force the developer to work explicitly

with tables, records, and columns, while modern languages such as C# and

VB.NET use object-oriented paradigms.

在应用程序中经常使用数据库的一个依赖是.NET Framework解决了APIs访问数据存储的需要。当然,这是自从.NET 首次出现时的一个例子。.NET Framewor的类库(FCL)包括了ADO.NET,它提供一个API 去访问相关的数据库和在内存里操作关系数据。这些API 是由如下的一些类组成,SqlConnection, SqlCommand, SqlReader, DataSet,DataTable 等。使用这些类的问题是,虽然现代的C#VB.NET使用面向对象的形式,但是它强使程序员的工作同表,记录,列明确。

Now that the object-oriented paradigm is the prevailing model in software

development, developers incur a large amount of overhead in mapping it to other

abstractions, specifically relational databases and XML. The result is that a lot of

time is spent on writing plumbing code.3 Removing this burden would increase

productivity in data-intensive programming, which LINQ helps us do.

But it’s not only about productivity! It also impacts quality. Writing tedious

and fragile plumbing code can lead to insidious defects in software or degraded

performance.

       面向对象的模式在现在的软件开发中是普遍的模式。现在的程序员承担了大量间接的数据映射到其它的抽象中去,特别是关系数据和XML。结果是大量的时间花费在这些配置数据的代码上去了。去掉这些负担后,在数据密集型的编程中会大量的提高我们的效率,而LINQ 就能够帮我们做好这方面的工作。这不仅是有关效率的问题,还有影响质量。编写泛味和脆弱的工作代码可能会导致软件隐藏问题和性能退化。

Listing 1.2 shows how we would typically access a database in a .NET program.

By looking at the problems that exist with traditional code, you’ll be able to see

how LINQ comes to the rescue.

       列表1.2 代表性地演示一个.NET 程序如何去访问数据库。 通过这些普通的代码来研究存在的问题,你会看到LINQ是怎样来营救的。

Listing 1.2 Typical .NET data-access code

using (SqlConnection connection = new SqlConnection("..."))

{

connection.Open();

SqlCommand command = connection.CreateCommand();

command.CommandText =

@"SELECT Name, Country

FROM Customers

WHERE City = @City";

command.Parameters.AddWithValue("@City", "Paris");

using (SqlDataReader reader = command.ExecuteReader())

{

while (reader.Read())

{

string name = reader.GetString(0);

string country = reader.GetString(1);

...

}

}

}

 

posted on 2009-07-15 17:13  leenshan  阅读(638)  评论(0编辑  收藏  举报