<?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>Deployment Zone &#187; C#</title>
	<atom:link href="http://www.deploymentzone.com/category/computers/programming/c-programming-computers/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.deploymentzone.com</link>
	<description></description>
	<lastBuildDate>Thu, 29 Jul 2010 19:28:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Side Effect Free Retrieval Pattern</title>
		<link>http://www.deploymentzone.com/2009/07/16/side-effect-free-retrieval-pattern/</link>
		<comments>http://www.deploymentzone.com/2009/07/16/side-effect-free-retrieval-pattern/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 21:11:00 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2009/07/16/side-effect-free-retrieval-pattern/</guid>
		<description><![CDATA[A colleague of mine and I were discussing good verb replacements for “GetOrCreate” data retrieval patterns, where the “Create” part is responsible for the instantiation of a new instance of something.&#160; While the pattern I present here did not solve his particular problem, its at least worth sharing.&#160; Its obvious, and probably in use commonly [...]]]></description>
			<content:encoded><![CDATA[<p>A colleague of mine and I were discussing good verb replacements for “GetOrCreate” data retrieval patterns, where the “Create” part is responsible for the instantiation of a new instance of something.&#160; While the pattern I present here did not solve his particular problem, its at least worth sharing.&#160; Its obvious, and probably in use commonly already, but I figure its worth noting.</p>
<p>Essentially the goal is to get away from a “GetOrCreate” master method call and just have two methods: Get(args) and Get(args, Foo default) and then leave the responsibility of default generation up to Foo.&#160; This way there’s no unintended side effects and you don’t have to explicitly coalesce (though with this pattern you could as easily coalesce; strictly speaking it isn’t as “discoverable”)…</p>
<pre class="brush: csharp; toolbar: false;">var foo = FooService.Get(23) ?? new Foo { ... };</pre>
<p>So here’s the pattern defined in code:</p>
<pre class="brush: csharp;">public class Foo : ICloneable
{
    private readonly static Foo __default =
        new Foo { Bar = &quot;...&quot;, Baz = Int32.MinValue };

    public string Bar { get; set; }
    public int Baz { get; set; }

    public static Foo Default()
    {
        return (Foo)__default.Clone();
    }

    public object Clone()
    {
        return new Foo { Bar = this.Bar, Baz = this.Baz };
    }
}

public class FooService
{
    public Foo Get(int baz)
    {
        // retrieve from data store...
        return null;
    }

    public Foo Get(int baz, Func&lt;Foo&gt; @default)
    {
        return Get(baz) ?? @default.Invoke();
    }
}</pre>
<p>You’ll see that any associated overhead with Clone only occurs if Get(baz) returns null.&#160; I have specifically stepped around the common naming standard of “GetDefault()” for the function – its named like a property - because its intended usage is as follows:</p>
<pre class="brush: csharp; toolbar: false;">var foo = new FooService().Get(12, Foo.Default);</pre>
<p>
  <br />An ICloneable reference type is pretty much required, <a href="http://www.stevecooper.org/2009/07/14/immutable-objects-in-c/">though any immutable reference type</a> could get by without cloning because any changes gives you a new copy of that type.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2009/07/16/side-effect-free-retrieval-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trying out SyntaxHighligher and PreCode</title>
		<link>http://www.deploymentzone.com/2009/04/29/trying-out-syntaxhighligher-and-precode/</link>
		<comments>http://www.deploymentzone.com/2009/04/29/trying-out-syntaxhighligher-and-precode/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 18:10:03 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2009/04/29/trying-out-syntaxhighligher-and-precode/</guid>
		<description><![CDATA[Just making sure this is correct.&#160; By this I mean PreCode (requires Windows Live Writer 2009) with SyntaxHighligher setup.&#160; PreCode is also a stand alone program.
&#160;
public bool Validate(IValidationDictionary modelState, string prefix)
{
    // xVal example code
    var dataAnnotationErrors = from prop in TypeDescriptor.GetProperties(_entity).Cast&#60;PropertyDescriptor&#62;()
        [...]]]></description>
			<content:encoded><![CDATA[<p>Just making sure this is correct.&#160; By this I mean <a href="http://precode.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=25426#ReleaseFiles">PreCode</a> (requires <a href="http://windowslivewriter.spaces.live.com/">Windows Live Writer 2009</a>) with <a href="http://alexgorbatchev.com/wiki/SyntaxHighlighter">SyntaxHighligher</a> setup.&#160; PreCode is also a stand alone program.</p>
<p>&#160;</p>
<pre class="brush: csharp;">public bool Validate(IValidationDictionary modelState, string prefix)
{
    // xVal example code
    var dataAnnotationErrors = from prop in TypeDescriptor.GetProperties(_entity).Cast&lt;PropertyDescriptor&gt;()
           from attribute in prop.Attributes.OfType&lt;ValidationAttribute&gt;()
           where !attribute.IsValid(prop.GetValue(_entity))
           select new ErrorInfo(prop.Name, attribute.FormatErrorMessage(string.Empty), _entity);

    var brokenRules = GetBrokenRules();

    if (!String.IsNullOrEmpty(prefix))
        prefix += &quot;.&quot;;

    if (dataAnnotationErrors.Any())
        dataAnnotationErrors.ForEach(ei =&gt; modelState.AddError(prefix + ei.PropertyName, ei.ErrorMessage));
    if (brokenRules.Any())
        brokenRules.ForEach(rule =&gt; modelState.AddError(prefix + rule.Property, rule.Message));

    return modelState.IsValid;
}</pre>
<p>&#160;</p>
<p>There is a “Fix Indentation” button in PreCode.&#160; I am in love.</p>
<p><strike>Now just to setup the clipboard SWF thing.</strike></p>
<p>I should seriously consider a theme with a wider content area.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2009/04/29/trying-out-syntaxhighligher-and-precode/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Lightweight Test Automation Framework</title>
		<link>http://www.deploymentzone.com/2009/03/30/using-lightweight-test-automation-framework/</link>
		<comments>http://www.deploymentzone.com/2009/03/30/using-lightweight-test-automation-framework/#comments</comments>
		<pubDate>Mon, 30 Mar 2009 12:09:33 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2009/03/30/using-lightweight-test-automation-framework/</guid>
		<description><![CDATA[The Lightweight Test Automation Framework is a browser automation framework like WatiN and Selenium RC that allows you to create automated integration tests.&#160; Its a beta release from MS and can be found on Codeplex at:
http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22739
I first read about it from Steve Sanderson's blog where he describes how to use it with an ASP.NET MVC [...]]]></description>
			<content:encoded><![CDATA[<p>The Lightweight Test Automation Framework is a browser automation framework like <a href="http://watin.sourceforge.net/">WatiN</a> and <a href="http://seleniumhq.org/projects/remote-control/">Selenium RC</a> that allows you to create automated integration tests.&#160; Its a beta release from MS and can be found on <a href="http://www.codeplex.com">Codeplex</a> at:</p>
<p><a title="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22739" href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22739">http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22739</a></p>
<p>I first read about it from <a href="http://blog.codeville.net/2009/03/27/first-steps-with-lightweight-test-automation-framework/">Steve Sanderson's blog</a> where he describes how to use it with an ASP.NET MVC project.</p>
<p>I am supporting a legacy web forms application and have a typical web application setup so I'll explain what I did to get up and running in a matter of minutes with LTAF.</p>
<ol>
<li>Download <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=22739">LTAF</a>; you can use the &quot;... for ASP.NET Samples&quot; link and pull the *.dll out of the Bin folder and the contents of the Test folder, or you could build from source using the &quot;... for ASP.NET Source&quot;</li>
<li>Create a new Class Library project in your solution and name it appropriately - <em>Company.Tests.Integration.Site</em> - for example</li>
<ol>
<li>For this project's properties alter the output assembly's name to begin with <em>App_Code</em> - this will slightly speed up web test load times and can help solve problems you may experience if your web application references tons of assemblies, i.e. <em>App_CodeCompany.Tests.Integration.Site</em></li>
</ol>
<li>Add a reference to <em>Microsoft.Web.Testing.Light.dll </em>to your <em>Company.Tests.Integration.Site</em> project</li>
<li><strong>Copy </strong>and add to your <em>web project (Company.Site)</em> the <em>Test</em> folder; you can name the folder anything you want and you don't strictly have to add it to your project but I did for convenience for Start &gt; Debug</li>
<ol>
<li>Remember to alter your build process to remove the <em>Test</em> folder for production releases</li>
</ol>
<li>Alter the <em>web.config</em> file for your <em>Company.Site</em> to allow anonymous access to the <em>Test</em> folder:</li>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">location </span><span style="color: red">path</span><span style="color: blue">=</span>&quot;<span style="color: blue">Test</span>&quot;<span style="color: blue">&gt;
  &lt;</span><span style="color: #a31515">system.web</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">authorization</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">allow </span><span style="color: red">users</span><span style="color: blue">=</span>&quot;<span style="color: blue">*</span>&quot; <span style="color: blue">/&gt;
    &lt;/</span><span style="color: #a31515">authorization</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">system.web</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">location</span><span style="color: blue">&gt;</span></pre>
<p>  <a href="http://11011.net/software/vspaste"></a></p>
<ol>
<li>Again, remember to alter your build process to eliminate this node from your production web.config</li>
</ol>
<li>Add a reference to your integration test project, <em>Company.Tests.Integration.Site</em>, to your web application, <em>Company.Site</em></li>
<li>Let's test the login page (here I assume that &quot;Login.aspx&quot; is the name of your login page and its in the root of the application and you've used an &lt;asp:Login /&gt; control)...</li>
<ol>
<li>Create a new class <em>LoginTests </em>in your <em>Company.Tests.Integration.Site</em> project and <strong>eliminate</strong> the file's contents - it is very important that all tests exist <strong>outside</strong> of a namespace</li>
<li>Paste this code into the body of your <em>LoginTests.cs</em> file:</li>
<pre class="code"><span style="color: blue">using </span>Microsoft.Web.Testing.Light;

[<span style="color: #2b91af">WebTestClass</span>]
<span style="color: blue">public class </span><span style="color: #2b91af">LoginTests
</span>{
    [<span style="color: #2b91af">WebTestMethod</span>]
    <span style="color: blue">public void </span>SignInAndSignOut()
    {
        <span style="color: blue">var </span>page = <span style="color: blue">new </span><span style="color: #2b91af">HtmlPage</span>(<span style="color: #a31515">&quot;Login.aspx&quot;</span>);

        page.Elements.Find(<span style="color: #a31515">&quot;UserName&quot;</span>).SetText(<span style="color: #a31515">&quot;someuser&quot;</span>);
        page.Elements.Find(<span style="color: #a31515">&quot;Password&quot;</span>).SetText(<span style="color: #a31515">&quot;yourPW&quot;</span>);
        page.Elements.Find(<span style="color: #a31515">&quot;LoginButton&quot;</span>).Click(<span style="color: #2b91af">WaitFor</span>.Postback);

        <span style="color: green">// you'll need to determine what a good landmark for your
        // application is upon successful login
        </span><span style="color: blue">var </span>logoutLink = page.Elements.Find(<span style="color: #a31515">&quot;LogoutHyperlink&quot;</span>);
        <span style="color: #2b91af">Assert</span>.AreEqual(<span style="color: #a31515">&quot;[Logout]&quot;</span>, logoutLink.GetInnerText());

        logoutLink.Click(<span style="color: #2b91af">WaitFor</span>.Postback);

        <span style="color: #2b91af">Assert</span>.IsNotNull(page.Elements.Find(<span style="color: #a31515">&quot;LoginButton&quot;</span>));
    }
}</pre>
<p>    <a href="http://11011.net/software/vspaste"></a></ol>
<li>Set your <em>Company.Site</em> as the startup project if its not already, and <em>Tests/Default.aspx</em> as the startup page; run from Visual Studio</li>
</ol>
<p>Magic!&#160; This is my first use of a browser automation framework for integration testing and while it has its pitfalls, it does work and looks very promising.</p>
<h5>Notes</h5>
<ul>
<li>Looks like all your integration tests must exist in the same assembly</li>
<ul>
<li>Source code for LTAF is available so making this a configuration option is a possibility</li>
</ul>
<li>All tests must exist outside of namespaces</li>
<li>Prefixing your integration tests assembly with <em>App_Code </em>can help work around assembly loading problems as the test runner searches for <em>WebTestClass</em> attributes</li>
<li>Remember to have your production build process eliminate the <em>Tests</em> folder and associated integration test assembly</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2009/03/30/using-lightweight-test-automation-framework/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cheap Deep Copy</title>
		<link>http://www.deploymentzone.com/2009/01/08/cheap-deep-copy/</link>
		<comments>http://www.deploymentzone.com/2009/01/08/cheap-deep-copy/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 22:38:36 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2009/01/08/cheap-deep-copy/</guid>
		<description><![CDATA[So I had written a SmtpClientWrapper for System.Net.SmtpClient because the aforementioned class implements no interface making it impossible to perform dependency injection using it (as anyone who has to unit test any sort of email functionality can attest to).&#160; Then I had the idea that during production we may want to mirror email messages sent [...]]]></description>
			<content:encoded><![CDATA[<p>So I had written a SmtpClientWrapper for System.Net.SmtpClient because the aforementioned class implements no interface making it impossible to perform dependency injection using it (as anyone who has to unit test any sort of email functionality can attest to).&nbsp; Then I had the idea that during production we may want to mirror email messages sent to a debugging email address so I also made a MirrorSmtpClientDecorator.&nbsp; Herein lies the problem - System.Net.MailMessage does not implement ICloneable and the MirrorSmtpClientDecorator.Send(MailMessage) either needs a copy or its going adversely affect MailMessage with a side affect (by altering the MailMessage.To MailAddressCollection for a second message sent to the debugging email addresses).&nbsp; I explored numerous ways to copy MailMessage and eventually I resorted to some brute force extension methods (to ObjectModel.Collection&lt;T&gt;; ShallowCopy and Replace, suck I know) but during my research I discovered some older MemoryStream serialization I refactored and felt its worth sharing.</p>
<p>The problem with this approach is that the class you want to deep copy requires that it be marked with the SerializableAttribute.&nbsp; I've noticed that a portion of my time is spent writing hackarounds for classes in the .NET BCL and MailMessage is no exception - yup no ICloneable and no SerializableAttribute either.</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">Serializer
</span>{
    <span style="color: blue">public static void </span>Serialize(<span style="color: #2b91af">Stream </span>target, <span style="color: blue">object </span>source)
    {
        <span style="color: blue">var </span>formatter = <span style="color: blue">new </span><span style="color: #2b91af">BinaryFormatter</span>();
        formatter.Serialize(target, source);
    }

    <span style="color: blue">public static </span>T Deserialize&lt;T&gt;(<span style="color: #2b91af">Stream </span>source) <span style="color: blue">where </span>T : <span style="color: blue">class
    </span>{
        <span style="color: blue">var </span>formatter = <span style="color: blue">new </span><span style="color: #2b91af">BinaryFormatter</span>();
        <span style="color: blue">var </span>obj = (T)formatter.Deserialize(source);
        <span style="color: blue">return </span>obj;
    }<br>}</pre>
<pre class="code">&nbsp;</pre>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">MemorySerializer</span>&lt;T&gt; : <span style="color: #2b91af">IDisposable </span><span style="color: blue">where </span>T : <span style="color: blue">class
</span>{
    <span style="color: blue">#region </span>Member Variables
    <span style="color: blue">private readonly </span><span style="color: #2b91af">MemoryStream </span>_ms;
    <span style="color: blue">#endregion

    #region </span>Constructors
    <span style="color: blue">public </span>MemorySerializer(<span style="color: #2b91af">MemoryStream </span>ms)
    {
        _ms = ms;
    }

    <span style="color: blue">public </span>MemorySerializer()
    {
        _ms = <span style="color: blue">new </span><span style="color: #2b91af">MemoryStream</span>();
    }
    <span style="color: blue">#endregion

    #region </span>Methods
    <span style="color: blue">public </span><span style="color: #2b91af">MemorySerializer</span>&lt;T&gt; Serialize(T obj)
    {
        <span style="color: #2b91af">Serializer</span>.Serialize(_ms, obj);
        _ms.Position = 0;
        <span style="color: blue">return this</span>;
    }
    <span style="color: blue">public </span>T Deserialize()
    {
        <span style="color: blue">return </span><span style="color: #2b91af">Serializer</span>.Deserialize&lt;T&gt;(_ms);
    }
    <span style="color: blue">#endregion

    #region </span>Implementation of IDisposable
    <span style="color: blue">public void </span>Dispose()
    {
        <span style="color: blue">if </span>(_ms == <span style="color: blue">null</span>)
            <span style="color: blue">return</span>;
        <span style="color: blue">try
        </span>{
            _ms.Close();
        }
        <span style="color: blue">finally
        </span>{
            _ms.Dispose();
        }
    }
    <span style="color: blue">#endregion
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a>
<pre class="code"><span style="color: blue">public static class </span><span style="color: #2b91af">ObjectUtility
</span>{
    <span style="color: gray">/// &lt;summary&gt;
    /// </span><span style="color: green">Creates a deep copy of an object for any object which
    </span><span style="color: gray">/// </span><span style="color: green">supports binary serialization.
    </span><span style="color: gray">/// &lt;/summary&gt;
    /// &lt;typeparam name="T"&gt;</span><span style="color: green">Type to serialize/deserialize.</span><span style="color: gray">&lt;/typeparam&gt;
    /// &lt;param name="source"&gt;</span><span style="color: green">Object to deep copy.</span><span style="color: gray">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color: green">New instance of the object with all properties and
    </span><span style="color: gray">/// </span><span style="color: green">fields that support serialization copied.</span><span style="color: gray">&lt;/returns&gt;
    /// &lt;remarks&gt;</span><span style="color: green">This method isn't as fast as a provided Clone method
    </span><span style="color: gray">/// </span><span style="color: green">but when ICloneable is not implemented on a BCL class its a
    </span><span style="color: gray">/// </span><span style="color: green">pretty close substitute.</span><span style="color: gray">&lt;/remarks&gt;
    </span><span style="color: blue">public static </span>T DeepCopy&lt;T&gt;(<span style="color: blue">this </span>T source) <span style="color: blue">where </span>T : <span style="color: blue">class
    </span>{
        T ret;
        <span style="color: blue">using </span>(<span style="color: blue">var </span>ms = <span style="color: blue">new </span><span style="color: #2b91af">MemorySerializer</span>&lt;T&gt;())
        {
            ret = ms.Serialize(source).Deserialize();
        }

       <span style="color: blue">return </span>ret;
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2009/01/08/cheap-deep-copy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Outsmarting ConfigurationManager for unit testing</title>
		<link>http://www.deploymentzone.com/2008/10/23/outsmarting-configurationmanager-for-unit-testing/</link>
		<comments>http://www.deploymentzone.com/2008/10/23/outsmarting-configurationmanager-for-unit-testing/#comments</comments>
		<pubDate>Thu, 23 Oct 2008 19:59:53 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[unit-test]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2008/10/23/outsmarting-configurationmanager-for-unit-testing/</guid>
		<description><![CDATA[Today I implemented the ConfigurationSection stuff in .NET you can use to interject your custom configuration structures into config files like web.config and app.config.  I wanted to test not only my implementation, but the classes that rely on the configuration but it wasn't exactly clear how I could get ConfigurationManager to load an arbitrary configuration.
The [...]]]></description>
			<content:encoded><![CDATA[<p>Today I implemented the <a href="http://www.c-sharpcorner.com/UploadFile/yougerthen/204082008072802AM/2.aspx">ConfigurationSection stuff</a> in .NET you can use to interject your custom configuration structures into config files like web.config and app.config.  I wanted to test not only my implementation, but the classes that rely on the configuration but it wasn't exactly clear how I could get ConfigurationManager to load an arbitrary configuration.</p>
<p>The solution is quite simple and I figured it out after a bit of trial and error.  What got me was that I had a custom configuration file in a child directory of my unit test project("WebServices") and I had set the configuration file's build action to "Copy Always".  I had thought it would end up in "bin\Debug" but it correctly ended up in "bin\Debug\WebServices" so that's an important thing to keep in mind.</p>
<p>Outsmarting ConfigurationManager also requires that you have an accompanying file (empty text file works) that the configuration applies to.  So here's what the start of my [now working] test looks like:</p>
<pre class="code">    <span style="color: blue;">private static </span><span style="color: #2b91af;">RequestHandlerSection </span>_section;

    [<span style="color: #2b91af;">TestFixtureSetUp</span>]
    <span style="color: blue;">public void </span>Setup()
    {
        <span style="color: blue;">using </span>(<span style="color: blue;">var </span>sw =
            <span style="color: #2b91af;">File</span>.CreateText(<span style="color: #a31515;">".\\WebServices\\RequestHandlerTests"</span>)
        )
        {
            sw.Close();
        }
        <span style="color: blue;">var </span>config = <span style="color: #2b91af;">ConfigurationManager</span>.OpenExeConfiguration(
            <span style="color: #a31515;">".\\WebServices\\RequestHandlerTests"
        </span>);
        _section = (<span style="color: #2b91af;">RequestHandlerSection</span>)
            config.Sections[<span style="color: #2b91af;">RequestHandlerSection</span>.Name];
    }

    [<span style="color: #2b91af;">Test</span>]
    <span style="color: blue;">public void </span>WorkDamnYouTest()
    {
        <span style="color: #2b91af;">Assert</span>.IsNotNull(_section);
        <span style="color: #2b91af;">Assert</span>.IsNotEmpty(_section.Security);
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>Ideally at some point in the future ConfigurationManager could get a Load(string xml) or some such so we don't have to deal with this nonsense.  While I could add one with an extension method I'd like to see it get into the framework to improve unit testability.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2008/10/23/outsmarting-configurationmanager-for-unit-testing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What C# Needs</title>
		<link>http://www.deploymentzone.com/2008/10/21/what-c-needs/</link>
		<comments>http://www.deploymentzone.com/2008/10/21/what-c-needs/#comments</comments>
		<pubDate>Tue, 21 Oct 2008 16:31:34 +0000</pubDate>
		<dc:creator>Chuck</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.deploymentzone.com/2008/10/21/what-c-needs/</guid>
		<description><![CDATA[Three things have been bugging me lately about C#.&#160; Well one since I started using C# coming from writing a ton of unmaintainable Perl code.&#160; What C# needs:

unless
implies
templates

unless
Unless is just a reversed if statement.&#160; Its actually quite simple and in some situations it improves code readability.&#160; This is personal opinion more so than anything else, [...]]]></description>
			<content:encoded><![CDATA[<p>Three things have been bugging me lately about C#.&#160; Well one since I started using C# coming from writing a ton of unmaintainable Perl code.&#160; What C# needs:</p>
<ol>
<li>unless</li>
<li>implies</li>
<li>templates</li>
</ol>
<h2>unless</h2>
<p>Unless is just a reversed if statement.&#160; Its actually quite simple and in some situations it improves code readability.&#160; This is personal opinion more so than anything else, but I feel strongly about it.&#160; Compare:</p>
<pre class="code"><span style="color: blue">if </span>(!property.CanRead)
    <span style="color: blue">continue</span>;</pre>
<p><a href="http://11011.net/software/vspaste"></a>to:</p>
<pre class="code"><span style="color: blue">continue </span>unless propery.CanRead;</pre>
<p>So what it does is remove the negation from the if test (I mean, it is <em>unless</em> after all, isn't it?).&#160; It just flows better.&#160; &quot;Oh, continue unless this condition is met.&quot;</p>
<p>&#160;</p>
<h2>implies</h2>
<p>Extracting interfaces from existing classes is certainly easier with ReSharper, but I want more.&#160; I don't want to have to maintain separate interface files when the interfaces will match the implementation all of the time.&#160; I understand when you make interfaces and then code the implementation to them however that approach isn't always needed.&#160; I'll give a specific example: unit testing.&#160; I've started using StructureMap and Moq to significantly improve the quality of our tests, but in order to do that I had to StructureMap pluginify all of the IBATIS mappers we have written.&#160; In 100% of the cases the implementation of the mappers are the interfaces of the mappers, and now that I've gone and done this anytime a change needs to be made to a concrete mapper the interface will need to be updated as well - the dependency has been essentially reversed.&#160; I think this scenario will become more common as more people work to improve their unit tests, so I'd like to see something where:</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">PersonMapper </span>: <span style="color: #2b91af">IPersonMapper
</span>{
    <span style="color: blue">public </span><span style="color: #2b91af">Person </span>Select(<span style="color: blue">int </span>id) { <span style="color: blue">return null</span>; }
    <span style="color: blue">public </span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Person</span>&gt; SelectAll() { <span style="color: blue">return null</span>; }
    <span style="color: blue">public void </span>Create(<span style="color: #2b91af">Person </span>p) { }
    <span style="color: blue">public void </span>Update(<span style="color: #2b91af">Person </span>p) { }
    <span style="color: blue">public void </span>Delete(<span style="color: #2b91af">Person </span>p) { }
}
<span style="color: blue">public interface </span><span style="color: #2b91af">IPersonMapper
</span>{
    <span style="color: #2b91af">Person </span>Select(<span style="color: blue">int </span>id);
    <span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Person</span>&gt; SelectAll();
    <span style="color: blue">void </span>Create(<span style="color: #2b91af">Person </span>p);
    <span style="color: blue">void </span>Update(<span style="color: #2b91af">Person </span>p);
    <span style="color: blue">void </span>Delete(<span style="color: #2b91af">Person </span>p);
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>becomes:</p>
<pre class="code"><span style="color: blue">public class </span><span style="color: #2b91af">PersonMapper </span>implies IPersonMapper
{
    <span style="color: blue">public </span><span style="color: #2b91af">Person </span>Select(<span style="color: blue">int </span>id) { <span style="color: blue">return null</span>; }
    <span style="color: blue">public </span><span style="color: #2b91af">IList</span>&lt;<span style="color: #2b91af">Person</span>&gt; SelectAll() { <span style="color: blue">return null</span>; }
    <span style="color: blue">public void </span>Create(<span style="color: #2b91af">Person </span>p) { }
    <span style="color: blue">public void </span>Update(<span style="color: #2b91af">Person </span>p) { }
    <span style="color: blue">public void </span>Delete(<span style="color: #2b91af">Person </span>p) { }
}</pre>
<p>So this isn't a solution for contract by design, its just the opposite, a tightly bound interface to a concrete class.&#160; But what it does do is reduce lines of code and increases maintainability and decreases the pain of writing good unit tests to a degree.&#160; And yes, a tool could be used to do this (I've wrote one in fact), but I'm talking maintainability and ease of use here!</p>
<p>&#160;</p>
<h2>templates</h2>
<p>C# generics are very nice, but they are not templates.&#160; Many people who can better articulate the problem than I have argued for them so I'll just post my recent headache as my argument of why I want templates:</p>
<pre class="code"><span style="color: blue">private static volatile </span><span style="color: #2b91af">INodeMapper </span>nodeMapper;</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: blue">public static </span><span style="color: #2b91af">INodeMapper </span>Node()
{
    <span style="color: blue">if </span>(nodeMapper == <span style="color: blue">null</span>)
    {
        <span style="color: blue">lock </span>(<span style="color: blue">typeof</span>(<span style="color: #2b91af">INodeMapper</span>))
        {
            <span style="color: blue">if </span>(nodeMapper == <span style="color: blue">null</span>)
                nodeMapper = <span style="color: #2b91af">ObjectFactory</span>.GetInstance&lt;<span style="color: #2b91af">INodeMapper</span>&gt;();
        }
    }
    <span style="color: blue">return </span>nodeMapper;
}</pre>
<p>Because nodeMapper is declared volatile you cannot pass it using ref (or even out which would be less ideal than ref).</p>
<p>So imagine that code having to be written for every entity in your database.&#160; Again, a tool can do it, but that's not the correct answer to the problem.&#160; A preprocessor template here would be the only way to do what I want to accomplish in a sane manner.&#160; Until then I've used partial classes to break our mappers down to more maintainable levels (about 2000 lines of repetitious code into several smaller files alphabetically organized).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.deploymentzone.com/2008/10/21/what-c-needs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
