<?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/"
	xmlns:series="http://unfoldingneurons.com/"
		>
<channel>
	<title>Comments on: Use Registry To Remember Objects (So You Don&#8217;t Have To)</title>
	<atom:link href="http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/</link>
	<description>The personal blog of Brandon Savage. Contains entries of a personal and professional nature focusing on PHP, Apple, LAMP, MySQL and Washington, DC.</description>
	<lastBuildDate>Fri, 03 Feb 2012 19:36:33 -0500</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Phillip Harrington</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-789</link>
		<dc:creator>Phillip Harrington</dc:creator>
		<pubDate>Fri, 31 Jul 2009 12:51:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-789</guid>
		<description>For multiple databases (like &#039;read&#039; and &#039;write&#039; in a replication situation) I wrote my own database class which is a &#039;multiple singleton&#039;.

It works just like a singleton, except instead of having one $instance variable, I have an array of $instances which is keyed by the name of the database connection I want.

My factory method gives me back the appropriate singleton instance like so:
$DB = DB::factory(&#039;write&#039;);

That way there are only database connections for as many names as I have open, which are usually only the two I mentioned:  &#039;read&#039; and &#039;write&#039;.

I manage the configurations in an array with names that match the connection names, &#039;read&#039; and &#039;write&#039; like so:

$database = array(
  &#039;read&#039; =&gt; array(
    &#039;host&#039; =&gt; &#039;localhost&#039;,
    &#039;user&#039; =&gt; &#039;web_db&#039;,
    &#039;pass&#039; =&gt; &#039;dog1cat&#039;,
    &#039;data&#039; =&gt; &#039;slave_database&#039;,
  ),
  &#039;write&#039; =&gt; array(
    // etc...
    &#039;data&#039; =&gt; &#039;master_database&#039;,
  ),
);</description>
		<content:encoded><![CDATA[<p>For multiple databases (like &#8216;read&#8217; and &#8216;write&#8217; in a replication situation) I wrote my own database class which is a &#8216;multiple singleton&#8217;.</p>
<p>It works just like a singleton, except instead of having one $instance variable, I have an array of $instances which is keyed by the name of the database connection I want.</p>
<p>My factory method gives me back the appropriate singleton instance like so:<br />
$DB = DB::factory(&#8216;write&#8217;);</p>
<p>That way there are only database connections for as many names as I have open, which are usually only the two I mentioned:  &#8216;read&#8217; and &#8216;write&#8217;.</p>
<p>I manage the configurations in an array with names that match the connection names, &#8216;read&#8217; and &#8216;write&#8217; like so:</p>
<p>$database = array(<br />
  &#8216;read&#8217; =&gt; array(<br />
    &#8216;host&#8217; =&gt; &#8216;localhost&#8217;,<br />
    &#8216;user&#8217; =&gt; &#8216;web_db&#8217;,<br />
    &#8216;pass&#8217; =&gt; &#8216;dog1cat&#8217;,<br />
    &#8216;data&#8217; =&gt; &#8216;slave_database&#8217;,<br />
  ),<br />
  &#8216;write&#8217; =&gt; array(<br />
    // etc&#8230;<br />
    &#8216;data&#8217; =&gt; &#8216;master_database&#8217;,<br />
  ),<br />
);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel O'Connor</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-784</link>
		<dc:creator>Daniel O'Connor</dc:creator>
		<pubDate>Fri, 31 Jul 2009 00:59:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-784</guid>
		<description>@Fake51 
&quot;Registry and Singleton are not bad design patterns, neither are they anti-patterns. People only think along those lines because they’re blind idealists – and if there’s one thing that should make alarm bells go off, it’s listening to fanatics.&quot;

Unfortunately, you haven&#039;t provided one scrap of evidence to support your assertation: that they are patterns that solve design problems with less drawbacks than the alternatives.



Perhaps it would do you well to look at the examples raised in this thread and the links to Misko Hervey&#039;s talks.

These are numerous examples grounded in real life experience about the negatives of using these patterns in your designs, and to date, there are very few drawbacks to dependency injection listed here.




The main reason we&#039;re so passionate about this is pretty simple: you, the author of the original code make a bunch of choices to use statics, global state, singletons, etc; and can&#039;t see the harm in it. 

We, the poor saps who have to unit test it, fix it, debug it, and maintain it get stuck with an impossible pile of code which refuses to play nicely with our expectations, breaks unexpectedly, and frustrates us.

That makes us cranky, and makes us think things like &quot;the person who implemented this design is being evil to us&quot;.</description>
		<content:encoded><![CDATA[<p>@Fake51<br />
&#8220;Registry and Singleton are not bad design patterns, neither are they anti-patterns. People only think along those lines because they’re blind idealists – and if there’s one thing that should make alarm bells go off, it’s listening to fanatics.&#8221;</p>
<p>Unfortunately, you haven&#8217;t provided one scrap of evidence to support your assertation: that they are patterns that solve design problems with less drawbacks than the alternatives.</p>
<p>Perhaps it would do you well to look at the examples raised in this thread and the links to Misko Hervey&#8217;s talks.</p>
<p>These are numerous examples grounded in real life experience about the negatives of using these patterns in your designs, and to date, there are very few drawbacks to dependency injection listed here.</p>
<p>The main reason we&#8217;re so passionate about this is pretty simple: you, the author of the original code make a bunch of choices to use statics, global state, singletons, etc; and can&#8217;t see the harm in it. </p>
<p>We, the poor saps who have to unit test it, fix it, debug it, and maintain it get stuck with an impossible pile of code which refuses to play nicely with our expectations, breaks unexpectedly, and frustrates us.</p>
<p>That makes us cranky, and makes us think things like &#8220;the person who implemented this design is being evil to us&#8221;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Fake51</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-770</link>
		<dc:creator>Fake51</dc:creator>
		<pubDate>Tue, 28 Jul 2009 10:39:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-770</guid>
		<description>Registry and Singleton are not bad design patterns, neither are they anti-patterns. People only think along those lines because they&#039;re blind idealists - and if there&#039;s one thing that should make alarm bells go off, it&#039;s listening to fanatics.
 You can abuse singletons and you can abuse the registry pattern ... just like you can abuse any other pattern. And that&#039;s what it comes down to.

To consider: you could very easily modify Brandon&#039;s code to make it &#039;write-once-read-forever&#039; - in order to allow for creating and storing a database connection once in the registry but not allow anything to overwrite this. Which obviously makes all the high-pitched cries about globals meaningless. In other words: use whichever design matches what you&#039;re doing - don&#039;t listen to people fervently arguing that something or other &#039;is evil&#039;.

Regards
Fake</description>
		<content:encoded><![CDATA[<p>Registry and Singleton are not bad design patterns, neither are they anti-patterns. People only think along those lines because they&#8217;re blind idealists &#8211; and if there&#8217;s one thing that should make alarm bells go off, it&#8217;s listening to fanatics.<br />
 You can abuse singletons and you can abuse the registry pattern &#8230; just like you can abuse any other pattern. And that&#8217;s what it comes down to.</p>
<p>To consider: you could very easily modify Brandon&#8217;s code to make it &#8216;write-once-read-forever&#8217; &#8211; in order to allow for creating and storing a database connection once in the registry but not allow anything to overwrite this. Which obviously makes all the high-pitched cries about globals meaningless. In other words: use whichever design matches what you&#8217;re doing &#8211; don&#8217;t listen to people fervently arguing that something or other &#8216;is evil&#8217;.</p>
<p>Regards<br />
Fake</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TheWorkingMan</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-753</link>
		<dc:creator>TheWorkingMan</dc:creator>
		<pubDate>Sun, 26 Jul 2009 17:34:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-753</guid>
		<description>I didn&#039;t know that using a singleton and registry seems like a bad coding practice. Basing on the discussions here, I can see the points of both sides. It&#039;s just sad that the use of these patterns are greatly influenced by how you will test your code. Testing is important but I think there should be a balance on the way you design the architecture of your code without regard if it&#039;s going to be easy to test or not. In that way, everyone is free to use whatever pattern there is that seems applicable.

I hope there are some sort of better solutions to make testing of singletons, registries, and similar patterns easier.</description>
		<content:encoded><![CDATA[<p>I didn&#8217;t know that using a singleton and registry seems like a bad coding practice. Basing on the discussions here, I can see the points of both sides. It&#8217;s just sad that the use of these patterns are greatly influenced by how you will test your code. Testing is important but I think there should be a balance on the way you design the architecture of your code without regard if it&#8217;s going to be easy to test or not. In that way, everyone is free to use whatever pattern there is that seems applicable.</p>
<p>I hope there are some sort of better solutions to make testing of singletons, registries, and similar patterns easier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fqqdk</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-749</link>
		<dc:creator>fqqdk</dc:creator>
		<pubDate>Sun, 26 Jul 2009 10:42:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-749</guid>
		<description>@Mastodont if you choose patterns you deem appropriate for the problem at hand, then I am happy for you. Other people will just copy+paste what they see on the net, and that&#039;s where lack of discussion, pros and contras and alternatives will become a problem.
Yes, I&#039;ve spent quite a lot of time browsing the ZF API, and I think the things you mention are problematic.</description>
		<content:encoded><![CDATA[<p>@Mastodont if you choose patterns you deem appropriate for the problem at hand, then I am happy for you. Other people will just copy+paste what they see on the net, and that&#8217;s where lack of discussion, pros and contras and alternatives will become a problem.<br />
Yes, I&#8217;ve spent quite a lot of time browsing the ZF API, and I think the things you mention are problematic.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CloCkWeRX</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-748</link>
		<dc:creator>CloCkWeRX</dc:creator>
		<pubDate>Sun, 26 Jul 2009 08:24:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-748</guid>
		<description>@mastodont
&quot;You can of course simulate roof without walls but in reality you need walls at first.&quot;

Why does the SMS code above need a database connection? It doesn&#039;t; and if the SMS code doesn&#039;t run because the database code is broken, that&#039;s annoying.

&quot;And the risk of omitting something important is high (I have an experience).&quot;

Perhaps, but you haven&#039;t given any code examples or scenarios to back your point of view, so...


&quot;By the way – Zend Framework, the flagship of PHP, is full of static methods, the Registry is also there. And some classes are singletons (e.g. Zend_Auth). The world is colorful.&quot;

Zend_Auth is annoying to test and code built using it is too.

// Why should this:
$auth = new Zend_Auth(); // Cause: Fatal error - private method __construct(); ?


// A typical example using a singleton, no injection
class GuardCow {
   /** Greets people it knows,  moos at strangers */
   function noise() {
      $auth = Zend_Auth::getInstance();

      return $auth-&gt;hasIdentity() ? &quot;Hi&quot; : &quot;moo&quot;;
   }
}

The above isn&#039;t easy to test if I want to make sure that $auth-&gt;hasIdentity() returns true or false or whatever I please.

How would you write the 2x unit test to assert the cow moos at strangers, and greets friends? 
 1. You have to open up the code in Zend_Auth, read it, and realise that you need to mock out the Storage layer.
 2. Create a mock
 3. In each test, load the auth object, set the desired result

function testShouldMooAtStrangers() {
    $auth = Zend_Auth::getInstance();
    $auth-&gt;setStorage(new MockStorage(false));

    $cow = new GuardCow();
    $this-&gt;assertSame(&quot;moo&quot;, $cow-&gt;noise());
}

function testShouldGreetFriends() {
    $auth = Zend_Auth::getInstance();
    $auth-&gt;setStorage(new MockStorage(true));

    $cow = new GuardCow();
    $this-&gt;assertSame(&quot;Hi&quot;, $cow-&gt;noise());
}

What happens if I forget the first two lines in each test case? Why do I need to mock out Auth Storage to say my guard cow doesn&#039;t know me? It&#039;s not obvious when I look at the GuardCow class that I have to think about storage at all.

However, if Zend_Auth wasn&#039;t a singleton, I could have just:

function testShouldMooAtStrangers() {
    $cow = new GuardCow(new MockAuth(false));
    $this-&gt;assertSame(&quot;Hi&quot;, $cow-&gt;noise());
}

function testShouldGreetFriends() {
    $cow = new GuardCow(new MockAuth(true));
    $this-&gt;assertSame(&quot;Hi&quot;, $cow-&gt;noise());
}

Less code, I don&#039;t need to read the internals of Zend_Auth, and I&#039;m doing what I care about - testing my guard cow.


So, I&#039;d love to see examples where statics, registry pattern or singletons both solve initial design problems and (this is the important one for me and many other DI fans) are still flexible enough when you want to unit test it that it&#039;s trivial / easy to do so, *and saves you time*.</description>
		<content:encoded><![CDATA[<p>@mastodont<br />
&#8220;You can of course simulate roof without walls but in reality you need walls at first.&#8221;</p>
<p>Why does the SMS code above need a database connection? It doesn&#8217;t; and if the SMS code doesn&#8217;t run because the database code is broken, that&#8217;s annoying.</p>
<p>&#8220;And the risk of omitting something important is high (I have an experience).&#8221;</p>
<p>Perhaps, but you haven&#8217;t given any code examples or scenarios to back your point of view, so&#8230;</p>
<p>&#8220;By the way – Zend Framework, the flagship of PHP, is full of static methods, the Registry is also there. And some classes are singletons (e.g. Zend_Auth). The world is colorful.&#8221;</p>
<p>Zend_Auth is annoying to test and code built using it is too.</p>
<p>// Why should this:<br />
$auth = new Zend_Auth(); // Cause: Fatal error &#8211; private method __construct(); ?</p>
<p>// A typical example using a singleton, no injection<br />
class GuardCow {<br />
   /** Greets people it knows,  moos at strangers */<br />
   function noise() {<br />
      $auth = Zend_Auth::getInstance();</p>
<p>      return $auth-&gt;hasIdentity() ? &#8220;Hi&#8221; : &#8220;moo&#8221;;<br />
   }<br />
}</p>
<p>The above isn&#8217;t easy to test if I want to make sure that $auth-&gt;hasIdentity() returns true or false or whatever I please.</p>
<p>How would you write the 2x unit test to assert the cow moos at strangers, and greets friends?<br />
 1. You have to open up the code in Zend_Auth, read it, and realise that you need to mock out the Storage layer.<br />
 2. Create a mock<br />
 3. In each test, load the auth object, set the desired result</p>
<p>function testShouldMooAtStrangers() {<br />
    $auth = Zend_Auth::getInstance();<br />
    $auth-&gt;setStorage(new MockStorage(false));</p>
<p>    $cow = new GuardCow();<br />
    $this-&gt;assertSame(&#8220;moo&#8221;, $cow-&gt;noise());<br />
}</p>
<p>function testShouldGreetFriends() {<br />
    $auth = Zend_Auth::getInstance();<br />
    $auth-&gt;setStorage(new MockStorage(true));</p>
<p>    $cow = new GuardCow();<br />
    $this-&gt;assertSame(&#8220;Hi&#8221;, $cow-&gt;noise());<br />
}</p>
<p>What happens if I forget the first two lines in each test case? Why do I need to mock out Auth Storage to say my guard cow doesn&#8217;t know me? It&#8217;s not obvious when I look at the GuardCow class that I have to think about storage at all.</p>
<p>However, if Zend_Auth wasn&#8217;t a singleton, I could have just:</p>
<p>function testShouldMooAtStrangers() {<br />
    $cow = new GuardCow(new MockAuth(false));<br />
    $this-&gt;assertSame(&#8220;Hi&#8221;, $cow-&gt;noise());<br />
}</p>
<p>function testShouldGreetFriends() {<br />
    $cow = new GuardCow(new MockAuth(true));<br />
    $this-&gt;assertSame(&#8220;Hi&#8221;, $cow-&gt;noise());<br />
}</p>
<p>Less code, I don&#8217;t need to read the internals of Zend_Auth, and I&#8217;m doing what I care about &#8211; testing my guard cow.</p>
<p>So, I&#8217;d love to see examples where statics, registry pattern or singletons both solve initial design problems and (this is the important one for me and many other DI fans) are still flexible enough when you want to unit test it that it&#8217;s trivial / easy to do so, *and saves you time*.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mastodont</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-747</link>
		<dc:creator>Mastodont</dc:creator>
		<pubDate>Sun, 26 Jul 2009 06:45:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-747</guid>
		<description>Herman, return value of calcAmountFinanced is completely independent on DB layer, so here is no problem with mocking. You can safely comment line with db-&gt;query. But what if return value is dependent? Why to bother yourself with writing more complex fake code and not to use directly DB? This way you are also pressed to deal with all “unavailable DB” issues.

@clockwerx: … I have to fix my problems connecting to the Database before …
Yes, you must. You can of course simulate roof without walls but in reality you need walls at first. No anger, no frustration. And the risk of omitting something important is high (I have an experience).

@fqqdk: I believe that programmer should use the best methods or patterns he can. I am big fan e.g. of stored procedures (evil). If analysis shows the singleton (evil) is applicable I can freely choose it. If I need more return values, I consider output parameters (evil). If total decoupling seems better, have no problems with it.

By the way – Zend Framework, the flagship of PHP, is full of static methods, the Registry is also there. And some classes are singletons (e.g. Zend_Auth). The world is colorful.</description>
		<content:encoded><![CDATA[<p>Herman, return value of calcAmountFinanced is completely independent on DB layer, so here is no problem with mocking. You can safely comment line with db-&gt;query. But what if return value is dependent? Why to bother yourself with writing more complex fake code and not to use directly DB? This way you are also pressed to deal with all “unavailable DB” issues.</p>
<p>@clockwerx: … I have to fix my problems connecting to the Database before …<br />
Yes, you must. You can of course simulate roof without walls but in reality you need walls at first. No anger, no frustration. And the risk of omitting something important is high (I have an experience).</p>
<p>@fqqdk: I believe that programmer should use the best methods or patterns he can. I am big fan e.g. of stored procedures (evil). If analysis shows the singleton (evil) is applicable I can freely choose it. If I need more return values, I consider output parameters (evil). If total decoupling seems better, have no problems with it.</p>
<p>By the way – Zend Framework, the flagship of PHP, is full of static methods, the Registry is also there. And some classes are singletons (e.g. Zend_Auth). The world is colorful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fqqdk</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-737</link>
		<dc:creator>fqqdk</dc:creator>
		<pubDate>Sat, 25 Jul 2009 11:48:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-737</guid>
		<description>(Amending the code for the sake of testing is bad. Why would i want to do that?)

Well I can&#039;t really argue with you now. I think it is better to always go for the most decomposed code with the least amount of coupling, and separating unit tests from integration tests gives me that. I can test the methods logic with the DB mocked out very quickly, and the actual integration in a separate suite of tests that won&#039;t run that frequently. That maximizes the speed of feedback, makes my code cleaner, with APIs that do not lie about the dependencies, and that makes me happy. You keep your logic and integration testing coupled, and have your classes depend implicitly on some god object.. erm.. registry, it&#039;s your call. But I still think it&#039;s wrong.</description>
		<content:encoded><![CDATA[<p>(Amending the code for the sake of testing is bad. Why would i want to do that?)</p>
<p>Well I can&#8217;t really argue with you now. I think it is better to always go for the most decomposed code with the least amount of coupling, and separating unit tests from integration tests gives me that. I can test the methods logic with the DB mocked out very quickly, and the actual integration in a separate suite of tests that won&#8217;t run that frequently. That maximizes the speed of feedback, makes my code cleaner, with APIs that do not lie about the dependencies, and that makes me happy. You keep your logic and integration testing coupled, and have your classes depend implicitly on some god object.. erm.. registry, it&#8217;s your call. But I still think it&#8217;s wrong.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CloCkWeRX</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-731</link>
		<dc:creator>CloCkWeRX</dc:creator>
		<pubDate>Sat, 25 Jul 2009 04:31:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-731</guid>
		<description>@Mastodont re the two examples, that&#039;s a great illustration of how registry can mask it.
They are two classes in the same application (and example 2 require_once&#039;s the first) - but it&#039;s not very obvious.

Re the ruling out through exceptions, imagine this sequence of events:


1. Build registry
2. Connect DB (exception thrown)
3. Connect SMS (test failed: exception in DB::connect)
4. Run DB test (test failed: exception in DB::connect)
5. Teardown (test failed: exception in DB::connect)
6. Build registry
7. Connect DB (exception thrown)
8. Connect SMS (test failed: exception in DB::connect)
9. Run SMS test (test failed: exception in DB::connect)
10. Teardown (test failed: exception in DB::connect)

I look at the SMS test first, and it&#039;s talking about failing when the database didn&#039;t connect. Huh? What? I look at the code for you and you don&#039;t even use a database!

Typically this results in anger, frustration, grumbling before.... Oh, it&#039;s some kind of outside influence! It&#039;s in the Registry building code, over here.

Suddenly, I have to fix my problems connecting to the Database before I can know if I&#039;ve even got problems with my SMS code.

Another example:
http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html

That post talks about singletons, but Registry::get() is similar - you have to set it up just right in all sorts of places which are non obvious, and can lead to nasty results and strange interactions for no clear reason.</description>
		<content:encoded><![CDATA[<p>@Mastodont re the two examples, that&#8217;s a great illustration of how registry can mask it.<br />
They are two classes in the same application (and example 2 require_once&#8217;s the first) &#8211; but it&#8217;s not very obvious.</p>
<p>Re the ruling out through exceptions, imagine this sequence of events:</p>
<p>1. Build registry<br />
2. Connect DB (exception thrown)<br />
3. Connect SMS (test failed: exception in DB::connect)<br />
4. Run DB test (test failed: exception in DB::connect)<br />
5. Teardown (test failed: exception in DB::connect)<br />
6. Build registry<br />
7. Connect DB (exception thrown)<br />
8. Connect SMS (test failed: exception in DB::connect)<br />
9. Run SMS test (test failed: exception in DB::connect)<br />
10. Teardown (test failed: exception in DB::connect)</p>
<p>I look at the SMS test first, and it&#8217;s talking about failing when the database didn&#8217;t connect. Huh? What? I look at the code for you and you don&#8217;t even use a database!</p>
<p>Typically this results in anger, frustration, grumbling before&#8230;. Oh, it&#8217;s some kind of outside influence! It&#8217;s in the Registry building code, over here.</p>
<p>Suddenly, I have to fix my problems connecting to the Database before I can know if I&#8217;ve even got problems with my SMS code.</p>
<p>Another example:<br />
<a href="http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html" rel="nofollow">http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-join-new-project.html</a></p>
<p>That post talks about singletons, but Registry::get() is similar &#8211; you have to set it up just right in all sorts of places which are non obvious, and can lead to nasty results and strange interactions for no clear reason.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Herman Radtke</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-718</link>
		<dc:creator>Herman Radtke</dc:creator>
		<pubDate>Fri, 24 Jul 2009 14:30:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-718</guid>
		<description>@Mastodont: The reason people dislike the Registry Pattern is because it makes unit testing hard.  The funamental idea of unit testing is that you are testing a single unit of functionality.  That means you want tests to be simple and concise.  Trying to use a real world database object to test functionality becomes very hard and can produce false positives.

Given some class that requires a database connection, mocking the database makes testing the business logic much easier:

class Finance {
    function __construct($db) { $this-&gt;db = $db; }

    function calcAmountFinanced($dealno, $term, $payment) {
        $amount = $term * $payment;
        
        // save amount in database
        $this-&gt;db-&gt;query( &quot;INSERT INTO deal (DealNum, Amount) VALUES ($dealno, $amount)&quot; );

        return $amount; // return to client so they can see amount
    }
}

class FakeDb {
    function query($sql) {
        return true;
    }
}

function testCalcAmountFinanced()
{
   $db = new FakeDb();

   $finance = new Finance($db);

   $amount = calcAmountFinanced(60, 150.00);
   if ($amount == 9000) {
      return true;
   }

   return false;
}

Now I can automate that test in a cronjob each night without managing a database.  This becomes even more useful when I test a particular method multiple times.  I don&#039;t have to reset the database information to do it.  This does not remove the requirement of integration testing with a live database, but it makes it easier for me to spot a problem earlier.</description>
		<content:encoded><![CDATA[<p>@Mastodont: The reason people dislike the Registry Pattern is because it makes unit testing hard.  The funamental idea of unit testing is that you are testing a single unit of functionality.  That means you want tests to be simple and concise.  Trying to use a real world database object to test functionality becomes very hard and can produce false positives.</p>
<p>Given some class that requires a database connection, mocking the database makes testing the business logic much easier:</p>
<p>class Finance {<br />
    function __construct($db) { $this-&gt;db = $db; }</p>
<p>    function calcAmountFinanced($dealno, $term, $payment) {<br />
        $amount = $term * $payment;</p>
<p>        // save amount in database<br />
        $this-&gt;db-&gt;query( &#8220;INSERT INTO deal (DealNum, Amount) VALUES ($dealno, $amount)&#8221; );</p>
<p>        return $amount; // return to client so they can see amount<br />
    }<br />
}</p>
<p>class FakeDb {<br />
    function query($sql) {<br />
        return true;<br />
    }<br />
}</p>
<p>function testCalcAmountFinanced()<br />
{<br />
   $db = new FakeDb();</p>
<p>   $finance = new Finance($db);</p>
<p>   $amount = calcAmountFinanced(60, 150.00);<br />
   if ($amount == 9000) {<br />
      return true;<br />
   }</p>
<p>   return false;<br />
}</p>
<p>Now I can automate that test in a cronjob each night without managing a database.  This becomes even more useful when I test a particular method multiple times.  I don&#8217;t have to reset the database information to do it.  This does not remove the requirement of integration testing with a live database, but it makes it easier for me to spot a problem earlier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mastodont</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-717</link>
		<dc:creator>Mastodont</dc:creator>
		<pubDate>Fri, 24 Jul 2009 13:37:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-717</guid>
		<description>Well, I do not understand why you mix DB amd SMS now, because originally you wrote it as two examples.
 
... can rule out the SMS component failing as a cause of your DB errors / vice versa; which is harder in scenario 1 ...
No way. We have e.g. exceptions.
class A {
function b() {
  try {
    $db = Registry::get(’db’);
    ....
  } catch ....
}</description>
		<content:encoded><![CDATA[<p>Well, I do not understand why you mix DB amd SMS now, because originally you wrote it as two examples.</p>
<p>&#8230; can rule out the SMS component failing as a cause of your DB errors / vice versa; which is harder in scenario 1 &#8230;<br />
No way. We have e.g. exceptions.<br />
class A {<br />
function b() {<br />
  try {<br />
    $db = Registry::get(’db’);<br />
    &#8230;.<br />
  } catch &#8230;.<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CloCkWeRX</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-714</link>
		<dc:creator>CloCkWeRX</dc:creator>
		<pubDate>Fri, 24 Jul 2009 11:15:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-714</guid>
		<description>@Mastodont - scenario 1:

 1. Build registry
 2. Connect DB
 3. Connect SMS
 4. Run DB test
 5. Teardown
 6. Build registry
 7. Connect DB
 8. Connect SMS
 9. Run SMS test
 10. Teardown

Scenario 2 (injecting only what you need):
 1. Connect DB
 2. Run DB test
 3. Teardown
 4. Connect SMS
 5. Run SMS test
 6. Teardown

Additionally, by controlling your environment tightly, you can rule out the SMS component failing as a cause of your DB errors / vice versa; which is harder in scenario 1</description>
		<content:encoded><![CDATA[<p>@Mastodont &#8211; scenario 1:</p>
<p> 1. Build registry<br />
 2. Connect DB<br />
 3. Connect SMS<br />
 4. Run DB test<br />
 5. Teardown<br />
 6. Build registry<br />
 7. Connect DB<br />
 8. Connect SMS<br />
 9. Run SMS test<br />
 10. Teardown</p>
<p>Scenario 2 (injecting only what you need):<br />
 1. Connect DB<br />
 2. Run DB test<br />
 3. Teardown<br />
 4. Connect SMS<br />
 5. Run SMS test<br />
 6. Teardown</p>
<p>Additionally, by controlling your environment tightly, you can rule out the SMS component failing as a cause of your DB errors / vice versa; which is harder in scenario 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mastodont</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-713</link>
		<dc:creator>Mastodont</dc:creator>
		<pubDate>Fri, 24 Jul 2009 11:08:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-713</guid>
		<description>@fqqdk: Maybe it surprises you, but I never mock DB connection. I am used to test real objects in &quot;real&quot; environment - and test DB server is a obvious requisite. But if you really want it, you can of course amend the code in registry class.
 
@clockwerx: ... you have to build a registry and fill it with both SMS and DB instances ... But to test your first method versions, you must pass existing instances as arguments. Is there any difference in term of speeed?</description>
		<content:encoded><![CDATA[<p>@fqqdk: Maybe it surprises you, but I never mock DB connection. I am used to test real objects in &#8220;real&#8221; environment &#8211; and test DB server is a obvious requisite. But if you really want it, you can of course amend the code in registry class.</p>
<p>@clockwerx: &#8230; you have to build a registry and fill it with both SMS and DB instances &#8230; But to test your first method versions, you must pass existing instances as arguments. Is there any difference in term of speeed?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brandon Savage</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-705</link>
		<dc:creator>Brandon Savage</dc:creator>
		<pubDate>Thu, 23 Jul 2009 23:43:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-705</guid>
		<description>I appreciate the discussion. I still think this is a matter of taste, and that dependency injection is a vital technique as well. Thanks those of you who have contributed.</description>
		<content:encoded><![CDATA[<p>I appreciate the discussion. I still think this is a matter of taste, and that dependency injection is a vital technique as well. Thanks those of you who have contributed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ivan Jovanovic</title>
		<link>http://www.brandonsavage.net/use-registry-to-remember-objects-so-you-dont-have-to/#comment-704</link>
		<dc:creator>Ivan Jovanovic</dc:creator>
		<pubDate>Thu, 23 Jul 2009 14:36:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.brandonsavage.net/?p=480#comment-704</guid>
		<description>@Diogo 

I agree with you about the problems with this design. Even if someone use Registry pattern it shouldn&#039;t be used within the class but prior to wiring the objects to pull the needed objects from the global repository.</description>
		<content:encoded><![CDATA[<p>@Diogo </p>
<p>I agree with you about the problems with this design. Even if someone use Registry pattern it shouldn&#8217;t be used within the class but prior to wiring the objects to pull the needed objects from the global repository.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 4/11 queries in 0.004 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: files.brandonsavage.net.s3.amazonaws.com

Served from: www.brandonsavage.net @ 2012-02-07 05:10:04 -->
