Re: What is the best practice for adding persistence to an MVC model?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



2009/10/27 Paul M Foster <paulf@xxxxxxxxxxxxxxxxx>:

> On Tue, Oct 27, 2009 at 05:27:07PM +1100, Eric Bauman wrote:

>> I'm in the process of implementing an ultra-light MVC framework in PHP.
>> It seems to be a common opinion that the loading of data from a
>> database, file etc. should be independent of the Model, and I agree.
>> What I'm unsure of is the best way to link this "data layer" into MVC.

> I disagree. There should be database class(es) which know how to deal

Paul, you already did this in one of Eric's threads a couple of weeks
back, and the best you could come up with when pressed was "Probably
not a great example, but...". Is it really necessary to rehash this?

Eric, I recommend reading up on how to develop testable code
(especially Mock Objects) - it's great discipline because it requires
everything to be decoupled.

The short version is that it's often useful in unit testing to "fake"
the classes that your target relies on, so that you're testing it in
isolation. For example you could provide the class under test with a
fake DB class that returns static results.

It's much easier to do this when you pass objects /to/ your class:

class MyClass {
    function MyClass($db) {
    }
}

than when your class inherits from the object:

class MyClass extends Db {
}

or when the class instantiates the object:

class MyClass {
    function MyClass() {
        $this->db = new  DB_CLASS;
    }
}

If you go with the first approach, you're writing code that you and
anyone who comes after you can write useful tests for. The others, and
you're denying maintenance programmers a useful tool.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux