<?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>JAPF &#187; refactoring</title>
	<atom:link href="http://www.japf.fr/tag/refactoring/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.japf.fr</link>
	<description>Jeremy Alles Presentation Foundation: WPF, .Net and modern software development</description>
	<lastBuildDate>Thu, 29 Jul 2010 07:29:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Analyzing events usage using a R# plugin</title>
		<link>http://www.japf.fr/2009/10/analyzing-events-usage-using-a-resharper-plugin/</link>
		<comments>http://www.japf.fr/2009/10/analyzing-events-usage-using-a-resharper-plugin/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 12:20:47 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[practises]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[resharper]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=286</guid>
		<description><![CDATA[As you might already know, even if the .Net framework has a garbage collector, you can easily create memory leaks in your application. The most common way to create a leak is to register to an event handler on a object that has a longer lifetime than the object where the handler is defined. The [...]]]></description>
			<content:encoded><![CDATA[<p>As you might already know, even if the .Net framework has a garbage collector, you can easily create memory leaks in your application.</p>
<p>The most common way to create a leak is to register to an event handler on a object that has a longer lifetime than the object where the handler is defined. The problem can also occurs by using static class such as EventManager (for more information see <a href="http://www.japf.fr/2009/08/wpf-memory-leak-with-eventmanager-registerclasshandler/">this blog post</a>). Some .Net developers have been working on a way to go round the problem using Reflection, Weak Reference and other cool stuff. You can check out for example <a href="http://www.codeproject.com/KB/cs/WeakEvents.aspx">this excellent article</a> on Code Project.</p>
<p>However, if you cannot change the way your declare events (because of internal policies in the company or because you don&#8217;t have the source code), you must be very carefull about the way you manage your events.</p>
<p>I&#8217;ve been working lately on a <a href="http://www.jetbrains.com/resharper/index.html">Resharper</a> plugin that helps detecting events that are never unsubscribed. Basically, what is does is the following:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/10/resharper_plugin.png" rel="lightbox[286]"><img class="size-full wp-image-287 alignnone" title="resharper_plugin" src="http://www.japf.fr/wp-content/uploads/2009/10/resharper_plugin.png" alt="resharper_plugin" width="528" height="213" /></a></p>
<p><strong>I&#8217;d like to have feedback from you .Net developpers, about whether you find such a plugin useful or not. </strong></p>
<ul>
<li>How do you deal with the event memory leak problem ?</li>
<li>Would you like to use my plugin ?</li>
<li>Would you like me to release it on a open source platform ?</li>
<li>What other kind of possible leaks are you thinking about to enhance the plugin ?</li>
</ul>
<p>Please write a comment to let my know what you think. Thank you for your feedback !</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/10/analyzing-events-usage-using-a-resharper-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The cost of building Visual Trees</title>
		<link>http://www.japf.fr/2009/10/the-cost-of-building-visual-trees/</link>
		<comments>http://www.japf.fr/2009/10/the-cost-of-building-visual-trees/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 07:36:25 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=268</guid>
		<description><![CDATA[In the current project I&#8217;m working on, I&#8217;m designing a diagramming component. Of course because it&#8217;s for work I can&#8217;t release the source code (however if you want a good starting point you can check out this article). Designing such a component is a challenging task and there are a number of things I learned [...]]]></description>
			<content:encoded><![CDATA[<p>In the current project I&#8217;m working on, I&#8217;m designing a diagramming component. Of course because it&#8217;s for work I can&#8217;t release the source code (however if you want a good starting point you can check out <a href="http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part4.aspx">this article</a>). Designing such a component is a challenging task and there are a number of things I learned during the last few months that I&#8217;m glad to share here.</p>
<p>As you problably know the WPF engine manages 2 trees: the logical tree and the visual tree. For each logical item (that you declare in the XAML), the item is expanded with its visual tree (for example, a scrollbar is made of RepeatButtons and a Thumb). To visualize the visual tree you can use a tool like <a href="http://blois.us/Snoop/">snoop</a>.</p>
<p>In a diagramming tool, you might want to draw lines betweeen entities, like the following:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/10/connection.png" rel="lightbox[268]"><img class="alignnone size-medium wp-image-270" title="connection" src="http://www.japf.fr/wp-content/uploads/2009/10/connection-300x71.png" alt="connection" width="300" height="71" /></a></p>
<p>In this article, we&#8217;re going to talk about the underlying control that we can use to do that. If you need to create that kind of control, you basically have 2 options:</p>
<ul>
<li>inherits from Control and override the OnRender method to do the basic graphic operations (such as drawing a line or an ellipse)</li>
<li>inherits from Control and create a ControlTemplate to fill the visual tree of your control</li>
</ul>
<p>Of course the first option has better performance, but it&#8217;s also less evolutive. Moreover, in the OnRender method, you can only do low level graphic operations.</p>
<p>Using the second option is generally fine and easy:</p>
<ol>
<li>create the class that inherit from Control</li>
<li>create a style that targets your new control</li>
<li>add a Setter to set the ControlTemplate property</li>
<li>fill the ControlTemplate&#8217;s content</li>
</ol>
<p>Here is a sample ControlTemplate for a basic connection (assuming that the Connection class has 4 double dependency properties (X1,Y1) (X2,Y2) that defines that start and the end point):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p268code2'); return false;">View Code</a> XML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p2682"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p268code2"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Style</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type VisualTreePerf:Connection}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter</span> <span style="color: #000066;">Property</span>=<span style="color: #ff0000;">&quot;Template&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ControlTemplate</span> <span style="color: #000066;">TargetType</span>=<span style="color: #ff0000;">&quot;{x:Type VisualTreePerf:Connection}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Line</span> <span style="color: #000066;">X1</span>=<span style="color: #ff0000;">&quot;{TemplateBinding X1}&quot;</span> <span style="color: #000066;">Y1</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Y1}&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">X2</span>=<span style="color: #ff0000;">&quot;{TemplateBinding X2}&quot;</span> <span style="color: #000066;">Y2</span>=<span style="color: #ff0000;">&quot;{TemplateBinding Y2}&quot;</span></span>
<span style="color: #009900;">                  <span style="color: #000066;">Stroke</span>=<span style="color: #ff0000;">&quot;Black&quot;</span> <span style="color: #000066;">StrokeThickness</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>                                
          <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Line<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ControlTemplate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter.Value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Setter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Style<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>Of course this is a very minimal template, you&#8217;re probable going to improve it by adding new features:</p>
<ul>
<li> 2 rectangles to show the extremities of the connection</li>
<li>2 thumb to be able to drag&#8217;n'drop the extremities</li>
<li>1 context menu to access connection&#8217;s operations</li>
</ul>
<p>You can do that by adding more controls to the template. Now, take a second and think about the performances. Time is needed to built and render the visual tree and the most complex it is, the longer it takes. I did a quick benchmark on my machine and here are the results for 500 connections added in a Canvas:</p>
<p><a href="http://www.japf.fr/wp-content/uploads/2009/10/performance.png" rel="lightbox[268]"><img class="alignnone size-medium wp-image-269" title="performance" src="http://www.japf.fr/wp-content/uploads/2009/10/performance-300x83.png" alt="performance" width="300" height="83" /></a></p>
<p>What we can see is that adding only 5 controls in the template, we multiply the rendering time by 4 ! That means we must be careful when we&#8217;re designing ControlTemplate that are going to be instantiated many time. Before creating the control we should think about whether the control is going to by displayed 1, 10, 100 or 1000 times in our application. More complex ControlTemplates mean more time to render them. Several techniques can be used to improved performance:</p>
<ul>
<li>use a panel that supports UI virtualization (such a VirtualizingStackPanel)</li>
<li>use adorners to give feedback while an item is selected to improve creation performance (create the Adorner only when the item is selected)</li>
</ul>
<p>To explore other performance related techniques:</p>
<ul>
<li> for ItemsControl, make sure to check out Charles Petzold excellent <a href="http://msdn.microsoft.com/en-us/magazine/dd483292.aspx">article on MSDN Magazine</a></li>
<li>for a virtualized canvas, check out <a href="http://blogs.msdn.com/jgoldb/archive/2008/03/08/performant-virtualized-wpf-canvas.aspx">this blog post</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/10/the-cost-of-building-visual-trees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why do I love Extension Methods in System.Linq ?</title>
		<link>http://www.japf.fr/2009/01/why-i-love-linq/</link>
		<comments>http://www.japf.fr/2009/01/why-i-love-linq/#comments</comments>
		<pubDate>Fri, 16 Jan 2009 10:06:03 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[refactoring]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=125</guid>
		<description><![CDATA[Because it helps me to write compact and clean code of course ! For example, I need to expose a property that gives the number of visible items in my ViewModel. Old way: ?View Code CSHARP1 2 3 4 5 6 7 8 9 10 11 12 13 public int VisibleItemsCount &#123; get &#123; int [...]]]></description>
			<content:encoded><![CDATA[<p>Because it helps me to write compact and clean code of course !</p>
<p>For example, I need to expose a property that gives the number of visible items in my ViewModel. </p>
<p><em>Old way:</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p125code5'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1255"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p125code5"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> VisibleItemsCount
        <span style="color: #008000;">&#123;</span>
            get
            <span style="color: #008000;">&#123;</span>
                <span style="color: #6666cc; font-weight: bold;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>var item <span style="color: #0600FF; font-weight: bold;">in</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>item<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVisible</span><span style="color: #008000;">&#41;</span>
                        i<span style="color: #008000;">++;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">return</span> i<span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
        <span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><em>Linq way:</em></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p125code6'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p1256"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p125code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> VisibleItemsCount
<span style="color: #008000;">&#123;</span>
    get
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Count</span><span style="color: #008000;">&#40;</span>item <span style="color: #008000;">=&gt;</span> item<span style="color: #008000;">.</span><span style="color: #0000FF;">IsVisible</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Which one do you prefer ?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2009/01/why-i-love-linq/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Becoming a Resharper Samourai</title>
		<link>http://www.japf.fr/2008/12/becoming-a-resharper-samourai/</link>
		<comments>http://www.japf.fr/2008/12/becoming-a-resharper-samourai/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 10:23:54 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[resharper]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=71</guid>
		<description><![CDATA[I already blogged about Resharper. Resharper is a must have plugin for any serious .Net developer because it will help you to write better code faster. If you&#8217;re not yet convinced by the power of this addin, 2 very nice short screencast (about 10min each) are available to show what you can do with Resharper: [...]]]></description>
			<content:encoded><![CDATA[<p>I already <a href="http://www.japf.fr/?p=55">blogged about Resharper</a>. <a href="http://www.jetbrains.com/resharper/">Resharper</a> is a must have plugin for any serious .Net developer because it will help you to write better code faster. If you&#8217;re not yet convinced by the power of this addin, 2 very nice short screencast (about 10min each) are available to show what you can do with Resharper:</p>
<ul>
<li><a href="http://www.dimecasts.net/Casts/CastDetails/61" target="_blank">http://www.dimecasts.net/Casts/CastDetails/61</a></li>
<li><a href="http://www.dimecasts.net/Casts/CastDetails/68" target="_blank">http://www.dimecasts.net/Casts/CastDetails/68</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/12/becoming-a-resharper-samourai/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>StyleCop + Resharper = StyleCop for Resharper addin</title>
		<link>http://www.japf.fr/2008/11/stylecop-resharper-stylecop-for-resharper-addin/</link>
		<comments>http://www.japf.fr/2008/11/stylecop-resharper-stylecop-for-resharper-addin/#comments</comments>
		<pubDate>Sat, 08 Nov 2008 09:22:30 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[practises]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[resharper]]></category>

		<guid isPermaLink="false">http://www.japf.fr/?p=55</guid>
		<description><![CDATA[If you&#8217;re a .Net developper then you MUST use ReSharper. I you don&#8217;t, I suggest you to have a look at this very nice video demonstrating some of the feature of this Visual Studio addin. If you also like having a clean C# code, then you might also use Microsoft StyleCop tool. I just found [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re a .Net developper then you <strong>MUST </strong>use ReSharper. I you don&#8217;t, I suggest you to have a look at <a href="http://www.dimecasts.net/Casts/CastDetails/61">this very nice video</a> demonstrating some of the feature of this Visual Studio addin. If you also like having a clean C# code, then you might also use <a href="http://code.msdn.microsoft.com/sourceanalysis">Microsoft StyleCop tool</a>.</p>
<p>I just found this a <a href="http://www.codeplex.com/StyleCopForReSharper">very cool Resharper</a> plugin that<span id="ctl00_ctl00_MasterContent_TabContentPanel_Content_wikiSourceLabel"> allows Microsoft StyleCop to be run as you type, generating real-time syntax highlighting of violations:</span></p>
<p><a href="http://www.japf.fr/wp-content/uploads/2008/11/stylecopresharper.png" rel="lightbox[55]"><img class="alignnone size-medium wp-image-56" title="stylecopresharper" src="http://www.japf.fr/wp-content/uploads/2008/11/stylecopresharper-300x231.png" alt="" width="397" height="305" /></a></p>
<p>The <a href="http://www.codeplex.com/StyleCopForReSharper">StyleCop for Resharper plugin</a> is hosted on <a href="http://www.codeplex.com">CodePlex</a> and is free. I think it&#8217;s a must have for any .Net developer. Its author really did a good work <img src='http://www.japf.fr/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fwww.japf.fr%2f%3fp%3d55"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.japf.fr%2f%3fp%3d55" border="0" alt="kick it on DotNetKicks.com" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.japf.fr/2008/11/stylecop-resharper-stylecop-for-resharper-addin/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
