linqsigmod

  • Upload
    el-alex

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

  • 8/8/2019 linqsigmod

    1/1

    LINQ: Reconciling objects, relations and XML in the .NETframework

    Erik MeijerMicrosoft CorporationRedmond, USA

    [email protected]

    Brian BeckmanMicrosoft CorporationRedmond, USA

    [email protected]

    Gavin BiermanMicrosoft ResearchCambridge, UK

    [email protected]

    Introduction Many software applications today need tohandle data from different data models; typically objectsfrom the host programming language along with the re-lational and XML data. The ROX impedance mismatchmakes 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, andprojection. Based on this pattern, any .NET language candene special query comprehension syntax that is subse-quently compiled into these standard operators (our codeexamples are in VB).

    Besides the general query operators, the LINQ frameworkalso 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-memoryXML representation to provide XQuery-style expressivenessin the host programming language. The operators over re-lational data provide a simple OR mapping by leveragingremotable queries that are executed directly in the back-endrelational store.

    Standard query operators It is well known that col-lections can be modeled as monoids and queries over themas monoid homomorphisms. Analogously, LINQ denes anAPI pattern that enables querying of any collection or .NETarray. This query operator set includes familiar constructssuch as ltering ( where ), mapping ( select ), monadic bind(selectMany ), sorting ( orderby ) and partitioning ( groupBy ).

    These query operators can be freely composed to formrich queries; e.g. the following code returns the name andphone numbers of customers from Seattle ordered by theirage:

    Dim cs = From c In CustomersWhere c.Address.City = "Seattle"Order By c.AgeSelect c.Name, c.Phone

    Permission to make digital or hard copies of all or part of this work forpersonal or classroom use is granted without fee provided that copies arenot made or distributed for prot or commercial advantage and that copiesbear this notice and the full citation on the rst page. To copy otherwise, torepublish, to post on servers or to redistribute to lists, requires prior specicpermission and/or a fee.SIGMOD 2006, June 2729, 2006, Chicago, Illinois, USA.Copyright 2006 ACM 1-59593-256-9/06/0006 ... $5.00.

    XLinq The LINQ framework also includes a new, modern,lightweight XML API that makes it simple to program XMLand integrates smoothly with the standard query operatorsof LINQ.

    In XLinq, nodes are rst-class citizens that can be passedaround 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 issimplied using the notion of universal names.

    In addition to the XLinq API, VB 9.0 adds XML liter-als with full namespace support. A variant of the previousquery that returns each customer as XML simply uses thefollowing expression in the select clause:

    DLinq The LINQ framework also provides infrastruc-ture for managing relational data as objects. It leveragesthe ability of LINQ to provide intensional representations of

    delegates as code trees by translating language-integratedqueries into SQL for execution by the database, and thentranslating the resulting table into objects. The DLinq in-frastructure maintains an identity cache and performs changetracking.

    The mapping between database tables and in-memory ob- jects is done via custom attributes (annotations) such as to indicate foreignkey associations and to indicate pri-mary keys.

    In the following query, the navigation between the cus-tomer and address objects will automatically be compiledinto the appropriate joins of the underlying tables.

    Dim db = new MyDataBase("...");

    Dim cs = From c In db.CustomersWhere c.Address.City == "Seattle"Select c.Name, c.Address

    As soon as objects are loaded into the data context, eitherby retrieving them through a query or by constructing newobjects and inserting them, DLinq tracks all the changesand transmits these objects back to the database when re-quested, automatically generating and executing the appro-priate SQL commands.

    http://msdn.microsoft.com/netframework/future/linq/

    1

    http://msdn.microsoft.com/netframework/future/linq/http://msdn.microsoft.com/netframework/future/linq/