tags:

views:

117

answers:

4

I've inherited some code that has a Public Module for data access:

Code looks like:

Public Module Foo
    Dim ds As New DataSet

    Public Function GetDataSet(ByVal sqlQuery As String) As DataSet
     ...
       Fill(ds)...
     ...
    return ds
    End Function
End Module

And all the pages call GetDataSet(sql). Am I correct is assuming that this is a bad idea? Worst case is that concurrent callers could get each others data?

+1  A: 

Very bad. With that architecture there is absolutely no way to use a parameterized query or stored procedure to protect yourself from SQL Injection...and that's just a start.

You're also going to be missing out on any kind of strongly typed data sets (which make life a whole lot easier).

Justin Niessner
A: 

i use something similar to this and have never had any problems. makes data access sooo easy. i don't mind writing SQL and in fact it kind of helps me understand exactly what data i'm getting. so long as data access is clearly separated (ie, you pull data consistently from the same location), and you sanitize all data in the SQL string, i don't see anything wrong w/this. i do agree that public variable is bad, though.

sigh... or maybe i'm a bad programmer :\ let the downvoting begin...

Jason
Easy, yes. But it's just not good practice (again, except for certain DB-specific Tools/Utilities).
RBarryYoung
i've always wondered if it were good practice or not... i use mysql as my db which to my knowledge (which may or may not be correct) doesn't translate directly to linq. i kind of like seeing my SQL strings, too. i think it's better practice than creating a new data access function for every single place you access the db, though. what happened to DRY?
Jason
DRY = Don't Repeat Yourself: If you put the SQL in a stored procedure, you won't have to repeat yourself. Well, no more that you would for embedded SQL (as in the question). The fact is that for any application that uses persistent structured data (i.e., a database), DRY is impossible to apply completely. Adding a field on a form requires DB changes and changes to the code in-between. Same with datatype changes, etc.
RBarryYoung
note: that's with current development technology. There's no theoretical reasons that the "DRY-Tiers" problem couldn't be fixed or at least made a lot better than it is. But putting everything in the middle-tier isn't the solution that that seems to be where the industry is headed.
RBarryYoung
+1  A: 
  1. Your concerns about concurrent callers is well founded. The solution is to just move the "Dim ds.." from the module into the GetDataSet routine. Then its local, on the stack, and theres no chance of stepping on each other.

  2. A larger problem is that composing SQL queries as string in client code is not good application design, it can be OK for SQL utilities, but nothing else. This approach will keep you from using Linq to SQL, Linq to Entities, Stored Procedures, most of the more desirable DB-access security models and probably exposes you to SQL Injection attacks.

RBarryYoung
A: 

Do you even have to ask?

epitka
More evidence never hurts :)
chris