<?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: Pass Around Ginormous Context Objects</title>
	<atom:link href="http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/</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: Enrico M.</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-639</link>
		<dc:creator>Enrico M.</dc:creator>
		<pubDate>Sat, 31 Jan 2009 19:09:55 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-639</guid>
		<description>You just made my day! This is exactly the info I needed. Thank you for your fast and helpful responses.Enrico</description>
		<content:encoded><![CDATA[<p>You just made my day! This is exactly the info I needed. Thank you for your fast and helpful responses.Enrico</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-638</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Sat, 31 Jan 2009 19:05:10 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-638</guid>
		<description>Hi Enrico,&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Check out this example:&lt;/div&gt;&lt;div&gt;http://code.google.com/p/unit-test-teaching-examples/source/browse/trunk/src/di/webserver/GuiceDI.java&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;It is only half finished, but it has the idea. I do the flag parsing in the GUICE module and than i bind the configuration to the primitives in the module itself.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;if you are doing manual DI than I would parse the flags into some configuration object and than pass the configuration object to the Factory. The factory would than extract the data from the configuration object and injected into the constructor which needed it.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;-- Misko&lt;/div&gt;</description>
		<content:encoded><![CDATA[<p>Hi Enrico,
<div></div>
<div>Check out this example:</div>
<div><a href="http://code.google.com/p/unit-test-teaching-examples/source/browse/trunk/src/di/webserver/GuiceDI.java" rel="nofollow">http://code.google.com/p/unit-test-teaching-examples/source/browse/trunk/src/di/webserver/GuiceDI.java</a></div>
<div></div>
<div>It is only half finished, but it has the idea. I do the flag parsing in the GUICE module and than i bind the configuration to the primitives in the module itself.</div>
<div></div>
<div>if you are doing manual DI than I would parse the flags into some configuration object and than pass the configuration object to the Factory. The factory would than extract the data from the configuration object and injected into the constructor which needed it.</div>
<div></div>
<div>&#8211; Misko</div>
]]></content:encoded>
	</item>
	<item>
		<title>By: Enrico M.</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-637</link>
		<dc:creator>Enrico M.</dc:creator>
		<pubDate>Sat, 31 Jan 2009 18:54:46 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-637</guid>
		<description>Wow, that was quick. I&#039;m not sure if you replied to my post because you addressed it to Artem, but I assume so ;)

I understand that you could do a binding like &quot;bind(int.class).annotatedWith(name(”httpPort”).to(8080);
and with manual DI you could simply hard-code the port number. But the question that remains is:

When you have a configuration file for specifying the port - where in
your application do you read the configuration file (needs to be done
before binding).

Could you please clarify this. Thank you!!!</description>
		<content:encoded><![CDATA[<p>Wow, that was quick. I&#8217;m not sure if you replied to my post because you addressed it to Artem, but I assume so <img src='http://misko.hevery.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I understand that you could do a binding like &#8220;bind(int.class).annotatedWith(name(”httpPort”).to(8080);<br />
and with manual DI you could simply hard-code the port number. But the question that remains is:</p>
<p>When you have a configuration file for specifying the port &#8211; where in<br />
your application do you read the configuration file (needs to be done<br />
before binding).</p>
<p>Could you please clarify this. Thank you!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-636</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Sat, 31 Jan 2009 18:39:10 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-636</guid>
		<description>@ Artem,

You are very quick! You are right

WebServer (Configuration config) {
  this.port = config.getPort();
}

This breaks the law of demeter and I would never do this. However this works just fine.

WebServer (int Port) {
  this.port = port;
}

But as you correctly pointed out now we are mixing service and value objects. The reason this is a problem is that DI frameworks will not know how to inject &#039;int&#039; as there are a lot of ints to choose from. But if you are doing manual DI this is not a problem. So to make DI frameworks happy we need one more step.

WebServer (@named(&quot;httpPort&quot;) int Port) {
  this.port = port;
}

By adding the annotation to the parameter you can now write this in your GUICE module

bind(int.class).annatatedWith(name(&quot;httpPort&quot;).to(8080);

and everyone is happy.</description>
		<content:encoded><![CDATA[<p>@ Artem,</p>
<p>You are very quick! You are right</p>
<p>WebServer (Configuration config) {<br />
  this.port = config.getPort();<br />
}</p>
<p>This breaks the law of demeter and I would never do this. However this works just fine.</p>
<p>WebServer (int Port) {<br />
  this.port = port;<br />
}</p>
<p>But as you correctly pointed out now we are mixing service and value objects. The reason this is a problem is that DI frameworks will not know how to inject &#8216;int&#8217; as there are a lot of ints to choose from. But if you are doing manual DI this is not a problem. So to make DI frameworks happy we need one more step.</p>
<p>WebServer (@named(&#8221;httpPort&#8221;) int Port) {<br />
  this.port = port;<br />
}</p>
<p>By adding the annotation to the parameter you can now write this in your GUICE module</p>
<p>bind(int.class).annatatedWith(name(&#8221;httpPort&#8221;).to(8080);</p>
<p>and everyone is happy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Enrico M.</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-635</link>
		<dc:creator>Enrico M.</dc:creator>
		<pubDate>Sat, 31 Jan 2009 18:28:25 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-635</guid>
		<description>Hi Misko,first of all I have to say thank you. With your blog posts about DI you completely changed my view on object creation / instantiation (which also implies I&#039;m new to the topic).It&#039;s been a while since your last response to that post, but I hope you are still reading this. The thing is, even after thoroughly considering all your posts about DI, I still cannot see a solution to Artem&#039;s question.The question it comes down to for me is: how could you support a user-configured port for the web-server. Let&#039;s say we have a configuration file for configuring the port for the web server. Sticking to the rule that in the creation-phase only object creation should be done, there&#039;s no way to read the configuration to determine the port. Even a DI framework could never provide this information, as it cannot be bound to an injectable without reading the configuration file before.So how to solve this? The solution I came up with is introducing another step in my main method, i.e. reading the configuration before creation phase and requiring the configuration object in the constructor of the application factory. This is somewhat bad as I now have a &quot;new Properties()&quot; in my main method. However, imho, this does not reduce testability, as each object still declares it&#039;s dependencies. So, besides the factory now calls ... = new WebServer(config.get(&quot;port&quot;));you can still instantiate the WebServer for a test like this:... = new WebServer([some int]);I&#039;m still not contented with this version. How would you solve this issue, Misko? Please note I&#039;m refraining from using a DI framework because the middleware I&#039;m developing is not too complex and other developers need to understand the code at first sight. Thank you!Enrico</description>
		<content:encoded><![CDATA[<p>Hi Misko,first of all I have to say thank you. With your blog posts about DI you completely changed my view on object creation / instantiation (which also implies I&#8217;m new to the topic).It&#8217;s been a while since your last response to that post, but I hope you are still reading this. The thing is, even after thoroughly considering all your posts about DI, I still cannot see a solution to Artem&#8217;s question.The question it comes down to for me is: how could you support a user-configured port for the web-server. Let&#8217;s say we have a configuration file for configuring the port for the web server. Sticking to the rule that in the creation-phase only object creation should be done, there&#8217;s no way to read the configuration to determine the port. Even a DI framework could never provide this information, as it cannot be bound to an injectable without reading the configuration file before.So how to solve this? The solution I came up with is introducing another step in my main method, i.e. reading the configuration before creation phase and requiring the configuration object in the constructor of the application factory. This is somewhat bad as I now have a &#8220;new Properties()&#8221; in my main method. However, imho, this does not reduce testability, as each object still declares it&#8217;s dependencies. So, besides the factory now calls &#8230; = new WebServer(config.get(&#8221;port&#8221;));you can still instantiate the WebServer for a test like this:&#8230; = new WebServer([some int]);I&#8217;m still not contented with this version. How would you solve this issue, Misko? Please note I&#8217;m refraining from using a DI framework because the middleware I&#8217;m developing is not too complex and other developers need to understand the code at first sight. Thank you!Enrico</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-340</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Thu, 13 Nov 2008 19:04:30 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-340</guid>
		<description>Port number is injectable. You can very well make a binding for it and ask for it in constructor.  I think I should clarify this more in future posts. To put it another way if you ask for a port in constructor there is never an ambiguity about what you are looking for. There is only one port.

to contrast this with something like CreditCard. That is not really injectable since in a system you can have a lot of credit cards and the number of them is dynamic at run-time.

-- Misko</description>
		<content:encoded><![CDATA[<p>Port number is injectable. You can very well make a binding for it and ask for it in constructor.  I think I should clarify this more in future posts. To put it another way if you ask for a port in constructor there is never an ambiguity about what you are looking for. There is only one port.</p>
<p>to contrast this with something like CreditCard. That is not really injectable since in a system you can have a lot of credit cards and the number of them is dynamic at run-time.</p>
<p>&#8211; Misko</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artem B</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-339</link>
		<dc:creator>Artem B</dc:creator>
		<pubDate>Thu, 13 Nov 2008 18:46:42 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-339</guid>
		<description>Misko,

I have read the post you recommended and it says:

&quot;However, Injectable can never ask for a non-Injectable (Newable) in its constructor.&quot;

As far as I understand, the port is NOT an injectable, but the WebServer is. The problem I have is exactly the problem you mention here: &quot;when you ask for things which Dependency Injection Frameworks can not provide&quot;. What do I do then?

thanx, Artem.</description>
		<content:encoded><![CDATA[<p>Misko,</p>
<p>I have read the post you recommended and it says:</p>
<p>&#8220;However, Injectable can never ask for a non-Injectable (Newable) in its constructor.&#8221;</p>
<p>As far as I understand, the port is NOT an injectable, but the WebServer is. The problem I have is exactly the problem you mention here: &#8220;when you ask for things which Dependency Injection Frameworks can not provide&#8221;. What do I do then?</p>
<p>thanx, Artem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: misko</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-338</link>
		<dc:creator>misko</dc:creator>
		<pubDate>Thu, 13 Nov 2008 18:06:08 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-338</guid>
		<description>Hi Artem,

Excellent question. Have a look at: http://misko.hevery.com/2008/09/30/to-new-or-not-to-new/ The key question is can a Dependency Injection Framework such as GUICE know what do you mean when you ask for @Port int portNumber. And the answer is yes, there is not ambiguity. (You need the annotation since there are lot of ints but only one @Port int)

So the prfered way is to ask for the port number in the constructor. If you think about it it makes perfect sense. In order to instantiate a WebServer you need to know the port number. So ask for it in constructor.

I think the trouble you can ge into is when you ask for things which Dependency Injection Frameworks can not provide. Such as a Song. Well there are houndreds of Songs in the system, which one do you want? And annotation does not help since the Song list is dynamic.

-- Misko</description>
		<content:encoded><![CDATA[<p>Hi Artem,</p>
<p>Excellent question. Have a look at: <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> The key question is can a Dependency Injection Framework such as GUICE know what do you mean when you ask for @Port int portNumber. And the answer is yes, there is not ambiguity. (You need the annotation since there are lot of ints but only one @Port int)</p>
<p>So the prfered way is to ask for the port number in the constructor. If you think about it it makes perfect sense. In order to instantiate a WebServer you need to know the port number. So ask for it in constructor.</p>
<p>I think the trouble you can ge into is when you ask for things which Dependency Injection Frameworks can not provide. Such as a Song. Well there are houndreds of Songs in the system, which one do you want? And annotation does not help since the Song list is dynamic.</p>
<p>&#8211; Misko</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Artem B.</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-337</link>
		<dc:creator>Artem B.</dc:creator>
		<pubDate>Thu, 13 Nov 2008 06:25:43 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-337</guid>
		<description>This is the point where I don&#039;t get it.

Assume that you have a WebServer class that controls a web server instance. It has a constructer like

WebServer (Configuration config) {
    this.port = config.getPort();
}

This breaks the law of Demeter and I want to refator it. But hey, I can&#039;t move the port to the constructor. This way I will be mixing the service-objects with value objects. And my application factory will have to know how to get the port configuration out of a config file which means doing work in the factory. 

Another way to refactor it is to move the configuration options to the start() method like this.

// This is a test code, not a production code
WebServer server = new WebServer();
server.start(8080);

But this obligates the callers to know what port they need to start the server on, which again breaks the law of Demeter, because they shouldn&#039;t need any configuration options they do not directly use.

Please, advice me on a way to refactor the code with Dependency Injection and the Law of Demeter in mind.</description>
		<content:encoded><![CDATA[<p>This is the point where I don&#8217;t get it.</p>
<p>Assume that you have a WebServer class that controls a web server instance. It has a constructer like</p>
<p>WebServer (Configuration config) {<br />
    this.port = config.getPort();<br />
}</p>
<p>This breaks the law of Demeter and I want to refator it. But hey, I can&#8217;t move the port to the constructor. This way I will be mixing the service-objects with value objects. And my application factory will have to know how to get the port configuration out of a config file which means doing work in the factory. </p>
<p>Another way to refactor it is to move the configuration options to the start() method like this.</p>
<p>// This is a test code, not a production code<br />
WebServer server = new WebServer();<br />
server.start(8080);</p>
<p>But this obligates the callers to know what port they need to start the server on, which again breaks the law of Demeter, because they shouldn&#8217;t need any configuration options they do not directly use.</p>
<p>Please, advice me on a way to refactor the code with Dependency Injection and the Law of Demeter in mind.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gerard</title>
		<link>http://misko.hevery.com/2008/10/27/pass-around-ginormous-context-objects/comment-page-1/#comment-310</link>
		<dc:creator>Gerard</dc:creator>
		<pubDate>Tue, 28 Oct 2008 12:40:01 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=272#comment-310</guid>
		<description>Perhaps you could quickly write what the FileCopier factory might look like in this case, and how you might use it.</description>
		<content:encoded><![CDATA[<p>Perhaps you could quickly write what the FileCopier factory might look like in this case, and how you might use it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
