Saturday, March 22, 2008

LINQ and Entity Framework Posts for 3/17/2008+

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

Updated 3/22/2008 for confirmation that SSDS "will support the full EDM/EF/ADO.Net V3 model in due time," per Soumitra Sengupta. (That's the primary justification for including SSDS-related posts in this blog.)

Updated 3/23/2008 by adding Ruurd Boeke's Sneak preview: serializing objects with original values post of 5/17/2008 (missed), which adds information about the IEditableBusinessObject class, to the "Ruurd Boeke Plans a Entity Framework Variant with a Lightweight Client" topic.

David DeWinter Adds Dynamic Menu Commands to his LINQ to SQL Database Update Sync Project

Dave started a project in January 2008 to write a Visual Studio 2008 plug-in with capabilities to:

  1. Transform property names to be Pascal case
  2. Remove prefixes from the names of database objects
  3. Automate mapping the types of columns to various (developer-defined) enumerations

for LINQ to SQL projects.

The following posts supplement his source code in CodePlex's Entity Framework Contrib (EFContrib) section:

Added: 3/23/2008

Dave Robinson Posts Source Code for an Excel Add-in That Saves and Retrieves Worksheets to/from SSDS

Dave, who's the "new SSD blogger" I mentioned in the "New SQL Server Data Services Blogger and a Third-Party View of SSDS (et al.)" topic below, has written an Excel 2007 add-in that stored worksheet data to and retrieves it from a SQL Server Data Services container. His Using Excel with SSDS post of March 21, 2008 includes the SSDS-specific source code.

He promises a later post that demonstrates syncing data with the Microsoft Synchronization Framework.

Added: March 22, 2008

Beth Massi Plans Yet Another VB and XML Webcast on April 4, 2008

Beth's Live From RedBethmond: VB9 - Working with XML in Visual Basic post of March 21, 2008 gives the following description:

4/4/2008 9:00 AM Pacific Time (US & Canada) | Duration:60 Minutes
XML permeates every modern application today from XHTML, XAML, RSS, SOAP, Office Open XML just to name a few. Even your Visual Studio project files and configuration settings are XML files. The latest version of Visual Basic in Visual Studio 2008 supports a new language syntax aimed at making you much more productive when working with XML. In this Webcast we'll walk through language features like XML literals, embedded expressions and axis properties in order to create, query and transform XML with this powerful but easy to use syntax. Say goodbye to XSLT and hello to Visual Basic 9.

Added: March 22, 2008

The SQL Server Data Services Team Wants to Answer Your Questions about SSDS

Jeff Currier posted What questions do you have about SQL Server Data Services? on March 21, 2008. His post contains this question:

I've written several posts now around coding (in C# and in Java) about using SSDS.  So, I'm a bit curious if anyone out there has specific questions that either I (or others on team) might be able to answer.  This could be code related, design related, etc.  So, what questions do you have?

My questions were (paraphrased):

  1. How and when do you plan to support associations/relationships plus lazy/eager loading?
  2. How and when will applicants be notified of acceptance or rejection as beta invitees?
  3. What's the timeframe for adding the promised Atom/AtomPub format?

I probably should have omitted lazy loading from question #1.

Added: March 21, 2008

Update March 22, 2008 from Soumitra Sengupta in his Answering a few questions from Jeff's blog response to my questions:

1. What are your plans and timing for supporting associations/relationships plus lazy- and eager-loading?

Answer to the first question is that we will support the full EDM/EF/ADO.Net V3 model in due time.  It is a bit of work.  We do not have exact timing for it.  Once we do that we will have to address lazy and eager loading, right?

2. When will folks who signed up for the beta know if they've been accepted?

First batch should go out next week.  We will keep provisioning on a weekly basis from there on out.  By the way once the first batch gets provisioned, everyone else should be able to get to the technical documentation.

3. What's the schedule for an Atom/AtomPub implementation?

We are on this one.  Do not have the exact schedule to share at this time.

Good news on all but the timing of #3, but especially #1.

Soumitra's post also responds to questions from several other readers.

Ruurd Boeke Plans a Entity Framework Variant with a Lightweight Client

Ruurd's Progress on light objects on the client, full blown EF entities on the backend post of March 21, 2008 describes his work to date on an n-tier variation on the Entity Framework (EF) that eliminates the need to deploy the entity framework to the client tier. His objectives are:

What I want is one visual studio project: 'Domain' that holds business objects for me that I can use on both the client and the server. What I do not want, is to have to think about Entity Framework contexts on the client. Since I do not persist my objects on the client, I see no reason to actually create a context on the client, just to be able to do change tracking. Nor do I want to see any EF specific plumbing in the messages between that client and server: I lose interoperability if I do that. Also, if my client is Silverlight (for instance) I might not want to bring the EF assemblies over (if that's even possible?).

So, the end result should be an easy way to serialize and deserialize objects on the client and server, and a way to build up the object correctly on the server so it can be attached to the context again.

Updated 3/23/2008: The client objects will be implementations of Ruurd's IEditableBusinessObject interface, which he describes in his earlier Adding Edit support to businessobjects post of March 16, 2008 and Sneak preview: serializing objects with original values of March 17. The "Ruurd Boeke Adds Complex Types to His IPOCO Entity Framework Implementation" topic of LINQ and Entity Framework Posts for 3/10/2008+ contains links to his earlier Entity Framework posts.

The Sneak Preview poses describes his classes for serializing objects with original values:

EditableBusinessObject implements IEditableObject for you and also allows you to copy the currentvalues to a loadedvalues state.

CreateSerializeSurrogate generates a surrogate class that knows how to deal with loadedvalues and with circular references.

It appears to me that Ruurd's objectives are similar to those of IdeaBlade for their DevForce EF project, which is presently in beta. Ruurd's client is likely to be much lighter, which would make it more appropriate for deployment in a Silverlight project. However, IdeaBlade is hard at work on a Silverlight 2 client that uses a version of their existing DevForce framework (see below).

Added: March 21, 2008

IdeaBlade Posts 45-minute Screencast of their DevForce EF n-Tier Entity Framework Implemetation

You can play the Camtasia screencast from a link on IdeaBlade's main DevForce EF page. The product looks quite interesting to me. Both IdeaBlade and Ruurd Boeke avoid serializing and transporting the ObjectContext across process boundaries.

Thanks to Julie Lerman for the heads up on the video's availability.

My original post about DevForce EF was the "IdeaBlade Offers Downloadable Beta of DevForce Entity Framework" topic of LINQ and Entity Framework Posts for 3/10/2008+.

Added: March 21, 2008

Mike Taulty Writes an Essay On Disposing LINQ to SQL Data Contexts

Mike normally encloses his LINQ to SQL data access code in a Using block to ensure that it's disposed expeditiously, closes the SqlClient connection and returns it to the connection pool as quickly as possible. I do the same if I don't want to cache collections as a generic List in a WinForm or Silverlight project.

Unlike me, Mike decided to determine whether the Using block was necessary to close the connection immediately, rather than waiting for finalization.

Read Mike's LINQ to SQL and SqlConnection post of March 20, 2008 to learn what he discovered.

As a bonus, you can download the demo code from Mike's LINQ Talk at UK Visual Studio 2008 Launch. Mike's LINQ to SQL demo starts with:

using (DataContext ctx = new DataContext(
      "server=.;database=northwind;integrated security=sspi"))

Added: 3/20/2008

New SQL Server Data Services Blogger and a Third-Party View of SSDS (et al.)

"Life in the clouds..." is the name of David Robinson's new SSDS blog. Dave is a program manager for SSDS and plans to evangelize SSDS on The Code Trip at the University of Southern California on March 27, 2008 and the Microsoft Health & Life Sciences Developer and Solutions Conference 2008 on April 22-24. The following is from his first post:

What’s this blog all about you ask? Well it is going to be mostly focused on developing solutions on top of SSDS, but there will also be tidbits of other cool technology and an occasional rant or two.

Dave's most March 19 post points to Max Zilberman's SQL Server Data Services (SSDS) - a powerful proposition... post of March 18, 2008. Max, who works in Philadelphia as an Enterprise Software Architect for a Fortune 100 firm, says this about SSDS:

Many blogs have written about the nice bits announced at the MIX such as IE8, Silverlight Mobile, Silverlight 2.0 Beta, among others.   The session that was not given high level of attention, the SQL Data Services sent a really powerful message.

And goes on to explain why hosted data services make business sense.

Max also has nice things to say about ASP.NET Dynamic Data and ADO Entities - The new beginning.....

Added: 3/20/2008

An ASP.NET Model-View-Controller Tech Brief, Mock Frameworks and LINQ to Mock (Moq)

My ASP.NET MVC: Is the new MVC pattern right for you? TechBrief for Redmond Developer News' March 2008 issue points out that Scott Guthrie makes the following claim for ASP.NET's MVC feature:

.NET MVC ships with System.Web.IHttpRequest and IHttpResponse interfaces; this simplifies mocking the interfaces to deliver improved unit-test performance.

Scott Hanselman followed up his ASP.NET MVC Preview 2 session at MIX 08 with the ASP.NET MVC Session at Mix08, TDD and MvcMockHelpers post of March 7, 2008. This item includes MvcMockHelper source code for RhinoMocks, which Scott demonstrated in his session, Moq, a.k.a. LINQ to Mock, and TypeMock, a commercial framework. The helper code for all three frameworks is of similar complexity.

Scott had previously reviewed MOQ in his Moq: Linq, Lambdas and Predicates applied to Mock Objects of December 19, 2007, which starts with:

Kzu and friends have a new pet project called Moq, which may be the coolest derivative open source project name ever.

And ends with:

Slick. Moq is definitely a project to watch.

In between, Scott compares NMock, RhinoMock, TypeMock and Moq.

The "Daniel Cazzulino and Juan Wajnerman Create LINQ to Mock (Objects)" topic of LINQ and Entity Framework Posts for 12/17/2007+ has more details on the release of LINQ to Mock. (From the "Scott Hanselman Endorses LINQ to Mock (Moq)" topic of LINQ and Entity Framework Posts for 1/7/2008+.)

Kaz posted his Why do we need yet another NET mocking framework? essay on March 17, 2008. Justin Etheredge's MVC Testing Imbalance of March 19, includes sample Moq code for a simple MVC Update action. Here's his complaint about the amount of mocking code required for a relatively simple unit test:

And that doesn't even include any of my helper objects or methods. Anyone see a problem with this? I am going to have to put some serious effort into streamlining this. I know that there is quite a bit I can abstract out across different controllers and actions so we will see how this goes.

I have the same problem with the mocking frameworks I've investigated. Regardless of the complexity issue, Moq is gaining well-deserved attention.

Updated 3/23/2008: Justin's Simplified MVC Controller Testing with Moq of March 22, 2008 post simplifies the UserControllerUpdateTest() method by refactoring with helpers, such as CreateMockUserRepository() and CreateMockUserController() methods and a MockHttpContextContainer() class.

Added: 3/20/2008

Updated 3/21/2008: David Longnecker offers two posts on unit testing ASP.NET MVC projects: Exploring the ASP.NET MVC Preview 2 - First Glance and Exploring the ASP.NET MVC Preview 2 - Tests and Mocks, both of March 20 2008. The latter uses LINQ to Mock (Moq).

Eugenio Pace Continues His LitWareHR for SQL Server Data Services Saga

His LitwareHR on SSDS - Part II - The data access layer explains that the new client data model for SSDS implements an SSDS SOAP Client, ProxyFactory and Repository<T> stack, which replaces the previous SQL Server-based version's Data Access Application Block (DAAB) and MultiTenantDataAccess layer.

His entity-specific classes derive from the ExtensibleEntry base class, which provides the Id property required by SSDS, as well as Code and Location properties, which are common to all LitwareHR entities. His example contains a simple unit test to verify Insert() invocations by invoking an InsertKindGetByIdDelete() TestMethod.

The "Eugenio Pace Starts a Series on the LitwareHR Demo on SQL Server Data Services" topic of LINQ and Entity Framework Posts for 3/10/2008+ covers the first part of this series.

Added: 3/19/2008

Jimmy Bogard Finds XML-File Mapping Delivers Persistence Ignorance to LINQ to SQL Business Objects

Jimmy purchased a copy of LINQ in Action and discovered that you have four choices for mapping SQL Server tables to LINQ to SQL entities and associations:

  1. Visual Studio 2008's graphical O/R Designer
  2. The SqlMetal.exe command-line tool
  3. Custom class files you write to define entities with mapping by attributes you add to the classes and their properties
  4. A custom XML mapping file you create and process with SqlMetal.exe

He concludes that choice #4 provides persistence ignorance.

But his Mapping options in LINQ to SQL post of March 18, 2008 concludes:

For me, the final verdict is: Though LINQ to SQL gives me powerful strongly-typed querying abilities with LINQ, the only maintainable mapping option still pales in comparison to NHibernate.

Added: 3/19/2008

Jason Hunter Demos a Master/Detail Form with a SQL Server Data Services Back End

In his SSDS Patterns Part 1: Master/Detail in a Heterogeneous World post of March 18, 2008, Jason creates the classic Windows read-only master/detail form that displays a drop-down list of Customers, a DataGridView for Orders from the customer, and another DataGrid view of details (Lines) for the selected Order. He shows the minimal source code to populate the list and bind to the BindingSource controls for the DataGridViews to the objects created from the SQL Server Data Services (SSDS) entities with XML serialization code.

Jason, who's a dev lead in the SSDS group, promises his next post will demo "XML serialization to bridge the gap between SSDS and objects."

See the related "Jeff Currier Shows Source Code for SQL Server Data Services HttpRequest and HttpResponse Methods>" topic below.

Added: 3/18/2008

Danny Simmons: An Essay on Deleting Entity Framework Entities

Danny's Just delete it already! post of March 18, 2007 describes in detail why deleting a "fake" entity with associated entities in the data access layer requires more than just creating the entity and it's EntityKey, attaching it to the ObjectContext, and then invoking DeleteObject(). The incentive for faking an entity is saving a roundtrip to the data store to retrieve it.

You must have access to the associated entities so you can delete their AssociationSets in the process. This means you must execute an ObjectQuery to retrieve them. In this case, you might as well retrieve the entity from the data store, because it will retrieve the entity of interest and its associated objects (as the "relationship span") with a single query.

Deleting many:1 EntitySets for AssociationSets represented in the store by nullable foreign key values isn't a problem. In this case, the generated SQL statement just sets the foreign-key value to null. Deletions become more complicated when associations are required by specifying NOT NULL foreign key values in the database. Deleting 1:many EntityReferences isn't a problem whether they're required or not; cascading deletion simply removes the dependent entities associated with the deleted parent along with the relation.

More about issues with deleting many:1 EntityRefs for required AssociationSets will follow.

Added: 3/18/2008

Greg Duncan Insists on Stored Procedures for Production LINQ to SQL Queries

Greg is Development Manager/Primary Developer for KMPG's Electronic Data Discovery (EDD) product line. Greg describes EDD as

Taking "electronic data" (files, emails in PST/NSF's,etc) gathering metadata, deduping, converting to PDF/Tiff/JPG and exporting for loading into litigation support systems.

His LINQ to SQL NOLOCK'ing post of March 18, 2008 includes a quote from Scott Hanselman's Getting LINQ to SQL and LINQ to Entities to use NOLOCK post of March 17, 2007, which suggests replacing the NOLOCK hint for queries with TransactionScope and System.Transactions.IsolationLevel.ReadUncommitted. Either prevents long-running transactions from blocking reads but can lead to retrieving inconsistent data.

Greg says "I use the NOLOCK hint in my SQL 2000 world a bit" and goes on to say:

Now that said, I'm still not going to "endorse" LINQ to SQL for production usage yet at work, UNLESS our existing stored procedures (or new SP's) are used. And you know what? SP's work nicely with LINQ to SQL. I was able to drop some tables into the design surface, drag over some SP's, set some properties and I was LINQ'ing quite well. Select, inserts and updates working easily all the time through our SP's. There were quite a few "oh that's just cool" moments.

Why SP's? Because they have saved my bacon a number of times over the years (I've been coding against SQL Server since 4.21a...). Practical real world experience has re-enforced my belief in SP's as my primary DB level API. Not a religious thing, just an experience thing...

LINQ to SQL's graphic designer makes substituting stored procedures (sprocs) for parameterized dynamic T-SQL in CRUD operations easy, but doing the same in Entity Framework involves manually editing the SSDL and MSL files with Visual Studio 2007's (or another) XML Editor. The XML editor has schemas for *.edmx, *.csdl, and *.ssdl files to support IntelliSense, but not for *.msl files. Mapping and testing sprocs for associations/relationships is an agonizing process.

The Stored Procedure Support (Entity Framework) online help topic for ADO.NET Entity Framework Beta 3 provides simple examples. See the ADO.NET Team's "Using Stored Procedures for Change Processing in the ADO.NET Entity Framework" post of March 8, 2007 and "How to Map Stored Procedures using the ADO.NET Entity Framework" post of September 14, 2007 by Mike Dodaro for examples with more detailed explanations.

If you're working with Entity Framework and your DBA insists on stored procedures for updates, you might try getting an exemption by explaining the mapping issues involved.

Added: 3/18/2008

Jim Wooley Explains LINQ Joins on Composite Keys

In Joining composite keys with LINQ of 3/17/2008, Jim observes that

[T]he Join extension method does not support the ability to provide the InnerKeySelector/OuterKeySelector as a series of expressions.

But the solution isn't obvious. Here's his VB expression for a join on City + State composite key:

Dim query1 = _
    From l In dc.LendingLimits _
    Join p In dc.Properties _
    On New With {l.County, l.State} Equals _
          New With {p.County, p.State} _
    Where p.PropertyId = SearchValue And _
          l.EffectiveDate = TargetDate _
    Select l.Amount


Added: 3/18/2008

Rob Conery Waxes Enthusiastic over Lambda Functions

Rob appears to have fallen in love with Lambdas. As he says in his My Personal Lambda Crusade post of March 17, 2008:

I love lambdas and I’m pretty sure they solve every problem imaginable. Why just the other day my daughter painted her face with leopard spots, using a Sharpie pen, and I said to myself "Rob(x=>x.Beer("immediately"))" and all of a sudden the world was a brighter place…

His post includes a link to "a very good read." It's Howard Dierking's Lambda, Lambda, Lambda! post of January 18, 2008, which has one of the best explanations of an expression tree I've ever come across. Howard's the Editor-in-Chief of (and tech lead for) MSDN Magazine.

Added: 3/18/2008

Jeff Currier Shows Source Code for SQL Server Data Services HttpRequest and HttpResponse Methods

Jeff's Some SQL Server Data Services Coding Examples post of March 16, 2008 include C# samples for making RESTful HttpRequest.CreateRequest calls that accept URI, HttpMethod, data, and contentType parameters, as well as and reading responses with a custom ReadResponse(HttpWebResponse) method that returns the data as a string.

A more user-friendly version requests and returns a list of an Authority's Containers that Jeff imports into a LINQ to XML XDocument and binds it to an ASP.NET dropdown list. He then constructs a query of the selected Container and binds a list of entities with Kind = Customers to a GridView control.

Jeff promises to post a similar demo for SOAP request and response messages as well as for Java.

(Jeff's originally from the WinFS group. His first blog post is a recommendation to catch the "DAT416  Building on WinFS: Developing a Great WinFS App from Scratch" session from Tech*Ed 2006!)

Ruurd Boeke Describes How to Build an Entity Data Model Client without an Entity Framework Reference

His Adding Edit support to businessobjects post of March 16, 2008 says:

I'm building toward a set of tools that will enable me to use EntityFramework on the server and use the same businessobjects on the client-side, without having to reference EntityFramework at all.

This first post implements an IEditableBusinessObject, which inherits from IEditableObject. Ruurd uses PostSharp's OnFieldAccessAspect to intercept properties and add their values to the statebag. His "next step will be to get this [object] to serialize nicely, with original values and such."

For more background on the EDM server side, see the "Ruurd Boeke Adds Complex Types to His IPOCO Entity Framework Implementation" topic of LINQ and Entity Framework Posts for 3/10/2008+.