Tuesday, November 13, 2007

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

Note: This compendium is updated daily or more often, depending on availability of articles. Updated: November 14, 15, 16 and 17, 2007.

LINQ to Entities Variables May be More Variable Than Expected

Zlatko Michailov points out in his November 16, 2007 Behavior of Variables in LINQ to Entities Queries this behavior of local variables used as constants that's specific to LINQ to Entities:

When a local variable is used in a lambda expression it behaves as a parameter, i.e. it’s value is re-evaluated each time the query is executed; when a variable is used in a static expression, it’s value is picked at compile-time, and then it’s used as a constant.

Zlatko provides sample code that demonstrates this problem.

Entity Framework Beta 3 for VS 2008 RTM will provide a CompiledQuery.Compile((ObjectContext, variable1, variable2 ...) => (QueryExpression)) method that results in a query in which all local variables used as constants retain their compile-time values during multiple query executions.

Looks to me like the hard way to freeze variable values.

Added: 11/17/2007

Zlatko Michailov Explains How to Parse Entity Framework DataReaders

EntityDataReaders with DbDataRecords returned by the Entity Framework's Entity SQL require parsing to determine whether the DbDataRecord represents a CollectionType, EntityType, ComplexType (value object), RowType, or RefType.

Zlatko's November 16, 2007 How to Parse an EntityDataReader post describes a class and methods that "'visit' an arbitrary EntityDataReader to obtain the metadata" for determining the built-in type. ("Visit" is appropriate because Zlatko's code emulates a simple implementation of the visitor pattern.)

The complexity of this process might convince you to take advantage of the object layer and, potentially, LINQ to Entities with complex entity graphs.

Added: 11/16/2007 16:30 PST

Rick Strahl Does C# 3.0 Anonymous Types

Rick's Anonymous Types in C# 3.0 post of November 15, 2007 explains that the anonymous types you create on the fly with the var keyword are immutable and have a scope that's limited to the currently executed method.

Rick didn't mention that anonymous types will infer field names. Writing 
var anon = new {DateTime.Now, SomeField=12};
causes Intellisense to treat Now as the field name.

VB 9.0 also supports anonymous types using With syntax such as:
Dim anon = New With {DateTime.Now, .SomeField = 12}

Added: 11/16/2007

XML Team Publishes LINQ to XML RTM Documentation

The LINQ to XML page points to the following elements:

See Also: Concepts offers these links:

The team also published an interesting interview of Chris Lovett, who's an architect on the Data Programmability Tools team in SQL Server and works with Visual Studio's XML tools.

Added: 11/16/2007

Ben Hall Unit Tests LINQ to SQL with MbUnit and TypeMock

Ben Hall takes on unit testing LINQ to SQL, which RhinoMocks developer Oren Eini says is Awkward Testability for Linq for SQL.

Ben starts his How To Unit Test - Linq to SQL and Mocking post of November 15, 2007 by running tests on the DataContext with MbUnit from a ProductController, then adds a layer called LinqProductRepository between the the DataContext and ProductController, which he test with a live database connection.

He then sets up an interface and a MockRepository with RhinoMocks and runs tests against the ProductController with lists created by C# collection initializers.

Finally Ben tests TypeMock, which claims to be able to mock any object. The  Mocking Linq - Preview post of August 19, 2007 by Eli Lopian, TypeMock's developer, shows how TypeMock can Mock an entire LINQ to SQL statement and return a fake Customers collection.

As usual, Ben provides the source code for this three mocking projects.

Added: 11/15/2007 17:00 PST

Update: 11/16/2007: Eric Hauser's Building a Generic DAO / Repository with LINQ post of November 8, 2007 contains code for an IRepository interface and its unit-testable implementation for the DataContext. The Repository class is a bit more filled out than Ben's.

IBM Announces "Blue Cloud," a Large-Scale Astoria Clone

Not to be outdone by Microsoft's Astoria and its RESTful "data in the cloud" capabilities, IBM announced from Armonk (NY) and Shanghai (China) on November 15, 2007 its "Blue Cloud" technology "will allow corporate data centers to operate more like the Internet by enabling computing across a distributed, globally accessible fabric of resources, rather than on local machines or remote server farms."

The press release names a single customer, the Vietnamese Ministry of Science and Technology, as having "this week announced a cloud computing project with IBM." Reading below the fold shows that the project is a simple Internet portal.

The way I read the tea leaves, corporate data centers presently are allowed to do what Blue Cloud purports to enable. I also question whether science and technology in Vietnam has reached a scale that requires giving "citizens, communities and government organizations" access to this "rich content source" by "a distributed, globally accessible fabric of resources." A 1U Dell server with ASP.NET and Astoria would probably satisfy their needs.

According to the press release, "IBM’s first Blue Cloud offerings are expected to be available to customers in the spring of 2008." I doubt that IBM will need to hire extra security folks to quell the crowds surging to sign up when Blue Cloud RTMs.

Added: 11/15/2007

Architectural Point of View for LINQ-Enabled Data Access Video

Architectural evangelist Bill Zack delivers an 11:47 Architectural Point of View (arcPOV) presentation on LINQ to SQL, LINQ to DataSets, and LINQ to Entities. Here's the Channel9 description:

Bill Zack presents an Architect Point of View (arcPOV) presentation on selecting the right LINQ model for your application. Bill covers 3 different flavors of LINQ; LINQ to SQL, LINQ to Datasets, and LINQ to Entities. Bill discusses how these flavors of LINQ can be used to access data in relational databases. He then covers the differences and the trade-offs involved in choosing between them.

Added: 11/15/2007

NLinq for .NET 2.0 Beta 2 Released

NLinq is an open-source (MIT-licensed) implementation of LINQ to Objects that runs under VS 2005 and .NET 2.0. NLinq accepts LINQ queries in a literal string, so you can emulate dynamic queries. It's not clear who needs this functionality but if anyone does, the source code is available from CodePlex.

Added: 11/14/2007

LINQ to Lucene 2.1 Query Version Released

LINQ to Lucene, an open-source LINQ implementation for the open-source Lucene.Net 2.0 full-text search engine reached release 4, where it's capable of querying Lucene indexes, on November 13, 2007. The source code is based on Visual Studio 2008 Beta 2, licensed under the Microsoft Permissive License (Ms-PL, no version number) and contains Unit Tests for the MSTest implementation that's included with VS 2008 Professional Edition.

According to Locksley, the project's coordinator:

The goal of LINQ to Lucene is to provide developers with the ability to enjoy full-text searching using a fast-proven search-engine within the .Net managed CLR. The plan is to get the LINQ to Lucene code-base pushed up to CodePlex for additional developers to generate feed-back and improve and enhance.

Lucene.Net 2.0 is "a source code, class-per-class, API-per-API and algorithmatic port of the Java Lucene search engine to the C# and .NET platform utilizing Microsoft .NET Framework."

Note: Dare Obasanjo reported in his RSS Bandit v1.5.0.17 Released post of September 16, 2007 "a ton of stability issues related to our usage of Lucene for search engine for our full-text search" feature in the RSS Bandit reader. This is not to say that LINQ to Lucene is unstable but it's something to watch for.

Added: 11/14/2007

Ben Hall's Unit Tests for LINQ to XML RSS Reader

In his How To Unit Test - Linq To XML post of November 13, 2007, Ben Hall demonstrates unit testing of a simple LINQ to XML RSS Reader project with MbUnit, an open-source "generative test framework." Ben hall is an MBUnit project commit member.

Sample code is available for download.

Added: 11/14/2007

Optimize LINQ to SQL Performance

is the cover story for Visual Studio Magazine's November 2007 issue. Here's the deck:

Persisting business objects to SQL Server 2005 or later with object/relational mapping with LINQ to SQL can exact a performance penalty. Learn how to take maximum advantage of LINQ's strongly typed query capabilities without overtaxing the database server or losing front-end agility.

The story has tables of relative performance data for lazy versus eager loading of entities of varying sizes, with and without associated EntitySets. In all cases, DataSets beat LINQ to SQL times to load DataGridView controls via BindingSource components. The sample test harness code is available for downloading. 

You'll notice that 1105 Media's Redmond Media Group (RMG) has updated the magazine's site design, which looks 20 years younger than it's FTPOnline predecessor.

The article also is a cover story for RedmondMag.com (November 2007).

Added: 11/13/2007

Scott Guthrie's Model-View-Controller Framework Tutorial uses LINQ to SQL

Scott's detailed ASP.NET MVC Framework (Part 1) post of 11/13/2007 describes the ASP.NET team's forthcoming Model-View-Controller (MVC) Framework for Visual Studio 2008 and .NET 3.5. His sample project is the starting point for a simple e-commerce storefront site based on the Northwind Products table.

The post, which I estimate is at least 15 feet long, describes MVC architecture and its benefits, especially separation of concerns, and then continues with a detailed description of building the MVC Framework starting with the ASP.NET MVC Web Application template. The template adds Models, Views, and Controller folders to the main project and a ProjectNameTestProject folder for adding unit tests of the controller with MSTest. (VS 2008 Profession Edition includes the MSTest client).

Scott then derives a ProductsController class from the Framework's new System.Web.MVC.Controller class, which contains helper methods and added controller functionality. For this tutorial, Scott uses LINQ to SQL as the object/relational mapping (O/RM) tool and data access layer (DAL), although he says:

The ASP.NET MVC Framework enables you to use any data access pattern or framework you want in order to retrieve and manage your models.  If you want to use ADO.NET DataSets/DataReaders (or abstractions built on top of them) you can.  If you prefer to use an object relational mapper (ORM) like NHibernate, LLBLGen, WilsonORMapper, LINQ to SQL/LINQ to Entities you can absolutely use those as well.

It's interesting to note that Scott didn't mention the SubSonic DAL and persistence layer. Rob Conery, SubSonic's creator says SubSonic will be the "Convention-Driven Toolset for Microsoft’s New MVC Framework" for ASP.NET and Rob went to work for Microsoft yesterday.

Scott mentions that the new System.Web.IHttpRequest and System.Web.IHttpResponse interfaces will be easy to mock. It will be interesting to see how his team mocks LINQ to SQL's DataContext, especially for shopping cart updates.

Overall, it looks to me as if the ASP.NET team has come up with an excellent MVC framework starter edition, but it seems to me that it needs SubSonic's (or someone else's) scaffolding, migrations, and RDBMS-agnostic persistence layer to compete with Ruby on Rails.

Added: 11/13/2007

Update 11/14/2007: Mike Taulty's tempted to use the MVC Framework to create a RESTful Web service. In today's ScottGu on ASP.NET MVC Framework post, Mike says: 

What I see is a framework that might very nicely help me build a RESTful service. It does a nice job (and presumably a very flexible job) of routing URLs and query strings through to my classes and then we go get some data and invoke the view. In Scott's case, the view is an ASPX page producing HTML but I might equally want to invoke something to produce me some XML or JSON data.

I'm not sure whether the framework comes with a mechanism for routing through to ASMX or not but, either way, it looks like it would be easy to take this and make use of it in a service context.

Interesting idea.

Dot Net Rocks! Interviews Pablo Castro About Astoria

Show #289 pits the Technical Lead of the Entity Framework and Astoria against Carl Franklin and Richard Campbell in a 72-minute interview described as follows:

Pablo Castro from the SQL Server Product Group at Microsoft talks about his work with Astoria, an infrastructure for bringing web technologies and data sources together. Astoria uses the Entity Framework, which Pablo is also involved with.

The question of the week: Has Pablo had his picture taken since High School?

Added: 11/13/2007 17:00 PST

Surprise: Don Box Likes VB

Yes Steve, I've Tried (11/12/2007): Personally, my dream stack would be ubiquitous WS-Security/WS-Trust over HTTP GET and POST and tossing out WSDL in favor of doing direct XML programming against payloads from VB9 (or XQuery), but hey, I have unusual tastes.

Lambda in VB (11/11/2007): I'm starting to write more VB code lately. The XML support rocks.

More importantly, a huge amount of people who use the technology I work on see it through that lens, so I'd be an idiot not to walk in their shoes as much as possible.

Query syntax in VB (11/12/2007): "One other feature I'm liking in VB is that you can omit the trailing [S]elect clause in a query."

Thanks to Beth Massi for the heads-up in her Don Box on Visual Basic 9 post today.

Added: 11/13/2007

Bart De Smet Explains the Origins of LINQ to SharePoint

LINQ to SharePoint Implementation is a five-minute interview video from day 5 of Tech*Ed Developer 2007 (Barcelona) carries the following description:

Bart De Smet, Software Developer Engineer, explores LINQ (Language-Integrated Query) - which is one of the key features in .NET 3.5 Visual Studio 2008.  LINQ allows you to write queries inside your favourite language – C#3.0 or Visual Basic 9.0. Bart explains the reasons for a running a recent LINQ to SharePoint implementation project.

Another reason for Bart writing a LINQ to SharePoint implementation might be its current and future ubiquity. According to Joe Wilcox's report, Ballmer: Advertising Is the Future, on the Microsoft shareholders' meeting held today:

Gates spoke about future investments and made absolutely clear that SharePoint, with "over 100 million customers," would anchor Microsoft's server applications portfolio. He likened SharePoint to Office, asserting that SharePoint would similarly take a "central position as a key tool."

Bart previously joined Glen Gordon and Lynn Langit for a geekSpeak Webcast on Wednesday, October 31, 2007. Here's a link to the LINQ to SharePoint code.

Added: 11/13/2007, Updated: 11/13/2007 15:00 PST

Beth Massi Fixes Her LINQ to XML VS Tip of the Day Browser App

In this brief Merging XML using Visual Basic 9 post of November 13, 2007, Beth discovers that MSDN blogs' Rss.xml files only contain the last 15 posts, so she adapts the LINQ to XML code to merge the current content with that stored on disk to maintain a complete archive.

The original code is in her Visual Studio Tip of the Day Browser post of a week ago.

Added: 11/13/2007

Mike Taulty: LINQ's Not a Fit with SQL Server's SQLCLR

I'm not sure why one would want to write an SQLCLR stored proc, function, or whatever that uses LINQ to Something queries, but Mike says it can't be done in his LINQ and SQLCLR post of November 13, 2007.

Mike's attempt to load System.Core.dll in SQL Server failed SAFE verification and he knows of no plans to make LINQ run with SQL Server 2008's SQLCLR feature.

Added: 11/13/2007

Mike Taulty Draws a XAML Sine Wave with LINQ to XML

Mike was more successful in demonstrating that you can write XAML with LINQ to XML. 

The few lines of code in his November 13, 2007 Drawing a XAML graph from VB post draws a white sine wave on a black canvas.

I would have been more interested to see the code for the "thing" he did at Tech*Ed Developer in Barcelona last week.

Added: 11/13/2007

Updated 11/14/2007: As Mike notes in a comment, the source code for the "thing" (LINQ to XML demo) is available here. (Also, I corrected his misspelled name in these two posts. Sorry, Mike.)

Rick Strahl Explains Type Initializers in C# (in Detail)

Rick gave an attendee of his LINQ to SQL session at Dev Connections a bum steer on "what happens behind the scenes when you use type initializers in C# 3.0." 

To set the record straight, Rick posted Type Intializers in C# 3.0 on November 12, 2007 with examples of how the compiler translates a type initializer and what an anonymous type looks like in MSIL.

For more on his Dev Connections sessions, see his My DevConnection Fall '07 Session Slides and Sample Links post or the "Rick Strahl Posts Sample Code for His DevConnections LINQ Presentation" topic of my LINQ and Entity Framework Posts for 11/5/2007+.

Added: 11/13/2007

Charlie Calvert Describes Partial Methods (in less Detail)

LINQ to SQL's autogenerated code includes CRUD partial methods for each table so you can substitute stored procedures for the dynamic SQL generated from the LINQ query by the  expression tree.

In his Partial Methods post of November 11, 2007, Charlie provides an example of a partial method and lists the rules governing use of partial methods.

Added: 11/13/2007

1 comments:

Anonymous said...

Hi,

The code for my LINQ to XML session from TechEd is all posted here;

http://mtaulty.com/CommunityServer/blogs/mike_taultys_blog/archive/2007/11/10/9909.aspx

That was the *thing* I was talking about :-)

Thanks,

Mike.