<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>igorbrejc.net &#187; snippets</title>
	<atom:link href="http://igorbrejc.net/category/development/c/snippets/feed" rel="self" type="application/rss+xml" />
	<link>http://igorbrejc.net</link>
	<description>Just another developer's weblog</description>
	<lastBuildDate>Sat, 12 Mar 2011 16:57:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Web Testing &amp; Gallio: A Little Helpful Trick</title>
		<link>http://igorbrejc.net/development/continuous-integration/web-testing-gallio-a-little-helpful-trick</link>
		<comments>http://igorbrejc.net/development/continuous-integration/web-testing-gallio-a-little-helpful-trick#comments</comments>
		<pubDate>Mon, 18 Oct 2010 05:59:08 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Gallio and MbUnit]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/?p=1290</guid>
		<description><![CDATA[When doing automatic testing of Web apps using unit testing frameworks, it can be a pain in the butt to pinpoint the proper HTML element. A lot of times tests will fail because you used a wrong locator, but since the browser will automatically close after the test, you don’t have an access to the [...]]]></description>
			<content:encoded><![CDATA[<p>When doing automatic testing of Web apps using unit testing frameworks, it can be a pain in the butt to pinpoint the proper HTML element. A lot of times tests will fail because you used a wrong locator, but since the browser will automatically close after the test, you don’t have an access to the HTML code of the page to look at what’s actually there.</p>

<p>Fortunately Gallio provides a class called <strong>TestContext</strong> which contains the current information about the running test and which you can use to determine if the latest test is successful or not. This can then be used to run your custom handling code during the test teardown:</p>

<p>
<pre class="brush: csharp; title: ; notranslate">
        [TearDown]
        protected virtual void Teardown()
        {
            if (TestContext.CurrentContext.Outcome.Status == TestStatus.Failed)
            {
                using (TestLog.BeginSection(&quot;Failed web page HTML&quot;))
                    TestLog.Write(WebDriver.PageSource);
            }
        }
</pre></p>

<p>In the above snippet, we record the current Web page’s HTML code into Gallio’s log (the <strong>TestLog</strong> class). To avoid spamming the log, we do this for failed tests only. </p>

<p>Gallio provides a powerful framework which I think is very much underused, mostly because the documentation is not very detailed (to say the least).</p>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/continuous-integration/web-testing-gallio-a-little-helpful-trick/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
	</item>
		<item>
		<title>C#: Service Caching Proxies With The A Little Help From Some Functional Code</title>
		<link>http://igorbrejc.net/development/c/c-service-caching-proxies-with-the-a-little-help-from-some-functional-code</link>
		<comments>http://igorbrejc.net/development/c/c-service-caching-proxies-with-the-a-little-help-from-some-functional-code#comments</comments>
		<pubDate>Tue, 27 Oct 2009 13:27:26 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[IoC]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[web services]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/development/c/c-service-caching-proxies-with-the-a-little-help-from-some-functional-code</guid>
		<description><![CDATA[On a current project we&#8217;re working on in our company, we are developing a web application which accesses the back-end through some web services. Nothing special really, except that certain web services provide pretty static information like lookup tables, which don&#8217;t change very often, so it&#8217;s not really necessary to refetch them all the time. [...]]]></description>
			<content:encoded><![CDATA[<p>On a current project we&#8217;re working on in our company, we are developing a web application which accesses the back-end through some web services. Nothing special really, except that certain web services provide pretty static information like lookup tables, which don&#8217;t change very often, so it&#8217;s not really necessary to refetch them all the time.</p>

<p>Since we are heavily relying on the dependency injection and all the back-end services are exposed through C# interfaces, it wasn&#8217;t really hard to develop a new implementation of such an interface which calls the back-end service and then caches the results in the HTTP cache to be used by any subsequent calls.</p>

<p>Here&#8217;s a simple example of a service interface:</p>

<div><pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> IBackendService
{
    List&lt;<span style="color: #0000ff">string&gt;</span> ListSomeStrings(<span style="color: #0000ff">string</span> parameter1, int parameter2);
}</pre></div>

<p>I won&#8217;t bore you with an implementation of this, since it&#8217;s really a dumb one. What&#8217;s interesting is how the cached implementation looks like:</p>

<div><pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> BackendServiceCachingProxy : CachingProxyBase&lt;IBackendService&gt;, IBackendService
{
    <span style="color: #0000ff">public</span> BackendServiceCachingProxy(
        IBackendService wrappedService, 
        Cache cache,
        TimeSpan slidingCacheExpiration)
        : <span style="color: #0000ff">base</span>(wrappedService, cache, slidingCacheExpiration)
    {
    }

    <span style="color: #0000ff">public</span> List&lt;<span style="color: #0000ff">string</span>&gt; ListSomeStrings(<span style="color: #0000ff">string</span> parameter1, <span style="color: #0000ff">int</span> parameter2)
    {
        <span style="color: #0000ff">return</span> CallServiceMethod&lt;<span style="color: #0000ff">string</span>&gt;(
            () =&gt; ConstructCacheKey(<span style="color: #006080">"SomeStrings"</span>, parameter1, parameter2),
            s =&gt; s.ListSomeStrings(parameter1, parameter2));
    }
}</pre></div>

<p>Our caching wrapper inherits from a base class <strong>CachingProxyBase</strong>. Before showing you the code for CachingProxyBase, let me just quickly explain what the <strong>BackendServiceCachingProxy</strong> code does.</p>

<p>We&#8217;re using HTTP cache, so we have to supply it to the class in the constructor. We also specify how long the cache should be valid (<strong>slidingCacheExpiration </strong>parameter). </p>

<p>Each service method in the caching wrapper now uses <strong>CallServiceMethod</strong> method to implement the cached service method calls. You specify two parameters:</p>

<ol>
<li>A function delegate for constructing the cache key. This key should be unique for each unique combination of method&#8217;s input parameters. The CachingProxyBase offers a helper method <strong>ConstructCacheKey </strong>to help you with this task.</li>
<li>A function delegate for calling the method on the service. This delegate will be used for calling the actual service implementation which will contact the back-end.</li></ol>

<h3>&nbsp;</h3>

<h3>CachingProxyBase</h3>

<p>And now the implementation of the CachingProxyBase:</p>

<div><pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> CachingProxyBase&lt;TService&gt;
{
    <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> ExpireAllCachedData()
    {
        <span style="color: #0000ff">foreach</span> (System.Collections.DictionaryEntry entry <span style="color: #0000ff">in</span> cache)
            cache.Remove(entry.Key.ToString());
    }

    <span style="color: #0000ff">protected</span> CachingProxyBase(
        TService wrappedService,
        Cache cache,
        TimeSpan slidingCacheExpiration)
    {
        <span style="color: #0000ff">this</span>.wrappedService = wrappedService;
        <span style="color: #0000ff">this</span>.cache = cache;
        <span style="color: #0000ff">this</span>.slidingCacheExpiration = slidingCacheExpiration;
    }

    <span style="color: #0000ff">protected</span> Cache Cache
    {
        get { <span style="color: #0000ff">return</span> cache; }
    }

    <span style="color: #0000ff">protected</span> TimeSpan SlidingCacheExpiration
    {
        get { <span style="color: #0000ff">return</span> slidingCacheExpiration; }
    }

    <span style="color: #0000ff">protected</span> TService WrappedService
    {
        get { <span style="color: #0000ff">return</span> wrappedService; }
    }

    <span style="color: #0000ff">protected</span> TValue CallServiceMethod&lt;TValue&gt;(
        Func&lt;<span style="color: #0000ff">string</span>&gt; constructCacheKeyFunc,
        Func&lt;TService, TValue&gt; serviceMethodFunc)
        <span style="color: #0000ff">where</span> TValue : <span style="color: #0000ff">class</span>
    {
        <span style="color: #0000ff">string</span> cacheKey = constructCacheKeyFunc();

        TValue cachedValue = GetCachedValue&lt;TValue&gt;(cacheKey);

        <span style="color: #0000ff">if</span> (cachedValue == <span style="color: #0000ff">null</span>)
        {
            cachedValue = serviceMethodFunc(WrappedService);
            CacheValue(cacheKey, cachedValue);
        }

        <span style="color: #0000ff">return</span> cachedValue;
    }

    <span style="color: #0000ff">protected</span> <span style="color: #0000ff">string</span> ConstructCacheKey(
        <span style="color: #0000ff">string</span> function, 
        <span style="color: #0000ff">string</span> environmentId, 
        <span style="color: #0000ff">params</span> <span style="color: #0000ff">object</span>[] args)
    {
        StringBuilder cacheKeyBuilder = <span style="color: #0000ff">new</span> StringBuilder();
        cacheKeyBuilder.AppendFormat(
            CultureInfo.InvariantCulture,
            <span style="color: #006080">"Proxy-{0}-{1}"</span>,
            function,
            environmentId);

        <span style="color: #0000ff">foreach</span> (<span style="color: #0000ff">object</span> arg <span style="color: #0000ff">in</span> args)
            cacheKeyBuilder.AppendFormat(CultureInfo.InvariantCulture, <span style="color: #006080">"-{0}"</span>, arg);

        <span style="color: #0000ff">return</span> cacheKeyBuilder.ToString();
    }

    <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> CacheValue(<span style="color: #0000ff">string</span> cacheKey, <span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>)
    {
        Cache.Insert(
            cacheKey,
            <span style="color: #0000ff">value</span>,
            <span style="color: #0000ff">null</span>,
            Cache.NoAbsoluteExpiration,
            SlidingCacheExpiration);
    }

    [SuppressMessage(<span style="color: #006080">"Microsoft.Design"</span>, <span style="color: #006080">"CA1004:GenericMethodsShouldProvideTypeParameter"</span>)]
    <span style="color: #0000ff">private</span> T GetCachedValue&lt;T&gt;(<span style="color: #0000ff">string</span> cacheKey) 
    {
        <span style="color: #0000ff">return</span> (T)cache.Get(cacheKey);
    }

    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> TService wrappedService;
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> Cache cache;
    <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> TimeSpan slidingCacheExpiration;
}</pre></div>

<p><strong>TService</strong> is a generic parameter representing the service interface the proxy is enhancing with the caching. I think the code is pretty self-explanatory. The <strong>ExpireAllCachedData</strong> method is exposed to be used in unit tests to make sure the cache is empty before doing any work.</p>

<h3>Using It With Windsor Castle</h3>

<p>I&#8217;ve made a helper method for configuring in Windsor Castle a specific back-end service with or without caching:</p>

<div><pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px">[SuppressMessage(<span style="color: #006080">"Microsoft.Design"</span>, <span style="color: #006080">"CA1004:GenericMethodsShouldProvideTypeParameter"</span>)]
<span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> RegisterServiceWithOptionalCache&lt;TService, TImplementation, TCachedImplementation&gt;(
    IWindsorContainer container,
    TimeSpan slidingCacheExpiration)
    <span style="color: #0000ff">where</span> TImplementation : TService
    <span style="color: #0000ff">where</span> TCachedImplementation : TService
{
    <span style="color: #0000ff">string</span> serviceName = <span style="color: #0000ff">typeof</span>(TService).Name;

    <span style="color: #0000ff">if</span> (IsCachingUsed)
    {
        container.Register(
            Component.For&lt;TService&gt;().ImplementedBy&lt;TCachedImplementation&gt;()
                .ServiceOverrides(ServiceOverride.ForKey(<span style="color: #006080">"wrappedService"</span>).Eq(serviceName))
                .Parameters(Parameter.ForKey(<span style="color: #006080">"slidingCacheExpiration"</span>).Eq(slidingCacheExpiration.ToString())));

        <span style="color: #0000ff">if</span> (log.IsDebugEnabled)
            log.DebugFormat(<span style="color: #006080">"Registered {0}"</span>, <span style="color: #0000ff">typeof</span>(TCachedImplementation).Name);
    }

    container.Register(
        Component.For&lt;TService&gt;().ImplementedBy&lt;TImplementation&gt;()
            .Named(serviceName));            
}</pre></div>

<h3>&nbsp;</h3>

<h3>Conclusion</h3>

<p>There are probably more elegant ways of doing this (probably using Windsor&#8217;s interceptors and reflection), but I think this code does its job well if you have a small number of service methods to cover.</p>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/c/c-service-caching-proxies-with-the-a-little-help-from-some-functional-code/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
	</item>
		<item>
		<title>SharpZipLib: Making It Work For Linux/Mac</title>
		<link>http://igorbrejc.net/development/c/sharpziplib-making-it-work-for-linuxmac</link>
		<comments>http://igorbrejc.net/development/c/sharpziplib-making-it-work-for-linuxmac#comments</comments>
		<pubDate>Tue, 27 Jan 2009 21:20:47 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/?p=306</guid>
		<description><![CDATA[photo credit: massdistraction I&#8217;m writing this post since I&#8217;ve found a lot of people having similar problems, so hopefully it will be useful to someone. I&#8217;ve had Linux/Mac people reporting that the ZIP package of GroundTruth which I released yesterday could not be extracted. The package was created programmatically using the SharpZibLib, an open source [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.flickr.com/photos/49503124519@N01/116400199/" title="Mr. Monster Mouth" target="_blank"><img src="http://farm1.static.flickr.com/39/116400199_9bc99bf3e4_m.jpg" alt="Mr. Monster Mouth" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-nc-nd/2.0/" title="Attribution-NonCommercial-NoDerivs License" target="_blank"><img src="http://igorbrejc.net/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/49503124519@N01/116400199/" title="massdistraction" target="_blank">massdistraction</a></small></p>

<p>I&#8217;m writing this post since I&#8217;ve found a lot of people having similar problems, so hopefully it will be useful to someone. </p>

<p>I&#8217;ve had Linux/Mac people reporting that the ZIP package of <a href="http://igorbrejc.net/openstreetmap/groundtruth-released" target="_blank">GroundTruth which I released</a> yesterday <a href="http://igorbrejc.net/?p=303" target="_blank">could not be extracted</a>. The package was created programmatically using the <a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/" target="_blank">SharpZibLib</a>, an open source .NET ZIP library. I myself did not detect any problems, it works OK on my Vista machines.</p>

<p>I&#8217;ve done some investigation (Google, inspecting other open source projects&#8230;) and found <a href="http://community.sharpdevelop.net/forums/p/4982/18637.aspx" target="_blank">a small hint what could be the culprit</a> &#8211; I wasn&#8217;t setting the ZipEntry.Size property when I was adding files to the package. So here I&#8217;m posting a code snippet on how to create a ZIP package with selectively added files:</p>

<div class="dean_ch" style="white-space: wrap;">using <span class="br0">&#40;</span>FileStream zipFileStream = new FileStream<span class="br0">&#40;</span><br />
&nbsp; &nbsp; zipFileName,<br />
&nbsp; &nbsp; FileMode.<span class="me1">Create</span>,<br />
&nbsp; &nbsp; FileAccess.<span class="me1">ReadWrite</span>,<br />
&nbsp; &nbsp; FileShare.<span class="me1">None</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; using <span class="br0">&#40;</span>ZipOutputStream zipStream = new ZipOutputStream<span class="br0">&#40;</span>zipFileStream<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; byte<span class="br0">&#91;</span><span class="br0">&#93;</span> buffer = new byte<span class="br0">&#91;</span><span class="nu0">1024</span> * <span class="nu0">1024</span><span class="br0">&#93;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span><span class="kw4">string</span> fileName in filesToZip<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> cleanedFileName = ZipEntry.<span class="me1">CleanName</span><span class="br0">&#40;</span>fileName<span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using <span class="br0">&#40;</span>FileStream fileStream = File.<span class="me1">OpenRead</span> <span class="br0">&#40;</span>fileName<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ZipEntry entry = new ZipEntry <span class="br0">&#40;</span>cleanedFileName<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.<span class="me1">DateTime</span> = File.<span class="me1">GetLastWriteTime</span><span class="br0">&#40;</span>fileName<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; entry.<span class="me1">Size</span> = fileStream.<span class="me1">Length</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipStream.<span class="me1">PutNextEntry</span> <span class="br0">&#40;</span>entry<span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">int</span> sourceBytes;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">while</span> <span class="br0">&#40;</span><span class="kw2">true</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sourceBytes = fileStream.<span class="me1">Read</span><span class="br0">&#40;</span>buffer, <span class="nu0">0</span>, buffer.<span class="me1">Length</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>sourceBytes == <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">break</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zipStream.<span class="me1">Write</span><span class="br0">&#40;</span>buffer, <span class="nu0">0</span>, sourceBytes<span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span><br />
&nbsp;</div>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/c/sharpziplib-making-it-work-for-linuxmac/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:thumbnail url="http://farm1.static.flickr.com/39/116400199_9bc99bf3e4_m.jpg" />
		<media:content url="http://farm1.static.flickr.com/39/116400199_9bc99bf3e4_m.jpg" medium="image">
			<media:title type="html">Mr. Monster Mouth</media:title>
		</media:content>
		<media:content url="http://igorbrejc.net/wp-content/plugins/photo-dropper/images/cc.png" medium="image">
			<media:title type="html">Creative Commons License</media:title>
		</media:content>
	</item>
		<item>
		<title>MbUnit: Inconclusive Test Results</title>
		<link>http://igorbrejc.net/development/continuous-integration/mbunit-inconclusive-test-results</link>
		<comments>http://igorbrejc.net/development/continuous-integration/mbunit-inconclusive-test-results#comments</comments>
		<pubDate>Fri, 09 Jan 2009 11:56:10 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[continuous integration]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/?p=223</guid>
		<description><![CDATA[photo credit: Oberazzi UPDATE: Jeff Brown pointed me to a better way of doing some of the things discussed in this post, so I&#8217;ve updated the post. One of the lesser known (and documented) features of MbUnit and Gallio is marking tests as inconclusive: &#160; &#160; &#160; &#160;&#91;Test&#93; &#160; &#160; &#160; &#160; public void InconclusiveTest&#40;&#41; [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Questions" href="http://www.flickr.com/photos/42788859@N00/318947873/" target="_blank"><img alt="Questions" src="http://farm1.static.flickr.com/134/318947873_12028f1b66_m.jpg" border="0" /></a>    <br /><small><a title="Attribution-NonCommercial-ShareAlike License" href="http://creativecommons.org/licenses/by-nc-sa/2.0/" target="_blank"><img height="16" alt="Creative Commons License" src="http://igorbrejc.net/wp-content/plugins/photo-dropper/images/cc.png" width="16" align="absMiddle" border="0" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="Oberazzi" href="http://www.flickr.com/photos/42788859@N00/318947873/" target="_blank">Oberazzi</a></small></p>

<p><strong>UPDATE:</strong> Jeff Brown pointed me to a better way of doing some of the things discussed in this post, so I&#8217;ve updated the post.</p>

<p>One of the lesser known (and documented) features of MbUnit and Gallio is marking tests as inconclusive:</p>

<div class="dean_ch" style="white-space: wrap;"> &nbsp; &nbsp; &nbsp; &nbsp;<span class="br0">&#91;</span>Test<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; public <span class="kw4">void</span> InconclusiveTest<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>WeDeterminedTheTestCannotBeRun<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Assert.<span class="me1">Inconclusive</span><span class="br0">&#40;</span><span class="st0">&quot;Inconclusive message&quot;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WeThrowAnExceptionButItDoesNotMatter<span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>

<p>By calling Assert.Inconclusive() we tell the test run it should mark this test case as inconclusive. Assert.Inconclusive() does not throw any exceptions, the tests continues to run, but even if the later code throws an exception or some assert fails, the test outcome will still be marked as inconclusive:</p>

<p>&#160;&#160;&#160;&#160;&#160; 456 run, 455 passed, 0 failed,<strong> 1 inconclusive</strong>, 1 skipped (1 ignored)</p>

<p>The build will not fail if we have one or more inconclusive tests. How does this come in handy? Sometimes you have tests which access certain external resources like internet pages. You want to be able to run such tests without causing the build to fail if internet connection is temporarily not available (I&#8217;m not saying that this is a good pattern for writing tests, just giving an example). One way to do this would be to first check the internet connection and mark the test as inconclusive if the connection is not available.</p>

<p>The second scenario (which we actually use in our acceptance test framework) is when you rely on certain test facility methods in order to execute the tests. Since these methods are often implemented in parallel with actual test code, we want to be able to mark them as not available until they are finished. <strike>We do this again by invoking Assert.Inconclusive() inside such methods, which will cause all test code that use these methods to have an inconclusive test result.</strike></p>

<p><strike>An alternative would be throwing NotImplementedExceptions, but we want to separate tests which actually failed from those which are not fully implemented.</strike></p>

<p>There is a better way: <a href="http://igorbrejc.net/?p=258">Gallio: Setting Test Outcome Anyway You Like</a></p>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/continuous-integration/mbunit-inconclusive-test-results/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:thumbnail url="http://farm1.static.flickr.com/134/318947873_12028f1b66_m.jpg" />
		<media:content url="http://farm1.static.flickr.com/134/318947873_12028f1b66_m.jpg" medium="image">
			<media:title type="html">Questions</media:title>
		</media:content>
		<media:content url="http://igorbrejc.net/wp-content/plugins/photo-dropper/images/cc.png" medium="image">
			<media:title type="html">Creative Commons License</media:title>
		</media:content>
	</item>
		<item>
		<title>Asserting Than An Assertion Has Failed</title>
		<link>http://igorbrejc.net/development/c/asserting-than-an-assertion-has-failed</link>
		<comments>http://igorbrejc.net/development/c/asserting-than-an-assertion-has-failed#comments</comments>
		<pubDate>Thu, 08 Jan 2009 14:40:11 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[snippets]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/?p=216</guid>
		<description><![CDATA[photo credit: turtlemom4bacon UPDATE: Jeff Brown, one of the architects of MbUnit and Gallio, responded to my post suggesting other (=better) ways of checking assertions. I&#8217;ve added his suggestions at the bottom of the post. We&#8217;re developing an acceptance test framework (which I will write more about when it reaches some maturity state) which will [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Opie thought he would find a kewl looking girl cat on the computer" href="http://www.flickr.com/photos/9146943@N06/1914397629/" target="_blank"><img alt="Opie thought he would find a kewl looking girl cat on the computer" src="http://farm3.static.flickr.com/2072/1914397629_9d1206ee88_m.jpg" border="0" /></a>     <br /><small><a title="Attribution-ShareAlike License" href="http://creativecommons.org/licenses/by-sa/2.0/" target="_blank"><img height="16" alt="Creative Commons License" src="http://igorbrejc.net/wp-content/plugins/photo-dropper/images/cc.png" width="16" align="absMiddle" border="0" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a title="turtlemom4bacon" href="http://www.flickr.com/photos/9146943@N06/1914397629/" target="_blank">turtlemom4bacon</a></small></p>

<p><strong>UPDATE</strong>: Jeff Brown, one of the architects of MbUnit and Gallio, responded to my post suggesting other (=better) ways of checking assertions. I&#8217;ve added his suggestions at the bottom of the post.</p>

<p>We&#8217;re developing an acceptance test framework (which I will write more about when it reaches some maturity state) which will use MbUnit and <a href="http://www.gallio.org/">Gallio</a> to execute the test code. We developed some utility assertions of our own (which in turn use MbUnit Assert* methods) and we wanted to test them using MbUnit. So basically we wanted to unit test the unit test code <img src='http://igorbrejc.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>

<p>An interesting problem occurred when we wanted to test that one of these assertions actually fails under certain conditions. MbUnit throws <strong>AssertionException</strong> when an assertion fails, but this gets eaten by test runners as an indicator that the test case has failed (obviously). Of course, we didn&#8217;t want the test to fail, since we expect our assertion method to fail&#8230; OK I know it sound complicated, so let me show you the code instead of blabbering too much:</p>

<div class="dean_ch" style="white-space: wrap;">try<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; runner.<span class="me1">AssertSmsReceived</span><span class="br0">&#40;</span><span class="st0">&quot;incorrect sms&quot;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; Assert.<span class="me1">Fail</span><span class="br0">&#40;</span><span class="st0">&quot;Exception should have been thrown here&quot;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
catch <span class="br0">&#40;</span>AssertionException ex<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// this is to filter out an assertion for wrong SMS received.</span><br />
&nbsp; &nbsp; Assert.<span class="me1">IsFalse</span><span class="br0">&#40;</span>ex.<span class="me1">Message</span>.<span class="me1">Contains</span><span class="br0">&#40;</span><span class="st0">&quot;Exception should have been thrown here&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>

<p>Explanation: we expect <strong>runner.AssertSmsReceived()</strong> to throw AssertException. That&#8217;s why we catch this exception afterwards. But if the method has not failed, we force the failure with Assert.Fail(). Since both conditions throw the same type of an exception (AssertionException), we check its message contents to find out if the proper condition was met.</p>

<p><strike>There&#8217;s probably a better way to do this, but I haven&#8217;t found it (other than throwing a different type of exception instead of calling Assert.Fail(), but I wanted to avoid this because Assert.Fail() gives a cleaner test result output). Or it&#8217;s just too late in the day for me to think&#8230;</strike></p>

<p>Yes, there&#8217;s a better way (thanks Jeff):</p>

<div class="dean_ch" style="white-space: wrap;"> &nbsp; &nbsp;<span class="co1">// check if received message is correct</span><br />
&nbsp; &nbsp; AssertionFailure<span class="br0">&#91;</span><span class="br0">&#93;</span> failures = AssertionHelper.<span class="me1">Eval</span><span class="br0">&#40;</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=&gt; runner.<span class="me1">AssertSmsReceived</span><span class="br0">&#40;</span><span class="st0">&quot;incorrect sms&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; Assert.<span class="me1">AreEqual</span><span class="br0">&#40;</span><span class="nu0">1</span>, failures.<span class="me1">Length</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; Assert.<span class="me1">IsTrue</span><span class="br0">&#40;</span>failures<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="me1">Message</span>.<span class="me1">Contains</span><span class="br0">&#40;</span><span class="st0">&quot;did not receive an expected SMS message&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>

<p>Jeff also posted a helper class which Gallio guys use for testing MbUnit v3 asserts:</p>

<div class="dean_ch" style="white-space: wrap;"><span class="br0">&#91;</span>TestFrameworkInternal<span class="br0">&#93;</span><br />
public <span class="kw4">static</span> AssertionFailure<span class="br0">&#91;</span><span class="br0">&#93;</span> Capture<span class="br0">&#40;</span>Gallio.<span class="me1">Action</span> action<span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; AssertionFailure<span class="br0">&#91;</span><span class="br0">&#93;</span> failures = AssertionHelper.<span class="me1">Eval</span><span class="br0">&#40;</span>action<span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>failures.<span class="me1">Length</span> != <span class="nu0">0</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; using <span class="br0">&#40;</span>TestLog.<span class="me1">BeginSection</span><span class="br0">&#40;</span>&amp;<span class="co2">#8221;Captured Assertion Failures&amp;#8221;))</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach <span class="br0">&#40;</span>AssertionFailure failure in failures<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; failure.<span class="me1">WriteTo</span><span class="br0">&#40;</span>TestLog.<span class="kw1">Default</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<br />
&nbsp; &nbsp; <span class="kw1">return</span> failures;<br />
<span class="br0">&#125;</span></div>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/c/asserting-than-an-assertion-has-failed/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:thumbnail url="http://farm3.static.flickr.com/2072/1914397629_9d1206ee88_m.jpg" />
		<media:content url="http://farm3.static.flickr.com/2072/1914397629_9d1206ee88_m.jpg" medium="image">
			<media:title type="html">Opie thought he would find a kewl looking girl cat on the computer</media:title>
		</media:content>
		<media:content url="http://igorbrejc.net/wp-content/plugins/photo-dropper/images/cc.png" medium="image">
			<media:title type="html">Creative Commons License</media:title>
		</media:content>
	</item>
		<item>
		<title>Visiting Regex.Replace() Method And MatchEvaluator Delegate</title>
		<link>http://igorbrejc.net/development/c/visiting-regexreplace-method-and-matchevaluator-delegate</link>
		<comments>http://igorbrejc.net/development/c/visiting-regexreplace-method-and-matchevaluator-delegate#comments</comments>
		<pubDate>Mon, 05 Jan 2009 16:46:18 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/?p=202</guid>
		<description><![CDATA[In his post about creating string formatting function which would use named parameters instead of numbered ones like {0}, {1} etc, Phil Haack mentions perhaps little known, but very useful method of the Regex class: Replace() method, specifically the override which uses MatchEvaluator delegate. I first used this method few days ago when I was [...]]]></description>
			<content:encoded><![CDATA[<p>In his <a href="http://haacked.com/archive/2009/01/04/fun-with-named-formats-string-parsing-and-edge-cases.aspx" target="_blank">post about creating string formatting function</a> which would use named parameters instead of numbered ones like {0}, {1} etc, Phil Haack mentions perhaps little known, but very useful method of the Regex class: Replace() method, specifically the override which uses MatchEvaluator delegate.</p>

<p>I first used this method few days ago when I was implementing labeling of map elements for <a href="http://igorbrejc.net/openstreetmap/groundtruth-a-new-garmin-mapmaking-tool" target="_blank">GroundTruth</a>, a new mapmaking tool for Garmin which I&#8217;m currently working on. The idea was to provide something like a format string which would tell the tool how to construct a label for map elements (roads, cities etc). One example would be to generate map label of a mountain peak with an attached elevation: &#8220;<em>Mount Everest, 8848 M</em>&#8220;.</p>

<p>The solution was to provide special functions (or properties) which would expand to actual values of map element&#8217;s tags, but by also taking account of the context (for example, do we show elevation in meters or feet?). These functions would be encoded in the string in form of <strong>$FunctionName</strong>. This is where Regex.Replace() comes in handy:</p>

<div class="dean_ch" style="white-space: wrap;"><span class="kw4">string</span> expandedString = regexFunction.<span class="me1">Replace</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; format, <br />
&nbsp; &nbsp; match =&gt; <br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw4">string</span> functionName = match.<span class="me1">Groups</span><span class="br0">&#91;</span><span class="st0">&quot;function&quot;</span><span class="br0">&#93;</span>.<span class="me1">Value</span>;<br />
<br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw2">false</span> == registeredFunctions.<span class="me1">ContainsKey</span><span class="br0">&#40;</span>functionName<span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; throw new ArgumentException<span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">String</span>.<span class="me1">Format</span><span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CultureInfo.<span class="me1">InvariantCulture</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="st0">&quot;Unknown label building function &#8216;{0}&#8217;&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; functionName<span class="br0">&#41;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; ILabelBuildingFunction <span class="kw2">function</span> = registeredFunctions<span class="br0">&#91;</span>functionName<span class="br0">&#93;</span>;<br />
&nbsp; &nbsp; <span class="kw4">string</span> result = <span class="kw2">function</span>.<span class="me1">Calculate</span><span class="br0">&#40;</span>mapMakerSettings, osmObject, osmTag<span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; <span class="kw1">return</span> result;<br />
<span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>

<p>So what are we doing here? We are telling Regex to give us each Regex match and we will then return a replacement string for it. The anonymous delegate extracts the name of the function from this Match and looks it up in the dictionary of supported functions. Since each function has a corresponding object, we call on this object to give us an actual value of the function (Calculate() method). We then return this value to Regex as a replacement.</p>

<p>The regexFunction is defined as a static class member:</p>

<div class="dean_ch" style="white-space: wrap;">private <span class="kw4">static</span> readonly Regex regexFunction = new Regex <span class="br0">&#40;</span>@<span class="st0">&quot;<span class="es0">\$</span>(?&lt;function&gt;<span class="es0">\w</span>+)&quot;</span>, <br />
&nbsp; &nbsp; RegexOptions.<span class="me1">Compiled</span> | RegexOptions.<span class="me1">ExplicitCapture</span><span class="br0">&#41;</span>;</div>

<p><a href="http://www.acorns.com.au/blog/?p=136" target="_blank">Creating Regex objects is expensive</a>, so it&#8217;s better to instantiate them once as static members. Using RegexOptions.Compiled is also recommended, as is reading <a href="http://www.codinghorror.com/blog/archives/000228.html" target="_blank">Jeff Atwood&#8217;s post about it</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/c/visiting-regexreplace-method-and-matchevaluator-delegate/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
	</item>
		<item>
		<title>C# snippet for the Disposable pattern</title>
		<link>http://igorbrejc.net/development/c/c-snippet-for-the-disposable-pattern</link>
		<comments>http://igorbrejc.net/development/c/c-snippet-for-the-disposable-pattern#comments</comments>
		<pubDate>Mon, 04 Feb 2008 08:58:41 +0000</pubDate>
		<dc:creator>breki</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://igorbrejc.net/development/c/c-snippet-for-the-disposable-pattern</guid>
		<description><![CDATA[This is the first post in a series of posts in which I will publish some C# code snippets I find useful in everyday C# coding. This snippets inserts all of the necessary code to implement Disposable pattern in a C# class. The code is basically the example of how to implement IDisposable interface taken [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first post in a series of posts in which I will publish some C# code snippets I find useful in everyday C# coding.</p>

<p>This snippets inserts all of the necessary code to implement Disposable pattern in a C# class. The code is basically the example of how to implement IDisposable interface taken from the FxCop help pages, so this snippet resolves that FxCop issue.</p>

<p>Note the two &#8220;TODO&#8221;-s where you can add your code for disposing of managed and unmanaged resources.</p>

<div class="dean_ch" style="white-space: wrap;"><br />
<span class="sc3"><span class="re1">&lt;codesnippets</span> <span class="re0">xmlns</span>=<span class="st0">&quot;http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;codesnippet</span> <span class="re0">format</span>=<span class="st0">&quot;1.0.0&quot;</span><span class="re2">&gt;</span></span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;description<span class="re2">&gt;</span></span></span>Implements a disposable pattern for the class.<span class="sc3"><span class="re1">&lt;/description<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;author<span class="re2">&gt;</span></span></span>Igor Brejc<span class="sc3"><span class="re1">&lt;/author<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;keywords<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;keyword<span class="re2">&gt;</span></span></span>IDisposable<span class="sc3"><span class="re1">&lt;/keyword<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;keyword<span class="re2">&gt;</span></span></span>Dispose<span class="sc3"><span class="re1">&lt;/keyword<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/keywords<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;snippettypes<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;snippettype<span class="re2">&gt;</span></span></span>Expansion<span class="sc3"><span class="re1">&lt;/snippettype<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/snippettypes<span class="re2">&gt;</span></span></span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;snippet<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;code</span> <span class="re0">language</span>=<span class="st0">&quot;CSharp&quot;</span> <span class="re0">kind</span>=<span class="st0">&quot;method decl&quot;</span><span class="re2">&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="coMULTI">&lt;!&#8211;[CDATA[<br />
&nbsp; &nbsp;&nbsp; &nbsp;#region IDisposable Members<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;/// &lt;summary&gt;</span><br />
</span><br />
&nbsp; &nbsp;&nbsp; &nbsp;/// Performs application-defined tasks associated with freeing, releasing, or<br />
&nbsp; &nbsp;&nbsp; &nbsp;/// resetting unmanaged resources.<br />
&nbsp; &nbsp;&nbsp; &nbsp;/// <span class="sc3"><span class="re1">&lt;/summary<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp;&nbsp; &nbsp;public void Dispose()<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;Dispose(true);<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;GC.SuppressFinalize(this);<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;/// <span class="sc3"><span class="re1">&lt;summary<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp;&nbsp; &nbsp;/// Disposes the object.<br />
&nbsp; &nbsp;&nbsp; &nbsp;/// <span class="sc3"><span class="re1">&lt;/summary<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp;&nbsp; &nbsp;///<br />
<span class="sc3"><span class="re1">&lt;div</span> &nbsp;<span class="re0">name</span>=<span class="st0">&quot;disposing&quot;</span><span class="re2">&gt;</span></span><span class="sc3"><span class="re1">&lt;/div<span class="re2">&gt;</span></span></span><br />
If <span class="sc3"><span class="re1">&lt;code<span class="re2">&gt;</span></span></span>false<span class="sc3"><span class="re1">&lt;/code<span class="re2">&gt;</span></span></span>, cleans up native resources.<br />
&nbsp; &nbsp;&nbsp; &nbsp;/// If <span class="sc3"><span class="re1">&lt;code<span class="re2">&gt;</span></span></span>true<span class="sc3"><span class="re1">&lt;/code<span class="re2">&gt;</span></span></span> cleans up both managed and native resources<span class="sc3"><span class="re1">&lt;/param<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp;&nbsp; &nbsp;protected virtual void Dispose(bool disposing)<br />
&nbsp; &nbsp;&nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (false == disposed)<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: clean native resources &nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;if (disposing)<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;{<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO: clean managed resources<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $end$<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;}<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp;disposed = true;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp;&nbsp; &nbsp;}<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;private bool disposed;<br />
<br />
&nbsp; &nbsp;&nbsp; &nbsp;#endregion<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]]&#8211;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/code<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/snippet<span class="re2">&gt;</span></span></span><br />
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/codesnippet<span class="re2">&gt;</span></span></span><br />
<span class="sc3"><span class="re1">&lt;/codesnippets<span class="re2">&gt;</span></span></span></div>
]]></content:encoded>
			<wfw:commentRss>http://igorbrejc.net/development/c/c-snippet-for-the-disposable-pattern/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
	</item>
	</channel>
</rss>

