<?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: Dependency Injection in Solar</title>
	<atom:link href="http://paul-m-jones.com/archives/218/feed" rel="self" type="application/rss+xml" />
	<link>http://paul-m-jones.com/archives/218?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=dependency-injection-in-solar</link>
	<description>It&#039;s not enough to be smart; you have to actually know things.</description>
	<lastBuildDate>Wed, 16 May 2012 17:27:27 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
	<item>
		<title>By: Dan Ports</title>
		<link>http://paul-m-jones.com/archives/218/comment-page-1#comment-226575</link>
		<dc:creator>Dan Ports</dc:creator>
		<pubDate>Sun, 16 Dec 2007 00:13:30 +0000</pubDate>
		<guid isPermaLink="false">http://paul-m-jones.com/blog/?p=218#comment-226575</guid>
		<description>Hans is on point -- loading a DI config for every page load would incur significant overhead. Other frameworks (I am thinking especially of MonoRail for ASP.NET, since that&#039;s what I&#039;m using at the moment) avoid this by loading the config on application startup and stashing it in some application-scoped storage. But I don&#039;t see any easy way to do this with PHP, since it lacks the concept of an application, AFAIK. Perhaps you could cache the config by doing code gen. DI seems to be a prerequisite for any serious TDD effort.

Re: the ImageTransformer example -- most DI tools wouldn&#039;t instantiate an object unless it was actually required as a transitive dependency, so this seems like a moot point. I think the real performance issue would be loading the DI config; i.e. how do you actually build an object of class XYZ.

All this said, I applaud Solar for including at least rudimentary DI support. I haven&#039;t seen much of this in PHP frameworks, and it&#039;s a shame that few of them consider testability a worthy goal.</description>
		<content:encoded><![CDATA[<p>Hans is on point &#8212; loading a DI config for every page load would incur significant overhead. Other frameworks (I am thinking especially of MonoRail for ASP.NET, since that&#8217;s what I&#8217;m using at the moment) avoid this by loading the config on application startup and stashing it in some application-scoped storage. But I don&#8217;t see any easy way to do this with PHP, since it lacks the concept of an application, AFAIK. Perhaps you could cache the config by doing code gen. DI seems to be a prerequisite for any serious TDD effort.</p>
<p>Re: the ImageTransformer example &#8212; most DI tools wouldn&#8217;t instantiate an object unless it was actually required as a transitive dependency, so this seems like a moot point. I think the real performance issue would be loading the DI config; i.e. how do you actually build an object of class XYZ.</p>
<p>All this said, I applaud Solar for including at least rudimentary DI support. I haven&#8217;t seen much of this in PHP frameworks, and it&#8217;s a shame that few of them consider testability a worthy goal.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans</title>
		<link>http://paul-m-jones.com/archives/218/comment-page-1#comment-47939</link>
		<dc:creator>Hans</dc:creator>
		<pubDate>Tue, 27 Jun 2006 15:29:14 +0000</pubDate>
		<guid isPermaLink="false">http://paul-m-jones.com/blog/?p=218#comment-47939</guid>
		<description>Yeah, I think perhaps that the alternative term &quot;Inversion of Control&quot; (which IIRC Fowler doesn&#039;t like) illustrates a little better the difference between DI and Service Locator.  With DI frameworks (that I&#039;ve used in Java, anyway), the important difference is that the classes do not have any links/knowledge of  any external registry (i.e. service locator).  Everything is just a very simple object and the &quot;dependencies&quot; are set (injected) in by an external agent.  This gives you lots &amp; lots of control over both what is instantaited (i.e. which particular classes and what params they are passed) and how those classes are instantiated (e.g. singleton or new instance for every object).  Another feature of a centralized DI/IoC framework is that you have a single place where AOP cutpoints, advice, etc. can be defined for your objects.  A DI solution almost has a language-level feel about it (indeed, it feels like a bit of hack to me with frameworks like Spring, since one is essentially writing application code in XML).

I still don&#039;t see how a true DI solution would work in PHP, though (as I mention on Jeff&#039;s blog).  To really wire everything up would add an unnacceptable level of overhead to my PHP scripts -- e.g. my framework may require that certain objects get wired with a desired ImageTransformer driver, but 90% of my page requests don&#039;t need an ImageTransformer at all.

This appears to me to be a problem that is inherent in trying to invert control for an environment like PHP.  Because PHP environments must (generally) be recreated for every request, there is a good reason to keep control of the object setup on-demand within the service classes themselves -- i.e. with PHP there is a good reason not to invert control. 

... Now, I suppose that this wouldn&#039;t be the case if the IoC layer could somehow magically wire-on-demand.  But any implementation I can think for that just starts to look like a service locator.

-H</description>
		<content:encoded><![CDATA[<p>Yeah, I think perhaps that the alternative term &#8220;Inversion of Control&#8221; (which IIRC Fowler doesn&#8217;t like) illustrates a little better the difference between DI and Service Locator.  With DI frameworks (that I&#8217;ve used in Java, anyway), the important difference is that the classes do not have any links/knowledge of  any external registry (i.e. service locator).  Everything is just a very simple object and the &#8220;dependencies&#8221; are set (injected) in by an external agent.  This gives you lots &amp; lots of control over both what is instantaited (i.e. which particular classes and what params they are passed) and how those classes are instantiated (e.g. singleton or new instance for every object).  Another feature of a centralized DI/IoC framework is that you have a single place where AOP cutpoints, advice, etc. can be defined for your objects.  A DI solution almost has a language-level feel about it (indeed, it feels like a bit of hack to me with frameworks like Spring, since one is essentially writing application code in XML).</p>
<p>I still don&#8217;t see how a true DI solution would work in PHP, though (as I mention on Jeff&#8217;s blog).  To really wire everything up would add an unnacceptable level of overhead to my PHP scripts &#8212; e.g. my framework may require that certain objects get wired with a desired ImageTransformer driver, but 90% of my page requests don&#8217;t need an ImageTransformer at all.</p>
<p>This appears to me to be a problem that is inherent in trying to invert control for an environment like PHP.  Because PHP environments must (generally) be recreated for every request, there is a good reason to keep control of the object setup on-demand within the service classes themselves &#8212; i.e. with PHP there is a good reason not to invert control. </p>
<p>&#8230; Now, I suppose that this wouldn&#8217;t be the case if the IoC layer could somehow magically wire-on-demand.  But any implementation I can think for that just starts to look like a service locator.</p>
<p>-H</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anon.</title>
		<link>http://paul-m-jones.com/archives/218/comment-page-1#comment-47914</link>
		<dc:creator>Anon.</dc:creator>
		<pubDate>Tue, 27 Jun 2006 14:34:06 +0000</pubDate>
		<guid isPermaLink="false">http://paul-m-jones.com/blog/?p=218#comment-47914</guid>
		<description>i would agree with you though there are many implementations of DI; The relationship between a Service Locator and true Dependency Injection is a close one.</description>
		<content:encoded><![CDATA[<p>i would agree with you though there are many implementations of DI; The relationship between a Service Locator and true Dependency Injection is a close one.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pmjones</title>
		<link>http://paul-m-jones.com/archives/218/comment-page-1#comment-47913</link>
		<dc:creator>pmjones</dc:creator>
		<pubDate>Tue, 27 Jun 2006 14:24:21 +0000</pubDate>
		<guid isPermaLink="false">http://paul-m-jones.com/blog/?p=218#comment-47913</guid>
		<description>Perhaps I have missed the point of DI then; I thought of Solar::dependency() as the manager.  It checks if the passed objects are of the proper class, etc., and the User object just calls Solar::dependency().

Perhaps it is a Service Locator after all; Fowler notes they are very similar, which would explain my selection of the wrong term.

http://www.martinfowler.com/articles/injection.html#DecidingWhichOptionToUse</description>
		<content:encoded><![CDATA[<p>Perhaps I have missed the point of DI then; I thought of Solar::dependency() as the manager.  It checks if the passed objects are of the proper class, etc., and the User object just calls Solar::dependency().</p>
<p>Perhaps it is a Service Locator after all; Fowler notes they are very similar, which would explain my selection of the wrong term.</p>
<p><a href="http://www.martinfowler.com/articles/injection.html#DecidingWhichOptionToUse" rel="nofollow">http://www.martinfowler.com/articles/injection.html#DecidingWhichOptionToUse</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Travis Swicegood</title>
		<link>http://paul-m-jones.com/archives/218/comment-page-1#comment-47912</link>
		<dc:creator>Travis Swicegood</dc:creator>
		<pubDate>Tue, 27 Jun 2006 14:11:36 +0000</pubDate>
		<guid isPermaLink="false">http://paul-m-jones.com/blog/?p=218#comment-47912</guid>
		<description>Technically, Paul, this don&#039;t count as a full dependency injection.  With dependency injection, your Solar_User object&#039;s construct would look like:

Solar_User::__construct(Solar_Auth $auth, Solar_Role, $role, Solar_Access $access);

With that, you would request a User object (i.e., $dependencyManager::object(&#039;Solar_User&#039;)) and the manager would take care of locating the dependencies, not the User object.  The idea is that the object, other than offerring what it needs, has no clue on how to go about getting them.  As soon as you add in the code within Solar_User, it moves from true DI to another very similiar pattern in outcome.  Service Locator sounds right, but I&#039;m not sure.

Of course, for true DI, you would have to either re-think your implementation of Solar (i.e., no single $config parameter) or have some way to define what&#039;s expected by an object (i.e., Solar_User::expectedParameters() : array()).</description>
		<content:encoded><![CDATA[<p>Technically, Paul, this don&#8217;t count as a full dependency injection.  With dependency injection, your Solar_User object&#8217;s construct would look like:</p>
<p>Solar_User::__construct(Solar_Auth $auth, Solar_Role, $role, Solar_Access $access);</p>
<p>With that, you would request a User object (i.e., $dependencyManager::object(&#8216;Solar_User&#8217;)) and the manager would take care of locating the dependencies, not the User object.  The idea is that the object, other than offerring what it needs, has no clue on how to go about getting them.  As soon as you add in the code within Solar_User, it moves from true DI to another very similiar pattern in outcome.  Service Locator sounds right, but I&#8217;m not sure.</p>
<p>Of course, for true DI, you would have to either re-think your implementation of Solar (i.e., no single $config parameter) or have some way to define what&#8217;s expected by an object (i.e., Solar_User::expectedParameters() : array()).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pmjones</title>
		<link>http://paul-m-jones.com/archives/218/comment-page-1#comment-47911</link>
		<dc:creator>pmjones</dc:creator>
		<pubDate>Tue, 27 Jun 2006 13:58:42 +0000</pubDate>
		<guid isPermaLink="false">http://paul-m-jones.com/blog/?p=218#comment-47911</guid>
		<description>Hi Hans,

Here&#039;s an example from the User object (see the __construct() method) --

http://solarphp.com/svn/trunk/Solar/User.php

The internal auth, access, and role objects are set up via the configuration params; as long as the passed objects (or object parameters) are descended from the proper class, the dependency is accepted for internal use.

If you send a Mock_Solar_Auth object descended from Solar_Auth, it&#039;ll work without changes to the User class.  If you send a string that represents a Mock_Solar_Auth object in the registry, that&#039;ll work too.  Finally, if you decide to use a Solar_Auth_Adapter_Mock adapter in the facade Solar_Auth class, but want the User object to instantiate that object internally, that will work as well.

With respect to storing objects in the registry via dependency injection: no, you register an actual created object and give it an identifying key string.  Registry objects can lazy-load themselves, though, if you pass a class name and configuration array instead of an object; this will cause the object to be instantiated on first request from the registry.

I hope this begins to answer your question; please let me know if it does not.</description>
		<content:encoded><![CDATA[<p>Hi Hans,</p>
<p>Here&#8217;s an example from the User object (see the __construct() method) &#8211;</p>
<p><a href="http://solarphp.com/svn/trunk/Solar/User.php" rel="nofollow">http://solarphp.com/svn/trunk/Solar/User.php</a></p>
<p>The internal auth, access, and role objects are set up via the configuration params; as long as the passed objects (or object parameters) are descended from the proper class, the dependency is accepted for internal use.</p>
<p>If you send a Mock_Solar_Auth object descended from Solar_Auth, it&#8217;ll work without changes to the User class.  If you send a string that represents a Mock_Solar_Auth object in the registry, that&#8217;ll work too.  Finally, if you decide to use a Solar_Auth_Adapter_Mock adapter in the facade Solar_Auth class, but want the User object to instantiate that object internally, that will work as well.</p>
<p>With respect to storing objects in the registry via dependency injection: no, you register an actual created object and give it an identifying key string.  Registry objects can lazy-load themselves, though, if you pass a class name and configuration array instead of an object; this will cause the object to be instantiated on first request from the registry.</p>
<p>I hope this begins to answer your question; please let me know if it does not.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hans</title>
		<link>http://paul-m-jones.com/archives/218/comment-page-1#comment-47908</link>
		<dc:creator>Hans</dc:creator>
		<pubDate>Tue, 27 Jun 2006 13:41:30 +0000</pubDate>
		<guid isPermaLink="false">http://paul-m-jones.com/blog/?p=218#comment-47908</guid>
		<description>Can you show an example of how you wire up your objects using this method?  Are objects wired into the registry using DI?  I assume they are, otherwise this sounds more like a service locator to me.

Maybe an example would make this clear:  Say I&#039;m using the Solar framework and I want to swap out all my &quot;real&quot; drivers for &quot;mock&quot; versions (e.g. my SMTPMailSender gets swapped out for MockMailSender).  What file(s) would I need to change to make this happen?

-H</description>
		<content:encoded><![CDATA[<p>Can you show an example of how you wire up your objects using this method?  Are objects wired into the registry using DI?  I assume they are, otherwise this sounds more like a service locator to me.</p>
<p>Maybe an example would make this clear:  Say I&#8217;m using the Solar framework and I want to swap out all my &#8220;real&#8221; drivers for &#8220;mock&#8221; versions (e.g. my SMTPMailSender gets swapped out for MockMailSender).  What file(s) would I need to change to make this happen?</p>
<p>-H</p>
]]></content:encoded>
	</item>
</channel>
</rss>

