<?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: Do it Yourself &#8211; Dependency Injection</title>
	<atom:link href="http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=do-it-yourself-dependency-injection</link>
	<description>Testability Explorer</description>
	<lastBuildDate>Thu, 19 Jan 2012 16:42:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Luke</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-9123</link>
		<dc:creator>Luke</dc:creator>
		<pubDate>Fri, 01 Jul 2011 09:28:35 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-9123</guid>
		<description>Right, I&#039;ve been tasked with writing a new module, and so I am seizing this opportunity to finally dive into dependency injection.  Though very quickly I&#039;ve hit some walls, and I have a few questions, which are kind of related to this section from the PDF:

&quot;Clients may resort to guess-and-check programming to find all the methods that need to be invoked in order to make the object usable. The solution is to ask for fully-initialized dependencies in the constructor. For example, instead of connecting to a database in a constructor or an init method, add a constructor parameter for either an open Connection object or preferably for the actual queried data.&quot;

In my particular case I am using JMS, here you have QueueConnections, QueueSessions, Queues, and QueueReceivers.  I have a class that is responsible for receiving messages, so requires a QueueReceiver.  Now, based on the above, this class should ask for a queueReceiver.  However, a QueueReceiver is created from a QueueSession (session.createReceiver(...)) and a session is created from a QueueConnection (connection.createQueueSession(...)).  

Now, depending on the way the session was created, messages received by the QueueReceiver need to be handled differently.  In my particular case, I need the session created with the following parameters, createQueueSession(false, Session.AUTO_ACKNOWLEDGE), if different parameters are used, the application may not behave as expected.  Where should this logic go, though, an inject method, a helper, a provider? and how does my class ensure it is passed a receiver created by the right kind of session?

Additionally, the connection that was used to create the session needs to have had its start() method called, or the receiver won&#039;t receive anything.  Again, what is responsible for calling this start method? How does my class ensure the connection was started?

I have a feeling that this is possibly the responsibility of providers, and I should have a StartedConnectionProvider and a NonTransactionalAutoAcknowledgeSessionProvider, and a NonTransactionalAutoAcknowledgeQueueReceiverProvider.  The NonTransactionalAutoAcknowledgeSessionProvider would ask for a StartedConnectionProvider in its constructor, and the NonTransactionalAutoAcknowledgeQueueReceiverProvider would ask for a NonTransactionalAutoAcknowledgeSessionProvider in its constructor, and my class would ask for a NonTransactionalAutoAcknowledgeQueueReceiverProvider.  Unit tests could then perhaps be written for the providers to ensure they return the correctly configured objects from their get() methods.

Is that the right kind of approach, or is there a better alternative?  I guess one answer could also be that it should just be the responsibility of the injectors, and it is down to scenario tests to ensure the glue is sticking things together correctly.</description>
		<content:encoded><![CDATA[<p>Right, I&#8217;ve been tasked with writing a new module, and so I am seizing this opportunity to finally dive into dependency injection.  Though very quickly I&#8217;ve hit some walls, and I have a few questions, which are kind of related to this section from the PDF:</p>
<p>&#8220;Clients may resort to guess-and-check programming to find all the methods that need to be invoked in order to make the object usable. The solution is to ask for fully-initialized dependencies in the constructor. For example, instead of connecting to a database in a constructor or an init method, add a constructor parameter for either an open Connection object or preferably for the actual queried data.&#8221;</p>
<p>In my particular case I am using JMS, here you have QueueConnections, QueueSessions, Queues, and QueueReceivers.  I have a class that is responsible for receiving messages, so requires a QueueReceiver.  Now, based on the above, this class should ask for a queueReceiver.  However, a QueueReceiver is created from a QueueSession (session.createReceiver(&#8230;)) and a session is created from a QueueConnection (connection.createQueueSession(&#8230;)).  </p>
<p>Now, depending on the way the session was created, messages received by the QueueReceiver need to be handled differently.  In my particular case, I need the session created with the following parameters, createQueueSession(false, Session.AUTO_ACKNOWLEDGE), if different parameters are used, the application may not behave as expected.  Where should this logic go, though, an inject method, a helper, a provider? and how does my class ensure it is passed a receiver created by the right kind of session?</p>
<p>Additionally, the connection that was used to create the session needs to have had its start() method called, or the receiver won&#8217;t receive anything.  Again, what is responsible for calling this start method? How does my class ensure the connection was started?</p>
<p>I have a feeling that this is possibly the responsibility of providers, and I should have a StartedConnectionProvider and a NonTransactionalAutoAcknowledgeSessionProvider, and a NonTransactionalAutoAcknowledgeQueueReceiverProvider.  The NonTransactionalAutoAcknowledgeSessionProvider would ask for a StartedConnectionProvider in its constructor, and the NonTransactionalAutoAcknowledgeQueueReceiverProvider would ask for a NonTransactionalAutoAcknowledgeSessionProvider in its constructor, and my class would ask for a NonTransactionalAutoAcknowledgeQueueReceiverProvider.  Unit tests could then perhaps be written for the providers to ensure they return the correctly configured objects from their get() methods.</p>
<p>Is that the right kind of approach, or is there a better alternative?  I guess one answer could also be that it should just be the responsibility of the injectors, and it is down to scenario tests to ensure the glue is sticking things together correctly.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Parry</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-6909</link>
		<dc:creator>Chad Parry</dc:creator>
		<pubDate>Sat, 26 Feb 2011 06:09:20 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-6909</guid>
		<description>@Luke, I think you would be successful going with either Miško&#039;s factory pattern or with DIY-DI. They&#039;re almost the same thing with different names. The goal is always the same: relegate the glue code (like &quot;new&quot; operators) to their own place, so that the business logic is easier to test. The reason I used static methods in the glue code is because I&#039;m not interested in unit testing either them or their callers. I focus all my unit tests on the business logic, where they are most effective and most needed. So statics in the injector are not harmful. On the other hand, if your co-workers feel more favorably about a DI idiom that uses only instance methods, you should probably humor them. If you get any flavor of DI adopted on your project, you&#039;ll be happy you did.</description>
		<content:encoded><![CDATA[<p>@Luke, I think you would be successful going with either Miško&#8217;s factory pattern or with DIY-DI. They&#8217;re almost the same thing with different names. The goal is always the same: relegate the glue code (like &#8220;new&#8221; operators) to their own place, so that the business logic is easier to test. The reason I used static methods in the glue code is because I&#8217;m not interested in unit testing either them or their callers. I focus all my unit tests on the business logic, where they are most effective and most needed. So statics in the injector are not harmful. On the other hand, if your co-workers feel more favorably about a DI idiom that uses only instance methods, you should probably humor them. If you get any flavor of DI adopted on your project, you&#8217;ll be happy you did.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Luke</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-6794</link>
		<dc:creator>Luke</dc:creator>
		<pubDate>Wed, 23 Feb 2011 11:28:17 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-6794</guid>
		<description>We&#039;re currently looking at switching to using a DI approach to coding, as our code is currently so untestable.

We&#039;ve found many articles on this site to be very helpful in educating us about our bad practices, and how we can improve matters.

I found this link to Chad&#039;s approach for DI particularly enlightening, as it was the first to give me a better idea of how it scales past trivial examples, and how it could be applied to our own code.

However, one of my colleagues is strongly opposed to the use of statics anywhere in our code (they are, in a large part, what we are looking to remove, so why do so by adding new ones in?), and also isn&#039;t a big fan of Chad&#039;s injector pattern.

We&#039;re hoping someone could offer some insight on the benefits/negatives of using the static injectors over using the factory pattern Misko shows in articles such as: http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/

Also, are the static methods in Chad&#039;s version a temporary solution, to be refactored out later, or the final state?</description>
		<content:encoded><![CDATA[<p>We&#8217;re currently looking at switching to using a DI approach to coding, as our code is currently so untestable.</p>
<p>We&#8217;ve found many articles on this site to be very helpful in educating us about our bad practices, and how we can improve matters.</p>
<p>I found this link to Chad&#8217;s approach for DI particularly enlightening, as it was the first to give me a better idea of how it scales past trivial examples, and how it could be applied to our own code.</p>
<p>However, one of my colleagues is strongly opposed to the use of statics anywhere in our code (they are, in a large part, what we are looking to remove, so why do so by adding new ones in?), and also isn&#8217;t a big fan of Chad&#8217;s injector pattern.</p>
<p>We&#8217;re hoping someone could offer some insight on the benefits/negatives of using the static injectors over using the factory pattern Misko shows in articles such as: <a href="http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/" rel="nofollow">http://misko.hevery.com/2008/07/08/how-to-think-about-the-new-operator/</a></p>
<p>Also, are the static methods in Chad&#8217;s version a temporary solution, to be refactored out later, or the final state?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Parry</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-4690</link>
		<dc:creator>Chad Parry</dc:creator>
		<pubDate>Tue, 02 Nov 2010 03:58:31 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-4690</guid>
		<description>@WW, when I explored using instance injection methods instead of static injection methods, it didn&#039;t provide any benefits. Also, you run into the problem of having to find the right injection instance to bind to. I found that the code was always better if I used a context object and static methods to configure everything. Try it out yourself and see if you agree.</description>
		<content:encoded><![CDATA[<p>@WW, when I explored using instance injection methods instead of static injection methods, it didn&#8217;t provide any benefits. Also, you run into the problem of having to find the right injection instance to bind to. I found that the code was always better if I used a context object and static methods to configure everything. Try it out yourself and see if you agree.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: WW</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-4688</link>
		<dc:creator>WW</dc:creator>
		<pubDate>Tue, 02 Nov 2010 00:13:20 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-4688</guid>
		<description>@Chad:

I&#039;m wondering why you used statics in your injector code.  Why not create an instance of your injector and then use that?

I feel that this would be useful because others could extend your injector, overwriting perhaps just one method to wire together different service objects.</description>
		<content:encoded><![CDATA[<p>@Chad:</p>
<p>I&#8217;m wondering why you used statics in your injector code.  Why not create an instance of your injector and then use that?</p>
<p>I feel that this would be useful because others could extend your injector, overwriting perhaps just one method to wire together different service objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mobile application testing</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-4223</link>
		<dc:creator>mobile application testing</dc:creator>
		<pubDate>Thu, 30 Sep 2010 06:23:25 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-4223</guid>
		<description>Many times i found no standers for DI container. Some time it is very hard to working with such conditions.</description>
		<content:encoded><![CDATA[<p>Many times i found no standers for DI container. Some time it is very hard to working with such conditions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Parry</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-3903</link>
		<dc:creator>Chad Parry</dc:creator>
		<pubDate>Thu, 19 Aug 2010 02:35:57 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-3903</guid>
		<description>@Martin, the most straightforward way is to use the factory pattern. The factory can ask the injector to provide parameters like all the various database hosts and ports, plus a flag that indicates which database to use. Then the factory&#039;s responsibility is to produce the appropriate connection object.</description>
		<content:encoded><![CDATA[<p>@Martin, the most straightforward way is to use the factory pattern. The factory can ask the injector to provide parameters like all the various database hosts and ports, plus a flag that indicates which database to use. Then the factory&#8217;s responsibility is to produce the appropriate connection object.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Martin Norbury</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-3896</link>
		<dc:creator>Martin Norbury</dc:creator>
		<pubDate>Mon, 16 Aug 2010 22:30:33 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-3896</guid>
		<description>Hi Chad, 

Many thanks for the DIY DI guide. It was a very enjoyable read.

One thing I&#039;m currently puzzling over at the moment is how one would go about implementing configurable injectors. For example, if you were using DI injectors in an application that is capable of connecting to multiple database types. Is the correct way to do this to use providers?</description>
		<content:encoded><![CDATA[<p>Hi Chad, </p>
<p>Many thanks for the DIY DI guide. It was a very enjoyable read.</p>
<p>One thing I&#8217;m currently puzzling over at the moment is how one would go about implementing configurable injectors. For example, if you were using DI injectors in an application that is capable of connecting to multiple database types. Is the correct way to do this to use providers?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chad Parry</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-3798</link>
		<dc:creator>Chad Parry</dc:creator>
		<pubDate>Tue, 27 Jul 2010 16:47:34 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-3798</guid>
		<description>Great observation, @Konstantin. We say that the first phase is exclusively object creation and the second phase is exclusively business logic. But we are telling a white lie. Really, the two get mixed together freely. This interleaving must happen every time you inject a dependency that gets created by a factory.

In DIY-DI, exceptions will be propagated normally, regardless of which phase they are thrown from. So my opinion is that it&#039;s not a problem. With Guice, some exceptions can get swallowed, so you should use ThrowingProviders liberally, and then it&#039;s not a problem.

It&#039;s important that your source code shows a clear separation between glue code and business logic, but less important that there is a strict boundary at runtime between the two.</description>
		<content:encoded><![CDATA[<p>Great observation, @Konstantin. We say that the first phase is exclusively object creation and the second phase is exclusively business logic. But we are telling a white lie. Really, the two get mixed together freely. This interleaving must happen every time you inject a dependency that gets created by a factory.</p>
<p>In DIY-DI, exceptions will be propagated normally, regardless of which phase they are thrown from. So my opinion is that it&#8217;s not a problem. With Guice, some exceptions can get swallowed, so you should use ThrowingProviders liberally, and then it&#8217;s not a problem.</p>
<p>It&#8217;s important that your source code shows a clear separation between glue code and business logic, but less important that there is a strict boundary at runtime between the two.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Konstantin Fadeyev</title>
		<link>http://misko.hevery.com/2010/05/26/do-it-yourself-dependency-injection/comment-page-1/#comment-3796</link>
		<dc:creator>Konstantin Fadeyev</dc:creator>
		<pubDate>Tue, 27 Jul 2010 14:16:06 +0000</pubDate>
		<guid isPermaLink="false">http://misko.hevery.com/?p=619#comment-3796</guid>
		<description>Misko and Chad, first I want to thank you both for your great ideas on DI, for your blogs, videos and articles – your insights helped me and my colleagues a lot to write better code.

Although I have a question about the article:
In step 7 you introduced SettlementCalculator to decouple two service objects so they collaborate through the value object – settlementAmount. But while you were doing that, you actually moved execution of your business logic from bookingService.buy() method to the phase of object graph creation. Isn’t it something we should avoid? What if statementCalculator.getSettlementAmount() throws an exception or reads from hard drive? I thought that first line in the main() method is just about object graph creation and ALL work should done after that, i.e. in bookingService.buy().</description>
		<content:encoded><![CDATA[<p>Misko and Chad, first I want to thank you both for your great ideas on DI, for your blogs, videos and articles – your insights helped me and my colleagues a lot to write better code.</p>
<p>Although I have a question about the article:<br />
In step 7 you introduced SettlementCalculator to decouple two service objects so they collaborate through the value object – settlementAmount. But while you were doing that, you actually moved execution of your business logic from bookingService.buy() method to the phase of object graph creation. Isn’t it something we should avoid? What if statementCalculator.getSettlementAmount() throws an exception or reads from hard drive? I thought that first line in the main() method is just about object graph creation and ALL work should done after that, i.e. in bookingService.buy().</p>
]]></content:encoded>
	</item>
</channel>
</rss>

