<?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: Constructor Injection vs Setter Injection</title>
	<atom:link href="http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/</link>
	<description>Trying to walk that thin line between intelligence and ignorance</description>
	<lastBuildDate>Thu, 11 Mar 2010 23:14:12 +0100</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Securing Your Agatha Service Layer &#124; The Inquisitive Coder &#8211; Davy Brion&#39;s Blog</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-26386</link>
		<dc:creator>Securing Your Agatha Service Layer &#124; The Inquisitive Coder &#8211; Davy Brion&#39;s Blog</dc:creator>
		<pubDate>Wed, 27 Jan 2010 20:05:17 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-26386</guid>
		<description>[...] In case you’re wondering why i’m using Setter-injection here instead of Constructor-injection, read this. [...]</description>
		<content:encoded><![CDATA[<p>[...] In case you’re wondering why i’m using Setter-injection here instead of Constructor-injection, read this. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Chiasson</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22944</link>
		<dc:creator>Brian Chiasson</dc:creator>
		<pubDate>Fri, 13 Nov 2009 14:06:50 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22944</guid>
		<description>@Davy - kudos to you and YAGNI to me :)</description>
		<content:encoded><![CDATA[<p>@Davy &#8211; kudos to you and YAGNI to me <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22943</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Fri, 13 Nov 2009 13:59:30 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22943</guid>
		<description>@Brian

that try/commit/catch/rollback block is actually the only one we have, which is why i don&#039;t bother with cleaning it up :)</description>
		<content:encoded><![CDATA[<p>@Brian</p>
<p>that try/commit/catch/rollback block is actually the only one we have, which is why i don&#8217;t bother with cleaning it up <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Chiasson</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22942</link>
		<dc:creator>Brian Chiasson</dc:creator>
		<pubDate>Fri, 13 Nov 2009 13:51:21 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22942</guid>
		<description>Late to the conversation (I just discovered the blog through Elegant Coders), sorry.

I am not seeing a violation of SRP here. The NhRequestHandler&#039;s job is to wrap the response in a transaction. It has only one reason to change - the nature of handling a transaction. If anything, the name is strange because it says Nh, I would rename the class to TransactionalRequestHandler. The only other thing I might do is expose a method on IUnitOfWork called WithinTransaction(Action work) as I am sure you have the try..commit...catch...rollback a lot in the code base and it would remove some of the &quot;noise&quot; that bothers others.

I would, however, consider changing the RequestHandler so that you don&#039;t have to call back into the base class&#039;s Handle method to properly finish handling the request. It justs seem strange to me and counter-intuitive. I have run into this a bit in my Test infrastructure and I can never remember if I have to call back into my base class or not (think test fixture setup). I think that would lead me to using the Decorator pattern here, because I am not sure inheritance is correct in this case - probably a huge undertaking that&#039;s not worth it at this point.

The reason I say Decorator, is because you are really wrapping the actual response handling code inside of a transaction. Your NhRequestHandler really does nothing else. I think it would be more intuitive to see RequestHandlerDecorator(RequestHandler).Handle and then your code would be more intuitive.

&lt;code&gt;
public Response Handle(Request request)
{
   UnitOfWork.WithinTransaction(() =&gt; 
      handler.Handle(request);
   );
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Late to the conversation (I just discovered the blog through Elegant Coders), sorry.</p>
<p>I am not seeing a violation of SRP here. The NhRequestHandler&#8217;s job is to wrap the response in a transaction. It has only one reason to change &#8211; the nature of handling a transaction. If anything, the name is strange because it says Nh, I would rename the class to TransactionalRequestHandler. The only other thing I might do is expose a method on IUnitOfWork called WithinTransaction(Action work) as I am sure you have the try..commit&#8230;catch&#8230;rollback a lot in the code base and it would remove some of the &#8220;noise&#8221; that bothers others.</p>
<p>I would, however, consider changing the RequestHandler so that you don&#8217;t have to call back into the base class&#8217;s Handle method to properly finish handling the request. It justs seem strange to me and counter-intuitive. I have run into this a bit in my Test infrastructure and I can never remember if I have to call back into my base class or not (think test fixture setup). I think that would lead me to using the Decorator pattern here, because I am not sure inheritance is correct in this case &#8211; probably a huge undertaking that&#8217;s not worth it at this point.</p>
<p>The reason I say Decorator, is because you are really wrapping the actual response handling code inside of a transaction. Your NhRequestHandler really does nothing else. I think it would be more intuitive to see RequestHandlerDecorator(RequestHandler).Handle and then your code would be more intuitive.</p>
<p><code><br />
public Response Handle(Request request)<br />
{<br />
   UnitOfWork.WithinTransaction(() =&gt;<br />
      handler.Handle(request);<br />
   );<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: TomC</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22880</link>
		<dc:creator>TomC</dc:creator>
		<pubDate>Wed, 04 Nov 2009 09:10:37 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22880</guid>
		<description>@Jeremy

The solution you provided, doesn&#039;t seem like a very good solution to me. 
By doing this, the RequestProcessor (the one who resolves the RequestHandlers and calls their Handle method) would have to know which types have a Handle method with a UnitOfWork and which don&#039;t, probably resulting in an if-cast and at least 2 different ways to call a RequestHandler. If for some reason another type of RequestHandler is introduced it would result in yet another if-cast-callTheSpecificHandleMethod.
Or am I missing something and how would the RequestProcessor look like then?</description>
		<content:encoded><![CDATA[<p>@Jeremy</p>
<p>The solution you provided, doesn&#8217;t seem like a very good solution to me.<br />
By doing this, the RequestProcessor (the one who resolves the RequestHandlers and calls their Handle method) would have to know which types have a Handle method with a UnitOfWork and which don&#8217;t, probably resulting in an if-cast and at least 2 different ways to call a RequestHandler. If for some reason another type of RequestHandler is introduced it would result in yet another if-cast-callTheSpecificHandleMethod.<br />
Or am I missing something and how would the RequestProcessor look like then?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22877</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Wed, 04 Nov 2009 06:37:47 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22877</guid>
		<description>@Jeremy

i&#039;m still not convinced of the approach you mentioned in your comment. The end result would be practically the same, and i wouldn&#039;t really get any benefit out of it (and it would hurt backwards compatibility which i have to take into account).  The reason why i don&#039;t really get any benefit out it is because nobody ever has a reference to a handler. A specific handler is instantiated by the IOC container whenever a request comes into the service layer, and the Handle(Request) method is called, after which the resulting Response is just returned.  You never instantiate or deal with a handler yourself, so the fact that the IUnitOfWork instance is exposed by a public getter/setter doesn&#039;t really hurt anyone.  Theoretically it certainly doesn&#039;t have to be there and it shouldn&#039;t be exposed.  But if nobody gets the chance to actually access it (apart from the IOC container that provides the dependency), is there really a problem?

As for the SRP thing... i guess it depends on how strict you are about it.  For me, a specific handler class will only have code to take care of the functionality it needs to provide.  Nothing more, nothing less.  It doesn&#039;t need to worry about the UnitOfWork or the database transaction and automatic commits/rollbacks.  Whether that is achieved through inheritance or composition doesn&#039;t really matter to me so long as there is no noticeable downside to it.

that said, i do enjoy these sort of discussions so please feel free to share your other variations :)</description>
		<content:encoded><![CDATA[<p>@Jeremy</p>
<p>i&#8217;m still not convinced of the approach you mentioned in your comment. The end result would be practically the same, and i wouldn&#8217;t really get any benefit out of it (and it would hurt backwards compatibility which i have to take into account).  The reason why i don&#8217;t really get any benefit out it is because nobody ever has a reference to a handler. A specific handler is instantiated by the IOC container whenever a request comes into the service layer, and the Handle(Request) method is called, after which the resulting Response is just returned.  You never instantiate or deal with a handler yourself, so the fact that the IUnitOfWork instance is exposed by a public getter/setter doesn&#8217;t really hurt anyone.  Theoretically it certainly doesn&#8217;t have to be there and it shouldn&#8217;t be exposed.  But if nobody gets the chance to actually access it (apart from the IOC container that provides the dependency), is there really a problem?</p>
<p>As for the SRP thing&#8230; i guess it depends on how strict you are about it.  For me, a specific handler class will only have code to take care of the functionality it needs to provide.  Nothing more, nothing less.  It doesn&#8217;t need to worry about the UnitOfWork or the database transaction and automatic commits/rollbacks.  Whether that is achieved through inheritance or composition doesn&#8217;t really matter to me so long as there is no noticeable downside to it.</p>
<p>that said, i do enjoy these sort of discussions so please feel free to share your other variations <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy Gray</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22875</link>
		<dc:creator>Jeremy Gray</dc:creator>
		<pubDate>Tue, 03 Nov 2009 16:10:43 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22875</guid>
		<description>By the way, I have some other variations on the implementation eluded to above, so just let me know if you&#039;d like to hear about them. They might align better with your implementation constraints, worldview, etc.</description>
		<content:encoded><![CDATA[<p>By the way, I have some other variations on the implementation eluded to above, so just let me know if you&#8217;d like to hear about them. They might align better with your implementation constraints, worldview, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy Gray</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22874</link>
		<dc:creator>Jeremy Gray</dc:creator>
		<pubDate>Tue, 03 Nov 2009 15:26:28 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22874</guid>
		<description>I have only a few moments to reply before I must head off to the office, but off the top of my head the main option jumping out at me is move your Template Method implementation away from inheritance and towards composition (via nested closure or the like). You can do this in a few steps to make it easier.

The first step is to change the signature for your template method body from base.Handle(request) to base.Handle(UnitOfWork, request), allowing you to remove the derived type dependencies on the UnitOfWork property. Next, just to enforce that change, take the getter off the UnitOfWork property, or at least make it private to the base type. Then, replace inheritance with composition. Instead of deriving to implement Handle(UnitOfWork, TRequest) you instantiate just one implementation of a request handler and then call Handle. Handle would do your familiar transaction setup and then call a closure of the form TResponse Handle(IUnitOfWork, TRequest). The closure itself would assume operation within a transaction that commits on successful return and rolls back on any exception thrown from the closure. The closure would have been passed into the call to Handle itself, assuming the Handle signature can be modified, or passed into the ctor (if you are using a container that supports dependency override (StructureMap does, for example)) if the Handle signature cannot be modified. If it cannot, the ctor injection would do nicely. If your container can&#039;t do dependency overrides, either get one that can or use setter injection for the closure.

Now, all of that said, if you ended up back at setter injection of a closure instead of setter injection of the unit of work, one would have to consider if the improvement of killing the SRP violation was worth the trouble. With that in mind, much of the above hinges itself on whether or not you can change the signature of the Handle method to accept the closure or your container (and instantiation pattern) will allow you to use ctor dependency override.

As for &quot;I really don’t see this is as a violation of the Single Responsibility Principle.&quot;, addressing unit of work and transactions is a separate concern from request-specific handling logic, and your current use of Template Method pattern simply proves the existence of the SRP violation, as much as it is occluded behind the responsibilities of the base type versus those of the derived types. That said, your current approach certainly isn&#039;t a huge SRP violation and there may well be more pressing matters to hunt down within your codebase since your application of Template Method does a good job of minimizing the visibility and impact.

You could for the moment just do part of the change, as a prep move and a valuable piece of clean-up in its own right: the first two steps, changing the base.Handle(request) signature to also accept the unit of work and then hiding the UnitOfWork property from the derived types (and from outside code, which has its own benefit.)</description>
		<content:encoded><![CDATA[<p>I have only a few moments to reply before I must head off to the office, but off the top of my head the main option jumping out at me is move your Template Method implementation away from inheritance and towards composition (via nested closure or the like). You can do this in a few steps to make it easier.</p>
<p>The first step is to change the signature for your template method body from base.Handle(request) to base.Handle(UnitOfWork, request), allowing you to remove the derived type dependencies on the UnitOfWork property. Next, just to enforce that change, take the getter off the UnitOfWork property, or at least make it private to the base type. Then, replace inheritance with composition. Instead of deriving to implement Handle(UnitOfWork, TRequest) you instantiate just one implementation of a request handler and then call Handle. Handle would do your familiar transaction setup and then call a closure of the form TResponse Handle(IUnitOfWork, TRequest). The closure itself would assume operation within a transaction that commits on successful return and rolls back on any exception thrown from the closure. The closure would have been passed into the call to Handle itself, assuming the Handle signature can be modified, or passed into the ctor (if you are using a container that supports dependency override (StructureMap does, for example)) if the Handle signature cannot be modified. If it cannot, the ctor injection would do nicely. If your container can&#8217;t do dependency overrides, either get one that can or use setter injection for the closure.</p>
<p>Now, all of that said, if you ended up back at setter injection of a closure instead of setter injection of the unit of work, one would have to consider if the improvement of killing the SRP violation was worth the trouble. With that in mind, much of the above hinges itself on whether or not you can change the signature of the Handle method to accept the closure or your container (and instantiation pattern) will allow you to use ctor dependency override.</p>
<p>As for &#8220;I really don’t see this is as a violation of the Single Responsibility Principle.&#8221;, addressing unit of work and transactions is a separate concern from request-specific handling logic, and your current use of Template Method pattern simply proves the existence of the SRP violation, as much as it is occluded behind the responsibilities of the base type versus those of the derived types. That said, your current approach certainly isn&#8217;t a huge SRP violation and there may well be more pressing matters to hunt down within your codebase since your application of Template Method does a good job of minimizing the visibility and impact.</p>
<p>You could for the moment just do part of the change, as a prep move and a valuable piece of clean-up in its own right: the first two steps, changing the base.Handle(request) signature to also accept the unit of work and then hiding the UnitOfWork property from the derived types (and from outside code, which has its own benefit.)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22873</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Tue, 03 Nov 2009 07:23:39 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22873</guid>
		<description>@Jeremy 

that actually is a real-world example :)

how would you change the design so that the handler executes within the scope of the UnitOfWork _and_ has access to it? I could&#039;ve gone the AOP route (either dynamically with Windsor or statically with Postsharp) but then i wouldn&#039;t have access to the UOW in the derived handlers. If you have anything else in mind, please do share :)

Also, classes that derive from this specific handler typically introduce another dependency for context-related data so it can easily be accessed in the handlers that derive from that.

I really don&#039;t see this is as a violation of the Single Responsibility Principle.</description>
		<content:encoded><![CDATA[<p>@Jeremy </p>
<p>that actually is a real-world example <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>how would you change the design so that the handler executes within the scope of the UnitOfWork _and_ has access to it? I could&#8217;ve gone the AOP route (either dynamically with Windsor or statically with Postsharp) but then i wouldn&#8217;t have access to the UOW in the derived handlers. If you have anything else in mind, please do share <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Also, classes that derive from this specific handler typically introduce another dependency for context-related data so it can easily be accessed in the handlers that derive from that.</p>
<p>I really don&#8217;t see this is as a violation of the Single Responsibility Principle.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeremy Gray</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22872</link>
		<dc:creator>Jeremy Gray</dc:creator>
		<pubDate>Tue, 03 Nov 2009 06:02:15 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22872</guid>
		<description>@Davy - Dmitriy is still right, though, even given your example. Heck, especially given your example. It would be trivial to refactor such a handler design so that individual handler implementations execute within a transaction and are provided a unit of work, yet perform none of the unit of work prep, transaction creation, commit, or even the rollback in case of a failure. Since the ability to perform such a refactoring might be dependent on the simplicity of your provided example, can you provide a more real-world example we can look at?</description>
		<content:encoded><![CDATA[<p>@Davy &#8211; Dmitriy is still right, though, even given your example. Heck, especially given your example. It would be trivial to refactor such a handler design so that individual handler implementations execute within a transaction and are provided a unit of work, yet perform none of the unit of work prep, transaction creation, commit, or even the rollback in case of a failure. Since the ability to perform such a refactoring might be dependent on the simplicity of your provided example, can you provide a more real-world example we can look at?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Davy Brion</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22869</link>
		<dc:creator>Davy Brion</dc:creator>
		<pubDate>Mon, 02 Nov 2009 12:10:15 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22869</guid>
		<description>@Dmitriy

in most cases, i&#039;d agree with that sentiment... but not in the context of the example given in the post :)</description>
		<content:encoded><![CDATA[<p>@Dmitriy</p>
<p>in most cases, i&#8217;d agree with that sentiment&#8230; but not in the context of the example given in the post <img src='http://davybrion.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dmitriy Naginryak</title>
		<link>http://davybrion.com/blog/2009/11/constructor-injection-vs-sette-injection/comment-page-1/#comment-22868</link>
		<dc:creator>Dmitriy Naginryak</dc:creator>
		<pubDate>Mon, 02 Nov 2009 12:06:54 +0000</pubDate>
		<guid isPermaLink="false">http://davybrion.com/blog/?p=1715#comment-22868</guid>
		<description>I believe if you have too much &#039;noise&#039; (too many dependencies) then you just violate SRP.
The class 99.9% should be split. Thus all dependencies will be split as well and make the code much cleaner.

Using propery injection to solve &#039;noisiness problem&#039; is just akward workaround for the real issue - SRP violation.

Cheers.</description>
		<content:encoded><![CDATA[<p>I believe if you have too much &#8216;noise&#8217; (too many dependencies) then you just violate SRP.<br />
The class 99.9% should be split. Thus all dependencies will be split as well and make the code much cleaner.</p>
<p>Using propery injection to solve &#8216;noisiness problem&#8217; is just akward workaround for the real issue &#8211; SRP violation.</p>
<p>Cheers.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
