Staying OO and Testable while working with a database

Posted by Adam Backstrom on Programmers See other posts from Programmers or by Adam Backstrom
Published on 2011-02-02T14:53:56Z Indexed on 2011/02/02 15:33 UTC
Read the original article Hit count: 341

Filed under:
|
|

What are some OOP strategies for working with a database but keeping thing testable? Say I have a User class and my production environment works against MySQL. I see a couple possible approaches, shown here using PHP:

  1. Pass in a $data_source with interfaces for load() and save(), to abstract the backend source of data. When testing, pass a different data store.

    $user = new User( $mysql_data_source );
    $user->load( 'bob' );
    $user->setNickname( 'Robby' );
    $user->save();
    
  2. Use a factory that accesses the database and passes the result row to User's constructor. When testing, manually generate the $row parameter, or mock the object in UserFactory::$data_source. (How might I save changes to the record?)

    class UserFactory {
        static $data_source;
    
        public static function fetch( $username ) {
            $row = self::$data_source->get( [params] );
    
            $user = new User( $row );
            return $user;
        }
    }
    

I have Design Patterns and Clean Code here next to me, but I'm struggling to find applicable concepts.

© Programmers or respective owner

Related posts about architecture

Related posts about oop