Sunday, April 27, 2008

LINQ and Entity Framework Posts for 4/21/2008+

Note: This post is updated daily or more frequently, depending on the availability of new posts.

Julie Lerman Unveils the Forthcoming ASP.NET EntityDataSource Control

Danny Simmons demonstrated the EntityDataSource control at the MVP Summit 2008 and Julie shares her notes in Sneak Peek at the EntityDataSource Control of April 27, 2008. The EntityDataSource control is the Entity Framework's answer to LINQ to SQL's LinqDataSource control for databinding ASP.NET GridView, ListView and other bindable server controls.

Two important EntityDataSource control features that Julie describes are:

Although I don't remember seeing it during the session, Danny did say that you can choose to eager load related data in the same way that you can with the Include method. I don't know how this is done or if it will impact updating, but I don't know why it would.

One of the most interesting things about the EntityDataSource is that along with its' ability to perform server side paging, it also performs client side caching - of current AND original data. The original data is not stored as complete objects, but the minimal data necessary to reconstruct state when it's time to update. Updates happen, like any other data source, one at a time. So you have to pick an item, edit it and update it.

Availability of the EntityDataSource control will enable ASP.NET Dynamic Data scaffolding to use EF as an alternative to LINQ to SQL as its data source. (See David Ebbo's Dynamic Data at MIX, and upcoming changes post of March 6, 2008.

Hopefully, the EntityDataControl won't share the problems with the LinqDataControl that Julie described in her January 22, 2008 Thinking about the EntityDataSource post.

Note: The link to the OakLeaf blog in the latter post goes to the home page. Julie might have intended to link to go to the Problems Using Stored Procedures for LINQ to SQL Data Retrieval (Updated) post of October 2, 2007. That post contains a "Problems with Stored Procedures That Affect ASP.NET Projects Only" section, which covers sorting and paging problems.

Added: April 27, 2008

Jeremy Miller Reports on the MVP Summit 2008: Still Not Thrilled with the Entity Framework

The NHibernate Mafia weren't fighting the Persistence Ignorance battle at this year's MVP Summit, but Jeremy still isn't happy about the current (Beta 3) Entity Framework (EF) implementation. He says in A Train of Thought -- MVP Summit 2008 Edition of April 23, 2008:

    • I'm still not thrilled with the Entity Framework and the direction they locked themselves into, but the EF team came prepared to talk about some compromises that might start pushing EF into the usable category for me. I honestly wouldn't care about the EF, but I'll probably be back in consulting at some point in the next 5 years at a shop that will only use Microsoft tooling.
    • Daniel Simmons gets the "Thickest Skin of the Year" award. Not the Hyperion/Endymion Dan Simmons though.

Apparently the NH-Mafia worked Danny over again on EF issues.

In the "What I want for Christmas from Redmond" department, Jeremy's wish list includes:

    • Transparent lazy loading in the Entity Framework. If they want the Entity Framework to be usable in constructing rich Domain Models, they're going to have to do this.

EF enables lazy loading by default, but requires invoking the Load() method to retrieve the associated objects form the server. As Danny Simmons says in item 11. Lazy Load and Eager Load of his Entity Framework FAQ (updated 1/4/2008):

The entity framework attempts to make everything explicit, so even with lazy loading, you must explicitly call the Load method on the relationship in order to load its related entities. That way you know exactly when round trips are made to the database. As systems grow larger and more complex, this kind of explicitness can do a lot to improve overall supportability of the system.

I'm fine with calling Load(). Most of my projects involve multiple tiers, stored procedures, or both, so I ordinarily use eager loading by invoking Attach() with a filter to control depth, as Danny described in his Filtered association loading and re-creating an entity graph across a web service boundary... post of December 21, 2007.

Added: April 26, 2008

Colin Meek Elaborates on Generating Dynamic LINQ Expressions

Colin presented "VMS307: Visual Studio 2008: LINQ Deep Dive and Best Practices" at DevConn 2008 Orlando. His Using LINQ Expressions to Generate Dynamic Methods post extends his session's discussion of the ADO.NET Entity Framework Extension that "materializes  arbitrary CLR types given a data reader or DB command." Colin says:

The System.Linq.Expressions API serves a specific need for integrated queries: it allows the compiler to describe the user’s code as a data structure that can then be translated to targets other than MSIL at runtime, like SQL, Web Services, etc. Expressions can also be compiled into delegates at runtime, which brings me to a .NET 3.5 solution to the default shaper problem… If the compiler can use expressions to describe code, so can we!

Added: April 26, 2008

Rafik Robeal on Local Storage and Synchronization in HTML 5

HTML5 - The Offline Web of April 25, 2008 observes that HTML 5 will contain APIs for persistent storage in key/value and SQL databases and for offline Web applications. Rafik observes:

Isn’t interesting that despite the current networking revolution people are building the next web to work offline!? It is not strange to me but for many people this is bizarre. ...

To me, offline is an experience and programming model. Offline experience is all about richness, responsiveness, and better use of the data pipe. An offline programming model is what I’ve been showing people with Sync Services.

It's obvious that Ray Ozzie and Dave Treadwell share Rafik's enthusiasm for synchronization.

Added: April 26, 2008

Beth Massi Explains How to Query HTML with LINQ to XML

Beth's Querying HTML with LINQ to XML post of April 25, 2008 shows you how to use LINQ to XML to query HTML pages by removing markup entities, such as  , that cause the document to be malformed by XML standards.

Added: April 26, 2008

Pat Helland to Present at UC Berkeley's Tribute to Honor Jim Gray

Pat Helland, an architect on the Visual Studio team, will give the "Jim as a Mentor: Colleagues" presentation at the University of California Berkeley's Tribute to Honor Jim Gray. Pat, who worked with Jim Gray at Tandem Computers in the 1980s, announced in his Knowledge and Wisdom -- A Tribute to Jim Gray post of April 25, 2008 that his two-page paper for a special edition of the SIGMOD Record is available for download. The paper summarizes Pat's "experiences with Jim's approach to nurturing and mentoring."

From the press release for the Tribute:

Three organizations dedicated to the advancement of computing science, IEEE Computer Society, ACM, and UC Berkeley, today announced they will join the family and colleagues of Jim Gray in hosting a tribute to the legendary computer science pioneer, missing at sea since Jan. 28, 2007.

The tribute will be held May 31 at UC Berkeley’s Zellerbach Hall. The general session will be from 9-10:30 a.m., followed by technical sessions that will require registration. Registration and other information can be found at:

You can learn more about Jim Gray's mysterious disappearance at sea of and the search for him from these two contemporaneous OakLeaf posts: U.S. Coast Guard Searching for Jim Gray off San Francisco Coast of January 29, 2008 and "Friends of Jim" Halt Volunteer Search for Jim Gray of February 17, 2007.

Added: April 26, 2008

Eugenio Pace Implements a Simple Unit of Work Pattern for SSDS

SQL Server Data Services don't support client-side transactions, so implementing the Unit of Work pattern requires compensating actions in the event one element of multiple related updates fails after previous actions have succeeded.

Eugenio's LitwareHR on SSDS - Part VI - Unit of Work support post of April 25, 2008 describes how he modified his Repository class for the LitwareHR application to implement a simple version of the Unit of Work pattern.

Added: April 25, 2008

Sync Guru (Rafik Robeal) Questions the Problems Live Mesh Will Solve

Rafik Robeal was the technical driving force behind ADO.NET Sync Services, which is now part of the Microsoft Synchronization Framework, in turn a component of the Microsoft's new Live Mesh project. Rafik's moved on to mobile development, but his The Mesh – A New Synchronization Backbone post of April 24, 2008 asks the same questions I've been pondering:

[W]hat is the key problem that Live Mesh is trying to solve?

  1. Is it remote access? ...
  2. Is it data sharing? ...
  3. Is it data collaboration? ...
  4. Is it data synchronization platform? ...

Rafik believes it's basically #3 and #4 for consumers. I've got access to the Live Mesh Technology Preview, but both Rafik and I are waiting for the Live Mesh SDK to learn more about its API and data models.

My initial answer to Rafik's question is loss of developer mindshare and potential platform lock-in to Platforms as a Service (Paas) from Google, Amazon, Adobe and, perhaps, SalesForce.

Podcast: For more details about Microsoft's plans for Live Mesh from David Treadwell, corporate vice president of Microsoft's Live Platform Services and the exec behind SQL Server Data Service (SSDS) deployment as a Live Platform, read Dan Farber's Live Meshing on the Gillmor Gang post of April 25, 2008 and the Gillmor Gang's interview podcast.

Mary Jo Foley's Ten things to know about Microsoft’s Live Mesh of April 22, 2008 provides additional Live Mesh background.

Added: April 25, 2008

Doug Rothaus Performs Identity Transforms with LINQ to XML's ReplaceWith() Method

Doug demonstrates replacing an <eEmail> element with an <a href="mailto:" & eMailAddress ... >... element by using VB's XML literals and LINQ to XML's ReplaceWith() method in his VB XML Cookbook, Recipe 3: Identity Transforms (Doug Rothaus) post of April 25, 2008. A relatively simple pair of VB functions make a substitution that would require a significantly more lengthy XSLT transform.

Added: April 25, 2008

Mike Flasko: Astoria Team Targeting Silverlight 2.0 Beta 2 for REST Services

In the Astoria Team's Using REST Services in Silverlight post of April 24, 2008, Mike Flasko says:

[W]e are targeting Beta 2 of the Silverlight SDK (no dates to announce for this just yet) to have a version of the client library for ADO.NET Data Services. Given that we use many of the core pieces of Silverlight to enable data service interaction, we've waited for those to come into the platform so that we can start to round out our Silverlight experience. 

The scenarios we're looking to enable in Beta 2 are: the ability to send async queries, inserts, updates and deletes for same domain requests.  We're still working through how we'll enable these types of requests in cross domain scenarios.

I believe most of us were expecting an Astoria CTP for Silverlight 2.0 Beta 1 at the end of April, along with Beta 4 or a post-Beta 3 CTP of the Entity Framework. Doesn't look that way now, at least for Silverlight.

Added: April 24, 2008

Randolph Cabral Starts Exploring N-Tier Architecture with LINQ to SQL Series

Randy's Part 1 - Analyzing LINQ to SQL Entities post of April 23, 2008 follows up his earlier Active Record Vs. Unit of Work analysis of April 3 with an attempt to "marry" the Active Record and Unit of Work patterns with LINQ to SQL's DataContext.

He promises:

In the next post, we’ll take a look at the DataContext class and evaluate how we could best take advantage of its object tracking capabilities.  I’ll also explore writing a context manager class that helps ease the pains in dealing with the DataContext.

Added: April 24, 2008

Eric White Posts Second Edition of His Functional Programming Tutorial for LINQ Queries

Query Composition using Functional Programming Techniques in C# 3.0 of April 23, 2008 is Eric's 34-part rewrite of his original 2006 version. Eric says:

As with the previous tutorial, I've targeted this tutorial to C# developers who have no functional programming experience, i.e. the typical object-oriented coder.

One thing that I want to say at this point: This stuff is easy. It's also really fun. You don't have to read academic papers to learn about, enjoy, and benefit from functional programming in C# 3.0. There are about half a dozen concepts you need to learn, each one easy. Concepts like just a new way to write a method (that has no name), or a new way to write a static method for a class. Then you put them all together, and the result is more than the sum of the parts.

As you might expect, based on Eric's day job as an Open XML technical evangelist, he "shows you how to write code to extract the text of paragraphs of an Open XML document, along with the associated style of each paragraph."

Added: April 24, 2008

Jean-Paul Boodhoo Argues His Preference for var with Meaningful Variable Names

J-P says in his Got var? post of April 22, 2008:

As I strive toward more readable, intent revealing code, I find myself looking at the intent behind the usage of the variable coupled with a meaningful name, vs the actual type. In reality, I find the the argument to not use var is a moot point in a static environment that provides you with all the information you need once you hit the "."

His IList<Customer> listOfCustomers = new List<Customer>() example looks a lot like VB's and Access VBA's Hungarian notation that I spent 15 years writing.

Be sure to read the comments.

Added: April 24, 2008

Scott Hunter: New Refresh of Dynamic Data Runtime and Tools is Posted

The headline says it all. Sample code that's updated to work with the latest runtime is also available from the Code Gallery. The new Runtime and Tools drop includes (incomplete) documentation in CHM format.

Added: April 24, 2008

Pablo Castro Analyzes RESTful Optimistic Concurrency Management for Astoria with HTTP 1.1 Entity Tags

Pablo's Optimistic Concurrency & Data Services post of April 22, 2008 describes how HTTP 1.1 entity tags (ETags) can be used to detect whether a server resource has changed before accepting a client modification. The client keeps a copy of "opaque data," such as a version number or timestamp obtained from the server by a GET request. The client submits it in an if-match request header with its HTTP PUT or DELETE request. A failed match returns a 412 "Precondition Failed" status code to the client. Clients can override the match requirement by substituting an asterisk (*) for the opaque data.

ETags in the request header work for single entities; for collections, the plan is to add the ETag as metadata <entry m:type= "someType" m:etag="someValue" ...> to AtomPub and and in JSON's "__metadata" property.

Pablo observes a fly in the ointment: the definition of "strong" and "weak" eTags in section 13.3.3, "Weak and Strong Validators," of RFC 2616 - Hypertext Transfer Protocol - HTTP 1.1. HTTP 1.1 lets validation values remain unchanged when "no significant change in semantics" occurs. Weak TAGS apply only to GET operations, not PUT and DELETE, where he wants to use them.

P.S.: In a post of the same date to his personal blog, The Astoria team is hiring!, Pablo notes that the Astoria team is "looking for folks for Development, Quality Assurance and Program Management positions." It's likely that the team is staffing up to take on the challenge of aligning ADO.NET Data Services and the Entity Data Model (EDM) with SQL Server Data Sources (SSDS).

Eugenio Pace Takes the Complexity Out of SSDS Cross-Container Queries with .NET Parallel Extensions

In his More on parallel queries across containers in SSDS post of April 22, 2008, Eugenio shows the original code he used to implement the CrossContainerSearch() method he described in his earlier LitwareHR on SSDS - Part V - Searching across Containers post.

In this post, he finds that the scheduling, join and state management operations can all be handled by the .NET Parallel Extensions' Parallel.For() method, which greatly simplifies the code.

Eric White Creates a GroupAdjacent LINQ Query Operator for OpenXML Documents and LINQ to XML

Eric notes in his The GroupAdjacent Query Operator Extension Method post of April 20, 2008 that:

Quite often when you are processing an Open XML document using LINQ to XML, you want to group adjacent paragraphs that have the same style.

Let's say that the above array represents paragraphs in a document.  If the value is > 0, the paragraphs are a heading of the specified level.  The zeros represent paragraphs styled as normal.  We want to do some processing on the collection of paragraphs in such a way that we process all adjacent paragraphs of the same style in a single query.  We could group them, and execute a query on each group, but as you saw above, the standard GroupBy extension method will group all of the 0's together and all of the 2's together.

Given this array: int[] ia = new int[] { 1, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0 };

GroupAdjacent groups them like this: In contrast, the standard GroupBy method groups them like this:
Group 1
Group 0
Group 2
Group 0
Group 2
Group 0
Group 1
Group 0
Group 2

I believe the GroupAdjacent() query operator will prove quite useful for general LINQ to XML processing, not just OOXML work.

Rob Conery Deep Dives into LINQ to SQL Data Access for his MVC Storefront Project

Rob continues the Webcasts of his ASP.NET MVC Storefront project in ASP.NET MVC: MVC Storefront, Part 4 of April 21, 2008:

In this webcast I dive pretty deep into Data Access and talk about some architectural decisions I need to make, specifically how to get LINQ To SQL to map to my model properly. ...

Steven Harman suggested I run a Spike (a quick prototyping exercise to test out a theory) to make sure that my data access strategy will work with an actual database. Good idea.

I quickly ran into some issues with LINQ To SQL, but found some neat ways to keep things clean and tidy so I don't explode my database all over my happy little application.

Previous episodes are:

Update April 24, 2008: Rob's ASP.NET MVC: MVC Storefront, Part 5 covers globalization issues.

Scott Hanselman Interviews Scott Hunter About ASP.NET Dynamic Data

Scott Hunter is a program manager on the ASP.NET Dynamic Data team. The Hanselminutes podcast is at <ASP.NET Dynamic Data with Scott Hunter/>.

Matthew Hunter Continues Improvements to his LINQ to SQL Entity Base Implementation

Matt describes these improvements:

  1. There is no longer need to have a timestamp field
  2. Option to keep original entity values.

to his LINQ to SQL Entity Base implementation in his Improvement: No requirement for timestamp column any longer post of April 23, 2008.