Guy Barrette, Microsoft, Regional Director, Montreal, Canada, Visual Studio, .NET Expertise LINQ to SQL - Stunned! - Guy Barrette

Guy Barrette

Microsoft Regional Director, Montreal, Canada

LINQ to SQL - Stunned!

[Looking for opinions, links, articles and so on]

How do you implement LINQ to SQL in a classic 3-tiers (presentation, BAL, DAL) app?  A simple question, right?

So, if I create my LINQ to SQL DataAccess classes in a DAL, this is where the transport objects will reside.  I usually create a layer just for that so each layer can reference those objects.  Now, the presentation layer must reference the DAL directly.  Yuck!

OK, so let's return POCOs to the presentation layer instead.  Well, I'm losing all the change tracking stuff provided by LINQ to SQL.  Yuck!

What if I need to expose my BAL layer as a set of WCF services?  Well, I'm also losing the change tracking.  Yuck!

And what if I want to data bind my grid?  Using the designer, I need to point to the DataClasses sitting in the DAL.  Yuck!

OK, I'll lose the change tracking stuff.  Now I'll have to reload each record before saving it?  Yuck!

The way I see it, the only way you can possibly use LINQ to SQL is if you develop your app in a single layer.  I must be wrong.  I must miss something.  This can't be real.  Stunned!

 

Posted: Feb 24 2008, 08:14 PM by guybarrette | with 13 comment(s)
Filed under:

Comments

teflon114 said:

Check out This article on CodeProject

www.codeproject.com/.../vs2008messageboard1.aspx

It is a good example of how to create a linq DataContext that maps your BAL objects to tables using an xml file.

# February 24, 2008 9:24 PM

tdavisjr said:

You can also take a look at this sample

weblogs.asp.net/.../building-an-n-layer-asp-net-application-with-linq-lambdas-and-stored-procedures.aspx

This is also a decent approach seperating the layers while using Linq to SQL.

Don't worry. I am trying to figure the same thing out now. Maybe the ASP.NET team could write a quick sample app to use as a reference.

# February 24, 2008 10:13 PM

Mohammad Azam said:

Hi,

I was facing the same problem when I started with DLINQ (LINQ to SQL Classes). I also send the question to the authors of "LINQ in Action" and they told me that LINQ can only be used when creating Rapid Applications. This means you cannot layer your application using LINQ.

Off course, you can generate your entity classes and then copy paste in the BLL layer but that would not make any sense and will be considered a constant change?

# February 24, 2008 10:44 PM

LukkhaCoder said:

What does POCO stand for?

# February 24, 2008 10:59 PM

reverseblade said:

use NHibernate, or even linq to nhibernate, your pain will ease

# February 25, 2008 2:57 AM

Rick Strahl said:

Guy you can do this if you use LINQ to SQL as your DAL. You can keep the data context on your business objects and pass the POCO objects out. This should work fine as long as you don't disconnect the data context (ie. keep a reference to the business object around). This obviously won't work in a truly disconnected environment in which case you shouldn't have connected objects anyway right? There'd be a messaging layer in the middle and the synching is up to you.

LINQ to SQL definitely has issues with disconnected scenarios and if you're using to something like nHibernate LINQ to SQL is going to really screw with you because in LINQ to SQL entities are always indirectly bound by the DataContext which contains all the change state.

I've written about a business layer with LINQ to SQL here and this might help:

www.west-wind.com/.../160237.aspx

But there's no doubt - LINQ to SQL is NOT a one stop solution for data access. There are a number of problems with it with the disconnected scenario being the most obvious. However, if it fits - pure Web, or pure desktop apps or apps that only use minimal cross physical boundaries then LINQ to SQL can be pretty sweet because of the LINQ functionality that is available on the data which can reduce a ton of code and provide a lot of flexibility in how data passed out of the middle tier can be used.

In the future LINQ hopefully will be common with other ORM systems that are more powerful, but for now LINQ to SQL is it if you want LINQ data massaging (for better or worse).

# February 25, 2008 3:26 AM

danny said:

POCO = plain old CLR objects (or an '80s country/rock band)

# February 25, 2008 12:04 PM

Travis said:

I ran into this same question, my answer that works 100%?  I went back to NHibernate and the Repository Pattern.

# February 25, 2008 2:29 PM

guybarrette said:

Thanks everyone for your comments and links.  They are very useful.

# February 25, 2008 8:38 PM

Richard's Rant said:

Interesting blog post describing some of the shortcomings of ASP.MVC Anotherblog post, this time about

# February 25, 2008 8:48 PM

Jonathan said:

As far as I can see, LINQ to SQL is a useful tool for developing prototypes and throwaway apps but isn't suitable for the kind of layered apps you're talking about. LINQ to Entities sounds like it will be the answer to what you're looking for.

# February 29, 2008 4:19 AM

liammclennan said:

The article below covers how to use Linq-to-sql with the repository pattern.

www.codeproject.com/.../linqrepository.aspx

# June 15, 2008 8:38 PM

Gregory A. Beamer said:

You are correct, overall. The DataContext forces keeping a context open to add all the data access sweetness. LINQ is a viable option for many things, but I find that it is better for manipulating objects that adhere to IEnumerable than to get data out of a database.

There are other LINQ to SQL implementations that are disconnected, but I have yet to genericize one to use <T>.

# August 15, 2008 10:38 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)