Why you should never build another website or app without LINQ

So you have this great idea for a new web application. You are all excited because you know this thing will be the best thing since Al Gore invented the internet. You know from experience that the best way to get started is at the bottom and work your way up…so you start with your Database…then you get to your stored procs…data classes…function libraries…zzzzz.

This is a part of just about every web app that is more than just a brochure. You need a good architecture, but building one is keeping you away from building the fun stuff that is what got you all excited in the first place. Its frustrating, but necessary because not having an architecture is a recipe for disaster.

Whats a saavy web developer to do? Use LINQ.

What is LINQ? From the 30,000 foot view, LINQ (or Language INtegrated Query) is a new way to model and interact with data. It makes it 1000 times easier and gets you doing what you actually want to do without have to hassle with the tedium of getting your architecture straight.

The goal of this article is not to explain to you HOW to use LINQ (I have links to some great tutotials at the end), but rather WHY you should be using it on every web project you build from now on (at least until something even cooler comes along).

So what does LINQ do for you? Lets start with an example.

Several weeks ago I wrote an article about the architecture I built for novella (my CMS application). Novella was built using a pretty standard architecture for any semi-complex web application. Its not necessarily sexy, but it is highly functional and easy to use. Let compare that architecture to what you would need in a LINQ world.

The novella architecture is basically 5 parts:

  • UI
  • Command
  • Framework
  • Data Classes
  • Stored Procedures

Now lets look at what the architecture would look like using LINQ.

  • UI
  • Command

See the difference? No data classes…no framework…and no annoying stored procs.

Basically what I am left with is the stuff that makes novella work and nothing more.

Here is a more detailed breakdown. Say I wanted to go get some data from the novella database. Here are the steps I would need to do.

  • Define the data I want (the UI)
  • Turn the user supplied data into a data class
  • Call a function that would get the data
  • Function calls the framework
  • Framework tells SQL what proc I want to run
  • SQL returns the data and the Framework transforms it into a data class
  • Command passes it back to the UI

Whew…the worst of it is, I had to write every single bit of that code.

Using LINQ I do this.

  • Define the data I want (the UI)
  • Call a Command function
  • Command calls into database using LINQ
  • Return the data

Basically I went from 7 to 4 steps. Doesn’t sound like much, but those 3 steps that are cut out are easily the most tedious and boring to write.

A LINQ query takes care of everything and makes it so simple to do whatever you want with the data. Here is an example.

I have a table called Customers…I would like to get all the customers that are older than 35 and order by name. Here is my query (in c#):

DBContext db = new DBContext();
var list = from tbl in db.Customers
             where tbl.Age > 35
             orderby tbl.Name ascending
             select tbl;

Now I will leave the specifics of what I just did there to the tutorials, but suffice it to say I have no procs, no data classes…just my data how I want it. In .NET 2.0 if I wanted to change my query to get all the customers that are born in Idaho, then I would need to go build a new proc, a new data access method…zzzzzzz. Here, I simply change my where and I am good to go.

Seriously, if you are a .NET developer, then you should stop what you are doing and go try this stuff out. If you are a PHP or Ruby developer, then you should rewrite all your code to use this…just kidding, but it is worth a look. It works for both online and offline apps so no excuses!!

Now the more advanced users out there are saying to themselves…”this is old news…this thing is just an ORM”…and you would be exactly right. The difference here is, its all baked into the cake (mmm cake). You get Visual Studio support, intellisense, etc. and you don’t have to worry about any expensive third party tools. If you have used ORM tools before, then this isn’t nearly exciting except for the fact that it is really easy to get started.

I have really just scratched the surface of the benefits of LINQ….parent/child relationships, XML querying, joins…its all made easier with this stuff. I honestly haven’t been more excited about a tool since I first discovered code behinds and breakpoints for my webpages and maybe Intellisense…

Onto the tutorials…

Scott Guthrie has a 9 part tutorial which gives a great overview of what can be done. It was written back when 3.5 was in beta so there are a few differences (i.e. to add a record you have to use InsertOnSubmit() not Add), but it is still chock full of great info.

Deitel has several links of LINQ tutorials

Eric White (a member on the LINQ team) has a mind boggling 17 part tutorial which gets more in depth as to what is going on.

ASP Alliance has a nice series of articles on building a simple blogging tool with LINQ.

I hope I have gotten you at least a little curious with all my raving. If not then you have to ask yourself…what is wrong with you? :P.

Please feel free to email me if you have any questions. I have only been working with it for a few weeks, but I will do my best to help.

Enjoy!

9 Comments so far »

  1. Donald Organ said,

    Wrote on March 10, 2008 @ 7:20 pm

    This approach has its mind in the right place how ever the amount of keystokes to get data is the same as actually typing out a query.

    If you want to take a framework to the next level check out http://www.andromeda-project.org This “un-framework” allows you to put all you business rules into the database and let the database worry about, as well a create a zero code administration area for all of your data.

    The andromeda database framework takes frameworks to the next level, and even includes the ability to use smarty templates to give you “Easy Reporting”

  2. onur Gumus said,

    Wrote on March 11, 2008 @ 4:24 am

    that’s not linq. That’s linq to sql. And that really isn’t very nice for me. I prefere linq to nhibernate

  3. David Baxter said,

    Wrote on March 11, 2008 @ 9:13 am

    @Donald: As someone who has done both the traditional approach and now LINQ, I can tell you that the amount of keystrokes is A LOT fewer with LINQ. Its not accurate to compare just typing in the SQL queries. If you do traditional queries, then you need a framework that supports them. Takes a lot of typing to get that setup. With LINQ, you don’t need the framework cause it is already there.

    I will check out the framework you mention though.

    @Onur: You are right, I am describing LINQ to SQL. There is also LINQ to XML and another one that I can’t remember off the top of my head. The fact that there are more ways to use LINQ just adds to the reasons to start using it.

  4. Martin Short said,

    Wrote on March 11, 2008 @ 9:14 am

    I’ve been drooling over LINQ for a while, sadly working in a Java shop where framework overload and closure discussions outshine everything else. *sigh*

  5. David Baxter said,

    Wrote on March 11, 2008 @ 9:33 am

    I completely understand Martin. I worked with Java for two years just about the time .NET was coming out. It can be frustrating when you see new technology coming out and you can’t do a thing about it…

    Imagine the poor souls still working with Powerbuilder :P

  6. Charlie said,

    Wrote on March 11, 2008 @ 10:44 am

    Nice article :-)

    Did you check the speed of LINQ? I have several major project to build now, and per several tests I did LINQ is slower than the traditional ADO methods.

    There is no doubt that starting a project with LINQ and managing it with LINQ is much easier. The question is about speed at runtime.

    10Q

  7. David Baxter said,

    Wrote on March 11, 2008 @ 10:52 am

    Hey Charlie, glad you like the article. I, personally, have not done any formal speed tests, but from what I have seen, they seem about the same.

    For most projects the queries aren’t gnarly enough that speed would be an issue. However, from the articles I have read, and the presentations I have seen (at last year’s TechEd), the queries that LINQ builds are supposedly highly efficient.

    If speed is a major issue for part of your app (i.e. the heavy queries), then maybe a traditional route would be better for those pieces. For the more mundane stuff (i.e. adds, updates, deletes, etc), LINQ would be a huge time saver.

    David

  8. Bill Craun said,

    Wrote on March 16, 2008 @ 11:16 am

    First link to the Scott Guthrie series is broken. Below is the link to the first post in the series which has pointers to the others in his series. Also, a PDF book format of the series has been created and can be downloaded from IT-Box.

    The links respectively:

    http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
    http://it-box.blogturk.net/2007/10/19/linq-to-sql-tutorial-series-by-scott-guthrie-pdf-book-format/

    -bill

  9. David Baxter said,

    Wrote on March 17, 2008 @ 8:09 am

    Thanks for the heads up and the links Bill. I fixed the typo in my link so it should be good now.

    David

Comment RSS · TrackBack URI

Leave a Comment

Name: (Required)

E-mail: (Required)

Website:

Comment: