Sunday, January 20, 2008

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

Danny Simmons Describes a Serializable EntityBag for n-Tier Entity Framework Deployment

I'm glad to see the ADO.NET Entity Framework team taking the pragmatic approach to Web service-enabling v1.0 with the serializable EntiyBag that Danny describes in his EntityBag Part I – Goals post of January 20, 2008.

For my take on Danny's answer to the LINQ to SQL Team's apparently-abandoned serializable "mini-connectionless DataContext" read Coming to the Entity Framework: A Serializable EntityBag for n-Tier Deployment, which ran too long for inclusion in this post.

Added: 1/20/2008

John Papa Explains How to Design an Entity Data Model with the EDM Tools

John's February 2008 "DataPoints" column for MSDN Magazine is subtitled "Designing an Entity Data Model." Topics include:

  • Understanding the EDM
  • Building an EDM with the Wizard
  • Stored Procedures in the Entity Model
  • Windows on EDM
  • Derived Entities

This article follows up his July 2007 "ADO.NET Entity Framework Overview" column.

Added: 1/20/2008

Eric White Explains How to Extract Comments from OOXML Documents

In his How to Extract Comments from Open XML Documents of January 16, 2008, Eric uses LINQ to XML to identify and extract comments as an XML list from WordprocessingML, SpreadsheetML, and PresentationML documents in a specified folder.

Added: 1/20/2008

Danny Simmons Posts the Finale to His Extension Methods Extravaganza

Danny's final post in the series, EF Extension Methods Extravaganza Part III – Fun with LINQ to Objects of January 17, 2008, includes the following LINQ to Objects extension methods that you invoke on an ObjectContext instance:

  • ObjectContext.GetEntities(EntityState state)
  • ObjectContext.GetRelationships(EntityState state)
  • ObjectContext.GetUnchangedManyToManyRelationships()
  • ObjectContext.GetRelationshipForKey(EntityKey key, EntityState state)
  • ObjectContext.GetRelationshipsByRelatedEnd(Entity entity, EntityState state)

He incorporates a caveat that these extension methods operate on internal methods that are subject to change in CTP and RTM releases that might be exposed in future releases, which potentially would obsolete some of these extension methods. (See Danny's comment of 1/18/2008.)

Added: 1/17/2008 Updated: 1/20/2008

Mike Taulty Produces Five ADO.NET Data Services Screencasts

His ADO.NET Data Services - Screencasts post of January 17, 2008 offers links to the following five video screencasts:

The "A Basic Silverlight Client" is a fast-paced, 14-minute segment that shows you how to create a simple read-only Silverlight Web client with WebDataGen.exe that's indistinguishable from the Windows version. Mike says:

I particularly enjoyed the Silverlight one which I stayed up into the small hours of the morning making. Why? Because of how closely it lines up with what you do in a full .NET application.

Once Silverlight grows some controls and databinding in version 2.0 I really see Silverlight+Data Services as being a killer combination.

Added: 1/17/2008

Rob Conery Continues the Subsonic 2.1 ("Pakala") Preview

Rob's SubSonic: 2.1 (Pakala) Preview, Part 2 post of January 16, 2008 shows an example of his Query2 syntax, which has decided LINQ overtones:

ProductCollection p = Northwind.DB.Select()

He also provides examples of code to return typed results from stored procedures and to substitute a new Repository pattern for the traditional ActiveRecord pattern.

Note: Pakala Village is on the Island of Kauai diagonally opposite Princeville, where Rob appears to live (on Weke Road, Hanalei).

Matt Warren Resurfaces with Part IX of the Building an IQueryable Provider Series

After a three-month hiatus, Matt finally posted LINQ: Building an IQueryable Provider - Part IX on January 16, 2008. (Matt blames the TV writer strike for the long delay.)

This post, which appears to be subtitled "Cleaning up the Mess," shows prospective LINQ IQueryable providers how to eliminate SELECT statement redundancy in generated SQL statements and eliminate unused columns from the query.

Be sure to read Frans Bouma's comment. He wants an episode that explains 'Funcletization' (and so do I.)

Note: Frans' Developing Linq to LLBLGen Pro, part 11 post (see the "Frans Bouma Tackles All/Any, Cast, and Contains but Abandons Aggregate and Concat" topic below) and Luke Hoban's response to a MSDN C# forum question are the only two Google hits I get on 'Funcletization':

What you describe is actually a generally useful thing to do for Expression Trees. We refer to it internally as "Funcletization" - though the name leaves something to be desired. Linq to SQL does something similar down in it's guts - turning sub-expression-trees which do not depend on any of the parameters into Constant nodes.

You can implement this yourself by:

1) Writing a visitor over the Expression Tree which recreates the tree at each node.

2) Specializing this to check at each node whether the subtree depends on any of the parameters, and if not - call .Compile on it and replace the sub tree with a Constant node with the value returned from calling the delegate returned from .Compile()

I've been meaning to post some sample code that does this - but maybe you can beat me to it :-)

Luke Hoban, who's a F#C# compiler PM, is probably the man to ask about Funcletization, but he appears to be blogging primarily (and infrequently) about functional programming.

Added: 1/17/2008 Updated: 1/20/2008

Danny Simmons Follows Up with EF Extension Methods Extravaganza Part II

The second episode, EF Extension Methods Extravaganza part II – Relationship Entry & IRelatedEnd of January 16, 2008, provides extension methods for querying relationship entries, EntityReference<T> and EntityCollection<T>. You're likely to find the following extension methods especially useful if the ADO.NET team doesn't provide the option to make foreign key values visible:

  • ObjectStateEntry.IsRelationshipForKey(EntityKey key)
  • ObjectStateEntry.OtherEndKey(EntityKey thisEndKey)
  • ObjectStateEntry.OtherEndRole(EntityKey thisEndKey)
  • IRelatedEnd.IsEntityReference()
  • IRelatedEnd.GetEntityKey()
  • IRelatedEnd.SetEntityKey(EntityKey key)
  • IRelatedEnd.Contains(EntityKey key)

Something to think about: All primary keys aren't necessarily surrogate keys; often it's a natural key that has business meaning, such as an employee ID number. Unless natural foreign key values are visible, you must instantiate the parent object to gain access to the value.

If part II is a harbinger, this looks to be a fast-paced series.

Added: 1/17/2008

Danny Simmons Launches Entity Framework Extension Methods Extravaganza

Danny's first episode, EF Extension Method Extravaganza Part I - ObjectStateEntry, of January 15, 2008 is "the first installment in a series of posts with lots of little extension methods which seem to make my recent Entity Framework experiments easier to write."

The first set of extension methods are:

  • GetEntityState()
  • ObjectStateEntry.UsableValues()
  • ObjectStateEntry.EdmType()
  • AssociationType.IsManyToMany()

This has the makings of an interesting series of posts.

Added: 1/16/2008

Marcelo Lopez Ruiz Explains ADO.NET Data Services' Built-In $filter Operators and Functions

Marcelo's Arithmetic and built-in functions for $filter include details about ADO.NET Data Services'

  • add (+)
  • sub (-)
  • div (/)
  • mul (*)
  • mod (%)

operators, as well as

  • String
  • DateTime
  • Math
  • Type

functions. The December 2007 "Using ADO.NET Data Services ('Project Astoria')" white paper for CTP1 also has descriptions and examples of these operators and functions.

Added: 1/16/2008

The Pfx Team Describes PLINQ's Debugger Display

The Debugger display of PLINQ queries of January 15, 2008 shows you how to get the most out of PLINQ's debugger with illustrations of the debugger display and debugger tips for simple parallel queries.

If you didn't follow early Parallel Programming with .NET posts about PLINQ, they started with little or no fanfare on November 29, 2007. Filter the posts on the PLINQ topic to see all seven related posts to date.

Added: 1/16/2008

David Hayden Delivers Illustrated ASP.NET Dynamic Data Images from Databases Demo

David's ASP.NET Dynamic Data - Displaying, Inserting and Editing Images in SQL Server Database post of January 15, 2008 says the following about Scott Hunter's Sample for Displaying Images from the Database using Dynamic Data post of January 14, 2008:

Scott's example shows off a number of really cool features and techniques:

  • Authoring Custom Dynamic Data Fields: DbImage.ascx, DbImage_Edit.ascx, and FileImage.ascx.
  • Using a Custom Attribute, ImageFormat, that contains format metadata.
  • Creating a Custom HttpHandler, ImageHandler.ashx, for displaying images from the database.
  • Generating a LINQ query on the fly, via LinqImageHelper, to get the image using LINQ To SQL.
  • Working with various Metadata and Reflection Classes in ASP.NET Dynamic Data and the .NET Framework.

His post also notes that "[t]here are numerous helper classes in a separate project that consist of the ImageFormat Attribute, ImageHandler HTTP Handler, LINQImageHelper for generating a LINQ To SQL query to get the image, etc."

See the "Scott Hunter Demos ASP.NET Dynamic Data Displaying Images from a Database" topic below for more details.

Added: 1/16/2008

Mike Taulty Continues His Reflections on Access Control for ADO.NET Data Services

His ADO.NET Data Services - More Sketchy Thoughts on Access Control post of January 15, 2008 proposes a ServiceAuthorizationManager implementation to "determine who it is that's calling my service and whether I allow them to do it based on a role."

Added: 1/16/2008

Michael Sync Demos a Databound Silverlight ListView

Michael's Consuming ADO.NET Data Service (Astoria) from Silverlight post of January 15, 2008 shows you how to how to access the ADO.NET Data Service (Astoria) from Silverlight with the  ADO.NET Data Services Silverlight Add-On. Michael says:

This article is just an introduction of how to use Astoria in Silverlight application. The sample that I used in this article is nearly as same as the sample which is mentioned in ASP.NET 3.5 Extensions Preview (Quick Start). But I used the Astoria Silverlight Addon and create the Silverlight listview that looks cool.

Thanks to Andy Conrad for the heads-up.

Added: 1/16/2008

Alex James Explains How to Perform Bulk Updates with LINQ to Entities: Part 2

From LINQ and Entity Framework Posts for 12/3/2007+:

Entity SQL v.1.0 doesn't include DML keywords, so you must use LINQ to Entities if you want to insert, update, or delete entities rather than operate on physical rows in the persistence store (a.k.a. database) directly. To update or delete entities, you must bring the entities into memory with the appropriate LINQ query before you modify or destroy them. Alex contends that this process "seems somewhat wasteful considering you could do this in T-SQL with one very simple update statement, thereby bypassing all the query, hydration and cross process shipping costs" when performing a batch update or deletion.

His previous post of December 7, 2007, Rolling your own SQL Update on-top of the Entity Framework - Part 1, showed about a third of the process for creating a generic Update<T>() function with the following signature:

public static int Update<T>(this IQueryable<T> queryable,  
       Expression<Func<T, T>> updator) where T : EntityObject 
    SqlDmlCommandFactory<T> factory = new SqlDmlCommandFactory<T>(queryable); 
    SqlCommand updateCmd = factory.GetUpdateCommand(updator); 
    return updateCmd.ExecuteNonQuery(); 

It's taken Alex more than a month to deliver Rolling your own SQL Update on top of the Entity Framework - Part 2 post of January 15, 2008, which describes creating the constructor for SqlDmlCommandFactory and its SqlDmlContext object, which also can provide the foundation for bulk deletion and insertion operations.

Alex's next post will cover the GetUpdateCommand() method. Hopefully, it won't take another month to deliver it.

Added: 1/16/2008

Frans Bouma Tackles All/Any, Cast, and Contains but Abandons Aggregate and Concat

Frans is nearing nearing completion of his LINQ to LLBLGen Pro implementation after having completed work on "all major parts of a SELECT query," including GroupJoin, GroupBy, and the aggregate functions.

In his Developing Linq to LLBLGen Pro, part 11 of January 15, 2008, he starts working on the remaining Standard Query Operators (SQOs) in alphabetical order.

Aggregate: Frans abandons his attempt to implement the VB-only Aggregate keyword because it won't compile in C# 3.0. Probably not a good idea, Frans. My LINQ to XML: Grouping and Aggregation Gotchas, Part II post of the same day has several examples of its use in conjunction with LINQ to XML documents created with VB's XML literal data type.

Update 1/20/2008: See Frans' comment of 1/18/2008 and my reply of 1/20/2008 regarding the Aggregate keyword.

All / Any: All() and Any() inside a Where clause will be supported. However, use without parameters as the final methods in the query won't be.

Cast: Enables compiling queries that otherwise would fail from missing types or type conflicts.

Concat: Frans abandons Concat because LLBLGen doesn't support UNION queries.

Contains: Contains proved difficult because it's schizophrenic: It can be "Queryable.Contains, IList.Contains, String.Contains, EntityCollection.Contains, etc." as Frans demonstrates with 12 sample queries. Contains is critical because it implements the IN () predicate, inter alia.

Frans clearly is on the home stretch and I'm looking forward to test-driving LINQ to LLBLGen Pro when it's done.

LINQ to XML: Grouping and Aggregation Gotchas, Part II

This is the promised sequel to my LINQ to XML: Grouping and Aggregation Gotchas, Part I post of January 7, 2008 which contains a single, large XML Infoset that demonstrates aggregation over business documents (such as sales orders or invoices) with rollups by customer, as well as grand totals for the document as a whole.

Rob Conery Describes Subsonic's Forthcoming MVC Implementation ("Makai")

Rob's SubSonic And MVC: Introducing Makai post of January 14, 2008 describes his vision and implementation of SubSonic's Scaffold control as a controller to provide a "complete editing suite with no tools to manage." Rob illustrates each current stock page template.

Subsonic "Makai" looks very similar to ASP.NET Dynamic Data and also resembles Blinq, but its editor includes the jQuery JavaScript library and a Membership controller is included. 

Scott Hunter Demos ASP.NET Dynamic Data Displaying Images from a Database

Scott's Sample for Displaying Images from the Database using Dynamic Data post of January 14, 2008 offers:

[A] sample project that shows some custom field template controls for Dynamic Data that allow images to be viewed/edited from a database. The sample also shows how you can view images that have their filenames in the database but the images on the physical disk. You can download the sample by clicking this download link: DOWNLOAD SAMPLE.

His post explains how to add the same functionality to your own Dynamic Data project.

LINQ to Google (GLinq) Surfaces on Source Forge

The latest third-party LINQ provider appears to be the Beta 1 version of Scott Brown's LINQ to Google (GLinq) that became available for download on December 3, 2007. According to Scott:

LINQ to Google allows developers to easily query Google's Data Sources using a strongly typed syntax. LINQ to Google shows an example of implementing IQueryable and IQueryProvider.

GLinq is an implimentation of the LINQ deferred execution model for querying Google's data sources. The initial release can be used to query the Google Base. Subsequent releases will target support for YouTube, Calendar, Email, etc. The ultimate goal of this project is to create a generic enough model to allow plugin providers for any REST API.

I will be adding posts in the near future on how the code works. For now you have to play with it yourself.

The source code carries a Common Development and Distribution License (CDDL)Common Development and Distribution License (CDDL).

Note: Don't confuse LINQ to Google with Luis Diego Fallas' LINQ to Google Desktop


Frans Bouma said...

"Frans abandons his attempt to implement the VB-only Aggregate keyword because it won't compile in C# 3.0. Probably not a good idea, Frans."
It's not a VB-only keyword, it's also a normal Queryable extension method. The thing is that it's not really translatable to SQL, if you use it in a Linq query where the Aggregate() call has to be ran on the DB. At least I couldn't find an example of that.

Your example is OK, but so is the MSDN example of Aggregate as an extension method of Queryable, however it's unclear how to even apply the method in a DB query.

DouglasH said...

Quick update or correction Luke Hoban was a C# program manager, he is now a F# program Manager.


Danny said...

"He incorporates a caveat that these extension methods operate on internal methods that are subject to change in CTP and RTM releases." Maybe the text in my blog post was unclear, but the caveat I meant to include was that in a future release we may be able to expose some internal methods which would make certain of these extension methods unnecessary. These extension methods should always work--it's just that there are faster ways that things could be done with a little more access to the internals.

- Danny

--rj said...

@Frans: By "VB-only Aggregate keyword" I meant that C# doesn't support a corresponding 'aggregate' verb in LINQ expression syntax. I agree that Aggregate is a Standard Query Operator and an IQueryable extension method.

@douglash: Corrected.

@danny: Clarified.