2/19/2010

The Repository Pattern

If you're still writing SQL statements to persist your objects to/from your database, STOP!!! If you fall into this category, your code is likely unreadable by anyone other than yourself. And before you know it, even you won't be able to read your own code. Believe me, I know because I have felt this pain.

In my experience, about 9/10 of database-driven applications could be vastly simplified by using a software pattern known as Repository.

public interface IRepository<T>
{
    T Get(object id);
    void Save(T value);
    void Update(T value);
    void Delete(T value);
    IList<T> GetAll();
}

That's all there is to it. By using this interface as a gateway for getting objects to/from your database, you're able to completely decouple your system from the data access layer (DAL, for short). The advantages to this approach are twofold:
  • Your code is 500% more readable because the reader doesn't even have to THINK about the implementation details of your DAL.
  • You're able to easily swap out the DAL for a different implementation.
The implementation of the Repository pattern should be capable of reading the type of the object passed to it and then know how to fit the object into the database. Now, I suppose you could write a whole crapload of SQL to implement this interface OR you could use an ORM. I've found that NHibernate is a great way to implement the Repository pattern and that's what I'll be covering in my next blog post.

Update: Here is the next blog post on Implementing the Repository Pattern with NHibernate.

5 comments:

Anonymous said...

Blogging on vacation?

Anonymous said...

Links? Don't make me google this crap :-P

James said...

What should I be linking to? Sorry for the delay. Yes, I am on vacation. :-P

throwawayjj said...

On how many database-driven applications have you personally worked? I'm wondering how you arrive at the conclusion that 9 out of 10 would appreciably improve by using the "Repository Pattern".

There are many good reasons to use plain SQL / stored procedures instead of ORM. For complex queries, ORM simply adds an unnecessary and bulky layer of abstraction that would result in more code than if you used plain SQL -- something you misrepresent in this post.

And if SQL statements are unreadable (that is, 0 readability), then how would increasing readability by 500% help?

All of this is not to say that there is no place for ORM, only that you've inflated the value and applicability of it.

James said...

@throwawayjj

The 9/10 factor was a ballpark estimate I made judging from the dozens of CRUD systems I designed from start to finish (plus other projects I've assisted with) and is not backed by any critical research aside from the half second it took me to think of it. It might be closer to 4/5, but I even doubt that.

It's not to say that some projects require some greater level of control than what this pattern has to offer. That's where the 1/10 (or whatever) comes in. In these cases, you may need to harness the power of the SQL namespace, or even better yet, a more appropriate software pattern. (Unit of Work?)

Perhaps your experiences are different than mine, but I've found that most reasons to go the way of SQL vs. a reusable software pattern are typically unfounded and reek of premature optimization syndrome.

Code readability issues are real, and are a huge threat to productivity where I work.

Post a Comment