Sunday, August 15, 2010

Microsoft.Data is so wrong!

Their was some rumor in the last days about microsoft addressing a developer type called "Mort" with a new Data Layer called Microsoft.Data. Mort is described by Nikhil Kothari's blog post as

Mort, the opportunistic developer, likes to create quick-working solutions for immediate problems and focuses on productivity and learn as needed. Elvis, the pragmatic programmer, likes to create long-lasting solutions addressing the problem domain, and learn while working on the solution. Einstein, the paranoid programmer, likes to create the most efficient solution to a given problem, and typically learn in advance before working on the solution. In a way, these personas have helped guide the design of features during the Whidbey product cycle.


While their were voices that addressing the Mort developer type is just plain wrong I support the idea to address Mort's way to develop on the .NET platform but I can't support the how microsoft tries to achieve this.

The how is just plain wrong! If you read the original announcement of Microsoft.Data David Fowler outlines the "pro" of Microsoft.Data by comparing these two code snippets:

using (var connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Northwind.mdf;Initial Catalog=|DataDirectory|\Northwind.mdf;Integrated Security=True;User Instance=True")) {
  using (var command = new SqlCommand("select * from products where UnitsInStock < 20", connection)) {
    connection.Open();
    using (SqlDataReader reader = command.ExecuteReader()) {
      while (reader.Read()) {
        Response.Write(reader["ProductName"] + " " + reader["UnitsInStock"]);
      }
    }
  }
}
Compared to the Microsoft.Data approach:
using (var db = Database.OpenFile("Northwind")) {
  foreach (var product in db.Query("select * from products where UnitsInStock < @0", 20)) {
    Response.Write(product.ProductName + " " + product.UnitsInStock);
  }
}

The "mental approach" of both code snippets does not fit Mort's needs! The assumption of both code snippets is: "If we want to access or store data we need to do that using SQL"! This assumption is just plain wrong!

Why bother Mort with SQL? Why should Mort learn how to prevent SQL Injections? Why should Mort be concerned about connection strings? Transactions? Why should be Mort concerned about Data modelling? And again - Why should Mort need to learn yet another language (SQL) to do just the simplest data access?

It's not the case that SQL is the best approach to store and access data objects! If we take a look at mongoDB with a mongoDB driver like NoRM or RavenDB we do not need SQL, we do not need to worry how to persist our object in an SQL server!

What I want as Mort is code like that (stolen from RavenDB tutorials)
using (var session = store.OpenSession())
{
    var order = session.Load<Order>("orders/1");
    Console.WriteLine("Customer: {0}", order.Customer);
    foreach (var orderLine in order.OrderLines)
    {
        Console.WriteLine("Product: {0} x {1}", orderLine.ProductId, orderLine.Quantity);
    }
    session.SaveChanges();
}

Dear Microsoft, don't try to hide the complexity dealing with a relational database and get the mental shift towards some (NoSQL) alternatives! And Mort will follow!

4 comments:

  1. With RavenDB (as with all document-databases) the hard part is to design the data model (decide what's a document and what's not). And Mort will always make wrong design decisions because he doesn't know the consequences of those decisions due to the lack of knowledge/learning. So let's just accept that Mort is a wrong persona to develop software of any complexity above zero.

    ReplyDelete
  2. Good point Sergey! It's no trivial task to design a document model in document databases. But my post is not about giving Mort some tools and frameworks to make him a super hero developer but to make Mort's life easier. And Mort's life will be easier without SQL/relational databases.

    I think we all started as Mort - for example I started programming with Turbo Pascal, my code was a mess, I had no know how of anything but I picked Turbo Pascal as programming language because it was easy to understand for me...

    So why not give Mort some nice language and framework and encourage him to get a better (or more precise a real) programmer.

    ReplyDelete
  3. I think the description they came up with for Mort is wrong. Basically Mort is a non-programmer techie. He knows a very little bit about development and a very little bit about SQL. Anything more than Programming 101 is over his head. Any talk of patterns, frameworks or best practices is way too much. Anything he develops will be cursed in future by 'real' developers who have to come in and change it.

    ReplyDelete
  4. Well, first of i liked to read this article and what should i say ....... you soooo right :-)

    ReplyDelete