<?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: Enabling IoC in ASP.NET ActionResults (or, a better ActionResult)</title>
	<atom:link href="http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/</link>
	<description>Strong opinions, weakly held</description>
	<lastBuildDate>Sun, 19 May 2013 03:22:18 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Mark Seemann</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2116</link>
		<dc:creator>Mark Seemann</dc:creator>
		<pubDate>Wed, 20 Jan 2010 22:19:08 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2116</guid>
		<description>My first reaction to this is very similar to Dan Malcolm&#039;s. The purpose of the Controller is to invoke the Domain Logic. The purpose of an ActionResult is to translate the result to a response stream that a browser can understand.

While it may look like a Command object, I think that its purpose is entirely different. You can hijack it to execute Domain code, but that was never the intention.

Keeping the Domain action in the Controller is just a lot simpler, but I&#039;m not sure I understand how this conflicts with Controller base classes and such (but I&#039;m totally a &quot;favor composition over inheritance&quot; guy myself, so I don&#039;t have any hierarchy when it comes to Controllers).

In any case, let&#039;s take a look at your first attempt at DeleteRequestResult&lt;TModel&gt; where you don&#039;t like having to do Service Location. You are already injecting the Model and redirect action into the type, so why not also inject the repository and the logger now that you&#039;re at it?

Not that I would inject the logger myself, though. Having the logger there violates the SRP. Logging is a cross-cutting concern and much better handled as such. Decorators are fantastic tools for implementing AOP-like features with DI, so I would rather prefer a LoggingDecorator that decorates that real action - that&#039;s what we do at Safewhere, and it works like a charm.</description>
		<content:encoded><![CDATA[<p>My first reaction to this is very similar to Dan Malcolm&#8217;s. The purpose of the Controller is to invoke the Domain Logic. The purpose of an ActionResult is to translate the result to a response stream that a browser can understand.</p>
<p>While it may look like a Command object, I think that its purpose is entirely different. You can hijack it to execute Domain code, but that was never the intention.</p>
<p>Keeping the Domain action in the Controller is just a lot simpler, but I&#8217;m not sure I understand how this conflicts with Controller base classes and such (but I&#8217;m totally a &#8220;favor composition over inheritance&#8221; guy myself, so I don&#8217;t have any hierarchy when it comes to Controllers).</p>
<p>In any case, let&#8217;s take a look at your first attempt at DeleteRequestResult<tmodel> where you don&#8217;t like having to do Service Location. You are already injecting the Model and redirect action into the type, so why not also inject the repository and the logger now that you&#8217;re at it?</p>
<p>Not that I would inject the logger myself, though. Having the logger there violates the SRP. Logging is a cross-cutting concern and much better handled as such. Decorators are fantastic tools for implementing AOP-like features with DI, so I would rather prefer a LoggingDecorator that decorates that real action &#8211; that&#8217;s what we do at Safewhere, and it works like a charm.</tmodel></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarrett Meyer</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2115</link>
		<dc:creator>Jarrett Meyer</dc:creator>
		<pubDate>Sat, 19 Dec 2009 14:37:33 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2115</guid>
		<description>Rails is simple because there are no dependencies - therefore no DI and no IoC. You don&#039;t have IRepositories, because EVERYONE writing a Rails app uses the built in ActiveRecord classes. You get very small amounts of wiring up in Rails because the Rails framework does everything for you.

.NET is just so much different. There are so many ways to communicate with a data store. In MVC, you have to do so much meta programming to do stuff that has nothing to do with solving your business problem.</description>
		<content:encoded><![CDATA[<p>Rails is simple because there are no dependencies &#8211; therefore no DI and no IoC. You don&#8217;t have IRepositories, because EVERYONE writing a Rails app uses the built in ActiveRecord classes. You get very small amounts of wiring up in Rails because the Rails framework does everything for you.</p>
<p>.NET is just so much different. There are so many ways to communicate with a data store. In MVC, you have to do so much meta programming to do stuff that has nothing to do with solving your business problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Malcolm</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2114</link>
		<dc:creator>Dan Malcolm</dc:creator>
		<pubDate>Mon, 14 Dec 2009 20:40:27 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2114</guid>
		<description>
&gt; .. up to 8-9 custom AR objects
Same here (stuff like PermanentRedirectResult etc), but they&#039;re all focussed on UI outcome of action. I&#039;m sure that this was the intention of the framework design. The OnActionExecuted hook implies that the controller has done its work and given us an ActionResult that represents the final outcome of the action in the browser (which we can safely inspect and / or modify to support things like caching model of a ViewResult, converting it to Json if it&#039;s an ajax request etc). Does adding the role of specifying business logic that needs to be executed break this &quot;contract&quot;?

&gt; Service location in each of the ExecuteResult is not acceptable

Yes my suggestion would involve a single dependency that would resolve the invoker for the command. There may be more elegant ways - this was just a starting point for exploring ways of invoking the command within the action method. I could just as easily have the dependency supplied to the base controller class via setter injection, which is ultimately what you&#039;ve done. Other times it makes sense to move key cross-cutting dependencies to a static service locator (things like event bus or SystemTime wrappers) and take a small hit in your test setup code.

You&#039;re definitely onto something with the commands though - interesting to see how this progresses. </description>
		<content:encoded><![CDATA[<p>> .. up to 8-9 custom AR objects<br />
Same here (stuff like PermanentRedirectResult etc), but they&#8217;re all focussed on UI outcome of action. I&#8217;m sure that this was the intention of the framework design. The OnActionExecuted hook implies that the controller has done its work and given us an ActionResult that represents the final outcome of the action in the browser (which we can safely inspect and / or modify to support things like caching model of a ViewResult, converting it to Json if it&#8217;s an ajax request etc). Does adding the role of specifying business logic that needs to be executed break this &#8220;contract&#8221;?</p>
<p>> Service location in each of the ExecuteResult is not acceptable</p>
<p>Yes my suggestion would involve a single dependency that would resolve the invoker for the command. There may be more elegant ways &#8211; this was just a starting point for exploring ways of invoking the command within the action method. I could just as easily have the dependency supplied to the base controller class via setter injection, which is ultimately what you&#8217;ve done. Other times it makes sense to move key cross-cutting dependencies to a static service locator (things like event bus or SystemTime wrappers) and take a small hit in your test setup code.</p>
<p>You&#8217;re definitely onto something with the commands though &#8211; interesting to see how this progresses. </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bogardj</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2113</link>
		<dc:creator>bogardj</dc:creator>
		<pubDate>Mon, 14 Dec 2009 19:00:07 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2113</guid>
		<description>@Gilligan

Yeah, at that point it&#039;s probably a preference thing.  However, I don&#039;t ever want to add a dependency to a base controller type.  If an entire family of Save actions now needs some extra thing to do its work, I don&#039;t want to add that dependency to all derived classes.  Just the difference btwn inheritance and composition.</description>
		<content:encoded><![CDATA[<p>@Gilligan</p>
<p>Yeah, at that point it&#8217;s probably a preference thing.  However, I don&#8217;t ever want to add a dependency to a base controller type.  If an entire family of Save actions now needs some extra thing to do its work, I don&#8217;t want to add that dependency to all derived classes.  Just the difference btwn inheritance and composition.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gilligan</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2112</link>
		<dc:creator>Gilligan</dc:creator>
		<pubDate>Mon, 14 Dec 2009 14:59:11 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2112</guid>
		<description>I understand the positions of both your team, Jimmy, and Dan&#039;s comment. I think the main issue is that this is a work-around to the fact that ASP MVC does not have strong support for Controllerless Actions. If the Controller infrastructure were more focused on a controller-per-action, then much of this logic could be moved to the controllers.</description>
		<content:encoded><![CDATA[<p>I understand the positions of both your team, Jimmy, and Dan&#8217;s comment. I think the main issue is that this is a work-around to the fact that ASP MVC does not have strong support for Controllerless Actions. If the Controller infrastructure were more focused on a controller-per-action, then much of this logic could be moved to the controllers.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Palermo </title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2111</link>
		<dc:creator>Jeffrey Palermo </dc:creator>
		<pubDate>Sun, 13 Dec 2009 15:20:45 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2111</guid>
		<description>This is the problem of getting a dependency from anywhere that cannot uses constructor injection. Using the normal abstract factory inside a singleton works nicely. 

Even though the dependency on the container has been technically avoided here, the code still depends on a fine-grained interface that only container can satisfy.  Removing the whole notion of a container. Is what you want. Then wire up the container on the side. 

CodeCampServer has some examples of regular abstract factories </description>
		<content:encoded><![CDATA[<p>This is the problem of getting a dependency from anywhere that cannot uses constructor injection. Using the normal abstract factory inside a singleton works nicely. </p>
<p>Even though the dependency on the container has been technically avoided here, the code still depends on a fine-grained interface that only container can satisfy.  Removing the whole notion of a container. Is what you want. Then wire up the container on the side. </p>
<p>CodeCampServer has some examples of regular abstract factories </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bogardj</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2110</link>
		<dc:creator>bogardj</dc:creator>
		<pubDate>Sun, 13 Dec 2009 14:29:29 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2110</guid>
		<description>@Zeus, @Dan

Both of these solutions require a layer supertype controller with a dependency.  The whole reason I moved towards something executing a parameter object (think of it as a message, and IHandler&lt;TMessage&gt;) was so that each handler could have its logic contained independent of all other handlers.

@Dan

We&#039;re up to 8-9 custom ActionResult objects.  Service location in each of the ExecuteResult is not acceptable.  Neither is putting all of the logic down into the base controller, I don&#039;t want layer super type dependencies, it just doesn&#039;t scale.

@Zeus

That works, but it&#039;s not an improvement.  I now have more moving pieces in a controller action, making it harder to test.  WIth a handler separate from the message, I can allow the structure of the message and creation of it grow orthogonal to the logic handling the message.</description>
		<content:encoded><![CDATA[<p>@Zeus, @Dan</p>
<p>Both of these solutions require a layer supertype controller with a dependency.  The whole reason I moved towards something executing a parameter object (think of it as a message, and IHandler<tmessage>) was so that each handler could have its logic contained independent of all other handlers.</p>
<p>@Dan</p>
<p>We&#8217;re up to 8-9 custom ActionResult objects.  Service location in each of the ExecuteResult is not acceptable.  Neither is putting all of the logic down into the base controller, I don&#8217;t want layer super type dependencies, it just doesn&#8217;t scale.</p>
<p>@Zeus</p>
<p>That works, but it&#8217;s not an improvement.  I now have more moving pieces in a controller action, making it harder to test.  WIth a handler separate from the message, I can allow the structure of the message and creation of it grow orthogonal to the logic handling the message.</tmessage></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ZeusTheTrueGod</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2109</link>
		<dc:creator>ZeusTheTrueGod</dc:creator>
		<pubDate>Sun, 13 Dec 2009 10:12:54 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2109</guid>
		<description>I would like to see a more simple solution without command executer

My idea is replace
public ActionResult DeleteMethod()
{
    //ViewData[&quot;Message&quot;] = &quot;Welcome to ASP.NET MVC!&quot;;
    return new DeleteResult({ ... })
}
with

public ActionResult DeleteMethod()
{
    //ViewData[&quot;Message&quot;] = &quot;Welcome to ASP.NET MVC!&quot;;
    return Kernel.Resolve&lt;DeleteResult&gt;({...})
}

now we are constructing the ActionResult via Dependency Injection

So DeleteResult can be defined as
public class DeleteResult:ActionResult
{
    public IMyLogger MyLogger{get;set;}
    public IMyRepository MyRepository{get;set;}

    public override void ExecuteResult(ControllerContext context)
    {
           MyLogger.DoSomething();
           MyRepository.DoSomething();
   }
}

I think this is a more simple way to use a dependency injection.
Of course controller must have initialized property &quot;public IKernel Kernel {get;set;}&quot;,  which is easy implemented when ControllerFactory is made as in the blog article - controller is obtained via IoC container.

Because DeleteAction is created with IoC it should be registered in it with &quot;Transient&quot; lifestyle.

</description>
		<content:encoded><![CDATA[<p>I would like to see a more simple solution without command executer</p>
<p>My idea is replace<br />
public ActionResult DeleteMethod()<br />
{<br />
    //ViewData["Message"] = &#8220;Welcome to ASP.NET MVC!&#8221;;<br />
    return new DeleteResult({ &#8230; })<br />
}<br />
with</p>
<p>public ActionResult DeleteMethod()<br />
{<br />
    //ViewData["Message"] = &#8220;Welcome to ASP.NET MVC!&#8221;;<br />
    return Kernel.Resolve<deleteresult>({&#8230;})<br />
}</p>
<p>now we are constructing the ActionResult via Dependency Injection</p>
<p>So DeleteResult can be defined as<br />
public class DeleteResult:ActionResult<br />
{<br />
    public IMyLogger MyLogger{get;set;}<br />
    public IMyRepository MyRepository{get;set;}</p>
<p>    public override void ExecuteResult(ControllerContext context)<br />
    {<br />
           MyLogger.DoSomething();<br />
           MyRepository.DoSomething();<br />
   }<br />
}</p>
<p>I think this is a more simple way to use a dependency injection.<br />
Of course controller must have initialized property &#8220;public IKernel Kernel {get;set;}&#8221;,  which is easy implemented when ControllerFactory is made as in the blog article &#8211; controller is obtained via IoC container.</p>
<p>Because DeleteAction is created with IoC it should be registered in it with &#8220;Transient&#8221; lifestyle.</p>
<p></deleteresult></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan Malcolm</title>
		<link>http://lostechies.com/jimmybogard/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult/#comment-2108</link>
		<dc:creator>Dan Malcolm</dc:creator>
		<pubDate>Sat, 12 Dec 2009 20:53:54 +0000</pubDate>
		<guid isPermaLink="false">/blogs/jimmy_bogard/archive/2009/12/12/enabling-ioc-in-asp-net-actionresults-or-a-better-actionresult.aspx#comment-2108</guid>
		<description>Yeah, we definitely need a good infrastructure to move logic out to commands and stop the code / dependency bloat that you see in contollers.

Is our humble ActionResult the best candidate though? To me their responsibility should be restricted to the delivery of UI functionality according to the outcome of the &quot;meat&quot; of the controller action. They have a nice tightly focussed role and I&#039;m just not sure if it&#039;s a good thing to be highjacking the ActionResult execution mechanism to execute the main body of your application / business logic.

Wouldn&#039;t the following controller logic be cleaner?

1. Issue a DeleteRequestCommand. This gets executed by DeleteRequestInvoker (controller has dependency on an ICommandInvoker, which finds relevant Invoker)
2. Controller receives a DeleteRequestResult that informs us of the outcome of the operation
3a. If result.Success then return ViewResult to render the &quot;well done&quot; view (or RedirectResult to get back to previous screen or something)
3b. If not, return ViewResult to render the &quot;sorry guys&quot; view to display contents of result.Problems collection

The main business logic coordination belongs in the command / invoker. The controller&#039;s responsibility is to decide what to do in the UI according to the outcome.

With a consistent convention for commands and their results and the following helper method in your controller base class...

protected ActionResult ExecuteCommand[TCommand,TResult](T command, Func[TResult,ActionResult] ifSucceeds, Func[TResult,ActionResult] ifFails)

... controller action code could probably be limited to 1 or 2 lines.



</description>
		<content:encoded><![CDATA[<p>Yeah, we definitely need a good infrastructure to move logic out to commands and stop the code / dependency bloat that you see in contollers.</p>
<p>Is our humble ActionResult the best candidate though? To me their responsibility should be restricted to the delivery of UI functionality according to the outcome of the &#8220;meat&#8221; of the controller action. They have a nice tightly focussed role and I&#8217;m just not sure if it&#8217;s a good thing to be highjacking the ActionResult execution mechanism to execute the main body of your application / business logic.</p>
<p>Wouldn&#8217;t the following controller logic be cleaner?</p>
<p>1. Issue a DeleteRequestCommand. This gets executed by DeleteRequestInvoker (controller has dependency on an ICommandInvoker, which finds relevant Invoker)<br />
2. Controller receives a DeleteRequestResult that informs us of the outcome of the operation<br />
3a. If result.Success then return ViewResult to render the &#8220;well done&#8221; view (or RedirectResult to get back to previous screen or something)<br />
3b. If not, return ViewResult to render the &#8220;sorry guys&#8221; view to display contents of result.Problems collection</p>
<p>The main business logic coordination belongs in the command / invoker. The controller&#8217;s responsibility is to decide what to do in the UI according to the outcome.</p>
<p>With a consistent convention for commands and their results and the following helper method in your controller base class&#8230;</p>
<p>protected ActionResult ExecuteCommand[TCommand,TResult](T command, Func[TResult,ActionResult] ifSucceeds, Func[TResult,ActionResult] ifFails)</p>
<p>&#8230; controller action code could probably be limited to 1 or 2 lines.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
