LinqDataSource vs ObjectDataSource vs SqlDataSource

Published 24 September 07 06:43 PM | despos

With ASP.NET 3.5, a new data source control joins the company: the LinqDataSource control. How does it position itself in the grid of data source controls?
The difference between SqlDataSource and ObjectDataSource is fairly clear. The first leads you to add SQL code directly in the markup of ASP.NET pages, which is not certainly a practice to recommend even though the resulting page will still work great. ObjectDataSource is right the opposite. It pushes the use of objects and makes the binding between  data-bound controls and data sources happen through made-to-measure objects. In most cases, the objects bound to ObjectDataSource won’t be just the top layer objects of your business tier. But ObjectDataSource allows you to preserve—and, to a good extent, it mandates you to have—a well designed business tier.
What about LinqDataSource, instead? To me, it falls somewhere in between the other two. 

I should have read somewhere that one of the advantages of LinqDataSource over ObjectDataSource is that it doesn't require you code manually the source of the classes in the object model. Uhmmmm. Tell me, what's your priority being a developer/architect?

If you're simply trying to save yourself a few hours of development, aiming at a longer weekend home with the family, then by all means pick up the LinqDataSource control. If, instead, your priority is the overall design of the application, then just forget about LinqDataSource; just as much as you should have forgotten about SqlDataSource.

To enable Linq-to-SQL (and subsequently LinqDataSource), you necessitate to create a data model—better if through the O/R designer embedded in Visual Studio 2008. This data model is rooted in a data context class and contains initially only data container classes, with data properties but no behavior. The idea behind LinqDataSource is that you use the auto-generated object model to manage data and provide any required behavior through direct injections of Linq-to-SQL commands. As you can see, the pattern behind LinqDataSource and SqlDataSource is nearly the same. The only difference is the language you use: Linq-to-SQL in one case, raw SQL in the other. Another key difference that must be noted between SqlDataSource and LinqDataSource is that SqlDataSource can accommodate any relational data source, whereas LinqDataSource is limited to SQL Server. 

The purposes of LinqDataSource and ObjectDataSource are clearly different. ObjectDataSource enables you to take advantage of RAD data binding on top of your existing middle tier. LinqDataSource and related tools give you instead a quick way to build an extremely thin and to some extent, anemic, object model that, in addition, can hardly be deployed on a physically different tier.

The bottom line that I see here is that you should only consider ObjectDataSource for enterprise class applications and wherever the complexity of the domain model is significant. LinqDataSource is a much smarter replacement for SqlDataSource with the current technological limitation of being limited to SQL Server.

This said, I wouldn't bother using either SqlDataSource or LinqDataSource for applications with a very simple domain model, a trivial business logic, or just in the context of temporary pages that are to be quickly arranged and just work. 

Using Linq-to-SQL in the Data Access Layer is all another story, instead. As long as your data store is SQL Server, Linq-to-SQL may be an excellent replacement for T-SQL and its underlying data model returns objects rather than tables as in VS 2005 table adapters. This is much more powerful and easy to maintain. But the interface towards the middle tier must still be rendered using contracted data transfer objects leaving the middle tier in charge of talking to the presentation and ASP.NET pages. If you intend to use data source controls for this, ObjectDataSource is the only option you have.

 

Comments

# GadgetGadget.info - Gadgets on the web » LinqDataSource vs ObjectDataSource vs SqlDataSource said on September 24, 2007 01:10 PM:

Pingback from  GadgetGadget.info - Gadgets on the web » LinqDataSource vs ObjectDataSource vs SqlDataSource

# Dino Esposito said on September 24, 2007 01:20 PM:

LinqDataSource, chi era costui?

# Patrick.O.Ige said on October 9, 2007 03:40 AM:

LinqDataSource vs ObjectDataSource vs SqlDataSource

# li chen said on November 26, 2007 01:58 PM:

Clear and concise article. I've got many of my questions answered with regarding to LINQ.

# Lorenzo said on November 26, 2007 05:07 PM:

One moment. In Linq for Sql the stored procedures are only for Insert/Update/Delete  in linqdatasource. For "SELECT" there are only dynamic sql and not store procedure Why???

# Cristian Remy Sola said on December 4, 2007 12:59 PM:

Excelent! Dino, clear and objective

# Braulio said on December 11, 2007 08:37 AM:

Hi Dino !

Mmmm.... how can you performe caching on a LINQDataSource?

And on the other hand, implementing efficient custom paging on a GridView / ObjectDataSource is a pain in the neck (ranking, sorting...). I think that's an strong point for LINQDataSource you get all this for free.

What's your opinion about this two topis?

btw. Greetings from Málaga, nice to hear you in the October Conference.

# Andreas Hilken said on December 19, 2007 08:43 PM:

I also on searching a posibility to use the SqlDependency / Linq Query and CommandNotification. But I still searching a solution for this topic

# A very brief overview about LINQ to SQL, serialization and concurrency | Sugata (beta) said on January 27, 2008 08:38 AM:

Pingback from  A very brief overview about LINQ to SQL, serialization and concurrency | Sugata (beta)

# Acer said on March 13, 2008 05:19 PM:

If I'm going to clone databases, and want to use the same application, but change the database it uses dynamically, which one is best.  

It seems objectdatasource has the database information embedded, and also has the connection string from web.config.  So, these cannot be changed on the fly.

# Adrian T. said on March 25, 2008 12:19 PM:

Hi,

Nice article!

Howevere, wouldn't be a much better approach for enterprice apps. to develop custom DataSource controls, also Custom DataSourceView controls (to keep the separation between UI & code) and to avoid  creating artifficial Model-View-Presenter, which now doesn't seem to belong trully to ASP.NET (and .NET) ?

Leave a Comment

(required) 
(required) 
(optional)
(required)