<?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>Elegant Code &#187; Tools and Utilities</title>
	<atom:link href="http://elegantcode.com/tag/tools/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com</link>
	<description></description>
	<lastBuildDate>Sun, 12 Feb 2012 04:40:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Profiling with dotTrace</title>
		<link>http://elegantcode.com/2008/06/09/profiling-with-dottrace/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=profiling-with-dottrace</link>
		<comments>http://elegantcode.com/2008/06/09/profiling-with-dottrace/#comments</comments>
		<pubDate>Tue, 10 Jun 2008 04:42:16 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/09/profiling-with-dottrace/</guid>
		<description><![CDATA[Here is a product that I&#8217;ve been meaning to do a review of for a while, dotTrace 3.1.&#160; A .net code profiling tool from JetBrains.&#160; In other words, dotTrace helps you figure out where your code is slow.&#160; How is that not handy.&#160; As an added bonus, it will tell you what your application&#8217;s memory [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a product that I&#8217;ve been meaning to do a review of for a while, <a href="http://www.jetbrains.com/profiler/index.html">dotTrace</a> 3.1.&nbsp; A .net code profiling tool from <a href="http://www.jetbrains.com">JetBrains</a>.&nbsp; In other words, dotTrace helps you figure out where your code is slow.&nbsp; How is that not handy.&nbsp; As an added bonus, it will tell you what your application&#8217;s memory usage is doing as well.</p>
<p>I&#8217;ve been playing around with it for a month or so now and it is wonderful to have.&nbsp; This isn&#8217;t the sort of tool that you whip out all the time, but when you need it, you need it.&nbsp; No doubt there.</p>
<p>But lets throw out some software development methodologies first.&nbsp; </p>
<p><strong>Rule #1: Premature optimization is the root of all evil.</strong> (Donald Knuth said that back in 1974 if you have any issues with it).&nbsp; Again, this isn&#8217;t a tool that you should be using all the time.</p>
<p><strong>Rule #2: If you don&#8217;t measure, you don&#8217;t know.&nbsp; </strong>The is the heart of the engineering part of software development.&nbsp; At my last Boise Code Camp (March 8, 2008) I had two separate people complain to me about how slow .net was for WinForms.&nbsp; OK, so what part was so slow?&nbsp; What were you trying to do?&nbsp; Was the database properly indexed?&nbsp; Did you have measurements about what is slow?&nbsp; Nope.&nbsp; Nothing.&nbsp; I can&#8217;t comment on their code, I didn&#8217;t see it.&nbsp; But for all they know the problem could have been anywhere in their code.&nbsp; It could have been the database was improperly tuned, it could be they were running on really slow hard-drives.&nbsp; They simply did not know.&nbsp; To me that is not acceptable.</p>
<p><strong>Rule #3: Where-ever you thing the problem is &#8212; is wrong.</strong>&nbsp; This really is just a continuation of Rule #2.&nbsp; But it is directly related to how many people I&#8217;ve talk to that get disgusted with me when I talk about ORMs.&nbsp; &#8220;They are too slow&#8221; is the answer I get.&nbsp; Accordingly, I&#8217;ve talked to two developers for whom that was true, for the particular application they were writing.&nbsp; For the rest, their app was slow, so they were assuming it mush be the fault of the ORM &#8212; and telling everyone else as much as well.&nbsp; Also not acceptable.&nbsp; They did not know where their problems actually were.</p>
<p>I&#8217;ve made this mistake too many times to count myself.&nbsp; Spending countless hours optimizing and tuning code that had no net effect in the end.&nbsp; I hadn&#8217;t tested, and it proved to be wrong.&nbsp; And how many of you checked that code in anyway, because you didn&#8217;t want to see all that time go to waist?</p>
<p><strong>Rule #4: This isn&#8217;t the only tool you should be using.</strong>&nbsp; A properly written application should be testable with unit tests.&nbsp; It should also have unit tests.&nbsp; If you find the problem with your code, you should be able to reproduce it with your unit tests, measure it from there, and then possibly fix it.&nbsp; That is the heart of the Red-Green-Refactor motto of TDD.&nbsp; A code profiler will help you FIND the problem.&nbsp; Unit Tests should help you FIX the problem and know it is fixed.</p>
<h2>Enough already, on to the review</h2>
<p>First of, on Rule #4: DotTrace paired with ReSharper actually make a wonderful paring.&nbsp; The two integrate in together so you can launch DotTrace from your test cases in the ReSharper unit test runner.&nbsp; That is just plain beautiful. </p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/06/dottrace-003.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="125" alt="DotTrace-003" src="http://elegantcode.com/wp-content/uploads/2008/06/dottrace-003-thumb.png" width="241" border="0"></a> </p>
<p>On Rule #3: DotTrace&#8217;s UI is a tree structure that really does allow you to follow the bread crumbs to find where the performance problems are.&nbsp; Here is a look at SubSonic SubStage (a nice little application that helps generate your SubSonic files for you, as well as show results of the tables).</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/06/dottrace-002.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="192" alt="DotTrace-002" src="http://elegantcode.com/wp-content/uploads/2008/06/dottrace-002-thumb.png" width="244" border="0"></a> </p>
<p>I was looking at the boot up time of the application.&nbsp; Using the tree and the small iconography I was quickly able to figure out that a good chuck of time was being spent in the NavigateFileBrowser method.&nbsp; Below the tree view a listing of the code is displayed as well.</p>
<p>Another quick note about the display, the grayed out text is system code.&nbsp; DotTrace doesn&#8217;t detect a pdb, so it can&#8217;t show you the code that is being executed.</p>
<p>On Rule #2: You can store snapshots &#8212; and compare them.&nbsp; Actually there are three ways to compare snapshots.&nbsp; Through the call tree (showing the call stack), plain view (showing slowest functions first), and Hot Spots (top 100 problem methods).&nbsp; </p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/06/dottrace-004.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="154" alt="DotTrace-004" src="http://elegantcode.com/wp-content/uploads/2008/06/dottrace-004-thumb.png" width="244" border="0"></a> </p>
<p>It is amazing how often some seemingly benign code will have significant performance impact.&nbsp; Best part, now you can find out where the problem is&#8230;and where it is getting worse.</p>
<p>As for Rule #1, it might take a while to figure out, but it is true. But the key to the rule, and all software development, is to have good tools to help you along.&nbsp; This is a good tool, and this is just the beginning of its features.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/09/profiling-with-dottrace/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ReSharper 4.0 Beta Available</title>
		<link>http://elegantcode.com/2008/06/03/resharper-40-beta-available/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resharper-40-beta-available</link>
		<comments>http://elegantcode.com/2008/06/03/resharper-40-beta-available/#comments</comments>
		<pubDate>Tue, 03 Jun 2008 16:33:11 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/06/03/resharper-40-beta-available/</guid>
		<description><![CDATA[The actual Beta for ReSharper 4.0 is out now.&#160;&#160; Also check out the new features. There was one cool bit I saw on the install (yes, I already installed it), was a little graphic showing a light saber with a little bit of text below it: Tools Matter. I absolutely agree.]]></description>
			<content:encoded><![CDATA[<p>The actual Beta for <a href="http://www.jetbrains.com/resharper/beta/beta.html">ReSharper 4.0</a> is out now.&nbsp;&nbsp; Also check out the <a href="http://www.jetbrains.com/resharper/beta/beta_newfeatures.html">new features</a>.</p>
<p>There was one cool bit I saw on the install (yes, I already installed it), was a little graphic showing a light saber with a little bit of text below it: <strong>Tools Matter</strong>.</p>
<p>I absolutely agree.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/06/03/resharper-40-beta-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft Source Analysis</title>
		<link>http://elegantcode.com/2008/05/25/microsoft-source-analysis/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=microsoft-source-analysis</link>
		<comments>http://elegantcode.com/2008/05/25/microsoft-source-analysis/#comments</comments>
		<pubDate>Mon, 26 May 2008 04:30:11 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/05/25/microsoft-source-analysis/</guid>
		<description><![CDATA[First question: do you use FxCop?&#160; Do I&#8230;not really.&#160; But I try not to tell too many people that either. So there is a new tool out there for all of you who need help keeping your source code up to snuff, similar to what FxCop does for your API&#8230;just at the source code level.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>First question: do you use FxCop?&nbsp; Do I&#8230;not really.&nbsp; But I try not to tell too many people that either.</p>
<p>So there is a new tool out there for all of you who need help keeping your source code up to snuff, similar to what FxCop does for your API&#8230;just at the source code level.&nbsp; It is called the <a href="http://code.msdn.microsoft.com/sourceanalysis">Microsoft Source Analysis</a> for C# (sorry VB). BTW, there is also a <a href="http://blogs.msdn.com/sourceanalysis/">blog</a>.</p>
<p>I happen to have a project that I am in the midst of doing some spring cleaning anyway, so I decided to try it out.&nbsp; You right click on the project or class you want to check, and select &#8220;Run Source Analysis&#8221;.&nbsp; Then in a tool window you will see a list of your failings as a developer.</p>
<p>Right off the bad I had a few issues with the list of failures.&nbsp; Every code file has to have a header.</p>
<p>Here is my &#8220;new&#8221; standard header to make it happy:</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   1:</span> <span style="color: #008000">// &lt;copyright file="Axis.cs" company="DiamondB Software"&gt;</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"><span style="color: #606060">   2:</span> <span style="color: #008000">//     Copyright 2008, DiamondB Software. All rights reserved.</span></pre>
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #606060">   3:</span> <font color="#008000">// &lt;/copyright&gt;</font></pre>
</div>
</div>
<p>Lucky for me ReSharper can make that easy to add, but the ReSharper standard &#8220;File Header Text&#8221; in the ReSharper options does not allow you to have macros, or that would have been a lot easier.</p>
<p>Also, the Source Analysis does not like underscores on field names.&nbsp; So this will get a red &#8216;x&#8217;</p>
<div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4">
<div style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none">
<pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: white; border-bottom-style: none"><span style="color: #0000ff">private</span> <span style="color: #0000ff">string</span> _name;</pre>
</div>
</div>
<p>and that is just not right.&nbsp; In fact, I want just the opposite. (Note: although, this will make one cranky old dutchman that I know very happy &#8212; he doesn&#8217;t like underscores).</p>
<p>But lucky for me, it is easy to turn things off, which I do.&nbsp; Right-click on your project file and select &#8220;Source Analysis Settings&#8221; and you can change anything you want.&nbsp; The bad news, it is on a project by project basis.&nbsp; The good news is that the files are merge-able.&nbsp;&nbsp; This also means there is a new file that gets added to your project, mine is: Settings.SourceAnalysis.&nbsp; Be sure to add that to your source control.&nbsp; <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The other thing I like about it: it detects Hungarian notation (a prior sin that I am currently making penance for).&nbsp; &#8220;SA1305: The variable name &#8216;oElement&#8217; begins with a prefix that looks like Hungarian notation.&#8221;&nbsp; But, if you like that sort of thing, back in the options is a way to tell the tools which Hungarian prefixes are acceptable.</p>
<h3>But this could get ugly</h3>
<p>OK, there are a few other concerns looming here.&nbsp; I&#8217;m calling it Dueling Source Officers.&nbsp; One tool will say two spaces between methods, the next will say one space.&nbsp; One tool like spaces, the other likes tabs.&nbsp; (for the record, Source Analysis HATES tabs, and thinks multiple lines between methods is plane evil).&nbsp; The other officer: ReSharper Code Analysis and ReSharper Code Reformat.&nbsp; Lucky for us ReSharper&#8217;s tools are very configurable&#8230;but it could take a while to get the two in sync.</p>
<p>Not throw in FxCop (now integrated into the Team Systems version of Visual Studio) and it can get very interesting.</p>
<h3>Next steps</h3>
<p>The next steps are simple: </p>
<p>1. Learn how to write your own rules (I want my underscores)<br />2. Figure out how to integrate into Team City and Cruise Control (Code Analysis ships with an MSBuild task), and get something meaning full out of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/05/25/microsoft-source-analysis/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>WPF startup vs NCover</title>
		<link>http://elegantcode.com/2008/05/21/wpf-startup-vs-ncover/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wpf-startup-vs-ncover</link>
		<comments>http://elegantcode.com/2008/05/21/wpf-startup-vs-ncover/#comments</comments>
		<pubDate>Wed, 21 May 2008 20:42:20 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/05/21/wpf-startup-vs-ncover/</guid>
		<description><![CDATA[An interesting issue I&#8217;ve been trying to figure out lately, why is WPF so blasted slow on my laptop?&#160; How slow?&#160; It takes 90 seconds to open a Hello World WPF app.&#160; Heck, it takes 90 seconds to load a WPF window with no &#8220;Hello World&#8221; or anything else for that matter.&#160; But to make [...]]]></description>
			<content:encoded><![CDATA[<p>An interesting issue I&#8217;ve been trying to figure out lately, why is WPF so blasted slow on my laptop?&nbsp; How slow?&nbsp; It takes 90 seconds to open a Hello World WPF app.&nbsp; Heck, it takes 90 seconds to load a WPF window with no &#8220;Hello World&#8221; or anything else for that matter.&nbsp; But to make life more interesting, if I was to load a WPF app that actually does something, like on of the Prism samples, it also takes 90 seconds to load.</p>
<p>I talked with <a href="http://elegantcode.com/about/scott-nichols/">one of my buddies who is a budding WPF expert</a> who told me I was crazy and needed to get a psychologist.&nbsp; He is probably right.&nbsp; But something was telling me that this was a system problem, not a WPF problem.&nbsp; But for the life of me I wasn&#8217;t seeing it.</p>
<p>Every programmer needs a toolbox.&nbsp; Turns out there are several performance analyzing tools for WPF that ship with the Windows SDK.&nbsp; One of them is the <a href="http://wpf.netfx3.com/files/folders/developer/entry10880.aspx">WPF Performance Suite</a>.&nbsp; While the tool is really cool&#8230;it didn&#8217;t help at all.&nbsp; If you want to check the performance of your WPF application while it is RUNNING, this tool will be great.</p>
<p>My problem was start up.&nbsp; Here was the breakdown: 60 seconds before I saw a WPF window, 30 seconds before anything displayed.&nbsp; After that, everything ran fine.&nbsp; Time for a different tool.&nbsp; Namely: <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a> from <a href="http://technet.microsoft.com/en-us/sysinternals/default.aspx">SysInternal</a> (via Microsoft.com).</p>
<p>I was able to find my Hello World WPF application in Process Explorer (on my system, all .Net processes are colored yellow), then displayed the lower pane to display the DLLs used by the process.&nbsp; (BTW: I just confirmed that Windows Live Writer is a .Net application).</p>
<p>I found one interesting dll that was referenced: <a href="http://www.ncover.com/">NCover</a>.&nbsp; A while back I had installed a trial version of the commercial product (this was not the <a href="http://ncover.sourceforge.net/">open source version</a>). I have no idea what it was doing there, but it was there.&nbsp; In fact, it was referenced in every WPF application that I loaded &#8212; including WPF applications that I did not write, like the <a href="http://windowsclient.net/wpf/starter-kits/sce/sce-get-more-details.aspx">Subscription Center</a>.</p>
<p>Long story made only slightly longer, I uninstalled NCover and now everything loads in about 10 seconds (a nine fold improvement), but I still don&#8217;t know what NCover was doing.&nbsp; If you know, please let me know.&nbsp; </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/05/21/wpf-startup-vs-ncover/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ReSharper 4.0 goes Beta (Candidate)</title>
		<link>http://elegantcode.com/2008/05/19/resharper-40-goes-beta/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resharper-40-goes-beta</link>
		<comments>http://elegantcode.com/2008/05/19/resharper-40-goes-beta/#comments</comments>
		<pubDate>Mon, 19 May 2008 16:14:18 +0000</pubDate>
		<dc:creator>Chris Brandsma</dc:creator>
				<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/05/19/resharper-40-goes-beta/</guid>
		<description><![CDATA[I just saw this on the wire: ReSharper 4.0 has entered Beta a Beta Candidate now as of build 804. If you want it, go get it here: http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds If you just want to see what is new: check out this page: http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+EAP+Notes Finally, there is another deal going on at JetBrains.&#160; DotTrace has gone [...]]]></description>
			<content:encoded><![CDATA[<p>I just saw this on the wire: ReSharper 4.0 has <strike>entered Beta</strike> a Beta Candidate now as of build 804. If you want it, go get it here: <a title="http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds" href="http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds">http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds</a></p>
<p>If you just want to see what is new: check out this page: <br /><a title="http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+EAP+Notes" href="http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+EAP+Notes">http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+EAP+Notes</a></p>
<p>Finally, there is another deal going on at JetBrains.&nbsp; <a href="http://www.jetbrains.com/profiler/index.html">DotTrace</a> has gone to version 3.1.&nbsp; For a limited time you can now buy the Personal version of DotTrace for <a href="http://www.jetbrains.com/profiler/buy/buy.html#personal">$199</a>.</p>
<p><em>Note: </em><a href="http://resharper.blogspot.com/"><em>Ilya Ryzhenkov</em></a><em> corrected me, this is just a Beta Candidate.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/05/19/resharper-40-goes-beta/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter</title>
		<link>http://elegantcode.com/2008/04/29/twitter/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=twitter</link>
		<comments>http://elegantcode.com/2008/04/29/twitter/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 05:18:06 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/29/twitter/</guid>
		<description><![CDATA[I am undecided on Twitter. The jury is still out in my mind. On any given day, there are things I need to do and things I would like to do. Not taking into account family things, I read through my emails, both work and personal, read through my RSS feeds, news feeds, weather reports, [...]]]></description>
			<content:encoded><![CDATA[<p>I am undecided on <a title="http://twitter.com" href="http://twitter.com" target="_blank">Twitter</a>. The jury is still out in my mind. On any given day, there are things I need to do and things I would like to do. Not taking into account family things, I read through my emails, both work and personal, read through my RSS feeds, news feeds, weather reports, IM with close friends and family, read a few credible news sources, and somewhere find time to work and research new and existing technologies. If I can get that far, that is a busy day, and that does not include family. </p>
<p>So where am I supposed to find time to update everyone on Twitter? How is Twitter fitting into your schedule?</p>
<p><a title="http://twitter.com/bamueller" href="http://twitter.com/bamueller" target="_blank">You can follow me</a>, but I am not really all that active, at least until I find more time. </p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/29/twitter/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Unity 1.0 EventBroker Sample CAB Style</title>
		<link>http://elegantcode.com/2008/04/09/unity-10-eventbroker-sample-cab-style/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=unity-10-eventbroker-sample-cab-style</link>
		<comments>http://elegantcode.com/2008/04/09/unity-10-eventbroker-sample-cab-style/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 06:55:52 +0000</pubDate>
		<dc:creator>Scott Nichols</dc:creator>
				<category><![CDATA[Esoterica]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Open Source Software]]></category>
		<category><![CDATA[Patterns and Practices]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/09/unity-10-eventbroker-sample-cab-style/</guid>
		<description><![CDATA[Ok so Unity 1.0 has shipped, or should I say the “Unity Application Block” as Microsoft is terming it now. However, in this post I am not going to talk about the Unity IoC contain but a pretty cool peace of code that comes bundled with Unity’s QuickStart samples, the Event Broker Extension. The Event [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so <a href="http://www.codeplex.com/unity">Unity 1.0</a> has shipped, or should I say the “Unity Application Block” as Microsoft is terming it now. However, in this post I am not going to talk about the Unity <a href="http://en.wikipedia.org/wiki/Inversion_of_Control">IoC</a> contain but a pretty cool peace of code that comes bundled with Unity’s QuickStart samples, the Event Broker Extension.</p>
<p>The Event Broker Extension QuickStart demonstrates how you can extend the Unity container by adding a custom extension. The QuickStart implements an Event Broker for the container as a container extension and demonstrates the new extension using the StopLight QuickStart application sample.</p>
<p>The Event Broker Extension QuickStart contains three projects:</p>
<ul>
<li><strong>EventBroker</strong>. This project implements a simple publish and subscribe mechanism that supports multiple event publishers and multiple subscribers.</li>
<li><strong>EventBrokerExtension</strong>. This project implements the custom container extension that allows applications to publish and subscribe to events using attributes or explicitly using code.</li>
<li><strong>StopLight</strong>. This project is basically the same as that described in the Unity StopLight QuickStart, but it uses the custom container extension to manage the publishing of, and subscription to, two events within the application.</li>
</ul>
<p>Now, why is this Unity EventBroker interesting? Well, if you have ever worked with <a href="http://www.cabpedia.com/index.php?title=Composite_UI_Application_Block">CAB</a> (Composite UI Application Block) or <a href="http://www.cabpedia.com/index.php?title=Smart_Client_Software_Factory">SCSF</a> (Smart Client Software Factory) then you know one of its main pillars was the implementation of a nice Pub/Sub event subscription framework which Microsoft called Event Broker. This was one of the best things about CAB. Unfortunately with CAB it is an all or nothing deal. You really cannot use the CAB Event Broker without using the rest of the CAB underpinnings.</p>
<p>The Unity implementation of the Event Broker is similar to CABs so you should feel right at home if you have any experience with CAB or SCSF. The one huge benefit is that the Unity Event Broker can easily be integrated into any existing WinForm or WPF application where you would like an easy to manage Event bus to handle all your event traffic.</p>
<p>It really is easy to implement, lets take a look at what you need to do in order to integrate the Unity based Event Broker into your application.</p>
<p>1) Download <a href="http://www.codeplex.com/unity">Unity 1.0</a> install it and unzip the “Unity QuickStart” package found in the Unity start menu installation path.</p>
<p>2) Compile the EventBrokerExtention and SimpleEventBroker projects.  Then grab the following DLLs found in the EventBrokerExtention bin folder and reference them in your application.<br />
  EventBrokerExtension.dll<br />
  SimpleEventBroker.dll<br />
  Microsoft.Practices.ObjectBuilder2.dll<br />
  Microsoft.Practices.Unity.dll</p>
<p>3) Now open your WinForm application and modify your entry point usually the Program.cs file which in my demo calls the FormMain.cs using a Unity Container like so.</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> System.Linq;
<span class="kwrd">using</span> System.Windows.Forms;
<span class="kwrd">using</span> EventBrokerExtension;
<span class="kwrd">using</span> Microsoft.Practices.Unity; 

<span class="kwrd">namespace</span> SampleWinApp
{
    <span class="kwrd">static</span> <span class="kwrd">class</span> Program
    {
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// The main entry point for the application.</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        [STAThread]
        <span class="kwrd">static</span> <span class="kwrd">void</span> Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(<span class="kwrd">false</span>); 

            <span class="rem">// Create parent container</span>
            IUnityContainer mainContainer = <span class="kwrd">new</span> UnityContainer()
                  .AddNewExtension&lt;EventBrokerBusExtension&gt;(); 

            Application.Run(mainContainer.Resolve&lt;FormMain&gt;());
        }
    }
}</pre>
<p>The important thing to notice is that I have wired up the EventBrokerExtention class through the Unity AddNewExtention method.  This will cause all the Events that are properly decorated with the correct attributes to be automatically registered in the EventBroker or SimpleEventBroker class.</p>
<p>You will also want to go and modified all the areas in your code where you are opening other forms to use a Unity container as well.  Here is an example opening a Form called FromA.cs </p>
<pre class="csharpcode"><span class="rem">//Create container</span>
IUnityContainer myContainer = <span class="kwrd">new</span> UnityContainer()
           .AddNewExtension&lt;EventBrokerBusExtension&gt;();
<span class="rem">//Open Form</span>
FormA newForm = myContainer.Resolve&lt;FormA&gt;();
newForm.Show();</pre>
<style type="text/css">    .csharpcode, .csharpcode pre  {  	font-size: small;  	color: black;  	font-family: consolas, "Courier New", courier, monospace;  	background-color: #ffffff;  	/*white-space: pre;*/  }  .csharpcode pre { margin: 0em; }  .csharpcode .rem { color: #008000; }  .csharpcode .kwrd { color: #0000ff; }  .csharpcode .str { color: #006080; }  .csharpcode .op { color: #0000c0; }  .csharpcode .preproc { color: #cc6633; }  .csharpcode .asp { background-color: #ffff00; }  .csharpcode .html { color: #800000; }  .csharpcode .attr { color: #ff0000; }  .csharpcode .alt   {  	background-color: #f4f4f4;  	width: 100%;  	margin: 0em;  }  .csharpcode .lnum { color: #606060; }</style>
<p>Oh, make sure you import the proper libraries on all your forms.</p>
<pre class="csharpcode"><span class="kwrd">using</span> Microsoft.Practices.Unity;
<span class="kwrd">using</span> EventBrokerExtension;
<span class="kwrd">using</span> SimpleEventBroker;</pre>
<p>Now you need to setup all the Event publications and triggers. Let’s start simple, say we have a button on our FormMain that is used to force a data refresh in your application. You would need to add a publish event to your page like so.</p>
<pre class="csharpcode"><span class="rem">//Using an attribute register an event in the EventBroker</span>
[Publishes(<span class="str">"RefreshData"</span>)]
<span class="kwrd">public</span> <span class="kwrd">event</span> EventHandler RefreshData ;
<span class="rem">//Method you will call to trigger the event in the EventBroker </span>
<span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnRefreshDataChange(<span class="kwrd">object</span> sender, EventArgs ea)
{
    EventHandler handlers = RefreshData ;
    <span class="kwrd">if</span> (handlers != <span class="kwrd">null</span>)
    {
        handlers(<span class="kwrd">this</span>, EventArgs.Empty);
    }
}</pre>
<p>On your button click trigger the event like so.</p>
<pre class="csharpcode"><span class="kwrd">private</span> <span class="kwrd">void</span> buttonRefresh_Click(<span class="kwrd">object</span> sender, EventArgs e)
{
    <span class="rem">//Trigger your event </span>
    OnRefreshDataChange(<span class="kwrd">this</span>, e);
}</pre>
<p>Now you need to setup all your Event subscriptions, this is supper easy.  You will need to create a method on all the forms in your application that will handle your refresh command i.e. implements the functionality you wish for each form or control in your application.  So far in this contrived example I have two forms FormMain and FormA.  You would add a method like so to each page.    </p>
<pre class="csharpcode">[SubscribesTo(<span class="str">"RefreshData"</span>)]
<span class="kwrd">public</span> <span class="kwrd">void</span> OnRefreshDataFired(<span class="kwrd">object</span> sender, EventArgs e)
{
         <span class="rem">//Do something, we need to do a data refresh</span>
}</pre>
<style type="text/css">    .csharpcode, .csharpcode pre  {  	font-size: small;  	color: black;  	font-family: consolas, "Courier New", courier, monospace;  	background-color: #ffffff;  	/*white-space: pre;*/  }  .csharpcode pre { margin: 0em; }  .csharpcode .rem { color: #008000; }  .csharpcode .kwrd { color: #0000ff; }  .csharpcode .str { color: #006080; }  .csharpcode .op { color: #0000c0; }  .csharpcode .preproc { color: #cc6633; }  .csharpcode .asp { background-color: #ffff00; }  .csharpcode .html { color: #800000; }  .csharpcode .attr { color: #ff0000; }  .csharpcode .alt   {  	background-color: #f4f4f4;  	width: 100%;  	margin: 0em;  }  .csharpcode .lnum { color: #606060; }</style>
<p>Now anytime the RefreshData button is pushed the event is fired and all objects subscribed to that event in the EventBroker will be triggered. </p>
<p>One important step you will want to do is unregister your subscribed events every time a modal form is closed.  Remember we are using the Unity IoC container to open a form which automatically registers all publisher and subscriber event handles on form_load (opened). </p>
<pre class="csharpcode"><span class="rem">// Unregister your subscription to help prevent runtime errors</span>
<span class="kwrd">private</span> <span class="kwrd">void</span> FormA_FormClosing(<span class="kwrd">object</span> sender, FormClosingEventArgs e)
{
     EventBroker myEB = <span class="kwrd">new</span> EventBroker();
     myEB.UnregisterSubscriber(<span class="str">"RefreshData"</span>, OnRefreshDataFired);
}</pre>
<p>That is about it, I used this to clean up an old legacy application that had Window events thrown everywhere.  The SimpleEventBroker class has several methods you can use to monitor what is going on inside the EventBroker such as GetPublisher, GetSubscribers, etc.</p>
<p>Oh, in my implementation I modified the Dictionary in the SimpleEventBroker class which holds the registered published events to be static, makes it easier if you have lots of modal forms, just make sure the Keys (string values) are unique.</p>
<pre class="csharpcode">

<span class="kwrd">public</span> <span class="kwrd">class</span> EventBroker
    {
        <span class="kwrd">private</span> <span class="kwrd">static</span> Dictionary&lt;<span class="kwrd">string</span>, PublishedEvent&gt; eventPublishers  

        = <span class="kwrd">new</span> Dictionary&lt;<span class="kwrd">string</span>, PublishedEvent&gt;();</pre>
<style type="text/css">    .csharpcode, .csharpcode pre  {  	font-size: small;  	color: black;  	font-family: consolas, "Courier New", courier, monospace;  	background-color: #ffffff;  	/*white-space: pre;*/  }  .csharpcode pre { margin: 0em; }  .csharpcode .rem { color: #008000; }  .csharpcode .kwrd { color: #0000ff; }  .csharpcode .str { color: #006080; }  .csharpcode .op { color: #0000c0; }  .csharpcode .preproc { color: #cc6633; }  .csharpcode .asp { background-color: #ffff00; }  .csharpcode .html { color: #800000; }  .csharpcode .attr { color: #ff0000; }  .csharpcode .alt   {  	background-color: #f4f4f4;  	width: 100%;  	margin: 0em;  }  .csharpcode .lnum { color: #606060; }</style>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/09/unity-10-eventbroker-sample-cab-style/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Removing Duplicate Code in Functions</title>
		<link>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=removing-duplicate-code-in-functions</link>
		<comments>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 04:06:40 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Testing/QA]]></category>
		<category><![CDATA[Tools and Utilities]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/</guid>
		<description><![CDATA[Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad. After seeing too many duplications across methods in one or more classes, I decided to [...]]]></description>
			<content:encoded><![CDATA[<p>Duplicate code to me is wrong. Writing duplicate code to me is like using poor grammar. If I am unaware of it, I am none the wiser. However, if I knowingly use poor grammar or duplicate code, I feel bad.</p>
<p>After seeing too many duplications across methods in one or more classes, I decided to investigate a way to remove these. I am always looking to remove duplicate code, even code that shares similarities, I look to refator. Removing duplications is important to adapt more easily to change. When a code change is required, we should only have to make it in one place. The following article will show two relatively simple means to address this code smell, delegates and an Aspect-Oriented approach.</p>
<p>The example I am using in this article is seen below. It is a unit test class, StackFixture class, and it is extremely trivial. This is a typical unit test taken from <a target="_blank" href="http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1207679353&amp;sr=8-1" title="Test-Driven Development in Microsoft .NET">Test-Driven Development in Microsoft .NET</a>. I have added the logging functionality as an easy means to show how these types of DRY violations can occur. While the duplications in this example deal with logging, they could pertain to other, more complex, functionality as well. The approaches to removing duplications across methods in this article can work with these simple scenarios, as well as more complex ones.</p>
<p>Take for example this sample test case. Each test method logs a message before and after executing the test logic. This violates DRY. We want to keep our test logic in our method, but factor out the duplicate logging. Again, if we were not logging, but doing some repetitive logic, we could factor that out as well.</p>
<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture : AbstractBaseFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Empty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>         Log.Info(<span style="color: #006080">"Starting Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         Log.Info(<span style="color: #006080">"Ending Empty test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span>         Log.Info(<span style="color: #006080">"Starting PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Log.Info(<span style="color: #006080">"Ending PushOne test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>         Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> PopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  48:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  49:</span>         Log.Info(<span style="color: #006080">"Starting PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  50:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  51:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  52:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  53:</span>         Log.Info(<span style="color: #006080">"Ending PopEmptyStack test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  54:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  55:</span> }</pre>
<p><strong>Remove Duplications with Delegates</strong></p>
<p>Quite simply, we can remove our method duplications using a delegate, in this case, the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. Thanks to <a target="_blank" href="http://www.ayende.com/" title="Ayende">Ayende</a> for helping me understand this option. We create a method, TestMethod (as shown below), and add it to our StackFixture class. The method takes two strings (string beforeMessage and string afterMessage) and the Action delegate, representing the test method logic. We will call this method within our test cases.</p>
<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestMethod(<span style="color: #0000ff">string</span> beforeMessage, <span style="color: #0000ff">string</span> afterMessage, Action action)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(beforeMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #008000">// invoke our method</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     action();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(afterMessage);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
<p>Then, in our tests, we replace the following test method&#8230;</p>
<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     Log.Info(<span style="color: #006080">"Starting Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>     Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>     Log.Info(<span style="color: #006080">"Ending Pop test"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
<p>With the same method using our delegate approach.</p>
<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Pop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     TestMethod(<span style="color: #006080">"Starting Pop test"</span>, <span style="color: #006080">"Ending Pop test"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span>                <span style="color: #0000ff">delegate</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>                    {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>                        stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>                        stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>                        Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span>                    });</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span> }</pre>
<p>We can replace each of our test methods with the same TestMethod using the <a target="_blank" href="http://msdn2.microsoft.com/en-us/library/system.action.aspx" title="System.Action delegate">System.Action delegate</a>. It will then easy to omit our before and after strings replacing them with &#8220;action.Method.Name,&#8221; or some other intelligent logic to determine what to log.</p>
<p>This approach works well, but delegates are often difficult to understand. If this solution suits your needs, make sure the implementation is easily maintainable. What is nice about this approach is how simple it is. There is no need to reference any assemblies outside of the .Net framework, i.e. no third party dependencies.</p>
<p><strong>The Aspect-Oriented Approach</strong></p>
<p><a target="_blank" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" title="Aspect-Oriented Programming">Aspect-Oriented Programming</a> (AOP) is an entirely different animal. It certainly deserves more than just a blog post. Please read more about it online. This article will not explain the details of AOP.</p>
<p>If you are reading this, welcome back. I will assume you are now familiar with AOP. There are several options for AOP frameworks. Some frameworks I have used are <a target="_blank" href="http://www.springframework.net/" title="Spring.net">Spring.net</a>, <a target="_blank" href="http://www.castleproject.org/" title="Castle Project">Castle Project</a>, and <a target="_blank" href="http://www.postsharp.org/" title="PostSharp">PostSharp</a>. The former two provide IoC capabilities as well. I suggest using a well-supported framework, one with community support and frequent updates. Investigate for yourself, there are several nice options available.</p>
<p><strong>AOP with Castle DynamicProxy</strong></p>
<p><a target="_blank" href="http://hammett.castleproject.org/" title="Hamilton Verissimo">Hamilton Verissimo</a> put together a good <a target="_blank" href="http://www.codeproject.com/KB/cs/hamiltondynamicproxy.aspx" title="sample using Castle's DynamicProxy">sample using Castle&#8217;s DynamicProxy</a>. It is a nice, clean implementation.This worked fine for me and would work well in other situations. However, in my scenario, if I were to use this approach, I needed to have each of my test classes extend from MarshalByRef. In addition, I would need to modify the underlying NUnit framework to create my proxies in order to provide advice. Since I could not do the latter, or did not want to investigate, I searched for other AOP options.</p>
<p>The DynamicProxy aproach to solving your AOP needs is a great option. It just did not work in my situation, only because NUnit executes each of my methods. I would need to somehow intercept the creation of my test class, create a proxy of it, and execute the test methods on it.</p>
<p><strong>AOP with PostSharp</strong></p>
<p><a target="_blank" href="http://www.postsharp.org" title="Gael Fraiteur's PostSharp">Gael Fraiteur&#8217;s PostSharp</a> is a great option for AOP needs. It is extremely clean and easy to use. It is the simplest AOP framework I have used. I suggest watching Gael&#8217;s <a target="_blank" href="http://www.postsharp.org/about/video/" title="video tutorial">video tutorial</a>.</p>
<p>The first thing I needed to do, besides downloading and installing PostSharp, is to add two references to my project, PostSharp.Laos and PostSharp.Public. After that, I create a simple class extending OnMethodInvocationAspect, called LoggingMethodInvocationAspect.</p>
<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [Serializable]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">sealed</span> <span style="color: #0000ff">class</span> LoggingMethodInvocationAspect : OnMethodInvocationAspect</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     <span style="color: #0000ff">private</span> ILogger log;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span>     <span style="color: #0000ff">public</span> LoggingMethodInvocationAspect(ILogger logger)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span>         log = logger;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> OnInvocation(MethodInvocationEventArgs eventArgs)</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>         <span style="color: #008000">// before</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         log.Info(<span style="color: #006080">"OnInvocation Before proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span>         <span style="color: #008000">// invoke</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>         eventArgs.Proceed();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span>         <span style="color: #008000">// after</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>         log.Info(<span style="color: #006080">"OnInvocation After proceed"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span> }</pre>
<p>At this point, all we need to do is apply our aspect on target methods. The &#8220;AttributeTargetMembers&#8221; attribute can use wildcards. This tells the AOP framework to only intercept those methods in the &#8220;MathService&#8221; class that start with &#8220;Test.&#8221; Below is the sample where I specify the target assembly, target type (class or classes to intercept), and target methods to intercept. There are many features beyond what I am showing so do further investigation.</p>
<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>     AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>     AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>     AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<p>Finally, my StackFixture class now looks something like this. Notice that the duplicate logging logic is now removed and each test method is prefixed with &#8220;Test&#8221; in the method name. This is so that PostSharp can filter what methods to intercept.</p>
<p style="font-size: 8pt; margin: 20px 0px 10px; overflow: auto; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border: gray 1px solid; padding: 4px">
<p style="font-size: 8pt; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px">
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   1:</span> [assembly: ClassLibrary.SampleInterfaces.LoggingMethodInvocationAspect(</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   2:</span>   AttributeTargetAssemblies = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   3:</span>   AttributeTargetTypes = <span style="color: #006080">"ClassLibrary.UnitTesting.Sandbox.MathService"</span>,</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   4:</span>   AttributeTargetMembers = <span style="color: #006080">"Test*"</span>)]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   5:</span></pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   6:</span> [TestFixture]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> StackFixture</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">   8:</span> {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">   9:</span>     <span style="color: #0000ff">private</span> Stack stack;</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  10:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  11:</span>     [SetUp]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  12:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> SetUp()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  13:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  14:</span>         stack = <span style="color: #0000ff">new</span> Stack();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  15:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  16:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  17:</span>     [TearDown]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  18:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TearDown(){}</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  19:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  20:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  21:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestEmpty()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  22:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  23:</span>         Assert.IsTrue(stack.IsEmpty);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  24:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  25:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  26:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  27:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPushOne()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  28:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  29:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  30:</span>         Assert.IsFalse(stack.IsEmpty, <span style="color: #006080">"After Push, IsEmpty should be false."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  31:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  32:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  33:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  34:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPop()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  35:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  36:</span>         stack.Push(<span style="color: #006080">"first element"</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  37:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  38:</span>         Assert.IsTrue(stack.IsEmpty, <span style="color: #006080">"After Push - Pop, IsEmpty should be true."</span>);</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  39:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  40:</span> </pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  41:</span>     [Test]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  42:</span>     [ExpectedException(<span style="color: #0000ff">typeof</span>(InvalidOperationException))]</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  43:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> TestPopEmptyStack()</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  44:</span>     {</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  45:</span>         stack.Pop();</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: #f4f4f4; border-style: none; padding: 0px"><span style="color: #606060">  46:</span>     }</pre>
<pre style="font-size: 8pt; margin: 0em; overflow: visible; width: 100%; color: black; line-height: 12pt; font-family: consolas, 'Courier New', courier, monospace; background-color: white; border-style: none; padding: 0px"><span style="color: #606060">  47:</span> }</pre>
<p>The end result, a StackFixture test class with duplicate logging logic removed. The PostSharp AOP framework post-processes the compiled assembly and inserts itself, easily providing points of interception.</p>
<p>The PostSharp approach does involve a third-party dependency, but I feel like it is cleaner than the delegate approach. Gael informed me future versions of PostSharp will provide a more configurable solution than adding the [assembly ...] reference for the aspects, perhaps an XML configuration option. This can be done today, but involves some work.</p>
<p>Whether you use delegates, AOP, or some other approach, remove duplicate code wherever possible. I am happy to use either approach in my projects. There are tradeoffs to either solution, so investigate for yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/08/removing-duplicate-code-in-functions/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>ReSharper Type Completion</title>
		<link>http://elegantcode.com/2008/04/04/resharper-type-completion/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=resharper-type-completion</link>
		<comments>http://elegantcode.com/2008/04/04/resharper-type-completion/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 20:38:23 +0000</pubDate>
		<dc:creator>Alex Mueller</dc:creator>
				<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/04/resharper-type-completion/</guid>
		<description><![CDATA[What happened to my ReSharper type completion functionality? &#34;Alt+enter&#34; recently stopped showing up for me. I checked around everywhere looking for a setting that enables type completion. The image below shows no ability to use a shortcut to provide type completion. After looking around and finding nothing, finally a co-worker mentioned clicking &#34;Apply&#34; again. Sure [...]]]></description>
			<content:encoded><![CDATA[<p>What happened to my ReSharper type completion functionality? &quot;Alt+enter&quot; recently stopped showing up for me. I checked around everywhere looking for a setting that enables type completion. The image below shows no ability to use a shortcut to provide type completion.</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image2.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb2.png" width="354" height="72" /></a> </p>
<p>After looking around and finding nothing, finally a co-worker mentioned clicking &quot;Apply&quot; again. Sure enough that worked. If you come across this problem, access your ReSharper options, ReSharper -&gt; Options -&gt; Environment -&gt; General. Then click &quot;Apply&quot; and see if that resolves it.</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image3.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb3.png" width="244" height="166" /></a> </p>
<p>Finally, it is back, in all its glory.</p>
<p><a href="http://elegantcode.com/wp-content/uploads/2008/04/image4.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2008/04/image-thumb4.png" width="354" height="68" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/04/resharper-type-completion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Castle Windsor vs writing your own</title>
		<link>http://elegantcode.com/2008/04/04/castle-windsor-vs-writing-your-own/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=castle-windsor-vs-writing-your-own</link>
		<comments>http://elegantcode.com/2008/04/04/castle-windsor-vs-writing-your-own/#comments</comments>
		<pubDate>Fri, 04 Apr 2008 18:18:57 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Architecture and Design]]></category>
		<category><![CDATA[Open Source Software]]></category>
		<category><![CDATA[Tools and Utilities]]></category>

		<guid isPermaLink="false">http://elegantcode.com/2008/04/04/castle-windsor-vs-writing-your-own/</guid>
		<description><![CDATA[Earlier this week I was in a meeting were someone stated that it was better to write your own IOC container than just using an existing one (the most excellent Castle Windsor in this case). I was just baffled to hear such statements. The argument for writing your own container was simplicity and the learning [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this week I was in a meeting were someone stated that it was better to write your own IOC container than just using an existing one (the most excellent <a TARGET="_blank" HREF="http://www.castleproject.org/container/index.html">Castle Windsor</a> in this case). I was just baffled to hear such statements.</p>
<p>The argument for writing your own container was simplicity and the learning curve for new developers on the team. I strongly disagree.</p>
<p>I can&#8217;t think of anything simpler than downloading an existing IOC container that has a near flat-line learning curve, like the Castle Windsor container. About a year ago, I introduced it into <a TARGET="_blank" HREF="http://elegantcode.com/2008/04/04/about-the-members-of-my-team/">the team</a>. No learning problems whatsoever. Just read <a TARGET="_blank" HREF="http://www.ayende.com/Blog/archive/2007/05/03/Windsor-Patterns.aspx">two blog posts</a> about it and you are on your way</p>
<p>But the main reason I disagree is that I strongly believe that we have to provide business value instead of writing <a TARGET="_blank" HREF="http://vanryswyckjan.blogspot.com/2007/12/why-o-why.html">yet-another-IOC-container</a>. We have to solve the problems of our customers. They simply don&#8217;t care about IOC containers.</p>
<p><a TARGET="_blank" HREF="http://www.hanselman.com/blog/">Scott Hanselman</a> has a nice <a TARGET="_blank" HREF="http://www.hanselman.com/blog/ListOfNETDependencyInjectionContainersIOC.aspx">list of Dependency Injection Containers</a>. Pick, choose and get over it!</p>
]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2008/04/04/castle-windsor-vs-writing-your-own/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

