<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: The Problem With Active Record</title>
	<atom:link href="http://misko.hevery.com/2009/05/05/the-problem-with-active-record/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Fri, 30 Jul 2010 03:59:03 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Marques Johansson</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-1424</link>
		<dc:creator>Marques Johansson</dc:creator>
		<pubDate>Tue, 21 Jul 2009 15:27:29 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-1424</guid>
		<description>In some environments, config files may be used to define the default database connection and a registry might store that connection.  I hear you grumbling about that already, as being hidden code.

In this case, however, the static getConnection method inherited from the database class DBX by the Table_ABC class falls back to a PDOConnection factory where connection settings are stored as constants or retrieved from files, Apache environment, whatever. 

My database object knows about its dependency on a connection to a particular db host + schema and this ability is passed on to the extending table objects.  This relationship is an implicit part of the model - a database object is a connection to a server/db.

And yet, it can still be overridden with setConnection() for testing or custom purposes.</description>
		<content:encoded><![CDATA[<p>In some environments, config files may be used to define the default database connection and a registry might store that connection.  I hear you grumbling about that already, as being hidden code.</p>
<p>In this case, however, the static getConnection method inherited from the database class DBX by the Table_ABC class falls back to a PDOConnection factory where connection settings are stored as constants or retrieved from files, Apache environment, whatever. </p>
<p>My database object knows about its dependency on a connection to a particular db host + schema and this ability is passed on to the extending table objects.  This relationship is an implicit part of the model &#8211; a database object is a connection to a server/db.</p>
<p>And yet, it can still be overridden with setConnection() for testing or custom purposes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-1420</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Mon, 20 Jul 2009 20:25:48 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-1420</guid>
		<description>@Marques,

So how would you create XYZ without a database and without resorting to Global Variable Magic?

If I do new XYZ() that how does $conn get set? From static GetConnection() method which resorts to global state. Not liking it :-(</description>
		<content:encoded><![CDATA[<p>@Marques,</p>
<p>So how would you create XYZ without a database and without resorting to Global Variable Magic?</p>
<p>If I do new XYZ() that how does $conn get set? From static GetConnection() method which resorts to global state. Not liking it <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_sad.gif' alt=':-(' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marques Johansson</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-1411</link>
		<dc:creator>Marques Johansson</dc:creator>
		<pubDate>Fri, 17 Jul 2009 11:28:46 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-1411</guid>
		<description>sorry - &#039;class DBX&#039; should be &#039;abstract class DBX&#039;</description>
		<content:encoded><![CDATA[<p>sorry &#8211; &#8216;class DBX&#8217; should be &#8216;abstract class DBX&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marques Johansson</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-1410</link>
		<dc:creator>Marques Johansson</dc:creator>
		<pubDate>Fri, 17 Jul 2009 11:26:46 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-1410</guid>
		<description>Can&#039;t you move your Blog to Blogger so your blog is more reusable ;-)  .. At least implement Google Friend Connect or something..

One of the niceties of some ActiveRecord implementations, is that the creation of an object will cause all of the necessary database table meta data to be resolved:

abstract class ActiveRecordXYZ {
  protected $schema; // db name
  protected $table; // db table name
  protected static PDO $defaultconn;
  protected PDO $conn;
  protected static $metadata; // sql field types and other meta data

  public static setConnection(PDO $pdo) {
     return static::$defaultconn = $pdo;
  }

  public static getConnection() {
     return static::$defaultconn;
  }

  public __constructor() {
    $this-&gt;conn = self::getConnection();
    if (! $this-&gt;tablename) {
     $this-&gt;tablename = $this-&gt;guessTablename();
    }
  }

  protected function guessTablename() {
    .. regex get_class($this) ...
  }

  public fetchMeta() { .. self::$metadata = ...; done once per class at first use .. }

  public __get() {...$this-&gt;fetchMeta()...}
  public __set() {...}
  public load() {...}
  public save() {...}
}

class DBX extends ActiveRecordXYZ {
  protected $schema = &#039;dbx&#039;;

  public static getConnection() {
    if (! parent::getPDO()) {
      $pdo = PDOFactory::getConn(&#039;dbx_host&#039;);
       return self::setConnection($pdo);
    }
  }
}

class DBX_TableY extends DBX {}

$user = new DBX_TableY; // PHP now
$user-&gt;load(array(&#039;id&#039;=&gt;123));
$user-&gt;name .= &#039; Jr.&#039;;
$user-&gt;save();

Everything is still testable and mockable and all run-time dependencies are optional.  Do you agree?

The getter/setters here are really tied to the Table class (as you put it), and I can&#039;t justify breaking them out which would only seem to add more complexity with no practical advantage.</description>
		<content:encoded><![CDATA[<p>Can&#8217;t you move your Blog to Blogger so your blog is more reusable <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />   .. At least implement Google Friend Connect or something..</p>
<p>One of the niceties of some ActiveRecord implementations, is that the creation of an object will cause all of the necessary database table meta data to be resolved:</p>
<p>abstract class ActiveRecordXYZ {<br />
  protected $schema; // db name<br />
  protected $table; // db table name<br />
  protected static PDO $defaultconn;<br />
  protected PDO $conn;<br />
  protected static $metadata; // sql field types and other meta data</p>
<p>  public static setConnection(PDO $pdo) {<br />
     return static::$defaultconn = $pdo;<br />
  }</p>
<p>  public static getConnection() {<br />
     return static::$defaultconn;<br />
  }</p>
<p>  public __constructor() {<br />
    $this-&gt;conn = self::getConnection();<br />
    if (! $this-&gt;tablename) {<br />
     $this-&gt;tablename = $this-&gt;guessTablename();<br />
    }<br />
  }</p>
<p>  protected function guessTablename() {<br />
    .. regex get_class($this) &#8230;<br />
  }</p>
<p>  public fetchMeta() { .. self::$metadata = &#8230;; done once per class at first use .. }</p>
<p>  public __get() {&#8230;$this-&gt;fetchMeta()&#8230;}<br />
  public __set() {&#8230;}<br />
  public load() {&#8230;}<br />
  public save() {&#8230;}<br />
}</p>
<p>class DBX extends ActiveRecordXYZ {<br />
  protected $schema = &#8216;dbx&#8217;;</p>
<p>  public static getConnection() {<br />
    if (! parent::getPDO()) {<br />
      $pdo = PDOFactory::getConn(&#8217;dbx_host&#8217;);<br />
       return self::setConnection($pdo);<br />
    }<br />
  }<br />
}</p>
<p>class DBX_TableY extends DBX {}</p>
<p>$user = new DBX_TableY; // PHP now<br />
$user-&gt;load(array(&#8217;id&#8217;=&gt;123));<br />
$user-&gt;name .= &#8216; Jr.&#8217;;<br />
$user-&gt;save();</p>
<p>Everything is still testable and mockable and all run-time dependencies are optional.  Do you agree?</p>
<p>The getter/setters here are really tied to the Table class (as you put it), and I can&#8217;t justify breaking them out which would only seem to add more complexity with no practical advantage.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: VaNTa</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-1011</link>
		<dc:creator>VaNTa</dc:creator>
		<pubDate>Mon, 11 May 2009 14:04:24 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-1011</guid>
		<description>I also think that there are another words for DAO pattern. Misko, what You say? :) </description>
		<content:encoded><![CDATA[<p>I also think that there are another words for DAO pattern. Misko, what You say? <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-1002</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Sun, 10 May 2009 22:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-1002</guid>
		<description>@Toby,

The testability issues are universal. But lets look at ruby

1) Static methods are hard to override in Java, but easy in Ruby. Overriding the method is equivalent to changing global state since you have to clean up after-yourself. In ruby you have nice frameworks to do that for you, but it is global state any ways, which means you can never run things in parallel. So if ruby ever gets multithreaded you are in trouble. Also you can only override the method for everyone, while in instance method you con override it per instance. So for example if you have a chain of responsibility pattern you will have hard time testing it since you have just overridden the methods for everyone in the chain.
2) Static methods do not make it clear where the configuration is coming from which causes your API to lie to you: http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/
3) Static methods are not OO, they are procedural. There is nothing wrong with procedural, but if your language of choice is OO, why are you writing procedural code.

Just because ruby has figured out clever frameworks to test static methods, it does not make it a good idea.</description>
		<content:encoded><![CDATA[<p>@Toby,</p>
<p>The testability issues are universal. But lets look at ruby</p>
<p>1) Static methods are hard to override in Java, but easy in Ruby. Overriding the method is equivalent to changing global state since you have to clean up after-yourself. In ruby you have nice frameworks to do that for you, but it is global state any ways, which means you can never run things in parallel. So if ruby ever gets multithreaded you are in trouble. Also you can only override the method for everyone, while in instance method you con override it per instance. So for example if you have a chain of responsibility pattern you will have hard time testing it since you have just overridden the methods for everyone in the chain.<br />
2) Static methods do not make it clear where the configuration is coming from which causes your API to lie to you: <a href="http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/" rel="nofollow">http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/</a><br />
3) Static methods are not OO, they are procedural. There is nothing wrong with procedural, but if your language of choice is OO, why are you writing procedural code.</p>
<p>Just because ruby has figured out clever frameworks to test static methods, it does not make it a good idea.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Weekly Links #52 &#124; GrantPalin.com</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-1000</link>
		<dc:creator>Weekly Links #52 &#124; GrantPalin.com</dc:creator>
		<pubDate>Sun, 10 May 2009 21:10:08 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-1000</guid>
		<description>[...] The Problem With Active Record Interesting critique of the Active Record data access methodology. [...]</description>
		<content:encoded><![CDATA[<p>[...] The Problem With Active Record Interesting critique of the Active Record data access methodology. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Toby Hede</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-997</link>
		<dc:creator>Toby Hede</dc:creator>
		<pubDate>Sun, 10 May 2009 05:42:36 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-997</guid>
		<description>I am a bit puzzled about critiquing one language using another. Static methods on a class may create &quot;testability concerns in other languages, but I have never encountered any issue like this in Ruby/Rails.Using rspec you can mock methods (static and otherwise) within a single test case without effecting other&#160; tests in your system. There are other mocking frameworks in the Ruby space that behave similarly.Similarly with the configuration - Rails uses the YAML settings as a convention but it is not required for testing ActiveRecord. </description>
		<content:encoded><![CDATA[<p>I am a bit puzzled about critiquing one language using another. Static methods on a class may create &#8220;testability concerns in other languages, but I have never encountered any issue like this in Ruby/Rails.Using rspec you can mock methods (static and otherwise) within a single test case without effecting other&nbsp; tests in your system. There are other mocking frameworks in the Ruby space that behave similarly.Similarly with the configuration &#8211; Rails uses the YAML settings as a convention but it is not required for testing ActiveRecord.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-993</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Sat, 09 May 2009 22:43:18 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-993</guid>
		<description>@GR,

I think the point of the article was that any implementation which has static methods on the classes for persistence is a bad Idea. Those that separate it to two classes one for the table and one for row are much more testable.</description>
		<content:encoded><![CDATA[<p>@GR,</p>
<p>I think the point of the article was that any implementation which has static methods on the classes for persistence is a bad Idea. Those that separate it to two classes one for the table and one for row are much more testable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2009/05/05/the-problem-with-active-record/comment-page-1/#comment-992</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Sat, 09 May 2009 22:40:36 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=466#comment-992</guid>
		<description>@Igor,

If you want to have Save on Account class rather than Account Table, than the Accounts needs to have a reference to AccountsTable. This means that some of Account class fields will be persisted and some will not. This is mixing of concerns. Check out: http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/</description>
		<content:encoded><![CDATA[<p>@Igor,</p>
<p>If you want to have Save on Account class rather than Account Table, than the Accounts needs to have a reference to AccountsTable. This means that some of Account class fields will be persisted and some will not. This is mixing of concerns. Check out: <a href="http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/" rel="nofollow">http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
