Thursday, December 07, 2006

Reduce Object/XML Impedance Mismatch with LINQ to XSD

LINQ to XSD from Microsoft's XML Team promises a similar reduction of the impedance mismatch between .NET objects and XML documents as LINQ to Entities and ADO.NET 3.0's Entiry Framework and Entity Data Model deliver for objects and relational data.

LINQ to XSD, which entered the public preview state on November 27, 2006 with the release of the Alpha 0.1 version, builds on LINQ to XML's generic XML trees to operate on typed XML trees, which are "instances of .NET types that model the XML types of a specific XML schema (XSD)." Thus LINQ to XSD carries the subtitle "LINQ to XML Objects."

Note: If you don't want to download the preview bits, which require the Microsoft .NET LINQ CTP (May 2006), read the "LINQ to XSD Overview: An Incubation Project on Typed XML Programming" white paper.

Updated: February 7, 2008

A First Look at LINQ to XSD Syntax

The XML Team's blog post and documentation for the LINQ to XML preview cites this prototypical LINQ for XML query that returns the total value of Item elements in in a purchaseOrder document:

(from item in purchaseOrder.Elements("Item") select (double)item.Element("Price") * (int)item.Element("Quantity")).Sum();

Here's the strongly typed LINQ to XSD version of the preceding query:

(from item in purchaseOrder.Item select item.Price * item.Quantity ).Sum();

Obviously, the type-safe version of the query's syntax is clearer. It also provides Intellisense for elements and corresponds more closely to a LINQ to SQL query against a purchaseOrder table with a related Item table.

Aside from the obvious syntatical benefits and compile-time vs. run-time syntax checking, LINQ to XSD supports contract-first rather than code-first development. Many Web services developers are contract-first devotees, but Dare Obasanjo—one of Microsoft's foremost XML/XSD gurus—recommends the code-first approach in his April 22, 2005 "Contract-First XML Web Service Design is No Panacea" post.

Note: Click here for a list of Dare's technical papers, articles, and other writings as of May 2005.

LINQ to XSD can handle code-first (or, more properly, document-first) design by inferring and refining XML schemas from existing XML documents with the Microsoft XSD Inference 1.0 tool. My "Generate XSD Schemas by Inference" article for the November 2002 issue of XML Magazine explains how to get the most out the tool. (Free registration is required to read page 2 through 4).

O/X Impedance Mismatch Background

My September 25, 2005 post, "More on Object-XML and Object-Relational Impedance Mismatch," cited Dare's "The Impedence Mismatch between W3C XML Schema and the CLR" post, which discusses XML serialization of object state and conversion of XML to objects. Dare suggests his earlier "XML Schema Design Patterns: Is Complex Type Derivation Unnecessary?" post and the "Programming with Circles, Triangles and Rectangles" paper by Gavin Bierman, Wolfram Schulte and Erik Meijer for more details on X/O impedance mismatch issues. This paper extends an earlier presentation to the Workshop on Declarative Programming in the Context of OO Languages (DP-COOL) 2003—Unifying Tables, Objects and Documents by Eric Meijer and Wolfram Schulte.

Note: The BSM trio also presented "LINQ: Reconciling Objects, Relations and XML in the .NET Framework" to SIGMOD 2006. Unfortunately, only the first page is available for download.

Apparently, Erik and his colleagues are sanguine that the longstanding issues with X/OM (XML-to-Object mapping) can be resolved. He and Data Progammability Team program manager Ralf Lämmel authored "Revealing the X/O Impedance Mismatch" (July 19, 2006 draft) for submission to and possible publication in the post-proceedings for Datatype Generic Processing (DPG '06), Lecture Notes in Computer Science (LNCS, Springer US). The paper begins with an excerpt from Dave Thomas's "The Impedance Imperative: Tuples + Objects + Infosets = Too Much Stuff!" article for the September-October 2003 issue of the Journal of Object Technology.

Another paper, "Mappings make data processing go ’round: An inter-paradigmatic mapping tutorial," by Erik and Ralf (2005) starts with the same Dave Thomas quotation. The abstract describes the tutorial's objective:

Each data-processing application depends on a horde of interrelated data models and artifacts that are derived from data models (such as data-access layers). Such conglomerations of data models are challenging due to paradigmatic impedance mismatches, performance requirements, loose-coupling requirements, and others.

This ubiquitous problem calls for a good understanding of techniques for mappings between data models, actual data, and operations on data. This tutorial lists and discusses mapping scenarios, mapping techniques, impedance mismatches and research challenges regarding mappings. [Emphasis added].

Note: Dave Thomas and Erik Meijer are speakers for the QCon Conference to be held March 12 - 16, 2007 in London. Erik and Martin Fowler are among the conference's keynoters.

Ralf presented two papers at XML 2006 on December 5, 2006: "Typed XML programming without abandoning XML" (a.k.a. "Functional OO Programming with Triangular Circles" with Dave Remy as co-author) and "XML streaming, XML trees and XML queries—Can they co-exist in harmony?" (by Michael Champion). Mike chaired the Enterprise XML Computing sessions and provides a blow-by-blow description of the December 5 and part of the December 6 sessions in his XML 2006 Observations post.

Note: Dave Remy said on June 16, 2006: "I am now working hands on a very cool XML developer tool (can't say specifically what quite yet) that uses WPF (also known as Avalon) and XLinq extensively. I look forward to doing some future blogs in these areas." (To my knowledge, no future blogs on the topic appeared as of early 2008.)

Charting the Course of LINQ to XSD in Orcas

The more general tutorial, "Mappings make data processing go 'round—An inter-paradigmatic mapping tutorial," "... lists and discusses mapping scenarios, mapping techniques, impedance mismatches and research challenges regarding mappings" with the emphasis on O/RM and X/OM. This paper is included in the Summer School Proceedings of the Generative and Transformational Techniques in Software Engineering (GTTSE) 2005, of which Ralf was an organizer.

Ralf offers the "XML Typed Programmer" quartet with the following posts, some of which are quite detailed (i.e., lengthy):

Note: The current LINQ to XSD implementation is C# 3.0 only, which is surprising when you consider Erik's penchant for Visual Basic and his recent VB IsNot C# seminar at Expo-C 2006.

Tech support for LINQ to XSD is from the XML and the .NET Framework newsgroup, not the expected LINQ Project General newsgroup.

LINQ to XSD is an "incubator" project and the preview comes with a "While the underlying LINQ to XML technology is expected for Orcas, no such commitment exists for LINQ to XSD" caveat. But it seems to me that LINQ to XSD is a necessary addition to mainstream LINQ to SQL (formerly DLinq) and LINQ to XML (formerly XLinq) APIs, and developing LINQ to Entities and LINQ to DataSets technologies. Without LINQ to XSD, the final Orcas LINQ implementation will be incomplete.

Updated: 12/8/2006 and 12/11/2006 Minor additions and corrections. Reformatted: 2/7/2008.

Technorati Tags: LINQ to XSD, LINQ to XML Objects, LINQ to XML, XLinq, C# 3.0, VB 9.0, XML, XSD, XML Schema, Orcas, Domain-Specific Languages, DSL, DSL Tools