<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>.NET Pillow Talk</title>
        <link>http://krisscott.net/Default.aspx</link>
        <description />
        <language>en-US</language>
        <copyright>Kris Scott</copyright>
        <generator>Subtext Version 1.9.5.177</generator>
        <image>
            <title>.NET Pillow Talk</title>
            <url>http://krisscott.net/images/RSS2Image.gif</url>
            <link>http://krisscott.net/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>Use a generator to test that pesky DataContext</title>
            <link>http://krisscott.net/archive/2008/07/14/use-a-generator-to-test-that-pesky-datacontext.aspx</link>
            <description>&lt;p&gt;There has been a lot of buzz about how exactly to get around the issues of testing the DataContext in LINQ to SQL.  I've seen a lot of good blog posts explaining very good solutions to the problem.  But so far I have yet to see a total solution written up yet.&lt;/p&gt;
&lt;p&gt;The first thing I set out to do was create an ITable&amp;lt;T&amp;gt; interface, and a TableProxy&amp;lt;T&amp;gt; class so that I could properly wrap the Table&amp;lt;T&amp;gt; objects which come on a DataContext.&lt;/p&gt;
&lt;p&gt;I created a simple class which takes a Table&amp;lt;T&amp;gt; into its constructor then simply passes through to the methods on that Table, this class implemented my new interface and voila.  Simple enough?  I also decided to make a TableFake&amp;lt;T&amp;gt; which would simply insert and remove from a predefined list, that way I could save myself a little work stubbing out ITable&amp;lt;T&amp;gt; for all of my tests.&lt;/p&gt;
&lt;h3&gt;Proxy Generation&lt;/h3&gt;
&lt;p&gt;The next trick was to create my IXXXDataContext and XXXDataContextProxy.  But this wasn't something I wanted to have to update manually every single time we added a new table or function call to the generated data context.  I started sifting through the DBML generated Xml file that is created to keep track of all the important database artifacts before creating the actual DataContext file itself.  It looked like it would be pretty simple to put together a small console application that could read this file, and spit out the interface and proxy into one file as partial's.&lt;/p&gt;
&lt;h3&gt;Testing Magic&lt;/h3&gt;
&lt;p&gt;The beauty of this approach can best be seen in the tests written against it.  Consider the following code:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; SomeClass
{
    &lt;span class="kwrd"&gt;public&lt;/span&gt; IDataContext Context { get; set; }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SaveBook(Book book)
    {
        &lt;span class="kwrd"&gt;if&lt;/span&gt; (book.IsNew)
           Context.Books.InsertOnSubmit(book);
        &lt;span class="kwrd"&gt;else&lt;/span&gt;
           Context.Books.Attach(book, &lt;span class="kwrd"&gt;true&lt;/span&gt;);
        Context.SubmitChanges();
    }
}&lt;/pre&gt;
&lt;p&gt;With your typical DataContext, the only way to test this would be if you were willing to actually save to some kind of database.  After generating the proxy however.  Our test will look as follows:&lt;/p&gt;
&lt;pre class="csharpcode"&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; When_saving_a_new_book : Specification
{
    &lt;span class="kwrd"&gt;private&lt;/span&gt; IDataContext _context;
    &lt;span class="kwrd"&gt;private&lt;/span&gt; SomeClass _someClass;

    &lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;override&lt;/span&gt; Before_each_spec()
    {
        _context = Mock&amp;lt;IDataContext&amp;gt;();
        _context.Stub(o =&amp;gt; o.Books).Return(&lt;span class="kwrd"&gt;new&lt;/span&gt; TableFake&amp;lt;Book&amp;gt;(&lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Book&amp;gt;()));
        _someClass.Context = _context;
        Mocks.ReplayAll();
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Then_submit_changes_should_be_called()
    {
        _someClass.Save(&lt;span class="kwrd"&gt;new&lt;/span&gt; Book());
        _context.AssertWasCalled(o =&amp;gt; o.SubmitChanges());
    }

    &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; Then_the_new_book_should_be_added_to_the_table()
    {
        var _book = &lt;span class="kwrd"&gt;new&lt;/span&gt; Book();
        _someClass.Save(_book);
        _context.Books.Count().ShouldBe(1);
        _context.Books.ShouldContain(_book);
    }
}&lt;/pre&gt;
&lt;p&gt;All of the code for the TableProxy&amp;lt;T&amp;gt;, ITable&amp;lt;T&amp;gt;, TableFake&amp;lt;T&amp;gt; and the generator application can be found in the CodeIncubator samples on Google Code.  Feel free to use this information as you see fit and let me know what you think.&lt;/p&gt;
&lt;h3&gt;Get the Code!&lt;/h3&gt;
&lt;ul&gt;&lt;li&gt;&lt;a href="http://code.google.com/p/codeincubator/source/browse/Samples/FakeContext/"&gt;Get the TableFake, ITable, TableProxy Code&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://code.google.com/p/codeincubator/source/browse/Samples/ContextInterfaceGenerator"&gt;Get the Generator Code&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;img src="http://krisscott.net/aggbug/3.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Kris Scott</dc:creator>
            <guid>http://krisscott.net/archive/2008/07/14/use-a-generator-to-test-that-pesky-datacontext.aspx</guid>
            <pubDate>Tue, 15 Jul 2008 01:35:06 GMT</pubDate>
            <wfw:comment>http://krisscott.net/comments/3.aspx</wfw:comment>
            <comments>http://krisscott.net/archive/2008/07/14/use-a-generator-to-test-that-pesky-datacontext.aspx#feedback</comments>
            <wfw:commentRss>http://krisscott.net/comments/commentRss/3.aspx</wfw:commentRss>
        </item>
        <item>
            <title>Why do I need resharper?</title>
            <link>http://krisscott.net/archive/2008/03/06/why-do-i-need-resharper.aspx</link>
            <description>&lt;p&gt;I thought that I'd put together this blog explaining some of the most useful benefits I've found as a long time user of Visual Studio without and extra bells and whistles of a third party refactoring plugin.&lt;/p&gt;
&lt;p&gt;One of the nicer features that I make extensive use of in my current project (which is a solution with 19 projects and probably near a thousand different classes) is the Camel Cased type and method search.  Instead of opening up the solution explorer and sifting through a long list of files, I can simply hit Ctrl-T and type the upper case letters of the class I need.  If I want to see the CustomFieldDesignerViewController class, I start typing, C-F-D-V-C and the list of matches will be reduced to only those who have upper case letters in that order.  The same "Camel Hump Casing" that applies to the type search also applies to the method search, and to the intellisense.  When I want a line that says:&lt;/p&gt;
&lt;p&gt;CustomSomethingDropDownList x = value;&lt;/p&gt;
&lt;p&gt;All I have to do is type C-S-D-D-L and the intellisense will be automatically filtered to only the things which match that camel casing.  The saves an incredible amount of time with typing by allowing the correct items to be selected faster than having to type out the first maybe 10+ characters to get to what you want (esspecially on projects when a lot of the classes start with the same first names, etc)&lt;/p&gt;
&lt;p&gt;The second thing that has come in extremely useful that resharper does is that it finds "illogical" logic.  For instance, if I were to say:&lt;/p&gt;
&lt;p&gt;If (x == 1)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
else if (x ==1)&lt;br /&gt;
{&lt;br /&gt;
}&lt;/p&gt;
&lt;p&gt;Resharper will highlight this else and explain that this will never happen.  Now in my example this is clearly very easy to see from reading it.  But imagine if you had a series of if/else statements with more complicated logic.  It's very useful to be able to detect these because it typically points out a mistake in a branch of the logic as opposed to a useless section.  This saves an immeasurable amount of time by helping proactively identify bugs before they become bugs.&lt;/p&gt;
&lt;p&gt;The highlighting that comes with Resharper is also extremely more detailed then what comes standard with Visual Studio.  Now a lot of people would argue that this highlighting is unnecessary.  It points out a lot of issues which range from "hints" to "errors".  Errors that might otherwise not show up until trying to compile (and compiling a 19 project solution is no small amount of time).  The best part about these issues is that with a simple click of ctrl-enter resharper can typically fix most errors for you.  This also has the added benefit of teaching programmers if they have a habit of doing something that is in reality, unnecessary.  It also helps large groups come up with standards (as resharper's hints and errors are configurable) that everyone can agree to follow.&lt;/p&gt;
&lt;p&gt;These are just a few of the many benefits, I'd feel remiss if I didn't at least mention the enhanced refactoring support, enhanced template support, and automated using inserting (for extension methods, which visual studio does not yet support).&lt;/p&gt;
&lt;p&gt;Take a look at the &lt;a href="http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds"&gt;Re-sharper 4.0 EAP's&lt;/a&gt; if you can, it's certainly worth checking out.&lt;/p&gt;&lt;img src="http://krisscott.net/aggbug/2.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Kris Scott</dc:creator>
            <guid>http://krisscott.net/archive/2008/03/06/why-do-i-need-resharper.aspx</guid>
            <pubDate>Thu, 06 Mar 2008 08:43:35 GMT</pubDate>
            <wfw:comment>http://krisscott.net/comments/2.aspx</wfw:comment>
            <comments>http://krisscott.net/archive/2008/03/06/why-do-i-need-resharper.aspx#feedback</comments>
            <wfw:commentRss>http://krisscott.net/comments/commentRss/2.aspx</wfw:commentRss>
        </item>
        <item>
            <title>LINQ vs Methodized Syntax</title>
            <link>http://krisscott.net/archive/2008/03/03/linq-vs-methodized-syntax.aspx</link>
            <description>&lt;p&gt;Recently we had a debate about which way we prefer to see Linq syntax written.  In the LINQ from/select syntax below:&lt;/p&gt;
&lt;p&gt;&lt;a title="Linq Code" rel="lightbox[LinqSyntax]" href="/Images/linqCodeblock.jpg"&gt;&lt;img alt="Linq Code" src="/Images/linqCodeBlock.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Or, in the Methodized syntax which follows:&lt;/p&gt;
&lt;p&gt;&lt;a title="Methodized Syntax" rel="lightbox[LinqSyntax]" href="/Images/MethodSyntax.jpg"&gt;&lt;img alt="Methodized Syntax" src="/Images/MethodSyntax.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I personally prefer the more methodized syntax where it can be used typically (esspecially when the statement contains only a simple where clause).  However it is worth noting that the Join method which takes in four seperate parameters is quite painful to write against, the inferred generics do not properly pick up what you are working with until you finish the entire statement.  I threw together a more "english friendly" syntax which also has the added benefit of allowing generics to properly infer at each step of the way.&lt;/p&gt;
&lt;p&gt;&lt;a title="Finished Syntax" rel="lightbox[LinqSyntax]" href="/Images/FinalSyntax.jpg"&gt;&lt;img alt="Finished Syntax" src="/Images/FinalSyntax.jpg" /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now granted some people will argue this syntax takes up "more lines", you could put some of these method calls onto one line if you felt it really necessary.  I've included the small sample of source code which you can get &lt;a href="http://www.krisscott.net/images/extension.txt"&gt;here.&lt;/a&gt;&lt;/p&gt;&lt;img src="http://krisscott.net/aggbug/1.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Kris Scott</dc:creator>
            <guid>http://krisscott.net/archive/2008/03/03/linq-vs-methodized-syntax.aspx</guid>
            <pubDate>Mon, 03 Mar 2008 06:41:51 GMT</pubDate>
            <wfw:comment>http://krisscott.net/comments/1.aspx</wfw:comment>
            <comments>http://krisscott.net/archive/2008/03/03/linq-vs-methodized-syntax.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://krisscott.net/comments/commentRss/1.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>