Sunday, November 11, 2007

SubSonic will be the "Convention-Driven Toolset for Microsoft’s New MVC Framework" for ASP.NET

Not the Entity Framework, not LINQ to SQL, but (gasp!!!) open-source SubSonic will deliver the default data abstraction and persistence layer and scaffolding for the forthcoming Model-View-Controller (MVC) add-on to ASP.NET 3.5. SubSonic's creator, Rob Conery, will start work at Microsoft next Monday, November 12, 2007, to assist with integration chores and evangelize ASP.NET developers. I missed Rob's October 26, 2007 Microsoft, SubSonic, and Me post, which announced that he was going to work for Shawn Burke, one of Scott Guthrie's direct reports in the Developer Division.

Updated 11/13/2007 and 11/18/2007: See below.

Scott announced the MVC add-on in his ASP.NET MVC Framework post of October 14, 2007. The MVC pattern for connecting the UI (View) to  business logic (Controller) and data (Model) originated in Smalltalk projects more than 30 years ago. Scott says the following about the advantages of the MVC pattern:

One of the benefits of using a MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application.  Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated. [Link added.]

The MVC pattern can also help enable red/green test driven development (TDD) - where you implement automated unit tests, which define and verify the requirements of new code, first before you actually write the code itself.

However Scott's post didn't mention how ASP.NET's MVC Framework will implement the persistence layer for the Model's business objects. In a comment, Scott says only that:

The ASP.NET MVC framework will work with any data provider.  LINQ to SQL and LINQ to Entities are both definitely fully supported (my sample at the conference was actually done with LINQ to SQL).

Update 11/13/2007: Scott Guthrie provides a very detailed demonstration and tutorial for the upcoming MVC Framework in his ASP.NET MVC Framework (Part 1) post of 11/13/2007 (3:45 AM!) LINQ to SQL provides the data access and persistence layers. I estimate the length of Scott's post to be at least 15 feet.

The problem with LINQ to SQL in a Test-Driven Development (TDD) environment is the difficulty of mocking persistent objects with in-memory objects. That's because there's no memory-based DataContext object you can easily substitute for the DataContext that's bound to the SQL Server 200x instance. The "mini connectionless DataContext" that was promised but not delivered for multi-tier use of LINQ to SQL might have served this purpose. (See the "Emulating the Phantom Mini Connectionless DataContext" section of my LINQ to SQL Has No "Out-of-the-Box Multi-Tier Story!" from October 18.)

Note: Ian Cooper says:

LINQ to SQL is usable with a TDD/DDD approach to development. Indeed the ability to swap between LINQ to Objects and LINQ to SQL promises to make much more of the code easily testable via unit tests than before. [Emphasis added.]

The preceding is quoted from Ian Cooper Takes on DDD, TDD and PI with LINQ to SQL of June 11, 2007. Ian's Being Ignorant with LINQ to SQL post of June 10, 2007, provides sample code for swapping tests of LINQ entity retrieval queries, but data retrieval is only a small part of the DataContext's feature set.

Update 11/13/2007: Frans Bouma added a comment about difficulties with mocking LINQ to SQL, particulary when you use GroupJoin.

An additional impediment with LINQ to SQL as an MVC data provider is that it's connected at the hip to SQL Server 200x and Compact Edition (SSCE), and Microsoft has no plans to extend it's reach to competitive RDBMSs. Oracle and IBM are working on EntityClient data providers for the Entity Data Model (EDM), but no one knows when Entity Framework (EF) or the alternative providers will be ready for prime time. It's equally uncertain whether mocking the EDM's heavier weight ObjectContext will be practical.

SubSonic, Scaffolding, LINQ, Migrations and the MVC Pattern

SubSonic is an open-source data abstraction layer (DAL), object/relational-mapping (O/RM) tool, and scaffolding environment that's provided under a commerce-friendly Mozilla Public License (MPL) Version 1.1. Its Scaffold server control requires only a single instruction, such as

<cc1:Scaffold ID="Customers1" runat="server" TableName="Customers"></cc1:Scaffold> 

to autogenerate fully operational GridView and details view pages for a site. Jon Galloway's October 30, 2006 Microsoft should ship SubSonic (formerly called ActionPack) with Atlas post offers a detailed review of SubSonic's capabilities by an expert third party.

SubSonic's default scaffolding closely resembles that of the ASP.NET Team's now-abandoned Blinq project and it's DAL shares "convention over configuration" terminology with Ruby on Rails (RoR) and Project Jasper, which appeared to have fallen into a black hole but now just seems to be in limbo.

Rob's SubSonic: Migrate Me post of October 3, 2007 says SubSonic will incorporate migrations (also a RoR feature) "as part of our next major release, which will also include our REST Handler and our new query tool - so it will be a few months until we can get this all tightened up." According to Rob, his "job will be to build out SubSonic as the 'sweetness' on top of ASP.NET and (more notably) the new MVC platform. SubSonic will be the convention-driven toolset for Microsoft’s new MVC framework." Presumably, this means Rob will be working full-time on the "next major release."

The new query tool presumably will substitute strongly typed, compiler-checked constructs, such as the following from Jon Galloway's post:

Query qry = new Query(Product.Table);
qry.Top = "10";
qry.OrderBy = OrderBy.Desc(Product.Columns.UnitPrice);

for the string column names in the current version. SubSonic's new query syntax appears sufficiently similar to LINQ as to make practical substitution of LINQ to SQL's expression tree for SubSonic's query processor. Limiting the persistence database to SQL Server won't please the vast majority of developers.

In his LINQ: How To Use LINQ To Query Just About Anything post of July 2, 2007, Rob says:

Recently I have been asked by a client to use LINQ for some interesting stuff (which I’ll blog about later), and one of the things I needed to do was to use it to query a REST service. Step 1 was to query Amazon (since there is an existing sample out there on how to do this), and I just wrapped up that part of it and thought I would share with you how I dove nose-first into refactoring the existing sample to work with .NET 3.5, and also some of the neat stuff I learned along the way.

Since Rob has been working for Shawn Burke's ASP.NET team "(on contract) for the past 8 months or so," it's probably a safe assumption that "a client" in the preceding quote is Microsoft. Although Rob says in the same post, "I have no plans with SubSonic and LINQ," it's reasonable to expect a definitive "LINQ to SubSonic" implementation in in the "next major release."

SubSonic shares the ActiveRecord model with RoR. Migrations are a shortened RoR term for ActiveRecordMigrations, which manage versioning of the project's underlying database schema. SubSonic's "next major release" undoubtedly will add the SubSonic MVC App template to My Templates, as shown in the video described in Rob's SubSonic: MVC Preview Video post of November 5, 2007. The eight-minute Silverlight segment includes previews of:

  • Migrations
  • MVC scaffold generation
  • Core site design (upgrades)

but no narrative on the audio track.

Rob's also tasked with "creating webcasts and writing up tutorials for MVC/ASP/SubSonic." I enjoyed the music behind the preview video, but conventional narration is probably the better approach as new SubSonic's RTM approaches.

SubSonic's Continuing Open-Source Status

Both Rob and Shawn emphasize that SubSonic will remain an open-source project under the MPL 1.1 license. Rob says:

I think it’s worth pointing out that SubSonic hasn’t been “bought”. Some might smell a conspiracy here, but I’ll leave that to the X-Files and Cap’n Crunch crowd to drum up all the evil reasons why the mothership has “beamed me up”. SubSonic will remain under the same MPL 1.1 license it always has, and will remain as completely Open Source as it always has - nothing will change at all. I’m just getting paid, essentially, to work on it :).

and Shawn goes on with:

To be clear Microsoft did NOT buy, annex, co-opt, or otherwise take over SubSonic.  SubSonic is still what it is, and a big part of Rob's job is going to be to make sure it continues to grow and provide a great platform for building data-centric apps.  Rob still has complete authority over SubSonic, it's his baby.  He'll also be pitching in to help find ways to deliver value for ASP.NET in general.

Jon Galloway explains:

[SubSonic]'s licensed under the Mozilla Public License, which is pretty simple - you can use, redistribute, and even sell software using MPL'd code, but if you modify the MPL'd code you have to publish your changes.

So it seems practical to me for the ASP.NET team to package SubSonic's "next major version" with the ASP.NET MVC Framework, as Jon recommended more than a year ago.

P.S. Rob won't be leaving Kaua'i, as he mentions in a comment. "[B]ut it’s only by virtue of the fact that there’s a dev center in Honolulu, that I’ll be flying to Redmond often, and that I wouldn’t mention it on my blog … oops."

Update 11/18/2007: Hamilton Verissimo, aka hammett, founder of the Castle Project wrote a MS MVC and the MVC Team post on November 18, 2007 about his trip to Redmond to review progress on the ASP.NET MVC Framework. Hammett concluded:

Right now, though, for folks with lot of expectations, the CTP might be a turn off. That’s because what you’re going to see is a very small framework, with a lot to be done to be useful. As they say, the CTP is mainly for feedback. I’m sure Milestone 2 would be something great, though.

And what happens to MonoRail? - you may ask. I’m not sure. I was really adamant that they should try to support all things that MonoRail support, but I’m not sure they are going for that. MonoRail 2.0 is pending their implementation. If the final MS MVC rocks, and provides so many things, I’d vote for forget the MR 2. If the final MS MVC has clearly something lacking, MR 2 could reuse the infrastructure and provide some nice extensions.



Frans Bouma said...

Indeed the ability to swap between LINQ to Objects and LINQ to SQL promises to make much more of the code easily testable via unit tests than before.
That's not true. Linq for example relies on GroupJoin. In memory, a groupjoin is predictable: you can determine what the outcome is. However, in a Linq to Sql query, a groupjoin has no real 1:1 equivalent in relational algebra: this means that the o/r mapper has to make a 'mapping' between the GroupJoin behavior and the relational algebra at hand, and unfortunately, there's no real 1:1 mapping, so it depends on the o/r mapper what the outcome is. This also means that it can be that a query for Linq to Objects using GroupJoin will give different results than the one executed on the DB.

One of the main issues is that GroupJoin's reference manual page suggests it's a left join, while the how-to pages suggest it's an equi-join (inner join) 'but not exactly'.

One of the main reasons for this is that the GroupJoin's behavior is actually done internally by the relational database system.

For example, you can create a query with a GroupJoin which makes Linq to Sql create a WHERE 0=1 clause. ;)
About SubSonic: I'm not sure how far MS will go in having SubSonic a fully supported toolset, as IMHO writing a tool like SubSonic requires a truckload of time, and it's my understanding that Rob's job is mostly about something else than writing full time SubSonic code.