<?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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Another .NET Developer</title>
	<atom:link href="http://revision22.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://revision22.wordpress.com</link>
	<description>still searching for the best practices</description>
	<lastBuildDate>Sat, 26 Mar 2011 01:44:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='revision22.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Another .NET Developer</title>
		<link>http://revision22.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://revision22.wordpress.com/osd.xml" title="Another .NET Developer" />
	<atom:link rel='hub' href='http://revision22.wordpress.com/?pushpress=hub'/>
		<item>
		<title>IronPython Scripts and C# Interop</title>
		<link>http://revision22.wordpress.com/2011/02/11/ironpythoncsharpinterop/</link>
		<comments>http://revision22.wordpress.com/2011/02/11/ironpythoncsharpinterop/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 22:07:25 +0000</pubDate>
		<dc:creator>David K</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://revision22.wordpress.com/?p=17</guid>
		<description><![CDATA[I&#8217;m currently working on a project with requires scripts to execute within the .NET environment. After choosing IronPython as the scripting language, I began scouring the web for examples&#8230; Here is the code I cobbled together! [edit] I&#8217;ve updated the source code to allow instantiating a class with constructor arguments. [/edit] The code here demonstrates [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=17&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on a project with requires scripts to execute within the .NET environment. After choosing IronPython as the scripting language, I began scouring the web for examples&#8230; Here is the code I cobbled together!</p>
<p><strong>[edit] I&#8217;ve updated the <a href="http://vitamink.googlecode.com/svn/projects/r22/trunk/">source code</a> to allow instantiating a class with constructor arguments. [/edit]</strong></p>
<p>The code here demonstrates <strong>recipes for 3 types of interop</strong>:</p>
<ol>
<li>Create a dynamic instance of a script and call a method.</li>
<li>Create a dynamic instance of a Python class and call a method.</li>
<li>Imlement a .NET interface in Python and call it from C#.</li>
</ol>
<p>This example is split between a helper class and a test class:</p>
<pre class="brush: csharp; wrap-lines: false;">
public class PythonScript
{
    private string _script;
    private readonly ScriptEngine _engine = Python.CreateEngine();

    public PythonScript() { }

    public PythonScript(string script)
    {
        _script = script;
        _engine = Python.CreateEngine();
    }

    public PythonScript(params string[] script)
        : this(CreateScriptString(script))
    {
    }

    private static string CreateScriptString(string[] script)
    {
        StringBuilder scriptBuilder = new StringBuilder();
        foreach (string line in script)
        {
            scriptBuilder.AppendLine(line);
        }
        return scriptBuilder.ToString();
    }

    public string Script
    {
        get { return _script; }
        set { _script = value; }
    }

    public dynamic CreateScriptObject()
    {
        dynamic scriptScope = _engine.CreateScope();

        ScriptSource scriptSource = _engine.CreateScriptSourceFromString(_script, SourceCodeKind.Statements);
        scriptSource.Execute(scriptScope);

        return scriptScope;
    }

    public T CreateInstance&lt;T&gt;(string nameOfClass)
    {
        return CreateInstance(nameOfClass);
    }

    public dynamic CreateInstance(string nameOfClass)
    {
        ScriptScope scriptScope = _engine.CreateScope();

        ScriptSource scriptSource = _engine.CreateScriptSourceFromString(_script, SourceCodeKind.Statements);
        scriptSource.Execute(scriptScope);

        dynamic pythonClass = scriptScope.GetVariable(nameOfClass);

        return pythonClass();
    }
}
</pre>
<p>&nbsp;</p>
<pre class="brush: csharp; wrap-lines: false;">
[TestFixture]
public class PythonScriptTests
{
    [Test]
    public void should_get_hello_message_from_scripted_python_method()
    {
        PythonScript script = new PythonScript(
            &quot;def say_hello():&quot;,
            &quot;    return 'Hello from Python!'&quot;
            );

        dynamic helloScript = script.CreateScriptObject();
        string helloMessage = helloScript.say_hello();

        Assert.AreEqual(&quot;Hello from Python!&quot;, helloMessage);
    }

    [Test]
    public void should_get_hello_message_from_python_class()
    {
        PythonScript script = new PythonScript(
            &quot;class HelloClass:&quot;,
            &quot;    def say_hello(self):&quot;,
            &quot;        return 'Hello from Python!'&quot;
            );

        dynamic helloClass = script.CreateInstance(&quot;HelloClass&quot;);
        string helloMessage = helloClass.say_hello();

        Assert.AreEqual(&quot;Hello from Python!&quot;, helloMessage);
    }

    [Test]
    public void should_get_hello_message_from_python_class_implementing_a_dot_net_interface()
    {
        PythonScript script = new PythonScript(
            &quot;import clr&quot;,
            &quot;clr.AddReference('R22.Tests')&quot;,
            &quot;from R22.Tests import ISayHello&quot;,
            &quot;class HelloClass (ISayHello):&quot;,
            &quot;    def SayHello(self):&quot;,
            &quot;        return 'Hello from Python!'&quot;
            );

        ISayHello helloClass = script.CreateInstance&lt;ISayHello&gt;(&quot;HelloClass&quot;);
        string helloMessage = helloClass.SayHello();

        Assert.AreEqual(&quot;Hello from Python!&quot;, helloMessage);
    }
}

public interface ISayHello
{
    string SayHello();
}
</pre>
<p>Please note that you will need to reference the IronPython and Microsoft.Scripting dlls in order to run this code. For a working example, please refer to <a href="http://vitamink.googlecode.com/svn/projects/r22/trunk/">this</a> VS2010 solution.</p>
<p>Hope you find this useful. Happy coding!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/revision22.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/revision22.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/revision22.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/revision22.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/revision22.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/revision22.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/revision22.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/revision22.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/revision22.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/revision22.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/revision22.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/revision22.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/revision22.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/revision22.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=17&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://revision22.wordpress.com/2011/02/11/ironpythoncsharpinterop/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a5db944e357789587ab7a040d8ebf96?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">davidrkirkland</media:title>
		</media:content>
	</item>
		<item>
		<title>A Definition of Test Driven Development</title>
		<link>http://revision22.wordpress.com/2008/05/13/tdd-definition/</link>
		<comments>http://revision22.wordpress.com/2008/05/13/tdd-definition/#comments</comments>
		<pubDate>Tue, 13 May 2008 13:52:01 +0000</pubDate>
		<dc:creator>David K</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://revision22.wordpress.com/?p=10</guid>
		<description><![CDATA[I just read a post by Jean-Paul Boodhoo in which he describes his view of TDD. From the post: I feel that test driven development is first and foremost a design activity that is used to flesh out the design of a component by creating a test that first describes the API it is going [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=10&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just read a <a href="http://www.jpboodhoo.com/blog/JustForClarificationReTheLatestDotNetRocksEpisode.aspx">post</a> by <a href="http://www.jpboodhoo.com/blog/">Jean-Paul Boodhoo</a> in which he describes his view of TDD.</p>
<p>From the post:</p>
<blockquote><p>I feel that test driven development is first and foremost a design activity that is used to flesh out the design of a component by creating a test that first describes the API it is going to expose and how you are going to consume it&#8217;s functionality. The test will help shape and mold the System Under Test until you have been able to encapsulate enough functionality to satisfy whatever tasks you happen to be working on.</p></blockquote>
<p>I think that is a pretty helpful definition. Thanks JP!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/revision22.wordpress.com/10/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/revision22.wordpress.com/10/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/revision22.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/revision22.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/revision22.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/revision22.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/revision22.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/revision22.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/revision22.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/revision22.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/revision22.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/revision22.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/revision22.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/revision22.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/revision22.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/revision22.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=10&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://revision22.wordpress.com/2008/05/13/tdd-definition/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a5db944e357789587ab7a040d8ebf96?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">davidrkirkland</media:title>
		</media:content>
	</item>
		<item>
		<title>Rounding numbers to the nearest x.</title>
		<link>http://revision22.wordpress.com/2008/03/26/rounding-numbers-to-the-nearest-x/</link>
		<comments>http://revision22.wordpress.com/2008/03/26/rounding-numbers-to-the-nearest-x/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 19:03:00 +0000</pubDate>
		<dc:creator>David K</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[numbers]]></category>
		<category><![CDATA[rounding]]></category>

		<guid isPermaLink="false">http://revision22.wordpress.com/2008/03/26/rounding-numbers-to-the-nearest-x/</guid>
		<description><![CDATA[Today we came across a very simple rounding problem: round a number up to the nearest 0.05. A quick search came up with quite a few hints, but no concrete answer. So we rolled our own. Here&#8217;s the code. using System; namespace Util { public class MathHelper { public static double RoundNumberUp(double number, double roundingFactor) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=6&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today we came across a very simple rounding problem: <strong>round a number up to the nearest 0.05</strong>.</p>
<p>A quick search came up with quite a few hints, but no concrete answer. So we rolled our own. Here&#8217;s the code.</p>
<pre class="brush: csharp;">
using System;

namespace Util
{
    public class MathHelper
    {
        public static double RoundNumberUp(double number, double roundingFactor)
        {
            double rounded = RoundNumber(number, roundingFactor);
            if ((number - rounded) &gt; 0)
            {
                rounded = rounded + roundingFactor;
            }

            return rounded;
        }

        public static double RoundNumber(double number, double roundingFactor)
        {
            double rounded = Math.Round(number * (1 / roundingFactor)) / (1 / roundingFactor);

            return rounded;
        }
    }
}
</pre>
<p>And here are the tests, which show how to use the class.</p>
<pre class="brush: csharp;">
using NUnit.Framework;
using Util;

namespace Util.Tests
{
    [TestFixture]
    public class MathHelperTests
    {
        [Test]
        public void Should_round_a_number_up_to_the_nearest_0_05()
        {
            double result = MathHelper.RoundNumberUp(6.66, 0.05);
            Assert.AreEqual(6.7, result, 0.001);

            result = MathHelper.RoundNumberUp(6.7, 0.05);
            Assert.AreEqual(6.7, result, 0.001);

            result = MathHelper.RoundNumberUp(6, 0.05);
            Assert.AreEqual(6, result, 0.001);

            result = MathHelper.RoundNumberUp(6.13, 0.05);
            Assert.AreEqual(6.15, result, 0.001);

            result = MathHelper.RoundNumberUp(6.18, 0.05);
            Assert.AreEqual(6.2, result, 0.001);
        }
    }
}
</pre>
<p>Hope this comes in handy! Bye for now.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/revision22.wordpress.com/6/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/revision22.wordpress.com/6/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/revision22.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/revision22.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/revision22.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/revision22.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/revision22.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/revision22.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/revision22.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/revision22.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/revision22.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/revision22.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/revision22.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/revision22.wordpress.com/6/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/revision22.wordpress.com/6/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/revision22.wordpress.com/6/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=6&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://revision22.wordpress.com/2008/03/26/rounding-numbers-to-the-nearest-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a5db944e357789587ab7a040d8ebf96?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">davidrkirkland</media:title>
		</media:content>
	</item>
		<item>
		<title>Get on the TDD bandwagon!</title>
		<link>http://revision22.wordpress.com/2008/03/23/get-on-the-tdd-bandwagon/</link>
		<comments>http://revision22.wordpress.com/2008/03/23/get-on-the-tdd-bandwagon/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 11:38:00 +0000</pubDate>
		<dc:creator>David K</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[agile]]></category>
		<category><![CDATA[tdd]]></category>

		<guid isPermaLink="false">http://revision22.wordpress.com/2008/03/23/get-on-the-tdd-bandwagon/</guid>
		<description><![CDATA[Earlier this month me and a few guys at work decided to delve head first into test driven development. We had always done things the old fashioned way and decided it was time to catch up with more robust development techniques. We knew it would be painful at the beginning, but the rewards of TDD [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=5&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Earlier this month me and a few guys at work decided to delve head first into test driven development. We had always done things the old fashioned way and decided it was time to catch up with more robust development techniques.</p>
<p>We knew it would be painful at the beginning, but the rewards of TDD are too good to ignore. And painful it was&#8230; At first development was extremely slow! Thinking about what to test was difficult. Thinking about how to test was also hard! But we persevered, moving slowly from one test to the next, and testing each new class and method as we invented them.</p>
<p>I think it was about one week in, that this experiment started to bear real fruit. We started to notice that, in the process of developing the tests, we were designing our program from the outside in. This reminded me of something <a href="http://www.jpboodhoo.com/blog/">Jean-Paul Boodhoo</a> has repeated over and over in various screencasts: In writing the tests for our programs we are actually driving out the design of our programs!</p>
<p>The most obvious benefit of the TDD approach is adherance to the <a href="http://en.wikipedia.org/wiki/YAGNI">YAGNI</a> agile principle. It is often very tempting to write some code which solves a problem that you have anticipated. The problem with this is that you may well <em>NOT</em> need this code! TDD overcomes this anticipation by dictating that you write only the code needed to satisfy your tests. At the start this can feel restrictive, but as you move on this becomes liberating.</p>
<p>The second obvious benefit of TDD is that you tend to write functional, loosely coupled code. By starting from class names, then filling in the gaps with the methods you need, I have found that I am creating more useful classes with less bloat. When you specify a method you are forced to think carefully what parameters you should be passing to it and what object, if any, it should return.</p>
<p>The approach of designing classes from the outside is totally different than designing them from the inside. I&#8217;d say you get a birds eye view over your software when using TDD. This is far better than forever being bogged down by tiny details&#8230;</p>
<p>All in all I&#8217;d say I&#8217;m now a huge fan of TDD!! I&#8217;m still taking baby steps, but the more I use agile development techniques the more I see the huge benefits!</p>
<p><strong>Recommendations:</strong><br />
<strong><a href="http://www.amazon.co.uk/Principles-Patterns-Practices-Robert-Martin/dp/0131857258/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1206273375&amp;sr=8-1">Agile Principles, Patterns, and Practices in C#</a></strong> by <a href="http://blog.objectmentor.com/">Robert C. Martin</a>.<br />
<strong><a href="http://www.dnrtv.com/">dnrTV!</a></strong> &#8211; Check out the episodes with <a href="http://www.jpboodhoo.com/blog/">JP Boodhoo</a> and <a href="http://www.agiledeveloper.com/blog/">Venkat Subramaniam</a>!<br />
<strong><a href="http://www.dotnetrocks.com/">DotNetRocks!</a></strong> &#8211; A twice weekly .NET podcast.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/revision22.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/revision22.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/revision22.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/revision22.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/revision22.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/revision22.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/revision22.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/revision22.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/revision22.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/revision22.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/revision22.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/revision22.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/revision22.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/revision22.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/revision22.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/revision22.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=5&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://revision22.wordpress.com/2008/03/23/get-on-the-tdd-bandwagon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a5db944e357789587ab7a040d8ebf96?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">davidrkirkland</media:title>
		</media:content>
	</item>
		<item>
		<title>Compress and decompress strings in .NET</title>
		<link>http://revision22.wordpress.com/2008/03/14/compress-and-decompress-strings-in-net/</link>
		<comments>http://revision22.wordpress.com/2008/03/14/compress-and-decompress-strings-in-net/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 13:20:00 +0000</pubDate>
		<dc:creator>David K</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[compression]]></category>

		<guid isPermaLink="false">http://revision22.wordpress.com/2008/03/14/compress-and-decompress-strings-in-net/</guid>
		<description><![CDATA[I recently had the need to store strings in a database in a compressed format. I wanted to store some potentially long strings in a SQL server image type column. After some time searching the internet for solutions I came across a neat .NET namespace called System.IO.Compression which looked like it could be the answer [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=4&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I recently had the need to store strings in a database in a compressed format. I wanted to store some potentially long strings in a SQL server image type column. After some time searching the internet for solutions I came across a neat .NET namespace called <a href="http://msdn2.microsoft.com/en-us/library/system.io.compression.aspx">System.IO.Compression</a> which looked like it could be the answer to my problem&#8230;</p>
<p>Here is the helper class I wrote to perform compression and decompression:</p>
<pre class="brush: csharp;">
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace Utility
{
    public static class CompressionHelper
    {
        public static byte[] CompressText(string text)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            MemoryStream compressedStream = new MemoryStream();
            DeflateStream compressedZipStream = new DeflateStream(compressedStream, CompressionMode.Compress, true);
            compressedZipStream.Write(buffer, 0, buffer.Length);
            compressedZipStream.Close();
            compressedStream.Position = 0;
            byte[] compressed = new byte[compressedStream.Length];
            compressedStream.Read(compressed, 0, (int)compressedStream.Length);
            return compressed;
        }

        public static string DecompressText(byte[] compressed)
        {
            MemoryStream compressedStream = new MemoryStream(compressed);
            DeflateStream zipStream = new DeflateStream(compressedStream, CompressionMode.Decompress);
            List&lt;byte&gt; bytes = new List&lt;byte&gt;();
            int offset = 0;
            while (true)
            {
                byte[] temp = new byte[offset + 100];
                int bytesRead = zipStream.Read(temp, offset, 100);
                if (bytesRead == 0)
                {
                    break;
                }
                for (int i = offset; i &lt; offset + bytesRead; i++)
                {
                    bytes.Add(temp[i]);
                }
                offset += bytesRead;
            }
            return Encoding.UTF8.GetString(bytes.ToArray());
        }
    }
}
</pre>
<p>Of course you could argue that this class is too specialised right now. It served my purpose well, but you could take and return byte arrays or streams as parameters to make it more general and re-use friendly.</p>
<p>Please let me know if you find any bugs&#8230;</p>
<p>Enjoy!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/revision22.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/revision22.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/revision22.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/revision22.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/revision22.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/revision22.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/revision22.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/revision22.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/revision22.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/revision22.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/revision22.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/revision22.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/revision22.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/revision22.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/revision22.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/revision22.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=revision22.wordpress.com&amp;blog=3707540&amp;post=4&amp;subd=revision22&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://revision22.wordpress.com/2008/03/14/compress-and-decompress-strings-in-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1a5db944e357789587ab7a040d8ebf96?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">davidrkirkland</media:title>
		</media:content>
	</item>
	</channel>
</rss>
