Monday, December 24, 2007

LINQ and Entity Framework Posts for 12/24/2007+

Note: Happy New Year! There probably won't be many updates to this linkblog post.

JSON vs. XML DataContract Serialization: Downloadable Test Harness

This VB 9.0 test harness compares the message size and performance of moderately complex objects serialized and deserialized by the NET Framework 3.5's new DataContractJsonSerializer (DCJS) class and the .NET Fx 3.0's XML-based DataContractSerializer (DCS) class.

Here's a screen capture of the test harness's second mode, which displays the formattted JSON string for Northwind Order and Order_Detail objects:

Click the image for a full-size screen capture.

You can download the VB 9.0 code for the JsonSerializationWinVB.sln test harness project  here. The project requires Visual Basic 2008 Express or Visual Studio 2008 Standard or higher but doesn't need a database connection. (Mock generic data objects are provided.) The code is based on Rick Strahl's original version described below.

Rick Strahl Dissects .NET Framework 3.5's JSON Serializer

Rick's DataContractJsonSerializer in .NET 3.5 post of December 29, 2007 delves into the System.Runtime.Serialization.Json.DataContractJsonSerializer namespace, which contains the serializer and related objects for the lightweight JavaScript Object Notation (JSON) transport (RFC 4627). Rick observes that this serializer produces "Plain Old JSON" (POJ?) text without the proprietary __type="TypeName" attribute that ASP.NET 2.0 AJAX Extensions introduced.

To run Rick's code, you must add references to System.Runtime.Serialization (v3.0 for WCF) and System.Service.Model.Web (v3.5 for JSON), and the following directives:

using System.Runtime.Serialization.Json;
using System.Collections.Generic;
using System.IO;
using System.Text;

Add a using System.Runtime.Serialization; directive if you want to decorate classes with [DataContract] instead of [Serializable] attributes and add the [DataMember] attribute to public fields or properties you want to serialize.

Added: 12/30/2007

Paul Yuknewicz: ASP.NET LinqDataSource Guided Tour

The VB Team's LINQ for the Web Using VB (By Paul Yuknewicz) post provides an illustrated guided tour of the LinqDataSource component and LINQ to SQL behind a simple ASP.NET 3.5 Web site. The post is similar to Scott Guthrie's LINQ to SQL: Part 5 - Binding UI using the ASP.NET LinqDataSource Control post of July 16, 2007 except that VB substitutes for C#.

Added: 12/28/2007

Paul Vick: Visual Basic 9.0 Spec (Almost) Cooked

Paul Vick's (Almost) final VB 9.0 language specification posted article of December 27, 2007 (almost) says it all. You can download the latest edition (sans final copy edits) here. According to Paul, the spec covers the following new VB 9.0 features:

  • Friend assemblies (InternalsVisibleTo)
  • Relaxed delegates
  • Local type inferencing
  • Anonymous types
  • Extension methods
  • Nullable types
  • Ternary operator
  • Query expressions
  • Object initializers
  • Expression trees
  • Lambda expressions
  • Generic type inferencing
  • Partial methods
  • XML Members
  • XML Literals
  • XML Namespaces

Added: 12/27/2007

.NET Rocks: Joe Duffy on the Parallel Extensions and PLINQ

Carl and Richard interview Joe Duffy, the PM behind the Task Parallel Library (TPL), Parallel Extensions for .NET (PFX), and Parallel LINQ (PLINQ), about parallel (concurrent) programming and multi-core processors in Joe Duffy on the Task Parallel Library of December 24, 2007.  See the "Parallel Extensions" topics in LINQ and Entity Framework Posts for 11/26/2007+ and my PLINQ Gathers Momentum post of December 8, 2006 for more background.

Note: The PFX Team now has a Parallel Programming with .NET blog that has several posts on the use of PLINQ. Links to this blog will be added to future posts.

Added: 12/27/2007

Mike Taulty Discovers that WebDataGen.exe Doesn't Recognize LINQ to SQL Associations

Mike's ADO.NET Data Services - LINQ to SQL and Associations post of December 24, 2007 points out that when you run WebDataGen.exe against the URL of a .NET Data Source based on LINQ to SQL associations are ignored. The metadata that exposes associations as a Property of the String data type. Using an Entity Framework data source designates associations with a typed NavigationProperty and a corresponding Association element group.

This obviously prevents writing LINQ to REST queries that traverse associations. Defining LINQ to SQL entities with hand-written classes, as outlined in Andy Conrad's Linq to REST post of December 10, 2007, enables associations. You can then substitute LINQ queries, such as

var query2 = from c in context.CreateQuery<Categories>("Categories")
             where c.CategoryID == 1
             select new { c.CategoryID, c.CategoryName, c.Products };

for WebDataQueries like

WebDataQuery<Categories> query =

However, I've found that any attempt to filter collections with Where criteria based on child member values, such as

var query = from c in context.CreateQuery<Customer>("Customers")
            where c.Country == "USA"
            && c.Orders.Any(o => o.Freight > 50)
            orderby c.CompanyName
            select c;

fail with a 400 - Bad Request error because the query string isn't composed properly. (Commenting the Freight amount test enables the query to execute.)

It's not clear to me how restricted LINQ to REST query syntax will be at RTM. It's likely that many Astoria users will load large chunks of data into memory-resident IEnumerable() sequences (probably generic Lists) and query them with client side LINQ to Objects expressions.

Mike Taulty Suggests Cure for Error 7005: 'Namespace' Attribute Is Invalid

The solution in Mike's ADO.NET Data Services and CLR Namespaces post of December 24, 2007 is to "put the types that you're exposing (i.e. the T that you have fed into WebDataService<T>) into a CLR namespace."

So far, I haven't encountered that exception.

A Christmas Present from Milan Negovan: A LINQ Cheat Sheet

You can read and download the two-page Cheat Sheet for Standard Query Operators (method call syntax) at Milan's ASP.NET Resources site.

Mike Flasko Proposes Eliminating Parent Key Duplication with Deep Addressing

Mike Flasko's Design Notes: URI Containment in Astoria post of December 21, 2007 (missing from previous linkblog post) observers that parent keys are duplicated in following case b:

a) entities are always addressable through top-level entitysets, e.g. /OrderLines(1,2)

b) deep addressing through container has redundant information, e.g. /Orders(1)/OrderLines(1,2)  (the container id, “1”, is repeated)

The plan is to annotate the CSDL and CLR object model to indicate a parent/child relationship to eliminate the repetition.