<?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: ASP.NET MVC View Best Practices – Keep logic out of your views</title>
	<atom:link href="http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/</link>
	<description>Jag Reehal on Agile Development, ASP.NET MVC, Silverlight and all manner of good stuff</description>
	<lastBuildDate>Sun, 25 Jul 2010 19:27:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Jag Reehal</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-46</link>
		<dc:creator>Jag Reehal</dc:creator>
		<pubDate>Thu, 06 May 2010 21:58:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-46</guid>
		<description>Hi Jamie,

You’re right to question what to do here because as soon as you open the ‘code smell’ Pandora’s box it’s difficult if not impossible to close.

Would it be possible to add boolean properties to your view model, such as CanAddUserVisible?

If so you could use an html helper in the view model to determine if the display value of the button/control should be block/inline or none it should be hidden.  

This approach would be testable and could be done using a single view without any conditional logic.

I’ll try a write a blog post in the next couple of days with an example of the above.

In the meantime, if you come up with an alternative solution let me know.

Cheers,

Jag</description>
		<content:encoded><![CDATA[<p>Hi Jamie,</p>
<p>You’re right to question what to do here because as soon as you open the ‘code smell’ Pandora’s box it’s difficult if not impossible to close.</p>
<p>Would it be possible to add boolean properties to your view model, such as CanAddUserVisible?</p>
<p>If so you could use an html helper in the view model to determine if the display value of the button/control should be block/inline or none it should be hidden.  </p>
<p>This approach would be testable and could be done using a single view without any conditional logic.</p>
<p>I’ll try a write a blog post in the next couple of days with an example of the above.</p>
<p>In the meantime, if you come up with an alternative solution let me know.</p>
<p>Cheers,</p>
<p>Jag</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jaime</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-43</link>
		<dc:creator>Jaime</dc:creator>
		<pubDate>Wed, 05 May 2010 16:02:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-43</guid>
		<description>What are your thoughts (and how to solve) on &quot;authorization/display/hide controls&quot; logic in the view. For example, let&#039;s say I have a view with a bunch of interaction controls/buttons that need to be enable/disable or shown/hidden depending on some business rules. The controllers fills up a view model with a bunch of &quot;CanAddXXX&quot; or &quot;CanChangeYYY&quot; properties that it got from the business service. So what are the options:

1. Have a bunch of if&#039;s in the view to control that gets rendered.... Back to the logic in the view, granted is &quot;view&quot; logic, but untestable, hard to maintain, etc.

2. Return different views depending on the combination of shown/hidden controls... this could get really messy really fast if you have many combination

3. Have a helper class emit the correct html... testable but we are back to the &quot;who render that html&quot;

Any other thoughts?</description>
		<content:encoded><![CDATA[<p>What are your thoughts (and how to solve) on &#8220;authorization/display/hide controls&#8221; logic in the view. For example, let&#8217;s say I have a view with a bunch of interaction controls/buttons that need to be enable/disable or shown/hidden depending on some business rules. The controllers fills up a view model with a bunch of &#8220;CanAddXXX&#8221; or &#8220;CanChangeYYY&#8221; properties that it got from the business service. So what are the options:</p>
<p>1. Have a bunch of if&#8217;s in the view to control that gets rendered&#8230;. Back to the logic in the view, granted is &#8220;view&#8221; logic, but untestable, hard to maintain, etc.</p>
<p>2. Return different views depending on the combination of shown/hidden controls&#8230; this could get really messy really fast if you have many combination</p>
<p>3. Have a helper class emit the correct html&#8230; testable but we are back to the &#8220;who render that html&#8221;</p>
<p>Any other thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jag Reehal</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-26</link>
		<dc:creator>Jag Reehal</dc:creator>
		<pubDate>Thu, 15 Oct 2009 07:54:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-26</guid>
		<description>Hi Richard,

Thanks for your comment.  

I have updated the post with you&#039;re suggestion.  I would not add a method onto the view model to do this but that&#039;s down to personal choice.  

Just be aware if your workplace requires developers and designers to work together you might not be able to use some of the templating features coming in ASP.NET MVC 2 such as UIHints.

Also if you&#039;re compiling your ASP.NET MVC views make sure this is only done on your CI server not on your machine because it increases build time.  Check out this &lt;a href=&quot;http://www.dimecasts.net/Casts/CastFeedDetails/143&quot; rel=&quot;nofollow&quot;&gt;DimeCast about Performing Static Page Checking in MVC&lt;/a&gt;.</description>
		<content:encoded><![CDATA[<p>Hi Richard,</p>
<p>Thanks for your comment.  </p>
<p>I have updated the post with you&#8217;re suggestion.  I would not add a method onto the view model to do this but that&#8217;s down to personal choice.  </p>
<p>Just be aware if your workplace requires developers and designers to work together you might not be able to use some of the templating features coming in ASP.NET MVC 2 such as UIHints.</p>
<p>Also if you&#8217;re compiling your ASP.NET MVC views make sure this is only done on your CI server not on your machine because it increases build time.  Check out this <a href="http://www.dimecasts.net/Casts/CastFeedDetails/143" rel="nofollow">DimeCast about Performing Static Page Checking in MVC</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Samual Jenkins</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-25</link>
		<dc:creator>Samual Jenkins</dc:creator>
		<pubDate>Wed, 14 Oct 2009 22:55:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-25</guid>
		<description>I vote for option 3 because the view model should be a strict DTO and NOT contain methods.</description>
		<content:encoded><![CDATA[<p>I vote for option 3 because the view model should be a strict DTO and NOT contain methods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard Miller</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-24</link>
		<dc:creator>Richard Miller</dc:creator>
		<pubDate>Wed, 14 Oct 2009 21:20:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-24</guid>
		<description>I think you are doing a great thing highlighting the use of view models any why they should be used more. I think the issue raised by Sebastien is a big one though. The views you have written have just become un-maintainable from a designers perspective. 

What a designer should be doing is controlling the css and placing the data (from a view model) appropriately within the tags. All the options presented don&#039;t allow this and force the designer down the route of contacting a developer for a re-compile if they want to alter a class. 

Yes the business logic of 3 levels should be controlled in code not the view as you highlighted, but isn&#039;t that where the view model comes in? In this case it&#039;s something we want to do a lot so we can delegate the detail from the view model to a helper as you have done but I feel the key is not to hide anything the designer may want to change. 

Why not have a method on the view model with the following signature string StockLevelCss(string noneCss, string lowCss, string highCss) This would make the decision about which (css) string to return based on the stock level known within the view model. Tests would be simple, straightforward and long lasting. 

Within the view the designer now has control to alter the css as they wish, ok so there is the chance of typos within the view but that&#039;s an acceptable price to pay as the toolset can easily help you there. 

If the business logic changes to have 4 levels then the method can either gracefully degrade to return one of 3 levels. Or more preferable fail MVC view compilation, highlighting the issue for the designer to fix.

My vote is for option 4 keep css and tags out of code.</description>
		<content:encoded><![CDATA[<p>I think you are doing a great thing highlighting the use of view models any why they should be used more. I think the issue raised by Sebastien is a big one though. The views you have written have just become un-maintainable from a designers perspective. </p>
<p>What a designer should be doing is controlling the css and placing the data (from a view model) appropriately within the tags. All the options presented don&#8217;t allow this and force the designer down the route of contacting a developer for a re-compile if they want to alter a class. </p>
<p>Yes the business logic of 3 levels should be controlled in code not the view as you highlighted, but isn&#8217;t that where the view model comes in? In this case it&#8217;s something we want to do a lot so we can delegate the detail from the view model to a helper as you have done but I feel the key is not to hide anything the designer may want to change. </p>
<p>Why not have a method on the view model with the following signature string StockLevelCss(string noneCss, string lowCss, string highCss) This would make the decision about which (css) string to return based on the stock level known within the view model. Tests would be simple, straightforward and long lasting. </p>
<p>Within the view the designer now has control to alter the css as they wish, ok so there is the chance of typos within the view but that&#8217;s an acceptable price to pay as the toolset can easily help you there. </p>
<p>If the business logic changes to have 4 levels then the method can either gracefully degrade to return one of 3 levels. Or more preferable fail MVC view compilation, highlighting the issue for the designer to fix.</p>
<p>My vote is for option 4 keep css and tags out of code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Henry</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-21</link>
		<dc:creator>Mike Henry</dc:creator>
		<pubDate>Wed, 14 Oct 2009 16:02:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-21</guid>
		<description>Thanks for sharing this. What are your thoughts about putting the logic from option 1 into your view model, e.g. Model.GetCssClassForUnitsInStock()? Or do you think it&#039;s better to keep the view model a strict DTO?</description>
		<content:encoded><![CDATA[<p>Thanks for sharing this. What are your thoughts about putting the logic from option 1 into your view model, e.g. Model.GetCssClassForUnitsInStock()? Or do you think it&#8217;s better to keep the view model a strict DTO?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paul Anthony</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-20</link>
		<dc:creator>Paul Anthony</dc:creator>
		<pubDate>Wed, 14 Oct 2009 15:43:31 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-20</guid>
		<description>As you&#039;ve mentioned - rendering HTML in the DLL is a no-no - and makes life difficult for a design team. HTML strings in a build should be avoided like the plague.</description>
		<content:encoded><![CDATA[<p>As you&#8217;ve mentioned &#8211; rendering HTML in the DLL is a no-no &#8211; and makes life difficult for a design team. HTML strings in a build should be avoided like the plague.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sebastien Lambla</title>
		<link>http://www.arrangeactassert.com/asp-net-mvc-view-best-practices-keep-logic-out-of-your-views/comment-page-1/#comment-19</link>
		<dc:creator>Sebastien Lambla</dc:creator>
		<pubDate>Wed, 14 Oct 2009 13:57:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.arrangeactassert.com/?p=319#comment-19</guid>
		<description>I take objection to putting the CSS class in the controller.

I suppose it depends what you see your controller responsibility as being, but for me it&#039;s the gateway between http and your system. As such, templating concerns should stay within the template, and the controller should have no idea about it, and no involvement in it.</description>
		<content:encoded><![CDATA[<p>I take objection to putting the CSS class in the controller.</p>
<p>I suppose it depends what you see your controller responsibility as being, but for me it&#8217;s the gateway between http and your system. As such, templating concerns should stay within the template, and the controller should have no idea about it, and no involvement in it.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
