Skip to main content

Linq, SQL, CAML

So I went to a presentation about Linq yesterday. The presentation was given by Scott Allen. Before I get into my impressions of the content, let me say a few things about the packaging. You know how they say first impressions count? Well Scott Allen either didn't know that or he just plain ignored it. I wasn't impressed with the dude's appearance. But, before you crush me with accusations of superficiality, I have to say 5 minutes into the talk I was already impressed with his content. His tone was just right and the approach he took to explaining Linq (and even the use of "var" which I, hitherto, thoroughly despised) was excellent.

That said, I am sold on Linq-to-everything but sql. Some of the language enhancements that were made to support Linq are things I have always wanted to do. For example, you have a list with several items and you want to find items that match a criteria. How do you do it? You write a loop that has a lot of what Scott called "ceremony" i.e. fluff:

List matches = new List();
foreach (MyType x in myTypes)
{
if (MatchesCriteria(x))
{
matches.Add(x);
}
}

With C# 3.0, you just write
var matches = from t in myTypes
where t.StartsWith("svchost")
select t;

Imagine being able to query an xml document, a web service and a collection of objects with the same syntax.

So we are all in agreement that Linq is great for querying objects. However, where it breaks down for me is when you start using Linq to query modern day databases. I have a few problems with LinqToSQL
  • Microsoft's implementation of LinqToSQL (and I suspect all other implementations) uses queries generated on the fly. Sure SQL Server will optimize and cache queries for you but how do test the generated queries? How do you know the queries being generated by Microsoft's LinqtoSQL provider generates sound queries when it has no prior knowledge of your database?
  • When you use LinqToSQL, you give up all the power of SQL for the simplicity of Linq's syntax. For trivial applications that might be a good trade off (after all who wants to bother with stored procedures, views and functions in a trivial app). But for non-trivial apps, I think you need the flexibility and power of SQL with a C#
  • Ignoring the efficiency issue, when you start mixing queries with C# code, isn't that just a recipe for maintenance nightmare? Imagine how much harder it'll be when renaming a table; adding new columns, dropping existing columns, modifying existings columns...if you've got C# code directly accessing those objects.
  • What's so wrong with plain old SQL? Attempts to replace SQL with a 4th generation language-based query language will almost always fail. Why? For one 4th generation languages are geared towards you (the developer) coming up with the algorithm to retrieve data. Whereas, SQL is all about tell the RDBMS what to get and it figures out the best way to get it. When you start writing queries in C#, C++, Java, XML...well to me it looks like you are trying to do too much. And you just might end up with CAML, which is a truly awful query language native to SharePoint.
I get why Microsoft might have felt the need to introduce LinqToSQL for completeness sake. After all, you can't talk query with talking SQL. But I fail to see how one could use LinqToSQL in non-trivial applications.

Comments

  1. While this tends to take away from a DBA's job, which I despise, some would argue Microsoft always has to have a response to J2EE. Hibernate, and JPAs have been a sore topic in some development circles for a while, but to others they are a God sent response to the need to do any true RDBMS designing leaving all the work to a 3rd party layer.

    Well Microsoft came up with Linq to respond to JPA, and while I can't feel your pain just yet since I haven't dealt with whatever nightmare of as schema Linq would cause. I can only imagine it is typical. Which would be under optimized queries and the other myriad of issues that go along with Model managing tools.

    ReplyDelete

Post a Comment

Popular posts from this blog

The Teenage Years Cometh

If you're lucky, a few days after your twins are born, the hospital just let's you walk out with them. In fact, they insist you take them with you. No training, no classes, no probation...they really just let you walk out with 2 humans. #fatherhood — Tundey A. (@realtundey) November 28, 2021 This was me 12 years ago: And now look at them. In a little over 6 months, they'll be teenagers!   Every time I look at them, I am reminded of Obama's quote on being a parent:  "One of my favorite sayings about having children is it's like having your heart walking around outside your body." — @POTUS — White House Archived (@ObamaWhiteHouse) October 21, 2015

Time Travel

 On November 25th , I ordered a couple of items from Bed, Bath & Beyond. It was late on Thanksgiving, I was up and decided to take a quick look at early Black Friday deals. The air fryer I had been eyeing for a few months was finally on sale. And I needed a comforter set. So why not kill 2 birds with one stone? I ordered both items for delivery (2-3 business days). Whatever, not an urgent need.  Friday afternoon, I received an email from BBB, my items have been split into 2 shipments. The first shipment is on the way. Here's the tracking number. Oh nice...I clicked on the tracking number and got this: I do not list in NJ Go ahead, click on it and take a look at the dates. According to that tracking info, the package was shipped on October 13th! Yeah, that's the reason for the title of this post. Somehow, BBB issued an already-used tracking number for my shipment. It gets worse. Both shipments were assigned different used tracking numbers. Both tracking numbers were for pack

How long should a blog post be?

A couple of times, people have accused me of writing really long blog posts. While I know that I have written some long ones , I think a blog ought to be more than a tweet or a facebook status update. This has been a problem for me because sometimes I want to write about a topic but I either can't quite write enough about it to justify making it a blog post or I just don't have the time to flesh out all my thoughts. So my blogger dashboard is littered with several unfinished blog posts that I started but didn't finished. For example, I had some really strong opinions on the Trayvon Martin case (back before Zimmerman was arrested). Mostly it was about how the case resonated with minority males (especially fathers) in a way in which non-minorities can't fathom. Not because they are insensitive but because they just can't do it. It's like expecting a 3rd world military dictator to understand the US Constitution. Oh yeah, where was I? Right, about writing really l