The LINQ Dilemma

Let me start off by saying that I'm a huge fan of Language Integrated Query (LINQ).  If you've ever had to write code to loop through objects to locate properties and then filter the collection based upon specific values then you'll definitely have an appreciation for what LINQ has to offer.  It's a phenomenal technology that will definitely make developing applications a more productive process.  Scott Guthrie has some really nice tutorials that help jumpstart learning more about LINQ and LINQ to SQL that you can view here.  For those that haven't seen many LINQ samples, here's a simple example of using LINQ to filter a collection of Customer types based upon a Country property:

List<Customer> custs GetCustomers();
var filteredCusts = for in custs
                           where c.Country 
== "USA"
                           
select new {CustomerID c.CustomerID, Name c.ContactName, 
                                             Country 
c.Country};

In addition to querying objects, LINQ can also be combined with other technologies such as SQL and ADO.NET (XML support is also included) to query databases and automatically fill collections without manually writting O/R mapping code.  This is a great feature and a big time saver.  However, it makes it really easy for developers to embed SQL logic directly into application classes without necessarily realizing it.  This leads to the LINQ dilemma....

Inline LINQ/SQL Versus Stored Procedures

If you've been developing for very long then you've certainly heard the old recommendation of "use stored procedures when calling a database".  There's a lot of wisdom to this recommendation since it promotes more fine-grained security, better re-use, and simpler maintenance as compared to embedding SQL directly in code.  It also lends itself well to schema modifications since changes can quickly be made to tables, views, triggers and stored procedures directly within a database without even touching any application code potentially (depending upon the changes of course). 

With the LINQ to SQL designer in Visual Studio 2008 you can use stored procedures (Scott demonstrates this in a few of the tutorials mentioned earlier), but many of the demos that you'll find out there covering LINQ to SQL perform LINQ queries that generate dynamic SQL directly within .NET classes.  Is that bad?  It depends on who you talk to.  Experienced developers will weigh their options here, but those who are newer to the world of LINQ to SQL may not consider the ramifications of embedding LINQ code to query databases directly in their application classes.

I personally think that embedding LINQ/SQL logic directly into classes will lead to maintenance nightmares down the road especially in large applications since any changes to a database now require not only updating the database tables, views, triggers, etc., but also locating the related C# or VB.NET code that also needs to be changed (similar to what happens with embedded SQL code now).  In enterprise situations there may be multiple applications that need to be modified.

Security is also a potential issue since instead of being able to lock-down specific stored procedures to defined users and roles, more generic queries against source tables are made (views can certainly be used as well though).  In talking with several DBAs about this at the DevConnections conference in Las Vegas, they're certainly not supportive of moving query code out of the database for security and maintenance reasons.  Some of them just didn't want to give up that type of control, but the majority just thought it was a bad idea from a security and maintenance standpoint. 

I think that adding LINQ to SQL code directly into application classes is extremely productive.  It's pretty amazing how quickly and easily you can access data.  However, I'm still on the stored procedures bandwagon when using LINQ to SQL because I'm sold on the security, re-use and maintenance story that goes along with them.  However, in talking with several other developers (some who are very experienced) they don't necessarily agree with this view and will be putting their LINQ/SQL queries directly within code since it's simply more productive.  If you have an opinion here I'd be interested in hearing it since I think this "LINQ dilemma" will certainly stir up a lot of discussions as .NET 3.5 is released.

 

Translate:
Published Thursday, November 15, 2007 8:09 AM by dwahlin
Filed under: ,

Comments

# re: The LINQ Dilemma

Friday, November 16, 2007 12:08 PM by Shan

I like the benefits of direct LINQ queries too but still will use stored procedures in larger enterprise scenarios. But I'm not ruling out using LINQ queries for prototypes and small scale apps or scenarios where the data will always be tightly integrated with just that application.

It definitely entrenches your applications more in the .NET world by using embedding LINQ queries versus database queries...hard to say what the long term prospects look like for any new / emerging technology....SQL stored procedures definitely have proven longevity behind them.

This technology will likely impact new projects more than existing. It's worth considering the additional ramp up needed to learn how LINQ behaves - the syntax is quite SQL like but the behaviour not necessarily intuitive without testing - so another layer on top of SQL to learn just for querying, inserting, updating data.

I also like stored procs because they are reusable across more than one application...how often do you run into scenarios where multiple applications are working with the same database data, or where database administrators need to maintain certain tables, or where developers that are maintaining an application need to perform periodic ETL type operations to their data store, custom reports, etc. etc. All of these types of scenarios lead me to believe that stored procs are still "better" just because of the additional re-use that you get from them outside of your .NET applications plus the self describing nature of stored procedures that makes those types of scenarios a lot easier in many cases.

I would definitely consider using LINQ but in enterprise applications I'd likely be calling stored procs. That's my 2 second brain blurb at least...it's an interesting technology

# re: The LINQ Dilemma

Friday, November 16, 2007 12:26 PM by dwahlin

I'm with you....thanks for adding your opinion.  It really depends on the application, requirements, size, time constraints, etc. (which are all unique of course), but having worked in several enterprise scenarios where a database may be used by multiple apps, having re-useable stored procedures that you know work properly is sure nice.

# re: The LINQ Dilemma

Friday, November 16, 2007 4:08 PM by Speednet

Thanks Dan for posting this opinion.  These issues have *ALWAYS* bothered me about LINQ.  To be honest, even though it seems *cool* I have a hard time understanding why it is getting such huge attention.  The best I can come up with is that is will help ASP.NET become a better beginner's platform.

I can't see LINQ being used as a primary SQL data access method in enterprise applications.

Similarly, I'm not all that keen on the late-bound variable types that were added to the language to support LINQ.  SO MANY bugs are eliminated from strict variable declarations.

# re: The LINQ Dilemma

Sunday, November 18, 2007 11:49 AM by Nick H

I'm trying to understand the discussion here.  It's pointed out that it's just fine to use stored procedures with LINQ - the issue in the article really is about whether giving people this tool is going to encourage them use inline SQL rather than stored procedures  and whether inline SQL is ever a good idea.  And these are excellent questions.

Yet the replies seems to say 'LINQ = no stored procs, therefore it's a bad idea'.  That's just not correct nor is it the point of the article. (Nor does using LINQ make you a beginner, btw)  

Using a tool incorrectly is always a possiblity, no matter what the tool.  If we stop adding tools because they might get misused..well.. maybe we need to rethink the whole IDE thing since it's what let some of these 'less' talented coders get into the business. VB.NET itself comes into question for what it allows.  Where does that logic end?

It doesn't - the logic is flawed.  LINQ is a tool.  VB.NET is a tool. C# is a tool.  Use them as wisely as you can, like any other tool, based upon your situation.   Sometimes, yes, you need quick and dirty access to the data - I welcome anything that makes that cleaner.  I welcome anything that makes a strongly type object for my dataset (however that dataset was obtained) easier to come by.  I welcome anything that lets me do some complicated stuff in my code eaiser  - stuff I was going to code the hard way anyway.  You can't put ALL your code in a stored procedure - if that were the case, we'd all be coding in T-SQL.  

Personally, I can't see writing off any tool simply because of how it might be used, enterprise or not.  Give me every tool you can think of - I want a swiss army knife language - I'll make the decision about when and where to use it because that's what I get paid to do.  I don't think anyone is suggesting that you should now only use late-bound variable types - if you have coders on your team doing this, the problem isn't the language.

Speednet, I find it interesting that you appear to program in VB based upon your blog examples - the king language of untyped variables.  I'm curious why you haven't moved to C#.

Anyway, this idea that most software is written in an ideal 'DBA, Coder, QA, etc' environment is pretty bogus.  I've been doing consultant work for 15+ years in large (very large) and small companies - 9 times out of 10 it's 'get it done yesterday', vague specs, probably no DBA and only a QA person if you are lucky.  That's how a lot of software is written.  Is it the right way? Nope.  Is it a business reality.  That'd be a big Y-E-S.  Unfortunately a lot of companies just don't understand it's pay for a DBA and/or QA person now or pay more later.   So any tool that helps make that cleaner is going to help.

Just my .02

# re: The LINQ Dilemma

Sunday, November 18, 2007 1:39 PM by Dewey Vozel

I'm torn by modern and future development practices. On one hand, it is great for productivity. No programmer likes to have to sit and fill in all of the "monkey work" code required to make an application work. It is quite tedious to manually design a database, then write the stored procedures, then create code entities that match up with your database tables, then write a data access layer to call stored procedures using those entities. It's a hassle because for the most part it is a mindless process once the database is designed. A lot of stored procedures will simply perform straight-up CRUD operations like SELECT * FROM MyTable WHERE SomeColumn = SomeValue. No one enjoys writing all of that code. It doesn't make the developers happy and it doesn't make corporations happy because the project plan is filled with these time consuming tasks.

On the other hand, development is supposed to be challenging. Developers used to be an elite cadre of people who understood the inner workings of the operating system and how to use programming languages to solve business and leisure needs. Today, it is a matter of drag-n-drop, step through a wizard, compile, and call yourself a developer. The result? A lot of developers who know nothing else. These developers flood the Internet with crappy programs they have written and they flood the workforce with people who cannot handle the job once a situation is encountered where you actually have to think and write code.

By making developers more productive, they are in-turn doing the development community as a whole a disservice. I hope this is an exaggeration, but in 5-10 years once all of the developers who actually know how to program have moved on professionally, who will add new features into the framework? Who will develop hardware drivers and other low-level specific items that can't simply be done by a drag-n-drop programmer?

So in the end, it's a trade-off. A dumbed-down development community and poorly written programs for slimmer project plans, happier "developers", and a loss of identity. No longer are we the elite, we are a dime-a-dozen...don't believe me? Search Monster.

# re: The LINQ Dilemma

Monday, November 19, 2007 12:39 AM by dwahlin

Nick,

Thanks for adding your comments.  I definitely agree that LINQ is simply a new tool for us to use and it's very productive whether or not SQL enters the equation or not.  Ultimately it's up to the developer to learn what works best in their situation and company/project.  A lot of companies do have DBAs ready to create stored procedures while the others you mention don't.  Either way, LINQ to SQL offers a much more productive way to do O/R mapping and once the entity framework is released the story will get even better.  I just hope that MS and others will focus on the best practices because a lot of new developers I've worked with in the past seem to take samples at face value and assume that they're "best practice" rather than taking the time to learn about what "best practice" really means.

# W&ouml;chentliche Rundablage: ASP.NET MVC, Powershell, .NET 3.5, Visual Studio 2008, YUI, Windows CE, Silverlight&#8230; | Code-Inside Blog

Pingback from  W&ouml;chentliche Rundablage: ASP.NET MVC, Powershell, .NET 3.5, Visual Studio 2008, YUI, Windows CE, Silverlight&#8230; | Code-Inside Blog

# re: The LINQ Dilemma

Monday, November 19, 2007 9:05 PM by Nick H

Dan,  You hit the nail on the head about the samples.  It's been my second biggest complaint (right behind how Delphi's databinding is still ahead of .NETs - I have high hopes that LINQ might solve some of this - we'll see)  There are sooo many ways to do things because of all the cool tools we've been provided that figuring out the 'correct' (whatever that means) way to do things is difficult.   The included sampels are usually to simplistic to be useful (there's that best use issue).   The microsoft asp.net forums are questionable at best (who had the bright idea of rewarding people for their number of posts? cause sooo many useless posts - I just stay away)  I get most of my info from books and blogs  - which are also a bit hit or miss.  Anyway, off to play with 2008!!!!

# re: The LINQ Dilemma

Wednesday, November 28, 2007 2:02 AM by The Greatest Rajesh B Patil

I think the LINQ is great in two scenarios, as my great freinds and you have explained here,i.e.

1. During the creation of the smaller portals and smaller applications.

2. To aviod the loops from the code for the better refactored code when used along with the generics, in short while working with the generic search kind of a work.

So, you are right i am also the one who agrees that still the stored procedures are the ones which i would prefer.

# Using LINQ to SQL XML Mapping Files – Step by Step

Monday, August 18, 2008 3:20 AM by Dan Wahlin's WebLog

.NET 3.5’s LINQ to SQL functionality provides a great way to write data access layer code that automatically