encapsulating SQL queries into OO framework

  • Thread starter Thread starter Zeno Lee
  • Start date Start date
Z

Zeno Lee

Right now I'm putting my query strings into static functions in a DbQuery
class that returns Datatables.

Eg.
public class DbQuery
{
public static Datatable GetMyData()
{
Dataset ds;
...
string query = "select * from SomeTable";
....
return ds.Tables[0];
}
}

Over time, I'm accumulating a lot of static methods in my DbQuery class.

This way seems very procedural. I'm trying to make it more object oriented.
Any recommendations on how to encapsulate individual queries into a more
object oriented framework?
 
I will suggest you to to use stored procedures instead of inline sql queries.
You said that you have static methods which fetch data for you. Using static
methods is a good approach if the data is more like lookup data, and further
if this is the case then its always better to use some sort of chaching
mechanism so that you reduce database trips.

Regards,

Deepak
[I Code, therefore I am]
 
Zeno Lee said:
Right now I'm putting my query strings into static functions in a DbQuery
class that returns Datatables.

Eg.
public class DbQuery
{
public static Datatable GetMyData()
{
Dataset ds;
...
string query = "select * from SomeTable";
....
return ds.Tables[0];
}
}

Over time, I'm accumulating a lot of static methods in my DbQuery class.

This way seems very procedural. I'm trying to make it more object
oriented. Any recommendations on how to encapsulate individual queries
into a more object oriented framework?

At this layer is supposed to be very flat and stateless. This layer is just
meant to interface with your database, and so it should look like your
database.

You can put a layer in front of this which groups related queries and
transactions into more stateful groupings.

David
 
One option is to use the Table Data Gateway Pattern. In this pattern you
effectively encapsulate data access in an object that acts as a Gateway to a
database table. One instance handles all the rows in the table. You'll find
lots of references online. You can then layer your business objects on top
which encapsulate your business rules. Sorry its not a detailed an answer
but I hope its a useful pointer

Cheers, Pete
 
Back
Top