<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
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/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Arrange Act Assert &#187; Resharper</title> <atom:link href="http://www.arrangeactassert.com/category/resharper/feed/" rel="self" type="application/rss+xml" /><link>http://www.arrangeactassert.com</link> <description>Jag Reehal on Agile Development, ASP.NET MVC and all manner of good stuff</description> <lastBuildDate>Thu, 24 Nov 2011 00:01:20 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>To assignment statements?  Yes please ReSharper</title><link>http://www.arrangeactassert.com/to-assignment-statements-yes-please-resharper/</link> <comments>http://www.arrangeactassert.com/to-assignment-statements-yes-please-resharper/#comments</comments> <pubDate>Mon, 21 Mar 2011 09:00:10 +0000</pubDate> <dc:creator>Jag Reehal</dc:creator> <category><![CDATA[Resharper]]></category><guid
isPermaLink="false">http://www.arrangeactassert.com/?p=1738</guid> <description><![CDATA[On the 22nd of February 2011 I wrote a blog post about how I’ve seen developers using object initializers without thinking about when are where to use them. I also said it would be nice if ReSharper could separate the assignments onto separate lines&#8230; and got reply from Jenya Legkiy who works at JetBrains who [...]]]></description> <content:encoded><![CDATA[<p>On the 22nd of February 2011 I wrote a blog post about how I’ve seen <a
href="http://www.arrangeactassert.com/use-an-object-initializer-no-thanks-resharper/">developers using object initializers without thinking about when are where to use them</a>.</p><p>I also said it would be nice if <a
href="http://www.jetbrains.com/resharper/">ReSharper</a> could separate the assignments onto separate lines&#8230; and got reply from Jenya Legkiy who works at <a
href="http://www.jetbrains.com/index.html">JetBrains</a> who said it already does.</p><p>So to make up for it I&#8217;ve done my first screencast to show how you can you use ReSharper to refactor your code to use an object initializer and then go back to having the assignments on separate lines.</p><p><iframe
title="YouTube video player" width="640" height="390" src="http://www.youtube.com/embed/sOzqkPU5Ilw" frameborder="0" allowfullscreen></iframe></p><p>ReSharper I love you!</p> ]]></content:encoded> <wfw:commentRss>http://www.arrangeactassert.com/to-assignment-statements-yes-please-resharper/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Use an Object Initializer? No thanks Resharper</title><link>http://www.arrangeactassert.com/use-an-object-initializer-no-thanks-resharper/</link> <comments>http://www.arrangeactassert.com/use-an-object-initializer-no-thanks-resharper/#comments</comments> <pubDate>Tue, 22 Feb 2011 09:00:06 +0000</pubDate> <dc:creator>Jag Reehal</dc:creator> <category><![CDATA[Resharper]]></category><guid
isPermaLink="false">http://www.arrangeactassert.com/?p=1655</guid> <description><![CDATA[I really like ReSharper in fact I&#8217;ll even go as far as to say I love it! I also like object initializers in C#. But as I’ll demonstrate in this post you can have too much of a good thing. When object initializers go bad The code below shows an example of mapping a model [...]]]></description> <content:encoded><![CDATA[<p>I really like <a
href="http://www.jetbrains.com/resharper/">ReSharper</a> in fact I&#8217;ll even go as far as to say I love it!</p><p>I also like <a
href="http://msdn.microsoft.com/en-us/library/bb384062.aspx">object initializers</a> in C#.</p><p>But as I’ll demonstrate in this post you can have too much of a good thing.</p><h3 class="subheading">When object initializers go bad</h3><p>The code below shows an example of mapping a model object from a data reader with and without using an object initializer.</p><pre class="brush: csharp; gutter: true; highlight: [23,24,25,26,27,28]; title: ; toolbar: true; notranslate">
using System;
using System.Data;

public class Mapper
{
    public static Model Map(IDataReader reader)
    {
        if (reader.Read())
        {
            Model model = new Model();
            model.FirstName = reader[0].ToString();
            model.LastName = reader[1].ToString();
            model.Age = Convert.ToInt32(reader[2].ToString());
            return model;
        }
        return null;
    }

    public static Model MapUsingObjectInitializer(IDataReader reader)
    {
        if (reader.Read())
        {
            return new Model
                              {
                                  FirstName = reader[0].ToString(),
                                  LastName = reader[1].ToString(),
                                  Age = Convert.ToInt32(reader[2].ToString())
                              };
        }
        return null;
    }
}

public class Model
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}
</pre><p>If we run two unit tests to map the model from a data reader we will get two passing tests.</p><pre class="brush: csharp; gutter: false; title: ; toolbar: true; notranslate">
using System.Data;
using NUnit.Framework;

[TestFixture]
public class When_Mapping_Model_Using_DataReader
{
    [Test]
    public void Should_Be_Able_To_Map_Model()
    {
        Mapper.Map(GetDataReader());
    }

    [Test]
    public void Should_Be_Able_To_Map_Model_Using_Object_Initializer()
    {
        Mapper.MapUsingObjectInitializer(GetDataReader());
    }

    public static IDataReader GetDataReader()
    {
        DataTable table = new DataTable();
        DataRow row = table.NewRow();
        table.Columns.Add(new DataColumn(&quot;FirstName&quot;));
        table.Columns.Add(new DataColumn(&quot;LastName&quot;));
        table.Columns.Add(new DataColumn(&quot;Age&quot;));
        row[&quot;FirstName&quot;] = &quot;Bob&quot;;
        row[&quot;LastName&quot;] = &quot;Smith&quot;;
        row[&quot;Age&quot;] = &quot;55&quot;;
        table.Rows.Add(row);
        return new DataTableReader(table);
    }
}
</pre><p><a
href="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Object_Initializers_Passing_Tests-e1298322760631.png"><img
src="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Object_Initializers_Passing_Tests-e1298322760631.png" alt="Object initializers passing tests" title="Object_Initializers_Passing_Tests" width="690" height="114" class="aligncenter size-full wp-image-1656" /></a></p><p>Now if we modified the GetDataReader method to return a string value for age</p><pre class="brush: csharp; gutter: false; highlight: [10]; title: ; toolbar: true; notranslate">
public static IDataReader GetDataReader()
{
    DataTable table = new DataTable();
    DataRow row = table.NewRow();
    table.Columns.Add(new DataColumn(&quot;FirstName&quot;));
    table.Columns.Add(new DataColumn(&quot;LastName&quot;));
    table.Columns.Add(new DataColumn(&quot;Age&quot;));
    row[&quot;FirstName&quot;] = &quot;Bob&quot;;
    row[&quot;LastName&quot;] = &quot;Smith&quot;;
    row[&quot;Age&quot;] = &quot;FiftyFive&quot;;
    table.Rows.Add(row);
    return new DataTableReader(table);
}
</pre><p>We get a failing test when mapping <strong>without</strong> an object initializer as shown below</p><p><a
href="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Without_Object_Initializers_Failing_Test.png"><img
src="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Without_Object_Initializers_Failing_Test.png" alt="" title="Without Object Initializers Failing Test" width="690" height="344" class="aligncenter size-full wp-image-1695" /></a></p><p>As you can see from the output message above we can easily find the line of code (line 13) that caused the exception.</p><p>When a test fails for the code using an object initializer</p><p><a
href="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Object_Initializers_Failing_Test.png"><img
src="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Object_Initializers_Failing_Test.png" alt="" title="Object Initializers Failing Test" width="690" height="348" class="aligncenter size-full wp-image-1697" /></a></p><p>the line of code that caused the exception (line 23) is the same one in which the object was created.</p><p><em>The fact is the more properties you initialize using an object initializer the harder it will be to find the one that causes an exception.</em></p><h3 class="subheading">What’s ReSharper got to do with this?</h3><p>In a word temptation. It makes it so easy to convert your code to use one.</p><p><a
href="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Resharper_Suggestion_To_Use_Object_initializer.png"><img
src="http://www.arrangeactassert.com/wp-content/uploads/2011/02/Resharper_Suggestion_To_Use_Object_initializer.png" alt="" title="Resharper Suggestion To Use Object Initializer" width="499" height="102" class="alignleft size-full wp-image-1660" /></a></p><p><br
class="clear"/></p><h3 class="subheading">That’s not ReSharpers fault</h3><p>I agree. Ultimately it&#8217;s a developers choice if they want to use the refactoring that tools like ReSharper suggest, after all just because you can doesn&#8217;t mean you should.</p><p>However it could give you an option not to convert to object initializer if more than say ten properties were going to be initialized.</p><p>Or detect if the object initializer wasn&#8217;t a straight forward mapping and then not suggest using one like in this example.</p><h3 class="subheading">What ReSharper is missing</h3><p>What would be really useful if you could convert code that was using an object initializer to the classic way of setting properties one line at a time.</p><p>C’mon JetBrains how about it?</p><blockquote><p>It turns out you can do this in ReSharper check out this post on <a
href="http://www.arrangeactassert.com/to-assignment-statements-yes-please-resharper">using assignment statements</a>.</p></blockquote> ]]></content:encoded> <wfw:commentRss>http://www.arrangeactassert.com/use-an-object-initializer-no-thanks-resharper/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>A solution for the Resharper Test Runner not picking up your latest changes</title><link>http://www.arrangeactassert.com/a-solution-for-the-resharper-test-runner-not-picking-up-your-latest-changes/</link> <comments>http://www.arrangeactassert.com/a-solution-for-the-resharper-test-runner-not-picking-up-your-latest-changes/#comments</comments> <pubDate>Tue, 17 Nov 2009 00:19:12 +0000</pubDate> <dc:creator>Jag Reehal</dc:creator> <category><![CDATA[Resharper]]></category> <category><![CDATA[Unit tests]]></category><guid
isPermaLink="false">http://www.arrangeactassert.com/?p=463</guid> <description><![CDATA[For a while now I thought there was a bug when using Resharper to run my unit tests. Because my latest changes were not being picked up, I always had to build my solution first and then run the unit tests. Not anymore. Today a colleague showed me this &#8216;Build Settings&#8217; option Selecting ‘Always Build’ [...]]]></description> <content:encoded><![CDATA[<p>For a while now I thought there was a bug when using <a
href="http://www.jetbrains.com/resharper/">Resharper</a> to run my unit tests.</p><p>Because my latest changes were not being picked up, I always had to build my solution first and then run the unit tests.</p><p><strong>Not anymore.</strong></p><p>Today a colleague showed me this &#8216;Build Settings&#8217; option</p><p><img
src="http://www.arrangeactassert.com/wp-content/themes/resources/images/ResharperUnitTestRunnerWindow.png" alt="Resharper Unit Test Runner Window" /></p><p><br/><br
/> Selecting ‘Always Build’ makes sure the latest changes (even to config files) are used when running unit tests. The default is &#8216;Automatic&#8217; as shown above.</p><p>Just be aware of the size of your solution will have an impact on how long the tests will take to run.</p> ]]></content:encoded> <wfw:commentRss>http://www.arrangeactassert.com/a-solution-for-the-resharper-test-runner-not-picking-up-your-latest-changes/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Served from: www.arrangeactassert.com @ 2012-02-05 23:29:20 by W3 Total Cache -->
