Monday, January 14, 2008

LINQ and Entity Framework Posts for 1/11/2008+

Note: Some posts as early as 1/9/2008 were missed and are added here.
Updated 1/14/2008: See individual updates below.

Andy Conrad's Pathetic Plea for Help Answered with Downloadable VB Project

Andy Conrad reports in his Pathetic Plea for help post of January 11, 2008:

A number of people have asked me for a VB version of the CopyToDataTable<T> sample I wrote a few months back.   Unfortunately, between getting some skiing in (lots of snow in Washington this year!) and getting Astoria ready for Mix, I have not had very little free time.  Hence, if anyone else wants to take a crack at this - or knows of an existing port of the code to VB, let me know.

I've ported the C# 3.0 version of the Copy to DataTable<T> sample to VB 9.0 as CopyToDataTableVB.sln. You can download the zipped project from Windows Live Skydrive. The VB version adds display by IE of the XML generated from the four use-case tables added to a DataSet. Click here to open CopyToDataTable.xml in IE's style sheet.

Notes: The port has been tested with the four provided use cases only and doesn't include the modification suggested by Nick Lucas.

Added: 1/13/2008

Wes Dyer Extols the Marvels of Monads

Wes's The Marvels of Monads essay/tutorial of January 11, 2008 cites Philip Wadler's 1992 paper, The Essence of Functional Programming, which "showed that monads are generally useful in computer programs to compose together functions which operate on amplified values rather than values." Amplified values, in Wadler's terminology "represent a computation."

Monads are essential elements of the Haskell function programming language to which Erik Meijer, whom many call the "Creator of LINQ," has made major contributions. Erik's abstract of his 1966 Confessions of a Used Programming Language Salesman: Getting the Masses Hooked on Haskell paper reads in part:

Programmers in the real world wrestle every day to overcome
the impedance mismatch between relational data, objects, and
XML. For the past ten years we have been working on solving
this problem by applying principles from functional programming,
in particular monads and comprehensions. By viewing
data as monads and formulating queries as comprehensions,
it becomes possible to unify the three data models and their
corresponding programming languages instead of considering
each as a separate special case. ...

Functional programming has finally reached
the masses, except that it is called Visual Basic instead of Lisp,
ML, or Haskell!

The LINQ Standard Query Operators and LINQ to XML exploit monads' functional composition features, as Erik and Brian Beckman explain in their XLinq: XML Programming Refactored (The Return Of The Monoids) paper for the XML 2005 Conference and Exhibition (November 2005, Atlanta, GA.).

Note: Monad is a synonym for monoid. Contradicted by an anonymous comment. My source for the statement was an incorrect assumption regarding use of "monoid or monad" phrases in the preceding paper. Wikipedia says the following:

In category theory, a monad or triple is an (endo-)functor, together with two associated natural transformations. Monads are important in the theory of pairs of adjoint functors. They can be viewed as monoid objects in a category of endofunctors (hence the name) and they generalize closure operators on posets to arbitrary categories.

Wes concludes:

Beautiful composition of amplified values requires monads.  The Identity, Maybe, and IEnumerable monads demonstrate the power of monads as container types.  The Continuation monad, K, shows how monads can readily express complex computation.

If you've taken monads for granted and not made the effort to understand their full repertoire, be sure to read Wes's essay/tutorial. 

Added: 1/13/2008

Mike Taulty Reflects on Astoria Access Control

Mike's ADO.NET Data Services - Sketchy Thoughts on Controlling Access of January 11, 2008 explores:

  • The IWebDataServiceConfiguration object's SetResourceContainerAccessRule() for controlling all users' access rights to ResourceContainers and ServiceOperations
  • Basic authentication for server-side resources with Windows accounts and ASP.NET membership
  • Basic authentication with WCF's ServiceAuthorizationManager for lower-level access control

and provides sample code to implement each.

Added: 1/13/2008

Sanjay Nagamangalam Explains CSDL Annotations

As Sanjay explains in his Annotations in CSDL post of January 11, 2008, the Entity Framework supports XML attributes in a custom namespace as annotations whose values are accessible from the metadata APIs.

Sanjay notes that ADO.NET Data Services makes extensive use of annotations, which undoubtedly is why Astoria appears to prefer the Entity Framework over LINQ to SQL as a relational data source by, for example, supporting associations natively.

Added: 1/13/2008

Rob Conery Publishes SubSonic 2.1 Preview for .NET 2.0 with "LINQy" Query Tool

Rob's SubSonic: Version 2.1 (Pakala) Preview: The New Query Tool post of January 10, 2008 promises to "[c]reate a 'LINQy' experience for .NET 2.0, using every trick I could think of to create a fluent API." You'll find the current preview code here.

As to integrating "real" LINQ with SubSonic, Rob has this to say:

Being serious for a second - I get asked about LINQ a whole bunch. We have a plan for LINQ - specifically LINQ to Sql - and as I mention above I’ll get into that in a later post. For now please know that this query tool is for .NET 2.0 (though it will indeedy be usable in 3.0). I’m not into competing with Microsoft (and never have been) - they (we) can do the heavy lifting (create the platform) - I’ll just make it pretty.

Added: 1/13/2008

Marcello Lopez Ruiz Explains Astoria's $filter Query Option

His $filter Query Option in ADO.NET Data Services post of January 10, 2008 explains how to apply the $filter= option with

  • eq (equals)
  • ne (not equals)
  • lt (less than)
  • le (less than or equal)
  • gt (greater than)
  • ge (greater than or equal)


  • and
  • or
  • not

operators. Marcello notes that ADO.NET Data Services don't support aggregates, so queries like /Customers?$filter=count(Orders) gt 5 won't work.

Added: 1/13/2008

Danny Simmons Finds Problems RoundTripping Serialization with XMLWriter and StringBuilder

Danny's When deserialize(serialize(x)) != x post of January 9, 2007 reports:

Everything was working fine until I happened to run this little baby on some data that had strings with embedded carriage returns (that’s \r for us [C]# dudes—in my case the data was RTF). Suddenly my comparisons were failing, and it took me quite some time to realize that it had nothing to do with the serializer or the code I was testing. The problem was in the above [serializing/deserializing] code. Apparently if you serialize using an XmlWriter over a StringBuilder, these carriage returns are lost. If you actually build a web service with WCF, though, everything goes through fine. ARRRGGGG.

I've found even worse problems with XmlTextWriter and StringBuilder combinations when creating moderate-sized XML files from tab-separated value (*.tab) files with the default format from Excel 2007, as well as comma-separated value (*.scv) files from Access 2007. For example, the Northwind Customers table exported to Excel and then saved as or saved directly as Customer.csv report having malformed tags in various locations that don't appear in Notepad. I've stopped using the XmlTextWriter for this reason.

Added: 1/13/2008

Danny Simmons Explores the EntityConnectionStringBuilder

If you need an Entity Framework connection string that contains metadata only (without the provider connection string) the EntityConnectionStringBuilder (ECSB) will not only create it for you but will also parse it, according to Danny's Did you know that connection string builders will parse connection strings not just create them? post of January 9, 2008.

The ADO.NET Entity Framework Class Library help document contains an ECSB topic, but promised examples are missing.

It sounds to me like Danny's working on the Entity Framework version of LINQ to SQL's infamous (and missing) mini connectionless serializable DataContext for multi-tier deployment. Is a maxi connectionless serializable ObjectContext in the works?

Added: 1/13/2008

Visit Jim Wooley's Site

As Jim says in his Annoucing post of January 9, 2008:

You may have seen me present it at a speaking engagement. You may have watched the podcasts. You may have even downloaded the sample application. Now you can see it in action. is now live.

The site was designed completely in VB with LINQ as the data access mechanism. The base application was built in 2 hours from not knowing RSS to being able to import a RSS feed, displaying it on a form and producing a new feed from the imported data. The site is a testimate to the power of LINQ and the RAD capabilities that it brings. Head on over to the site and check it out.

Added: 1/13/2008

Rico Mariani Asks: "What's the ROI for Compiled LINQ Queries?"

Updated 1/14/2008: See below for Rico's breakeven reuse number.

Rico's Performance Quiz #13 -- Linq to SQL compiled queries cost post of January 11, 2008 asks, but doesn't answer this question: How many compiled query executions does it take to pay for compilation time?

If you believe JD Conley's Linq to Sql Surprise Performance Hit article of November 28, 2007, and I have no reason to doubt his results, the answer is 1. As noted in the LINQ and Entity Framework Posts for 11/26/2007+ post's "JD Conley: Compile Your LINQ to SQL Queries for Performance" topic:

JD ran into a serious performance problem in his Photo Feeds Facebook app, which uses LINQ to SQL, that was causing his page to load slowly. It turned out that three page loads caused 1,176,879 calls to SqlVisitor.Visit and pinned the CPU. Compiling the query decreased load time by a factor of five and the app now "barely registers on the CPU when the page loads."

On the other hand I've never been able to achieve a non-trivial LINQ to SQL query execution time close to 93% of the performance of direct execution of SqlClient's SqlDataReader that Rico claimed in his early posts on the topic. (See "The VB Team: Compiled LINQ to SQL Queries Redux" topic of LINQ and Entity Framework Posts for 11/5/2007+. The VB Team's Doug Rothaus didn't report any timing test results.)

The test harness that I described in Rico Mariani's DLinq Performance Tips (Part 2) and Compiled Queries showed performance improvement ratios in the range of 3.30 to 2.31 for 0 to a maximum of 122 rows of the Northwind Orders table with Orcas Beta 2 bits. For 6 rows, which is close to Rico's 5, compiled parameterized queries ran 3.08 times faster than their uncompiled counterparts.

The same tests with RTM bits and 6 rows ran the compiled query 4.04 times faster than the uncompiled version. Using Rico's terminology, my compiled query achieved 60% of the performance of the SqlDataReader version, which I believe to be to be a significant feat. What I need to do now is measure the average time to compile the query. Stay tuned.

Update 1/14/2008: Rico's Performance Quiz #13 -- Linq to SQL compiled query cost -- solution post of January 14, 2008 concludes that using the same query 1.5 times is the breakeven point for compiling it, which is about what I'd expect from my tests.

Andy Conrad and Mike Flasko: New ADO.NET Data Services Client for Silverlight Available

Andy's Updated Astoria Silverlight client now available post of January 11, 2008 says:

This client is 95% compatible with the client library we released back in the December CTP (we hope to make this 100% source code compatible by RTM) + works with the latest Silverlight 1.1 Alpha.

When the next preview of Silverlight releases this spring, we will release an update of the Astoria Silverlight client.

You can download the bits for the new client, officially called the Microsoft ASP.NET 3.5 Extensions Preview (Dec 2007) – ADO.NET Data Services Silverlight Add-On here.

Mike Flasko adds the following in his ADO.NET Data Services Silverlight Add-On post of the same date:

The ADO.NET Data Services Silverlight Add-On is a prototype CTP that extends the Dec 2007 CTP by introducing a version of the client library that can run inside the Microsoft Silverlight 1.1 September Refresh environment. Like the .NET Framework version, this library also allows developers to execute queries asynchronously, providing a more natural interaction model for a web-centric application.

We still have a number of things to do on the library to round out the experience, but we wanted to get a very early drop out to share our thinking in this space.  We look forward to hearing what you think .....



Anonymous said...

Monad is not a synonym for monoid.
Even though monads form a monoid in a certain category of endofunctors they are quite different.

--rj said...

Agreed and corrected.


fabrice said...

We have another VB port of Andrew Conrad's CopyToDataTable query operator in the samples' source code of the LINQ in Action book.

In the complete archive, look for the files DataSetLinqOperators.vb and ObjectShreder.vb under Chapter14.VB\Chapter14.

Note: In our version, we kept the original names of the query operators for clarity (ToDataTable and LoadSequence).

DouglasH said...

Roger, on the ideas of Monads and monoids and functional programming that lead to Linq. Brian Beckman gave a good short layman's overview on channel 9.

And if you really want to get geeky on it and get into the maths there are links in the comments to Video on google that goes much deeper (category theory)