Database Gateway

by March 29, 2010 06:14 AM

A buddy of mine asked for a copy of the database access strategy on my current project. We're not talking about repositories or data access layers or anything like that. We're talking about all the way down to where the call is actually made to the real database. So I give you...

DatabaseGateway.cs
IQuery.cs
or get both as code in DatabaseGateway.zip

I'm not going to walk you through the code here. You can do that at your own leisure if you want to. The interesting part of the gateway to me is its use. Most of the time, you'll find something like this in our code: _db.ExecuteQuery(Query.For(prepare, map)). "prepare" and "map" being delegates declared locally. In other places you'll see something like _db.ExecuteQuery(new MySelectSOHeaderQuery(salesOrderNumber)) where "MySelectSOHeaderQuery" is a private nested class that implements IQuery (or IResultQuery<SalesOrderHeaderDTO> in this case).

The main thing I wanted when I created this was to simplify data access down to the fact that we want to execute queries against the database. Since that's the way we say it, that's the way the code should read. Therefore, when you're reading our data access, you see (if you look at it just right) the statement, "database, execute this query".

If you look at the code, you'll notice there are three different types of queries: queries that just do things, queries that return things, and queries that should be audited. No matter the type, they're still just queries. That's why in the database gateway the three overloads are named the same - because we're still just executing a query against a database and that's the way the code should read.

Keep in mind the vast majority of our data access is done with nhibernate. There are a few left-over queries against our own database that still use this gateway (left over from before we started using NH). Most of the gateway's use comes from our interaction with other systems. You see, we still value connection string sharing as an acceptable SOA strategy ("we" doesn't include a few of "us" so please be easy).

Feel free to comment with any questions or thoughts about it. Enjoy!

Tags: ,

Comments are closed