Welcome to Professional ASP.NET - Chris Love's Official Blog Sign in | Join | Help

Chris Love's Official Blog - Professional ASP.NET

Chris Love's Helpful tips, tricks and pragmatic development knowledge for the ASP.NET world.
Add to Technorati Favorites


Join me at CodeStock
I Still Say Use Stored Procedures!

First, wow, I never ever dreamed pushing that publish button on something I wrote in about 20 minutes would get so much play! Thanks for all who shared their opinions, I enjoyed them all. Please feel free to share more, I think this is such a good discussion to have.

But seriously you will not find me breaking from my stances. I will write more detailed Blogs over the next month or so on my other points. I really want to collect some really good content to demonstrate what I am talking about. Once I do, I think many of you will understand better.

I want to go over the Ad-Hoc SQL (Yuck) versus Stored Procedures argument. First I guess things may be worse than I ever thought. I used to use ad-hoc queries, I really did. Then I stopped supporting MS Access for my back-end and just focused on SQL Server for my data store.

I know, I know, I could use ad-hoc queries for about 95% of my SQL. And no I most likely would not notice any sort of performance gains or loss either way. I tend to keep my sprocs to basic CRUD, but do find situations where more complicated queries need to be in the database, such as returning all locations within 25 miles of a point. But I do put much more of the real business logic in my application code, but frown on hold large sets of data in my applications to filter and sort.

I do however feel I would create a much harder to maintain application. I try so hard not to hard code strings in my application and an ad-hoc query is just that. From a maintenance point of view I can change a stored procedure real quick if I need to, without building my app and deploying it all over again. In fact I most likely would never have to touch the application code for many of these changes.

So where does my frustration come from, well it is the way things are taught. We have way too many examples handed to us as new .NET programmers with ad-hoc queries. My peer Craig Utley shared this thought with me on the topic:

First, when learning .NET, all the examples show in-line SQL in the application. So it's a learned behavior from the start, which is of course the hardest thing to unlearn.

I see a lot of development environments these days. And I see a lot of good things, but way too many bad things that make .NET applications get a poor reputation. Most of this comes from a poor introduction in the initial tutorials we are offered. I admit it hurt me for the first few years with ASP.NET as my framework of choice. I sort of resent it. I wish I would have been challenged more to get rid of the tee and learn to hit a real curve ball. Don't candy coat things for me, when I need to really know how to accomplish something.

Another peer Itzik Ben-Gan shared his opinion on why developers fall on one side of the fence or the other:

People like to use tools they have more control over. DBAs and Database Developers have control over the database side and are comfortable with stored procedures. Developers  don’t want to be at the mercy of the database folks, and require confirmation for every move, which is more of a case when using stored procedures.

I think this has a lot to do with it as well. I have been places where it takes an act of congress to get a DBA to execute a simple script update for you on the production database server. But I have also been in places where the DBA will under no circumstances allow you to have direct permissions to any of their tables. It is a security thing and we all know how DBAs guard their databases. So sometimes you will not even have the permissions to use ad-hoc queries. Sometimes you will and the ability to update stored procedures is just not going to be an option, so I can see running with ad-hoc queries.

Now if I were to use ad-hoc queries how would I do it? Well I would abstract it down to a single point of failure and maintenance, as close to the database as I could get it. Andrew was good enough to point us in the right direction with suggesting an ORM, and not to mention he is onto how to page a repeater. You should have a good n-tier architecture in place for your applications.

How do I do it, well I just use the database, with stored procedures with a data access layer above it. My stored procedures generally are not terribly complicated, 95% do not have to be more than just simple basic CRUD, let's face that fact. So having a good data access layer is key.

In this layer I have an abstracted class to manage interactions with the database. I seriously have had this written and baked for years and it abstracts me from really caring about how to get a dataset or a repeater and I am fine with that. I have my code written and it works great, so no if you asked me to write how to do that boring mundane code at this point I would not know where to start. I would write something like this (and this is some real code from one of my sites):

Public Function GetBonusTypes(ByVal BonusType As Integer) As BonusTypesInfo

    Dim dr As New SafeDataReader(ExecuteReader("spGetBonusTypes", _
        CommandType.StoredProcedure, "@BonusType", BonusType))

    Dim si As BonusTypesInfo = Nothing

    If dr.Read Then
        si = BindBonusTypesInfo(dr)
    End If

    dr.Close()

    Return si

End Function

Oh and I use the CSLA SafeDataReader class for a datareader and I highly recommend using it instead of the provided SQLDataReader. But that is another story for another day!

The next layer is the business layer and here I do perform many of my data manipulations, but again for the most part they are not typically complex. I have found that if I have to make them fairly complex a stored procedure is a much better way to go. Let's face it the database is optimized to manipulate large sets of data fast. Therefor enter the stored procedure for sure.

The next layer is the user interface, which is where we get into the argument against the Views, and like I stated that will be covered in the near future.

I am not going to get into doing some sort of performance tests or things like that. What gripes me is more of a style of programming. I like having parts that perform specific tasks, making it, in my opinion, easier to maintain once the application is alive and during the initial run-up to production.

I wanted to glean some good quote to Adam Machanic's article, but there are a lot of great things in it about stored procedures and the API approach I think you should just check it out.

Now onto things that I honestly have not really had a good opportunity to start playing with LINQ and Data Entities. I know all about LINQ to SQL, and I really like all the LINQ extensions we are seeing. But the more I examine things I really think the strength will be applying LINQ to Objects, which is where I see myself going pretty quick. I just need some more time (if you can tell me where to buy some send me the link). Data Entities has not officially been released and I am still reading about it, so I will leave that for later too.

Finally, SQL-Injection. Many of you pointed out you should use parameterized queries. Absolutely you should, but face it many programmers and you know who you are, do not. There are way too many examples of this type of bad programming:

"Select * from Customers where CustomerId = " & CustId

Oh so bad and I see so much of it in person and posted on the ASP.NET forums. Right now I think I am too tired to cover parameterized queries. So another Blog is in the works! I am sure there are plenty out there already.

So to leave on a funny note, some of my peers offered up the ultimate stored procedure (take it in good humor!):

CREATE PROC p_FireThisDeveloper @IamStupid varchar(8000)
AS
exec (@IamStupid) 

Again thanks for all who commented and sharing your opinions. Keep it up, I really enjoyed each of the comments. This was a good banter and I hope we all learn and grow from it.

Share this post :
Posted: Thursday, April 03, 2008 12:25 AM

by Chris Love
Filed under: ,

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# April 3, 2008 12:27 AM

JV said:

So because a developer who isn't even you collegeau is stupid enough to concatenate queries, instead of just using SqlParameters, you prefer sprocs? Hope you don't jump of a cliff when your neighbour does too....
# April 3, 2008 3:53 AM

Chris Love said:

JV, you missed the point, but oh well. The point is that we teach too many newbies to concatenate and not do things the rigth way.

# April 3, 2008 7:11 AM

John S. said:

The argument typically gets boiled down to Ad-hoc vs. Stored Procedures, but those aren't the only two options. The discussion should be Ad-hoc (awful) vs. SProcs-only (bad) vs. ORM/sprocs when needed (practical, maintainable). I only use a few stored procedures, but the rest of my sql is generated by SubSonic. I don't have a single hard-coded piece of SQL in my apps. Just because you're not doing Sprocs-only doesn't mean you're writing gobs of inline sql.
# April 3, 2008 11:22 AM

Alan Mojab said:

In my opinion the discussion about procedures vs. ad-hoc sql is really pointless because depending who you discuss it with you would either get hammered or would be praised for your opinion, why? Simple, in software development one problem can be solved in many ways and all the solutions have pros and cons one way or other. The only way you can see the other side of the argument by being the other side yourself. I’ve got a feeling you haven’t been the other side yet. To be the other side you need to start developing a mini OR/M with few database engines to support. You would start thinking differently about your approach to database programming all together. I’ve been on both sides so I can clearly say to you that your approach to use stored procedure is not wrong as long as the application system in question passes a check list. The check list contains a list of constraints that your application needs to pass i.e. would all your clients want to use the same database engine? The Provider Model pattern can’t solve all the problems. Using ad-hoc sql can be as safe as using stored procedures for passing parameters but you need to know how. Depending what database engine you use stored procedure does not provide any performance advantages over ad-hoc sql. This is very true with Ms SQL Server because it has an excellent execution planning engine. Incidentally, SQL Server is Microsoft’s best product ever but never would forgive them for not supporting pagination support in SQL 2000. The "Extract Method" refactor command in VS allows you to encapsulate logics to reuse them this is very similar to SPs. In my humble opinion this is what makes SPs attractive to many developers but other reasons such as performance and safety are not really true. Developers that don’t like SPs and hate writing ad-hoc sql directly into the code would love OQL (Object Query Language). OR/M strength is in OQL. OR/M has weaknesses too but that is for another blog post.
# April 3, 2008 12:15 PM

Manu Parashar said:

I really enjoyed reading your post and Adam Machanic's article that you suggested as I am a big fan of SP
# May 16, 2008 5:42 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS