LINQ: Reconciling objects, relations and XML in the .NET framework
Linq:统一Microsoft .Net Framework中的对象、事务日志以及Xml
作者:不译
Erik Meijer
Microsoft Corporation
Redmond, USA
emeijer@microsoft.com
Brian Beckman
Microsoft Corporation
Redmond, USA
bbeckman@microsoft.com
Gavin Bierman
Microsoft Research
Cambridge, UK
Introduction Many software applications today need to
handle data from different data models; typically objects
from the host programming language along with the re-
lational and XML data. The ROX impedance mismatch
makes programs awkward to write and hard to maintain.
The .NET Language-Integrated Query (LINQ) framework,
proposed for the next release of the .NET framework, ap-
proaches this problem by dening a design pattern of general-
purpose standard query operators for traversal, lter, and
projection. Based on this pattern, any .NET language can
dene special query comprehension syntax that is subse-
quently compiled into these standard operators (our code
examples are in VB).
引言:当今的许多应用软件都要通过不同的数据Model来操作数据;其中典型的编程方式就是关系数据库与XML文档(数据)之间的协作。关系与XML的互阻导致了错误的产生我们称之为[阻抗失谐],这种编程方式使程序难以更改并且实现起来较为复杂。(ROX 我感觉应该是relational of XML)被建议整合在.net framework 下一版本(.net 3.5已发布)中的.net 语言集成查询框架(LINQ)以一种内置的合乎设计模式的操作符定义来进行数据的访问,过滤和发送操作。基于这种模式,任何一种.net下的编程语言都可以使用这些操作符定义一个编译器可以编译的查询语句语法(本文章的示例都是VB.net的)。
Besides the general query operators, the LINQ framework
also denes two domain-specic APIs that work over XML
(XLinq) and relational data (DLinq) respectively. The oper-
ators over XML use a lightweight and easy-to-use in-memory
XML representation to provide XQuery-style expressiveness
in the host programming language. The operators over re-
lational data provide a simple OR mapping by leveraging
remotable queries that are executed directly in the back-end
relational store.
此外,一般的查询操作符,Linq框架中分别定义了操作XML的XLINQ和操作关系数据库的DLINQ两种Apis。XML下的操作符使用一个轻量级的易用的以XQuery-style形式体现出来的内存运行的编程语句。这些操作符提供了一个简单的对象关系映射作为直接的关系存储的体现。
Standard query operators It is well known that col-
lections can be modeled as monoids and queries over them
as monoid homomorphisms. Analogously, LINQ denes an
API pattern that enables querying of any collection or .NET
array. This query operator set includes familiar constructs
such as ltering (where), mapping (select), monadic bind
(selectMany), sorting (orderby) and partitioning (groupBy).
These query operators can be freely composed to form
rich queries; e.g. the following code returns the name and
phone numbers of customers from Seattle ordered by their
age:
众所周知标准查询操作符可以有各种灵活多变的形式,与此类似的,LINQ定义了一个API模式的查询方式,可以查询任意的泛型或。NET数组。查询操作符集包含了一些大家熟悉的过虑操作符(where),映射(Select),单一绑定(selectMany),排序(orderBy)以及分类查询(groupBy),这些查询操作符可以以查询形式轻松组建。以下面这段代码为例,从Seattle中查询客户的姓名和电话,前按用户年龄排序:
Dim cs = From c In Customers
Where c.Address.City = "Seattle"
Order By c.Age
Select c.Name, c.Phone
XLinq The LINQ framework also includes a new, modern,
lightweight XML API that makes it simple to program XML
and integrates smoothly with the standard query operators
of LINQ.
XLinq Linq 框架也包含一个与时俱进的轻量级的类似于编写XML来综合运用标准查询请用操作Linq的XML API 。
In XLinq, nodes are rst-class citizens that can be passed
around independently of an enclosing document. Nested el-
ements are constructed in an expression-oriented fashion.
Elements and attributes are accessed uniformly using famil-
iar XPath axis-style methods, while namespace handling is
simplied using the notion of universal names.
在XLinq中,结点是 类优先的 封闭标签文档构成的统一结构。层叠结点则由一个样式向导创建。当命名空间引用相应的抽象类库时,其结点与属性可以像XPATH一样统一访问方法 。
In addition to the XLinq API, VB 9.0 adds XML liter-
als with full namespace support. A variant of the previous
query that returns each customer as XML simply uses the
following expression in the select clause:
附加的XLinq Api中VB 2008 (9.0)和XML有着全面的命名空间支持,从前一个变量可以用以下方式采用XML简单地遍历查询用户信息。
<Customer Name=<%= c.Name %>>
<Phone><%= c.Phone %></Phone>
</Customer>
DLinq The LINQ framework also provides infrastruc-
ture for managing relational data as objects. It leverages
the ability of LINQ to provide intensional representations of
delegates as code trees by translating language-integrated
queries into SQL for execution by the database, and then
translating the resulting table into objects. The DLinq in-
frastructure maintains an identity cache and performs change
Tracking.
DLinqLinq类库也支持关系数据库映射为对象的方法。它与SQL及中间语言高度内聚,可以将文档树与对象互相转换,DLinq可以缓存数据和进行事务跟踪。
The mapping between database tables and in-memory ob-
jects is done via custom attributes (annotations) such as
<Association(OtherKey="CustomerID")> to indicate foreign
key associations and <Column(Id=true)> to indicate pri-
mary keys.
数据库表和内存对象之间的映射关系通过类似<Association(OtherKey="CustomerID")>的声明式编程方法来实现,一个外键和一个主键用类似 <Column(Id=true)>的声明编程方式来表示。
In the following query, the navigation between the cus-
tomer and address objects will automatically be compiled
into the appropriate joins of the underlying tables.
下面的这段查询代码,表示了用户与地址对象将自动编译并指向相应的表。
Dim db = new MyDataBase("...");
Dim cs = From c In db.Customers
Where c.Address.City == "Seattle"
Select c.Name, c.Address
As soon as objects are loaded into the data context, either
by retrieving them through a query or by constructing new
objects and inserting them, DLinq tracks all the changes
and transmits these objects back to the database when re-
quested, automatically generating and executing the appro-
priate SQL commands.
当对象被上下文使用时,无论是通过一个查询来检索还是要构造一个新对象来进行插入。DLinq总会跟踪所有变化,并将它们在查询时用SQL语句传回数据库。
http://msdn.microsoft.com/netframework/future/linq/
1