Tuesday, October 09, 2007

LINQ and Entity Framework Posts for 10/5/2007+

Jim Wooley Demos WCF 3.5's SyndicationFeed Feature for RSS/ATOM Feeds

Jim's October 12, 2007 Adding RSS posts to ThinqLinq using System.ServiceModel.Syndication.SyndicationFeed post demonstrates the progression of a VB 9.0 LINQ to SQL application that uses LINQ to XML to persist objects from RSS feeds to a database:

  1. Query the feed document and use a For Each loop and invoke the DataContext.Add() method to insert each object into the DataContext. Finally apply the DataContext.Submit() method to persist the collection.
  2. Early-bind element names and execute a LINQ query to generate an IEnumerable(Of PostItem) collection of projections created with object initializers. This eliminates the need for the For Each loop.
  3. Substitute SyndicationFeed.Load(Uri) for XDocument.Load("URL") to obtain a strongly typed object model that handles RSS and ATOM formats interchangeably.

System.ServiceModel.SyndicationFeed is a new Windows Communication Foundation feature in .NET 3.5's System.ServiceModel.Web namespace.

Added: October 12, 2007, 0930 PST

Erick Thompson Finally Describes LINQ to DataSet's LinqDataView Object

Erick provided a brief review of databinding with LINQ to DataSet in his LINQ to DataSet – Data Binding post of August 24, 2007 and promised to "be digging into more detail soon." Almost a month and a half later, he posted LINQ to DataSet Data Binding – Introducing LinqDataView on October 5, 2007, but I missed it. Thanks to the ADO.NET team who added a LINQ to DataSet DataBinding post today with a pointer to Erick's earlier post, which probably wasn't what they intended, I caught the October 5 post.

LinqDataView is an internal object from the System.Data.DataSetExtensions namespace that inherits DataView and implements LinqDataView, IBindingListView, IList, ICollection, and IEnumerable. Implementing the IBindingListView interface enables advanced (multi-column) sorting and filtering, as mentioned in my September 7, 2007 Support Sorting and Filtering for List<T> Collections post. You create a LinqDataView instance when you invoke the AsDataView() method on a LINQ to DataSet query.

Erick recommends using the LinqDataView for databinding UI controls and fast indexed lookup operations.

Erick says his "next post will talk about restriction joins and leveraging the index for fast lookups." Let's hope it comes a bit quicker than this one.

Earlier LINQ to DataSet references from LINQ and Entity Framework Posts for 9/7/2008+:

  • Andrew's Science Project post of September 7, 2007 includes the source code for a LINQ to DataSet extension method that removes the CopyToDataTable<T> method's restriction of the generic T type parameter to DataRow. The extension method enables loading instances of anonymous types into DataRows.
  • Daniel Moth has a July 27, 2007 LINQ to DataSet item with details of the classes in the System.Data.DataSetExtensions namespace.
  • Here are links to Erick Thompson's three introductory LINQ to DataSet posts from February 2007: Part 1, Part 2 and Part 3.

Added: October 12, 2007, 0930 PST

Frans Bouma Moves from Days to Parts and Admits Matt Warren Was Right About Sets

In Developing Linq to LLBLGen Pro, part 6 of October 12, Frans recounts at length a misstep when attempting to "outsmart the algorithm of Topological Sorting in a Directed Acyclic Graph (DAG)."

Object graphs are DAGs and Frans mentions that "cycles in relational models which are reflected in entity models is a Bad Thing and typically have to be redesigned." I mention the issue with cycles in serializing entities generated from LINQ to SQL that have both m:1 and 1:n associations in my LINQ to SQL and Entity Framework XML Serialization Issues with WCF - Part 1 of October 9, 2007.

Update October 15, 2007: Frans clarifies in a comment that he's referring specifically to cycles in relational models rather than cycles in related object graphs. The latter topic is one of the subjects of my XML serialization issues posts.

Frans then equates his earlier DAT and Topological Sorting foible with the requirements for handling a multiplying number of special cases in his previous (day 5) post, concludes that Matt Warren's following recommendation in a comment to use sets was right.

Dinesh Kulkarni Compares Capabilities of SqlMetal, the O/R Designer and Code

Dinesh is on a blogpost roll with a second post in two days. His LINQ to SQL: features not in the designer / SqlMetal post is dated October 9, 2007 but didn't show up on my reader until this morning. The article covers design features that:

  1. You can implement with SqlMetal.exe but not the graphical O/R Designer
  2. You can implement with the graphical O/R Designer but not SqlMetal.exe
  3. You can't implement with SqlMetal.exe nor the O/R Designer and require writing code

Dinesh mentions two items under category 3:

    1. POCO (Plain Old CLR Objects) - this is worth another blog post
    2. User overrides (e.g. sprocs) for relationship loading

My Save Server Round Trips by Preloading LINQ to SQL EntityRefs post of October 1, 2007 describes how to minimize round-trips and limit the size of associated EntitySets.

Added: October 11, 2007, 0930 PST

Joe Cheng Solves the Wide Finder Problem with LINQ and PLINQ

Tim Bray's Wide Finder Project originated as an exercise to improve the performance of a small Ruby program that reads Apache logfiles, applies a regular expression, counts, sorts, and finally prints the 10 most popular blog posts. The challenge is to take advantage of the CPU's multiple cores by using parallel processing to improve performance.

Joe Cheng, a developer on the Windows Live Writer team, posted on October 2, 2007 Wide Finder with LINQ, which contains a compact LINQ query with a few lines of C# code that beats the performance of the Ruby by a couple of seconds when reading a million-line logfile.

A pure C# version beat the LINQ query by a couple of seconds, but required 85 lines of code. As Joe notes, the LINQ version has the potential to use Parallel LINQ (PLINQ) from the forthcoming Parallel FX Library to improve performance.

See the "Luke Hoban Gets the Most Complex LINQ Query Award (and PLINQ Compatibility)" topic of my LINQ and Entity Framework Posts for 10/2/2007+ post, which describes the Parallel FX program and task parallel library:

The obvious advantage of moving from "conventional" C# 3.0 constructs to a single LINQ query is Parallel LINQ (PLINQ, which is part of the Parallel FX or PFX program) compatibility for simplified parallel processing on multi-core machines. There's a substantial Microsoft team at work on PLINQ and the October 2007 issue of MSDN Magazine carries a "Running Queries On Multi-Core Processors" story about PLINQ by Joe Duffy and Ed Essey.

Joe plans to release a PFX CTP in 2007. Watch his blog for details.

Daan Leijen and Judd Hall describe the more complex forthcoming Task Parallel Library (TPL) in an "Optimize Managed Code For Multi-Core Machines" article in the same issue. The C# 3.0 ray-tracer version upgraded with TPL will be an sample application in the PFX CTP.

Joe promises to publish performance data when Microsoft releases the Parallel FX Library.

Note: Don Box published in Wide Finder in C# - the Naive implementation of October 9, 2007 a more complex LINQ query that reads a stream from the logfile but appears to be slower than Joe Cheng's version (which Don cites in a ednote update.)

Credit: I use Windows Live Writer to author this blog. My review of the first beta version is Windows Live Writer (Beta) Blogger Test-Drive of August 14, 2006.

Added October 11, 2007, 0800 PST: I found this jewel thanks to Tim Bray's WF IX: More, More, More post of October 10, 2007)

Beth Massi Conducts Channel9 Sync Services Interview with Milind Lele

This blog also covers SQL Server CE and Microsoft Synchronization Services (a.k.a. Occasionally Connected Systems or OCS) topics, which don't see as much action as LINQ and the Entity Framework. Milind is a program manager on the VB Team, so you get to see some VB code instead of Sync Guru Rafik Robeal's C#-only samples.

You can catch the interview here.

Backstory: Mary Jo Foley's Microsoft ‘Arrowhead’ to up support for ‘occasionally connected’ apps post of October 10, 2007 makes this Sync Services/OCS interview more interesting. Apparently, Microsoft wouldn't respond to Mary Jo's "How does Arrowhead dovetail with Microsoft’s current offline synchronization platform, ADO.Net Sync Services?" query.

Sync Services gained considerable momentum at Microsoft after Google launched Google Gears. See my Google Gears Piques New Interest in Data Synchronization post of June 7, 2007.

Updated: October 11, 2007, 1300 PST

Julie Lerman Describes How to Customize Your Astoria Data Source

Her Custom Operations and hiding entities in Astoria post of October 10, 2007 shows how to "customize your service by providing explicit query operations and also blocking specific query operations." She also notes that you can build read-only views into the service, but you'll have to "wait wait until we have bits that are not just prototype" to learn how to enable stored procedures. 

Added: October 10, 2007, 1230 PST

Mike Taulty Implements an Unbound DataContext in a WCF Client

My updated version of the LINQ to SQL and Entity Framework XML Serialization Issues with WCF - Part 1 post mentions Mike Tulty's displeasure (which I share) with the tedium of writing code to clone and save original values to implement value-based concurrency conflict management. So Mike described in his LINQ to SQL and WCF - Sharing types, subverting the DataContext on the client side post of October 9, 2007 how to implement in a Windows Communication Foundation (WCF) client a DataContext class that's not bound to a database. The starting point is client and service described in his initial Disconnected LINQ to SQL post of September 14, 2007.

The unbound DataContext provides Attach(), AttachAll(), Add(), AddAll(), Remove()and RemoveAll() methods for Table<TEntity> implementations of the ITable interface and stores the original and changed entity values. You apply changes to the attached entities directly. The Table<TEntity>.GetModifiedMembers() method contains references to added, removed, and modified members.

Invoking Mike's GetInserted(), GetModified() and GetDeleted() methods supply modified argument values to InsertEntity(modified, original) and UpdateEntity(original, modified) service methods. The Table<TEntity>.GetOriginalEntityState() method provides original argument values to the preceding two service methods, plus the DeleteEntity(original, modified) method. (DeleteEntity() requires both arguments because you must attach the entity before you remove it.)

Mike includes a simple client console app to test his methodology, which he says "is really a bit subversive and possibly a bit evil."

What's interesting about this approach is its similarity to Matt Warren's promised "mini connectionless DataContext" that I discussed in my Changes Coming to LINQ to SQL post of May 15, 2007. This article resulted in a long LINQ: Next CTP thread in which Matt Warren reneged with these two posts:

No, this feature will not be in the first release of LINQ to SQL. ...

It is only missing because of lack of time.  When I mentioned it, I was talking about our forward thinking and a sample I'm trying to put together.

WCF's proxy generation tool creates the client-side classes that Mike includes in his DataTypes class library generated by executing sqlmetal.exe /server:. /database:northwind /serialization:Unidirectional /pluralize /code:northwind.cs, with these exceptions:

  • The proxy classes don't implement the INotifyPropertyChanging interface.
  • The class library doesn't add the ExtensionData property to the entities.

Neither of the above problems should be fatal, so I'll give Mike's approach a test with the C# version of the test harness described in yesterday's LINQ to SQL and Entity Framework XML Serialization Issues with WCF - Part 1 post and report my results.

Note: Mike uses the terms Shared Contract and Shared Types differently than the WCF team. Sowmy Srinivasan describes the difference between the two terms in his All About KnownTypes post of June 06, 2006 by how they're serialized on the wire. Aaron Skonnard discusses the history of WCF SharedContract and SharedType modes in his WCF's NetDataContractSerializer post of April 21, 2006. Services that use the default DataContractSerializer implement the SharedContract mode.

Added: October 10, 2007, 1100 PST

Update October 11, 2007: Mike's DurableServices in WCF V3.5 :-( post today indicates to me that he's working on another approach or a supplement to maintaining context with WCF features akin to Durable Services.

Update October 14, 2007: Corrected service method names and clarified client-side operations in conjunction with the

Dinesh Kulkarni Links to Breaking Changes from LINQ to SQL Beta 2 to RTM

Apparently Dinesh doesn't believe that folks are checking posts in the LINQ Project General forum, so he added in his LINQ to SQL breaking changes from beta2 to RTM (compiled from my LINQ forum posts) post of October 9, 2007 links to his three previous posts on the subject:

I can only assume that these two important changes not included in the preceding list won't beak Beta 2 code:

because Dinesh didn't mention them in his post or the liked threads.

Added: October 10, 2007, 1200 PST

Eric White Summarizes Changes to LINQ to XML from Beta1 to Beta2 and RTM

Not to be outdone by the LINQ to SQL team, Eric White's October 9, 2007 Upcoming changes in LINQ to XML post lists four changes between Beta 1 and Beta 2 and three more coming up in RTM.

Added: October 10, 2007, 1200 PST

Beth Massi Links to Scott Hanselman's Audio Podcast about LINQ to XML

Scott Hanselman recorded on August 31, 2007 a <LINQ to XML/> Hanselminutes podcast that's hosted by Carl Franklin.

Beth announced that she'll present Conquering XML with LINQ in Visual Basic 9.0 at the QCon Conference in San Francisco (see QCon 2007 and Erik Meijer Coming to San Francisco November 7 - 9 of August 2, 2007.)

There's a list of 14 LINQ to XML resources and she also promises future "How Do I?" LINQ to XML videos at the bottom of her Scott Hanselman on VB9's New "XML Hotness" post.

Added: October 10, 2007, 1230 PST

LINQ to SQL and Entity Framework XML Serialization Issues with WCF - Part 1

This post introduces the issues I encountered when attempting to create a sample application that illustrates interactions between a real-world, data-bound Windows client, Windows Communication Foundation (WCF), and LINQ to SQL data access layer (DAL).

The post deals with the full lack of object-graph schema fidelity to the original entity's properties. You lose Attached Associated Objects Bugs for m:1 associations if your object has EntitySets for 1:n associations that share a common property.

There's also a graphic example of the "Attached Associated Objects Bug" that duplicates associated entities when attaching a Table<TEntity>  object that has EntitySet properties to a DataContext.

Added: October 9, 2007, 1700 PST

Matt Warren Tackles the Four OrderBy Flavors in Part VIII of His Building an IQueryable Provider Saga

LINQ: Building an IQueryable Provider - Part VIII begins with Matt's observation:

Fortunately, there is only one way to do ordering, and that's using the LINQ ordering specific query operators. The bad news is that there are four different operators.

Matt then runs into (and around) corners resulting from the immutability of expression trees and devises an amazing OrderByRewriter class that he describes as "a whole lot of something!" to handle what appears to be this simple LINQ query:

var query = from c in db.Customers
    orderby c.City
    where c.Country == "UK"
    select new { c.City, c.ContactName };

As usual, the source code is available to download.

Added: October 9, 2007, 1700 PST

"Leverage LINQ in ASP.NET 3.5 Projects" Available for Download from WROX

My Leverage LINQ in ASP.NET 3.5 Projects article is one of four inaugural Wrox Blox downloadable "informational pieces" that "help get you up-to-speed and keep you up-to-date" plus "build the knowledge you need quickly on a wide variety of topics."

For $3.99, you can download the 38-page tutorial and it's accompanying C# and VB source code for a complete LINQ to SQL data access layer (DAL). The DAL binds to a LinqDataSource or ObjectDataSource control and an updatable GridView control on ASP.NET 3.5 pages. (The source code requires Visual Studio 2008 Beta 2 or later and the Northwind sample database installed on an SQL Server 2005 Express instance.)

You can add a new "BOGUS" Customer entity with code, edit its properties in the grid, and delete the entity without affecting your original Northwind Customers table's contents.

Click image for full-size screen capture.

The source code for the LinqDataSource and ObjectDataSource test harness is available here.

Added: October 9, 2007

Update October 9, 2007: Wrox Blox editor Jim Minatel offers more details on the new format in Wrox Blox launches with Silverlight, Adobe AIR, IPhone and LINQ and announces Wrox's ASP3wiki, the second Wrox electronic product to launch this week.

Catch 10 LINQ-Related Sessions at Tech*Ed Developer 2007

Tech*Ed Developers 2007 to Include 10 LINQ-Related Presentations includes abstracts and presenter names of the 10 sessions in Tech*Ed 2007 Barcelona being held on November 5 - 9, 2007. LINQ Luminaries Luca Bolognese, Mike Taulty, and Bart De Smet are among the presenters. This post eliminates the need to register to open and search the EMEA Events page for the conference.

Added: October 9, 2007

Dinesh Kulkarni Addresses Attach()/Detach() Confusion in LINQ to SQL Projects

Dinesh's Attach() if you have something detached post of 10/8/2007 attempts to clear up the confusion surrounding use of the DataContext.TableName.Attach() method in stateless ASP.NET and Windows Communication Foundation (WCF) service projects. Dinesh attributes much of the confusion to the term detached object support, which he characterizes as a "misnomer." As an example, Dinesh cites

  • 8. Streamlined and enhanced support for detached objects (see Attach() APIs) for multi-tier apps

in his LINQ to SQL (fka DLinq): What's new in Orcas beta2 post of July 26, 2007. This post appears to be the result of the quest for a:

  • 6. A story for detached object - this is handy for stateless situations like ASP.NET, web services etc.

which appears to have been the most favored DLinq feature from Dinesh's DLinq: What's cooking in the kitchen post of March 16, 2006.

Java's Hibernate and .NET's NHibernate object/relational mapping (O/RM) tools offer detached object support. Here's the definition from Sun Microsystems' J2EE Persistence: Explore Your Persistence Options presentation by Sang Shin:

  • Detached Objects – Instances that lose association with persistence manager when you close() the session
  • Hibernate allows for selective reassociation of a subgraph. [For example,] you may serialize objects to the web tier, then serialize them back to the EJB tier in the next request.
  • Reference to detached object outside of a transaction/persistence manager can be
    retained and reassociated with a new persistence manager.

Note: For more about detached objects in Hibernate, see section 4.1.3 Detached objects on page 142 (Chapter 4) of Hibernate in Action by Christian Bauer and Gavin King (Manning, 2005).

Hibernate and NHibernate are mature, widely used, and the standard of comparison for O/RM tools. What's interesting is that LINQ to SQL doesn't support selective reassociation of a subgraph or reassociation with a a new persistence manager. Reassociation appears to be explicitly verboten (by exception) in the RTM version.

Perhaps the confusion about detached objects arises from differences in definition between LINQ to SQL and Hibernate.

Another source of confusion might be the bug in LINQ to SQL Beta 2 that causes entities associated with a newly-attached root entity to be inserted rather than assumed to be persisted in the database and awaiting update. For more details, see Feedback: Incorrect Update Behavior - Attaching an Entity after Calling a Relationship Property of August 27, 2007 (resolved as fixed on September 5, 2007) and Rick Strahl's Complex Detached Entities in LINQ to SQL - more Nightmares post of October 1, 2007. If the LINQ to SQL team delivered their long-promised sample project to demonstrate LINQ to SQL with WCF services, they might have discovered the issue before locking down the Beta 2 bits. (This assumes that the example included associated entities, which seems to be uncommon in sample projects.)

Note: See sample WCF project issues with LINQ to SQL and Entity Framework in my "From the 'Talk is Cheap' Department" subhead of the "Julie Lerman Demonstrates Detached Updates with Entity Framework" of LINQ and Entity Framework Posts for 9/28/2007+ post.

Stay tuned for a LINQ to SQL/WCF test harness that demonstrates this bug.

Added: October 9, 2007

Julie Lerman on Persisting Entity Framework Object Graphs in ASP.NET ViewState

Julie's Entity Framework object graphs and viewstate of October 5, 2007 confirms that Entity Framework object graphs can be binary-serialized into an ASP.NET page's Viewstate property value. Unlike with conventional XML serialization, which ignores associated objects,  Viewstate preserves associations but not root object state. ObjectContext (like DataContext) objects can't be serialized.

Julie also discovered that viewstate synchronizes object updates:

One saving grace is that objects stored in viewstate get updated automagically during postback. In other words, if I create a cust object and store it in viewstate, then make a change to the cust object, then postback and pull the object back out of viewstate, it will have the newer property values. Viewstate is synced up to the object.

I'm sure we'll hear more on this topic after Julie delivers her Accessing Data Services in the Cloud with Astoria presentation to REMIX07 Boston today (2:30 - 3:45 p.m.)

Added: October 9, 2007

Dom Demsak and LINQ to XML Parts 1 and 2 on DNR TV

From the "How Did I Miss This" Department

Dom Demsak (DomXML) recorded two episodes of "Dom Demsak and LINQ to XML" for Franklins.net's DotNetRocks TV:

Don lists his "great takaways" from the series in his .Net Rocks - XML Literals - You Got VB In My XML post of September 11, 2007.

2 comments:

Anonymous said...

"I mention the issue with cycles in serializing entities generated from LINQ to SQL that have both m:1 and 1:n associations in my LINQ to SQL and Entity Framework XML Serialization Issues with WCF - Part 1 of October 9, 2007."
No, that's not the same thing :). What you mentioned is a problem for graph handling, and one of the reasons why my own algorithm didn't work that well.

What I meant with cycles is this:
You have a Department entity which has a 'Manager' FK to 'Employee'. You also have a 'WorksFor' FK in employee to Department.

This is a cycle. You can't save them with 2 insert statements, you need 2 insert statements and 1 update. (insert NULL in one of the two FK fields). This is the reason why it's bad.

What YOU mentioned is a cycle in the in-memory graph, but that's actually artificial. I.e.: the relation Customer - Order (1:n) is the same as the relation Order - Customer (m:1), they're based on the same fundamental association between order and customer, namely based on the FK in order to customer, and nothing else.

So the graph in memory has cyclic references, but the ENTITIES don't have a cycle, the dependency order -> customer is only one way, so a directed graph. (my algo tagged entities already seen to avoid the cyclic references, which failed in some edge cases where multiple paths ran through the same in-memory object)

Anonymous said...

Hi,

Thanks a lot for taking what I started on the "disconnected LINQ" bits a lot further - really impressive stuff.

On the comment you made around my "Durable Services" post - I wasn't actually looking at this in the context of LINQ at all, I was just revisiting it as I have to give a WCF talk later this week :-)


Thanks,

Mike.