<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Schwammy Says...</title>
  <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/" />
  <link rel="self" href="http://www.blog.ingenuitynow.net/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2010-08-23T22:30:05.6870221-04:00</updated>
  <author>
    <name>Andrew Schwam</name>
  </author>
  <subtitle>Whatever comes to mind.</subtitle>
  <id>http://www.blog.ingenuitynow.net/</id>
  <generator uri="http://www.dasblog.net" version="1.9.6264.0">DasBlog</generator>
  <entry>
    <title>Visual Studio Tip for working with Code Regions</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Visual+Studio+Tip+For+Working+With+Code+Regions.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,b3b292a4-4844-4e99-a0a7-f252f029c133.aspx</id>
    <published>2010-08-23T22:30:05.6870221-04:00</published>
    <updated>2010-08-23T22:30:05.6870221-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="Visual Studio" label="Visual Studio" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Visual%2BStudio.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I love working with other developers, whether it be formal “paired programming” or
just one person helping the other with a problem.  I love it so much because
I always learn something new.  Sometimes what I learn is some major, powerful
technique for doing something complicated but usually it is not.  Usually it
is one of those simple, cool, helpful tidbits.  You probably know what I mean. 
You sit with a coworker and without any fanfare they type some keystroke and you say,
“Whoa, I didn’t know you could do that… how did you do that?”  Here’s an example
of something I do that I’ve noticed many developers aren’t aware of. It’s nothing
fancy, but I find it helpful.
</p>
        <p>
Like many developers, I like to surround my code in collapsible regions.  I don’t
get too carried away with this technique, but I usually have a region for Public Properties,
another for Public Methods, maybe one for Class Level variables.  Stuff like
that.  Anyway, most people label the top of the region but I’ve found that many
developers don’t know you can label the bottom.  That extra label on the bottom
is really helpful when you are scrolled down far.  Without the label, you find
an #endregion and you don’t know what region it is the end of.  This is especially
tricky when working with nested tags.  How do you do it?  Just type the
label :).
</p>
        <h5>#endregion with label:
</h5>
        <pre class="code">
          <span style="color: blue">#region </span>Public Methods <span style="color: green">//
put stuff here... </span><span style="color: blue">#endregion </span>Public Methods</pre>
        <p>
I think the reason people don’t do this more is that (I’m pretty sure) you could NOT
do this with some earlier version of Visual Studio.  Those of us that have worked
with VS for a long time just got used to doing #endregions without the label. 
Back then I used to put a comment at the end of my region like this, but it isn’t
necessary anymore:
</p>
        <pre class="code">
          <span style="color: blue">#region </span>Public Methods <span style="color: green">//
put stuff here... </span><span style="color: blue">#endregion </span><span style="color: green">//Public
Methods </span></pre>
        <p>
But at some point, maybe VS2008, we were allowed to just put the label at the end.  
</p>
        <h5>But wait, there’s more…
</h5>
        <p>
Recently I had the idea to update the code snippet for “Surround with Region”. 
You can use this built in Visual Studio snippet by highlighting some code and hitting
“Ctrl + s”, then select #region from the dropdown.  When you do, your code is
automatically surrounded by the #region and #endregion, plus you’ll be prompted for
the text for the label.  I tweaked it so it puts the label at the beginning and
the end.  
</p>
        <ul>
          <li>
You can find your snippets by going to the Code Snippets Manager (Tools &gt; Code
Snippets Manager, or Ctrl + k, Ctrl + b). 
</li>
          <li>
Then select Visual C# (or VB I guess, I don’t use it :) ) 
</li>
          <li>
When you click on the folder or the name of the snippet, the path is shown in the
window.  See the screen shot below… 
</li>
          <li>
In my case, and I think it is pretty standard, I open the snippet in Notepad at the
following path: C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual
C#\pp_region.snippet 
</li>
          <li>
Then change the snippet and use the XML listed below. 
</li>
          <li>
The only thing I changed was the 24th line.  After “#endregion” I put in the
$name$ variable.  The variable was already used in the snippet on line 22 so
it was easy.  
</li>
          <li>
Note: If you can’t save this snippet with notepad, make sure Visual Studio is closed
and run notepad “as administrator”. 
</li>
        </ul>
        <h5>Snippet XML:
</h5>
        <p>
&lt;?xml version="1.0" encoding="utf-8" ?&gt; 
<br />
&lt;CodeSnippets  xmlns="<a href="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet&quot;">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"</a>&gt; 
<br />
    &lt;CodeSnippet Format="1.0.0"&gt; 
<br />
        &lt;Header&gt; 
<br />
            &lt;Title&gt;#region&lt;/Title&gt; 
<br />
            &lt;Shortcut&gt;#region&lt;/Shortcut&gt; 
<br />
            &lt;Description&gt;Code
snippet for #region&lt;/Description&gt; 
<br />
            &lt;Author&gt;Microsoft
Corporation&lt;/Author&gt; 
<br />
            &lt;SnippetTypes&gt; 
<br />
               
&lt;SnippetType&gt;Expansion&lt;/SnippetType&gt; 
<br />
               
&lt;SnippetType&gt;SurroundsWith&lt;/SnippetType&gt; 
<br />
            &lt;/SnippetTypes&gt; 
<br />
        &lt;/Header&gt; 
<br />
        &lt;Snippet&gt; 
<br />
            &lt;Declarations&gt; 
<br />
               
&lt;Literal&gt; 
<br />
                   
&lt;ID&gt;name&lt;/ID&gt; 
<br />
                   
&lt;ToolTip&gt;Region name&lt;/ToolTip&gt; 
<br />
                   
&lt;Default&gt;MyRegion&lt;/Default&gt; 
<br />
               
&lt;/Literal&gt; 
<br />
            &lt;/Declarations&gt; 
<br />
            &lt;Code Language="csharp"&gt;&lt;![CDATA[#region
$name$ 
<br />
        $selected$ $end$ 
<br />
    #endregion $name$]]&gt; 
<br />
            &lt;/Code&gt; 
<br />
        &lt;/Snippet&gt; 
<br />
    &lt;/CodeSnippet&gt; 
<br />
&lt;/CodeSnippets&gt;
</p>
        <h5>Code Snippets Manager:
</h5>
        <p>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/VisualStudioTipforworkingwithCodeRegions_13286/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/VisualStudioTipforworkingwithCodeRegions_13286/image_thumb.png" width="494" height="365" />
          </a>
        </p>
        <p>
Code Snippets are really cool and easy to use.  For more information, <a href="http://www.blog.ingenuitynow.net/Using+Custom+Code+Snippets+In+VS2008.aspx" target="_blank">please
see this post</a>.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=b3b292a4-4844-4e99-a0a7-f252f029c133" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Silverlight: Creating a WriteableBitmap from a Uri Source</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Silverlight+Creating+A+WriteableBitmap+From+A+Uri+Source.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,7e7870a9-c5bf-476c-bd94-fad3f0d3ae2a.aspx</id>
    <published>2010-08-13T22:22:52.1283523-04:00</published>
    <updated>2010-08-13T22:22:52.1283523-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’ve seen a few posts describing how to use a Uri as a source for a BitmapImage, and
then the BitmapImage as the source for the WriteableBitmap.  But that didn’t
work so well until I figured out the trick.  Here goes…
</p>
        <p>
First, here is the code that I <em>THOUGHT</em> would work, but did not.
</p>
        <pre class="code">
          <span style="color: #2b91af">Uri </span>uri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"http://somedomain.com/someimage.png"</span>); <span style="color: #2b91af">BitmapImage </span>bitmapImage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>(uri); <span style="color: #2b91af">WriteableBitmap </span>writeableBitmap
= <span style="color: blue">new </span><span style="color: #2b91af">WriteableBitmap</span>(bitmapImage);</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>When you run that, you’ll most likely
get an exception (I did) because the BitmapImage is not set when it is used for the
WriteableBitmap.  My hypothesis was that the BitmapImage doesn’t render itself
until it is needed.  I started looking through the properties of the BitmapImage,
luckily there aren’t too many, when I came across the CreateOptions property – that
sounded interesting!  It is an Enum with 3 values: DelayCreation, IgnoreImageCache,
and None.  Guess which one is set as the default?  DelayCreation! 
Here was my next pass at the code:
</p>
        <pre class="code">
          <span style="color: #2b91af">Uri </span>uri = <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"http://somedomain.com/someimage.png"</span>); <span style="color: #2b91af">BitmapImage </span>bitmapImage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>();
bitmapImage.CreateOptions = <span style="color: #2b91af">BitmapCreateOptions</span>.None;
bitmapImage.UriSource = uri; <span style="color: #2b91af">WriteableBitmap </span>writeableBitmap
= <span style="color: blue">new </span><span style="color: #2b91af">WriteableBitmap</span>(bitmapImage);</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
That’s it, pretty simple and it works.  But I took it one step further just in
case.  I’m not positive this is necessary but I like to play it safe.  When
inspecting the properties of the BitmapImage, I saw there is an ImageOpened event.
I was thinking that some images may load slowly and I wasn’t sure if that happens
asynchronously or not.  Would my application wait around for them to finish loading? 
I don’t really know but it sure sounds like the ImageOpened event would be a way to
make sure my images were loaded.  Here is my final take:
</p>
        <pre class="code">
          <span style="color: blue">public void </span>SomeMethod() { <span style="color: #2b91af">Uri </span>uri
= <span style="color: blue">new </span><span style="color: #2b91af">Uri</span>(<span style="color: #a31515">"http://somedomain.com/someimage.png"</span>); <span style="color: #2b91af">BitmapImage </span>bitmapImage
= <span style="color: blue">new </span><span style="color: #2b91af">BitmapImage</span>();
bitmapImage.CreateOptions = <span style="color: #2b91af">BitmapCreateOptions</span>.None;
bitmapImage.ImageOpened += ImageOpened; bitmapImage.UriSource = uri; } <span style="color: blue">void </span>ImageOpened(<span style="color: blue">object </span>sender, <span style="color: #2b91af">RoutedEventArgs </span>e)
{ <span style="color: #2b91af">BitmapImage </span>bm = (<span style="color: #2b91af">BitmapImage</span>)sender; <span style="color: #2b91af">WriteableBitmap </span>wbm
= <span style="color: blue">new </span><span style="color: #2b91af">WriteableBitmap</span>(bm); <span style="color: green">//now
I can use wbm for whatever I need... </span>}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>Good luck with your WriteableBitmaps!<img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=7e7870a9-c5bf-476c-bd94-fad3f0d3ae2a" /></div>
    </content>
  </entry>
  <entry>
    <title>Creating Custom Silverlight 4 Controls That Support Command Binding</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Creating+Custom+Silverlight+4+Controls+That+Support+Command+Binding.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,e5da246b-41c7-42b7-9b14-ac9cca84f622.aspx</id>
    <published>2010-08-04T22:05:17.5218709-04:00</published>
    <updated>2010-08-04T22:05:17.5218709-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="MVVM" label="MVVM" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,MVVM.aspx" />
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’m a big fan of the MVVM Pattern for Silverlight development, so naturally I was
excited about Silverlight 4 having support for Commanding.  I recently upgraded
my current project to Silverlight 4 and I found that binding my buttons to my View
Model commands was pretty easy.  However, it wasn’t long before I needed to create
a custom control and I wasn’t sure how the Command Binding would work.  I searched
the web for some samples but found none so I was on my own.  It wasn’t too hard
to figure out the solution, here is how I did it.
</p>
        <p>
The setup… For the sake of this demo, I’ll need a resuable control that combines 2
buttons:  Save and Cancel.  I’ll call it a SaveCancel control :). 
The control is simple enough.  Thanks to Silverlight 4, buttons already support
the command binding – all you have to do is bind the command in your View Model to
the Command property of the button (and optionally the CommandParameter property). 
If you aren’t familiar with the basics of commanding, I suggest you check out this <a href="http://johnpapa.net/silverlight/5-simple-steps-to-commanding-in-silverlight/" target="_blank">blog
post by John Papa</a>.  Anyway, when it is time to use the SaveCancel control
on another page/control, it becomes pretty clear that you can’t access the individual
button’s properties to bind to them!  The individual buttons are not publicly
available since they are “child” controls of the SaveCancel control.  The solution: 
The Command and CommandParameter properties of each button are just DependencyProperties
so all you have to do is create matching properties on the new control that “wrap”
the internal properties.
</p>
        <h5>Here’s how it’s done…
</h5>
        <p>
First, the XAML of the SaveCancel control is pretty straightforward:
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">UserControl </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Class</span>
          <span style="color: blue">="CustomControlWithCommanding.SaveCancel" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">="http://schemas.microsoft.com/winfx/2006/xaml/presentation" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">:</span>
          <span style="color: red">x</span>
          <span style="color: blue">="http://schemas.microsoft.com/winfx/2006/xaml" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">:</span>
          <span style="color: red">d</span>
          <span style="color: blue">="http://schemas.microsoft.com/expression/blend/2008" </span>
          <span style="color: red">xmlns</span>
          <span style="color: blue">:</span>
          <span style="color: red">mc</span>
          <span style="color: blue">="http://schemas.openxmlformats.org/markup-compatibility/2006" </span>
          <span style="color: red">mc</span>
          <span style="color: blue">:</span>
          <span style="color: red">Ignorable</span>
          <span style="color: blue">="d"
&gt; &lt;</span>
          <span style="color: #a31515">StackPanel </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="LayoutRoot" </span>
          <span style="color: red">Orientation</span>
          <span style="color: blue">="Horizontal"
&gt; &lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="Save" </span>
          <span style="color: red">Width</span>
          <span style="color: blue">="50" </span>
          <span style="color: red">Height</span>
          <span style="color: blue">="25" </span>
          <span style="color: red">Content</span>
          <span style="color: blue">="Save" </span>
          <span style="color: red">Margin</span>
          <span style="color: blue">="0,0,5,0"
/&gt; &lt;</span>
          <span style="color: #a31515">Button </span>
          <span style="color: red">x</span>
          <span style="color: blue">:</span>
          <span style="color: red">Name</span>
          <span style="color: blue">="Cancel" </span>
          <span style="color: red">Width</span>
          <span style="color: blue">="50" </span>
          <span style="color: red">Height</span>
          <span style="color: blue">="25" </span>
          <span style="color: red">Content</span>
          <span style="color: blue">="Cancel"
/&gt; &lt;/</span>
          <span style="color: #a31515">StackPanel</span>
          <span style="color: blue">&gt;
&lt;/</span>
          <span style="color: #a31515">UserControl</span>
          <span style="color: blue">&gt; </span>
        </pre>
        <p>
Next the Code Behind of the SaveCancel control.  Here is where all of the wiring
is done for each button’s commands. There are a few parts.  First is the DependencyProperty
for the Save button’s command.  Like any dependency property, you need a public
property with a Get/Set that sets the value of the Dependency Property as well as
a DependencyProperty:
</p>
        <pre class="code">
          <span style="color: blue">public </span>
          <span style="color: #2b91af">ICommand </span>SaveCommand
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>(<span style="color: #2b91af">ICommand</span>)GetValue(SaveCommandProperty);
} <span style="color: blue">set </span>{ SetValue(SaveCommandProperty, <span style="color: blue">value</span>);
} } <span style="color: blue">public static readonly </span><span style="color: #2b91af">DependencyProperty </span>SaveCommandProperty
= <span style="color: #2b91af">DependencyProperty</span>.Register(<span style="color: #a31515">"SaveCommand"</span>, <span style="color: blue">typeof</span>(<span style="color: #2b91af">ICommand</span>), <span style="color: blue">typeof</span>(<span style="color: #2b91af">SaveCancel</span>), <span style="color: blue">new </span><span style="color: #2b91af">PropertyMetadata</span>(<span style="color: blue">null</span>,
OnSaveCommandChanged));</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>Note that in the PropertyMetaData
argument for the SaveCommandProperty we are passing in a callback reference (OnSaveCommandChanged). 
We’ll use that method to set the value of the new DependencyProperty to the actual
Save button.  Here is the code:
</p>
        <pre class="code">
          <span style="color: blue">private static void </span>OnSaveCommandChanged(<span style="color: #2b91af">DependencyObject </span>d, <span style="color: #2b91af">DependencyPropertyChangedEventArgs </span>e)
{ <span style="color: #2b91af">SaveCancel </span>sc = (<span style="color: #2b91af">SaveCancel</span>)d;
sc.Save.Command = (<span style="color: #2b91af">ICommand</span>)e.NewValue; }</pre>
        <p>
To be complete, we’ll do some similar work to make sure that we can wire up the CommandParameter
to the Save button as well, even though we don’t really need it to make the button
work for this sample.   
</p>
        <pre class="code">
          <span style="color: blue">public object </span>SaveCommandParameter
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>GetValue(SaveCommandParameterProperty);
} <span style="color: blue">set </span>{ SetValue(SaveCommandParameterProperty, <span style="color: blue">value</span>);
} } <span style="color: blue">public static readonly </span><span style="color: #2b91af">DependencyProperty </span>SaveCommandParameterProperty
= <span style="color: #2b91af">DependencyProperty</span>.Register(<span style="color: #a31515">"SaveCommandParameter"</span>, <span style="color: blue">typeof</span>(<span style="color: blue">object</span>), <span style="color: blue">typeof</span>(<span style="color: #2b91af">SaveCancel</span>), <span style="color: blue">new </span><span style="color: #2b91af">PropertyMetadata</span>(<span style="color: blue">null</span>,
OnSaveCommandParameterChanged)); <span style="color: blue">private static void </span>OnSaveCommandParameterChanged(<span style="color: #2b91af">DependencyObject </span>d, <span style="color: #2b91af">DependencyPropertyChangedEventArgs </span>e)
{ <span style="color: #2b91af">SaveCancel </span>sc = (<span style="color: #2b91af">SaveCancel</span>)d;
sc.Save.CommandParameter = e.NewValue; }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
To use the new SaveCancel control in another Silverlight control, all I need is some
code like the sample below.  It works perfectly with no events wired up and no
code behind on the page where it is used!
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">CustomControlWithCommanding</span>
          <span style="color: blue">:</span>
          <span style="color: #a31515">SaveCancel </span>
          <span style="color: red">SaveCommand</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">SaveCommand</span>
          <span style="color: blue">}" </span>
          <span style="color: red">CancelCommand</span>
          <span style="color: blue">="{</span>
          <span style="color: #a31515">Binding </span>
          <span style="color: red">CancelCommand</span>
          <span style="color: blue">}"
/&gt; </span>
        </pre>
        <p>
Of course, my ViewModel must have the commands (SaveCommand and CancelCommand) to
support this as well.  But that is just normal ViewModel commanding.  Again,
if you aren’t familiar with that, check out <a href="http://johnpapa.net/silverlight/5-simple-steps-to-commanding-in-silverlight/" target="_blank">John
Papa’s blog post</a>.
</p>
        <h5>Complete Source Code: 
</h5>
        <p>
To see the complete sample solution, <a href="http://www.blog.ingenuitynow.net/content/binary/CustomControlWithCommanding.zip" target="_blank">download
it here</a>.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=e5da246b-41c7-42b7-9b14-ac9cca84f622" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Work-around for VS 2010 &amp;amp; Silverlight Add Reference Hint Path Bug</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Workaround+For+VS+2010+Amp+Silverlight+Add+Reference+Hint+Path+Bug.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,59296bb8-87e5-469a-8b28-cc7d05475b31.aspx</id>
    <published>2010-07-25T15:19:34.395137-04:00</published>
    <updated>2010-07-25T15:19:34.395137-04:00</updated>
    <category term="Silverlight" label="Silverlight" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Silverlight.aspx" />
    <category term="Visual Studio" label="Visual Studio" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Visual%2BStudio.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
There seems to be a bug in Visual Studio 2010 when adding references to Silverlight
projects.  You can add “regular” .Net references just fine using the add reference
dialog.  However if you use the browse feature to find a reference in an “unusual”
location, Visual Studio is supposed to record that location along with the reference
in the .csproj file.  It includes the location via the HintPath property.
</p>
        <p>
At my company, we use build machines and we don’t like to install a lot of software
on them.  So in source control we have a folder (assets\lib) that includes all
of the necessary .dll files that we need to build or projects.  When we add references
to our project, like for a Silverlight control library, we add it from the assets\lib\Silverlight
folder, even though that library is installed on my local machine.  Anyway, when
I tried doing this in Visual Studio 2010 (I’m upgrading our existing 2008 solution),
VS 2010 isn’t including the HintPath.  If you aren’t careful, you may not realize
this is happening.  That’s because since the library is installed on my machine,
the build works just fine, except it pulls the .dll from the wrong place.  If
I were to try this build from my build server, it would fail though.  
</p>
        <p>
Anyway, here is the workaround.  It’s a pain but it works…
</p>
        <ol>
          <li>
Add your references as you would normally do it via Visual Studio. 
</li>
          <li>
Right click the project file in the Solution Explorer and choose Unload Project. 
</li>
          <li>
Once the project is unloaded, right click again and chose edit.  This will open
the file, and xml document, in Visual Studio. 
</li>
          <li>
Find the incorrect reference in the file and edit it manually.  You’ll need to
make it look something like this: <pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Reference </span><span style="color: red">Include</span><span style="color: blue">=</span>"<span style="color: blue">System.Windows.Controls,
Version=2.0.5.0, Culture=neutral, 
<br />
PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL</span>" <span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">HintPath</span><span style="color: blue">&gt;</span>..\..\..\Assets\lib\Silverlight\System.Windows.Controls.dll<span style="color: blue">&lt;/</span><span style="color: #a31515">HintPath</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Reference</span><span style="color: blue">&gt; </span></pre><a href="http://11011.net/software/vspaste"></a></li>
          <li>
Save the file and close it. 
</li>
          <li>
Reload the project. 
</li>
        </ol>
        <p>
When you are done you can verify that it works.  Here’s how…
</p>
        <ol>
          <li>
In Visual Studio, choose Tools &gt; Options. 
</li>
          <li>
Find the Projects and Solutions section and select Build and Run. 
</li>
          <li>
In the section “MsBuild Project build output verbosity”, select “Detailed”. 
</li>
          <li>
Now build your project.  When you do so, you’ll see a lot of details of what
goes on in the Output Window.  You’ll need to find the output that relates to
the file that you specified, in my case it is System.Windows.Controls and the important
output looks like this: <blockquote><p><em>Primary reference "System.Windows.Controls, Version=2.0.5.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35". 
<br />
      Resolved file path is "C:\Projects\MC\BackOffice\UI\branches\2010Migration\Assets\lib\Silverlight\System.Windows.Controls.dll". 
<br />
      Reference found at search path location "{HintPathFromItem}".</em></p></blockquote></li>
          <li>
You will note that it tells you specifically which path was used to resolve the reference! 
And if you are so lazy that you don’t want to read the entire path used, you see that
it says the reference was found at the search path location “{HintPathFromItem}”. 
That means it used the path you specified.</li>
        </ol>
        <p>
I hope this helps!  Good luck and let’s hope they fix this bug soon.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=59296bb8-87e5-469a-8b28-cc7d05475b31" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Welcome Sarah Maya Schwam!</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Welcome+Sarah+Maya+Schwam.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,e79e3570-abe1-4137-bf4f-8d61231d6c86.aspx</id>
    <published>2010-07-08T21:17:14.6415223-04:00</published>
    <updated>2010-07-08T21:17:14.6415223-04:00</updated>
    <category term="Ben" label="Ben" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Ben.aspx" />
    <category term="Family" label="Family" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Family.aspx" />
    <category term="Sarah" label="Sarah" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Sarah.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
We went to the beach for a long weekend as a family of three, we came home as a family
of four!  Here are the details of our adventure.  Read it if you want or
just look at the pictures!
</p>
        <p>
On Thursday afternoon Rebecca went for her weekly checkup at the doctor.  Our
baby was due in 3.5 weeks but they said she was doing great and had nothing to worry
about.  We should go enjoy ourselves at the shore.  We were both really
looking forward to the long weekend.  We had just found a new house, sold our
house (although both closings are in September), and we have a baby on the way. 
Plus our son Ben was really hitting his stride as a two year old, mastering the art
of “No”.  So some time off was really in order.  We had a nice relaxing
day on Friday, the weather was perfect and Ben was having a blast playing in the sand.
</p>
        <p align="center">
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0014.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="IMG_0014" border="0" alt="IMG_0014" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0014_thumb.jpg" width="244" height="184" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0023.jpg">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="IMG_0023" border="0" alt="IMG_0023" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0023_thumb.jpg" width="244" height="184" />
          </a>
        </p>
        <p align="center">
          <em>Ben at the beach playground with cousins Hannah and Sam.  And Ben playing
with Hannah (and Uncle Andy)</em>
        </p>
        <p>
Part 1 – Controlled Panic:  Later that evening we were headed for a walk on the
boardwalk with family.  Ben and I were a few minutes ahead of Rebecca. 
When Rebecca didn’t follow us, we had to head back to see what was up.  We met
up with Rebecca and she said “I think my water just broke”.  FYI, that didn’t
happen when Ben was born so she wasn’t positive what was happening.  There was
a lot of family at the house and lots of opinions and ideas.  “You should call
the doctor”, “You should pack up and leave”, “Relax and go for a walk”, etc. 
We called the doctor back home.  Keep in mind, we’re on the Jersey shore near
Atlantic City, about 1.5 hrs from home.  The doctors advice was to head right
to the hospital.  After water breaks anything could happen, and happen fast. 
I thought, the last thing we want to have happen is to deliver a baby on the side
of the AC Expressway.  Next, we had a nice family discussion/argument about which
hospital to go to.  There were three to choose from.  My brother-in-law
and sister-in-law took charge of that and called to figure out which is best. 
That was a big help.  By this time it was about 8:30pm.  The next problem
to deal with was how to get Ben to sleep.  Since it is the beginning of the summer,
the house is a bit strange to him and he wanted me to sleep in the room with him. 
I laid down for a while and every minute felt like an hour.  He finally fell
asleep, we packed up and we were off to Atlanticare Regional Medical Center Mainland
Campus.  
</p>
        <p align="center">
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0796.jpg">
            <img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="IMG_0796" border="0" alt="IMG_0796" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0796_thumb.jpg" width="244" height="164" />
          </a>
          <em>On
our walk before things got interesting.  Ben’s with cousin Sylvia (Sylvie) but
Ben calls her “So-be”.  I think he is love with her.</em>
        </p>
        <p>
One nice part of this whole crazy situation is that we had plenty of people to help
with Ben.  If we were at home we would have had to find someone to come over
and watch him for the night, plus we’d need people to help out all weekend long too. 
Instead, we just drove off.  Well, first we spewed out a bunch of instructions
about how to keep him happy while everyone nodded their heads saying “Sure, we’ve
got it”.  Most of it didn’t matter, we knew he’d be in great hands if they followed
our advice or not.
</p>
        <p>
Part 2 – Hurry Up and Wait:  We got to the hospital and finally were in our room
by around 11pm or so.  The staff now told Rebecca that since her water broke,
she had to stay in bed.  Rebecca was seriously unhappy with that information. 
When Ben was born she was told the best way to work through labor and keep things
moving was to be up and around.  And she wasn’t that far along, so we’d likely
not have a baby until the next day!  We both slept in the hospital but I’m sure
that everyone knows you can’t really get much sleep in a hospital.  
</p>
        <p>
Part 3 – Time to Rock and Roll:  Around 5:30 am they gave Rebecca a shot of Pitocin
to move things along.  At 6:30 they injected some pain killers into her epidural
and at 7:30 am on July 3rd Sarah Maya Schwam was born!  She weighed 5 lbs, 12
oz and was 19.25 inches long.  As a side note, I thought she looked shorter than
that.  This week the pediatrician measured her at 18.5 inches.  I’m not
sure what is up with that.  Being 3.5 weeks early, the staff was a little concerned
and put the NIC unit on alert.  But after a check up, they decided that she was
ok to stay with us and they’d keep an extra close eye on her.  That was a BIG
relief to us.  10 fingers, 10 toes, and TONS of dark brown hair and cute as a
button (whatever that means).  
</p>
        <p>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0028.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0028" border="0" alt="IMG_0028" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0028_thumb.jpg" width="244" height="184" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0808_1.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0808" border="0" alt="IMG_0808" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0808_thumb_1.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0815.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0815" border="0" alt="IMG_0815" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0815_thumb.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0821.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0821" border="0" alt="IMG_0821" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0821_thumb.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0831.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0831" border="0" alt="IMG_0831" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0831_thumb.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0850.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0850" border="0" alt="IMG_0850" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0850_thumb.jpg" width="244" height="164" />
          </a>
        </p>
        <p>
Part 4 – Now What?:  OK, so we’ve got a healthy baby but we are pretty far from
home with no “baby stuff”.  Well it turns out you don’t need too much stuff and
in our neck of the woods (even down at the shore) there are always stores around. 
My sister-in-law Dawn hit the drug store for some diapers and stuff, and a little
boutique at the beach for a few outfits.  But you can’t leave the hospital without
a car seat!  We planned to send the family out to Baby’s R Us to buy a new one
when a few friends came to the rescue.  Neighbors Megan and Glen were on their
way to the shore for July 4th when they saw my email baby announcement.  They
offered to stop at our house and pick up the baby seat and other supplies and drop
them at the hospital!  How cool is that?  We were all set!
</p>
        <p>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0849.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0849" border="0" alt="IMG_0849" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0849_thumb.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0840.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0840" border="0" alt="IMG_0840" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0840_thumb.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0860.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0860" border="0" alt="IMG_0860" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0860_thumb.jpg" width="244" height="164" />
          </a>
        </p>
        <p align="center">
          <em>Our family including Aunt Dawn, Megan (who came to our rescue) and Sarah, and
the four of us leaving the hospital via the required wheel chair.</em>
        </p>
        <p>
Part 5 – Home At Last:  After 2 nights in the hospital (3 if you count the night
before Sarah was born) everyone was released and we went back to the beach house. 
It was Monday of a holiday weekend and we didn’t want to get stuck in any traffic
coming home so we spent the night at the beach.  Of course, we didn’t have a
bassinette there so we had to improvise with a drawer!   By noon on Tuesday
we were finally back in our house!  It was great coming home and Benny was really
happy.  I think the trip was toughest on him.
</p>
        <p>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0865.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0865" border="0" alt="IMG_0865" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0865_thumb.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0869.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0869" border="0" alt="IMG_0869" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0869_thumb.jpg" width="244" height="164" />
          </a>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0883.jpg">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="IMG_0883" border="0" alt="IMG_0883" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/WelcomeSaraMayaSchwam_D7FC/IMG_0883_thumb.jpg" width="244" height="164" />
          </a>
        </p>
        <p align="center">
          <em>Sarah with Rebecca’s dad, with my Mom, in her drawer (it looks safe and comfy,
right?).</em> 
</p>
        <p>
What a crazy “vacation” this turned out to be!  It would have been a lot harder
without our family that helped out so much with Benny.  He had a great time playing
at the beach with his cousins.  I have no idea how we could have got through
this without the family helping out.  Now Benny is totally in love with Sarah,
like nothing I have ever seen before.  He just wants to kiss her and hug her
and tickle her and just be around her ALL of the time.  “I want Sarah” is the
new phrase heard around the house… over and over and over again.  We’re glad
he likes her but it is making us a little crazy.  I’ve been told that this will
wear off pretty soon and he’ll get used to having her around.  Having a sister
certainly did not cure the “terrible two’s”.  This little guy really tries to
run the show around here.  Lucky for us he’s still a sweet kid most of the time.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=e79e3570-abe1-4137-bf4f-8d61231d6c86" />
      </div>
    </content>
  </entry>
  <entry>
    <title>How to Unit Test Code that uses AppSettings from Web.Config</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/How+To+Unit+Test+Code+That+Uses+AppSettings+From+WebConfig.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,ac7d1789-cba7-4461-bfcc-85e4fa308840.aspx</id>
    <published>2010-05-25T23:37:59.948-04:00</published>
    <updated>2010-05-28T09:15:26.4304774-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="asp.net" label="asp.net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,asp.net.aspx" />
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="MOQ" label="MOQ" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,MOQ.aspx" />
    <category term="NUnit" label="NUnit" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,NUnit.aspx" />
    <category term="Unit Testing" label="Unit Testing" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Unit%2BTesting.aspx" />
    <content type="html">&lt;p&gt;
I’ve said this so many times, writing unit tests is easy if the code was written well
in the first place. And when I say “written well”, what I really mean is “written
to be testable”.
&lt;/p&gt;
&lt;p&gt;
So here is the situation: I have a simple service that does some logging, most of
the details are not important. But in the LogDebug() method, the code checks the web.config
file to see if the debug logging is enabled. That too in itself is pretty simple stuff.
Typically it would look like this: 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;configDebugMode = &lt;span style="color: blue"&gt;bool&lt;/span&gt;.Parse(&lt;span style="color: #2b91af"&gt;WebConfigurationManager&lt;/span&gt;.AppSettings[&lt;span style="color: #a31515"&gt;"Logging.Debug"&lt;/span&gt;]);&lt;/pre&gt;
&lt;p&gt;
&lt;i&gt;&lt;b&gt;EDIT: &lt;/b&gt;I should point out what I mean when I say below that testing this
code isn't very easy. Sure, testing it is easy if I only want to test one value from
the web.config file, such as 'true'. But in this situation, I want to test when the
config file value is 'true', and also when it is 'false'.&lt;/i&gt;
&lt;/p&gt;
&lt;p&gt;
But testing that isn’t very easy at all. You’d have to do some crazy stuff with multiple
config files or something. But if the code was written differently, it’s very easy
to test. The key is to wrap the call to WebConfigurationManager in another class that
implements and interface, then use &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163739.aspx" target="_blank"&gt;dependency
injection&lt;/a&gt;. And with the class designed like that, you can use a simple &lt;a href="http://en.wikipedia.org/wiki/Mock_object" target="_blank"&gt;mock&lt;/a&gt; to
make the testing simpler. It’s really easy, here is how I’m doing it.
&lt;/p&gt;
&lt;p&gt;
First I define an interface, I’m calling it IConfigurationManager: 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public interface &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IConfigurationManager &lt;/span&gt;{ &lt;span style="color: blue"&gt;string &lt;/span&gt;GetAppSetting(&lt;span style="color: blue"&gt;string &lt;/span&gt;key); &lt;span style="color: #2b91af"&gt;ConnectionStringSettings &lt;/span&gt;GetConnectionString(&lt;span style="color: blue"&gt;string &lt;/span&gt;key);
}&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The interface has two simple methods
so I can use it to get AppSettings and ConnectionStrings. Could be I’ll need to add
some more methods later but I don’t need them yet. Next I implement the interface
in a concrete class that can get the values from the web.config. This too is really
simple. I’m calling this class WebConfigConfigurationManager. That may sound weird
and redundant but it tells me that it is a ConfigurationManager that gets values from
WebConfig. If I need an implementation that pulls values from a db I could make another
one called DbConfigurationManager. Here is the code:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebConfigConfigurationManager &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;IConfigurationManager &lt;/span&gt;{ &lt;span style="color: blue"&gt;public
string &lt;/span&gt;GetAppSetting(&lt;span style="color: blue"&gt;string &lt;/span&gt;key) { &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebConfigurationManager&lt;/span&gt;.AppSettings[key];
} &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ConnectionStringSettings &lt;/span&gt;GetConnectionString(&lt;span style="color: blue"&gt;string &lt;/span&gt;key)
{ &lt;span style="color: blue"&gt;return &lt;/span&gt;&lt;span style="color: #2b91af"&gt;WebConfigurationManager&lt;/span&gt;.ConnectionStrings[key];
} }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
So far this is was pretty easy to write and you can see it isn’t too complicated so
it should work pretty well. But this doesn’t make it any more testable. The key to
making it testable is Dependency Injection. My LoggingService will “depend on” this
WebConfigConfigurationManager class. But instead of just instantiating WebConfigConfigurationManager
within LoggingService, I’ll inject it in. By doing that, I’ll also be able to inject
something different during unit tests. Hang in there, I’ll show you how. First, here
is the injection part, it is much simpler than it sounds. I inject the dependency
via the constructor, you’ll note I am injecting a few other dependencies too:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;LoggingService(&lt;span style="color: #2b91af"&gt;IEnterpriseLoggingRepository &lt;/span&gt;loggingRepository, &lt;span style="color: #2b91af"&gt;ILoggingInformationAdapter &lt;/span&gt;loggingAdapter, &lt;span style="color: #2b91af"&gt;IConfigurationManager &lt;/span&gt;configurationManager)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(loggingRepository == &lt;span style="color: blue"&gt;null&lt;/span&gt;)
{ &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"loggingRepository"&lt;/span&gt;);
} _loggingRepository = loggingRepository; &lt;span style="color: blue"&gt;if &lt;/span&gt;(loggingAdapter
== &lt;span style="color: blue"&gt;null&lt;/span&gt;) { &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"loggingAdapter"&lt;/span&gt;);
} _loggingAdapter = loggingAdapter; &lt;span style="color: blue"&gt;if &lt;/span&gt;(configurationManager
== &lt;span style="color: blue"&gt;null&lt;/span&gt;) { &lt;span style="color: blue"&gt;throw new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ArgumentNullException&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"configurationManager"&lt;/span&gt;);
} _configurationManager = configurationManager; }&lt;/pre&gt;
&lt;p&gt;
If you were expecting something fancy, sorry to disappoint you. The Dependency Injection
part is pretty simple too. Here is the LogDebug() method too. Please don’t worry about
the details of _loggingAdapter.ToDomainModel(). That is something I need to turn the
“message” into something that the LoggingRepository can work with. 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;LogDebug(&lt;span style="color: blue"&gt;string &lt;/span&gt;message)
{ &lt;span style="color: blue"&gt;try &lt;/span&gt;{ &lt;span style="color: green"&gt;//are we in debug
mode? &lt;/span&gt;&lt;span style="color: blue"&gt;bool &lt;/span&gt;configDebugMode = &lt;span style="color: blue"&gt;bool&lt;/span&gt;.Parse(_configurationManager.GetAppSetting(&lt;span style="color: #a31515"&gt;"Logging.Debug"&lt;/span&gt;)); &lt;span style="color: blue"&gt;if &lt;/span&gt;(!configDebugMode) &lt;span style="color: blue"&gt;return&lt;/span&gt;; &lt;span style="color: green"&gt;//convert
the message into something the logging repo can use. &lt;/span&gt;&lt;span style="color: #2b91af"&gt;LoggingData &lt;/span&gt;data
= _loggingAdapter.ToDomainModel(message); &lt;span style="color: green"&gt;//log it! &lt;/span&gt;_loggingRepository.LogDebug(UiSystem,
data); } &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;loggingException)
{ &lt;span style="color: green"&gt;// if an exception occurred during logging, capture the
message that was being logged in the first place &lt;/span&gt;&lt;span style="color: blue"&gt;throw
new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;LoggingException&lt;/span&gt;(loggingException)
{ OriginalMessage = message }; } }&lt;/pre&gt;
&lt;p&gt;
OK the code is written, so how does this make the testing easier? Since the tough
to test code is in a dependency, when I write the test, I can inject something different
in. In this case, I want to inject in a class that I can easily control and return
either true or false for the Logging.Debug setting. There are a bunch of ways to do
this, I’ll use a mocking framework to help. In this case I’m using &lt;a href="http://code.google.com/p/moq/" target="_blank"&gt;MOQ&lt;/a&gt;.
Here’s most of the code from my &lt;a href="http://www.nunit.org/" target="_blank"&gt;NUnit&lt;/a&gt; TestFixture
(I took out a bunch of extra tests for brevity. An explanation follows.
&lt;/p&gt;
&lt;pre class="code"&gt;[&lt;span style="color: #2b91af"&gt;TestFixture&lt;/span&gt;] &lt;span style="color: blue"&gt;public
class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;LoggingServiceTests &lt;/span&gt;{ &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;IEnterpriseLoggingRepository&gt;&gt;
_loggingRepository; &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;ILoggingInformationAdapter&gt;&gt;
_adapter; &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;IConfigurationManager&gt;&gt;
_configurationManager; &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;LoggingService &lt;/span&gt;_loggingService;
[&lt;span style="color: #2b91af"&gt;SetUp&lt;/span&gt;] &lt;span style="color: blue"&gt;public void &lt;/span&gt;Setup()
{ _loggingRepository = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;IEnterpriseLoggingRepository&gt;&gt;();
_adapter = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;ILoggingInformationAdapter&gt;&gt;();
_configurationManager = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Mock&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;IConfigurationManager&gt;&gt;();
_loggingService = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;LoggingService&lt;/span&gt;(_loggingRepository.Object,
_adapter.Object, _configurationManager.Object); } [&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public
void &lt;/span&gt;LoggingDebugIsSkippedIfConfigSettingIsFalse() { _configurationManager.Setup(cm
=&gt; cm.GetAppSetting(&lt;span style="color: #a31515"&gt;"Logging.Debug"&lt;/span&gt;)).Returns(&lt;span style="color: #a31515"&gt;"false"&lt;/span&gt;);
_loggingService.LogDebug(&lt;span style="color: #a31515"&gt;"This is a test"&lt;/span&gt;); _adapter.Verify(a
=&gt; a.ToDomainModel(&lt;span style="color: #2b91af"&gt;It&lt;/span&gt;&lt;&lt;span style="color: blue"&gt;.IsAnystring&gt;&gt;()), &lt;span style="color: #2b91af"&gt;Times&lt;/span&gt;.Never());
_loggingRepository.Verify(lr =&gt; lr.LogDebug(&lt;span style="color: #2b91af"&gt;It&lt;/span&gt;&lt;&lt;span style="color: blue"&gt;.IsAnystring&gt;&gt;(), &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;.IsAnyLoggingData&gt;&gt;()), &lt;span style="color: #2b91af"&gt;Times&lt;/span&gt;.Never());
} [&lt;span style="color: #2b91af"&gt;Test&lt;/span&gt;] &lt;span style="color: blue"&gt;public void &lt;/span&gt;LoggingDebugIsExecutedIfConfigSettingIsTrue()
{ _configurationManager.Setup(cm =&gt; cm.GetAppSetting(&lt;span style="color: #a31515"&gt;"Logging.Debug"&lt;/span&gt;)).Returns(&lt;span style="color: #a31515"&gt;"true"&lt;/span&gt;);
_loggingService.LogDebug(&lt;span style="color: #a31515"&gt;"This is a test"&lt;/span&gt;); _adapter.Verify(a
=&gt; a.ToDomainModel(&lt;span style="color: #2b91af"&gt;It&lt;/span&gt;&lt;&lt;span style="color: blue"&gt;.IsAnystring&gt;&gt;()), &lt;span style="color: #2b91af"&gt;Times&lt;/span&gt;.Exactly(1));
_loggingRepository.Verify(lr =&gt; lr.LogDebug(&lt;span style="color: #2b91af"&gt;It&lt;/span&gt;&lt;&lt;span style="color: blue"&gt;.IsAnystring&gt;&gt;(), &lt;span style="color: #2b91af"&gt;It&lt;/span&gt;&lt;&lt;span style="color: #2b91af"&gt;.IsAnyLoggingData&gt;&gt;()), &lt;span style="color: #2b91af"&gt;Times&lt;/span&gt;.Exactly(1));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
If you aren’t familiar with &lt;a href="http://en.wikipedia.org/wiki/Mock_object" target="_blank"&gt;Mocking&lt;/a&gt; or &lt;a href="http://code.google.com/p/moq/" target="_blank"&gt;MOQ&lt;/a&gt;,
you may want to read up on the topic. I’ll try to hit the highlights. Check out the
SetUp() method. You can see that instead of using “real” classes, I instantiate a
bunch of Mocks so I can control them easily. In the last line of the method I “inject”
the mocks into the LoggingService. Next, let’s examine the method LoggingDebugIsSkippedIfConfigSettingIsFalse().
The first line of code pretty much says when the ConfigurationManager’s GetAppSetting()
method is called with the argument “Logging.Debug”, just return false. I really like
the MOQ syntax, if you are used to Lambda expressions, the MOQ stuff reads pretty
easily, I think. The mock class won’t actually look at a config file or anything.
Remember, this is a mock so there is no implementation. With code like this, you can
actually step through with the debugger and see the mocks act as you told them too!
If you look back to the actual LogDebug() method you will see that if the Logging.Debug
value is false the method simply ends. So then the last two lines of code in the test
verify that the adapter’s ToDomainModel() and the logging repository’s LogDebug()
method are never called at all! That’s all there is to it. Plus the second unit test
shown here does the opposite, it makes sure that all of the code is executed if Logging.Debug
is set to true.
&lt;/p&gt;
&lt;p&gt;
I can assure you it took me a lot longer to write this blog post than it did to code
the &lt;span style="color: #2b91af"&gt;IConfigurationManager&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;WebConfigConfigurationManager&lt;/span&gt;,
and &lt;span style="color: #2b91af"&gt;LoggingServiceTests&lt;/span&gt;. I hope you find this
code helpful.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=ac7d1789-cba7-4461-bfcc-85e4fa308840" /&gt;</content>
  </entry>
  <entry>
    <title>Using Custom Code Snippets in VS2008</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Using+Custom+Code+Snippets+In+VS2008.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,4c8239ab-3d77-4be2-a652-3ffa3eb2f2f8.aspx</id>
    <published>2010-05-02T16:36:46.1730024-04:00</published>
    <updated>2010-05-02T16:36:46.1730024-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="C#" label="C#" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,C%23.aspx" />
    <category term="Visual Studio" label="Visual Studio" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Visual%2BStudio.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Code snippets are a great time saver and they are pretty simple to create.  Any
time you find yourself typing the same thing over and over again, you may want to
consider creating your own code snippet to make life easier.
</p>
        <p>
Here’s an example… I was recently working on a large refactor of some error handling
code for a user interface.  Basically, I went from form to form, refactoring
the way the error handling was implemented and then test it.  How did I test
it?  By throwing exceptions in my code.  Doing so is pretty easy, just find
the portion of code that needs to be tested and type “throw new Exception(“This is
a test”);”  Plus, for good measure and safety I’d add this above it: “//TODO
– Don’t forget remove this test code”
</p>
        <pre class="code">
          <span style="color: green">//TODO: Don't forget to remove this test
code: </span>
          <span style="color: blue">throw new </span>
          <span style="color: #2b91af">Exception</span>(<span style="color: #a31515">"This
is a test"</span>);</pre>
        <p>
Sure, it is easy enough to type but it got old pretty quick and I decided to create
a simple snippet.  Once I did, all I needed to type was “tne” (it stands for
“Throw New Exception) and hit “tab” twice (hitting “Tab” twice tells Visual Studio
to use the code snippet based on what you just typed).  Sounds cool huh? 
Ok so here is how you create a simple code snippet…
</p>
        <p>
Snippets are stored in XML files on your pc.  To create a new one, it is easiest
to just start with an existing snippet and modify the xml to meet your needs. 
Here is an example of the one I am using:
</p>
        <pre class="code">
          <span style="color: blue">&lt;?</span>
          <span style="color: #a31515">xml </span>
          <span style="color: red">version</span>
          <span style="color: blue">=</span>"<span style="color: blue">1.0</span>" <span style="color: red">encoding</span><span style="color: blue">=</span>"<span style="color: blue">utf-8</span>" <span style="color: blue">?&gt;
&lt;</span><span style="color: #a31515">CodeSnippets </span><span style="color: red">xmlns</span><span style="color: blue">=</span>"<span style="color: blue">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">CodeSnippet </span><span style="color: red">Format</span><span style="color: blue">=</span>"<span style="color: blue">1.0.0</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Title</span><span style="color: blue">&gt;</span>Throw
new Exception<span style="color: blue">&lt;/</span><span style="color: #a31515">Title</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;</span>tne<span style="color: blue">&lt;/</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Description</span><span style="color: blue">&gt;</span>This
will generate code to throw a new Exception<span style="color: blue">&lt;/</span><span style="color: #a31515">Description</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Author</span><span style="color: blue">&gt;</span>Andy
Schwam<span style="color: blue">&lt;/</span><span style="color: #a31515">Author</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">SnippetTypes</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">SnippetType</span><span style="color: blue">&gt;</span>Expansion<span style="color: blue">&lt;/</span><span style="color: #a31515">SnippetType</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">SnippetTypes</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Code </span><span style="color: red">Language</span><span style="color: blue">=</span>"<span style="color: blue">csharp</span>"<span style="color: blue">&gt;
&lt;![CDATA[ </span><span style="color: gray">//TODO: Remove this test code: throw
new Exception("An Exception has been thrown"); $end$</span><span style="color: blue">]]&gt;
&lt;/</span><span style="color: #a31515">Code</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">CodeSnippet</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">CodeSnippets</span><span style="color: blue">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
If you want to use your own snippet, just change the Title, Shortcut, Description
and Author elements.  And of course, you need to put your snippet inside of the
CDATA area of the the Code Element. 
</p>
        <p>
Next, you just need to save this file in a place where Visual Studio will pick it
up.  To tell VS2008 where to look for it, go to Tools &gt; Code Snippets Manager. 
Make sure you select the correct language in the drop down, otherwise Visual Studio
can’t connect your snippet to the language you meant it for.  You’ll also notice
the existing folders for snippets.  That may give you an idea of where you should
store your own snippets ;).
</p>
        <p>
You can also write more complex snippets that allows you to use the snippet and then
type in values that get used within the snippet.  For instance, I do a lot of
work with Silverlight and for databinding my public variables must implement INotifyPropertyChanged. 
Instead of manually typing a property that does so, plus a backing variable, I use
a snippet with the shortcut “npc”.  In the sample below, you will see the words
TYPE, PROPERTY and FIELD highlighted in Green.  Those are placeholders and when
I utilize the code snippet, VS will prompt me to replace those values.  So if
I want to create a public property and backing variable for a string FirstName, I
just type the following:  “npc” TAB TAB “string” TAB “FirstName” TAB “_firstName”
(but don’t type the quotes!).  It may seem weird at first but makes coding much
easier.
</p>
        <pre class="code">
          <span style="color: blue">private </span>TYPE FIELD; </pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <a href="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/UsingandWritingCodeSnippetsinVS2008_D91F/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.blog.ingenuitynow.net/content/binary/WindowsLiveWriter/UsingandWritingCodeSnippetsinVS2008_D91F/image_thumb.png" width="317" height="172" />
          </a>
        </p>
        <p>
Here is the XML for my “NotifyPropertyChanged” snippet:
</p>
        <pre class="code">
          <span style="color: blue">&lt;?</span>
          <span style="color: #a31515">xml </span>
          <span style="color: red">version</span>
          <span style="color: blue">=</span>"<span style="color: blue">1.0</span>" <span style="color: red">encoding</span><span style="color: blue">=</span>"<span style="color: blue">utf-8</span>" <span style="color: blue">?&gt;
&lt;</span><span style="color: #a31515">CodeSnippets </span><span style="color: red">xmlns</span><span style="color: blue">=</span>"<span style="color: blue">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">CodeSnippet </span><span style="color: red">Format</span><span style="color: blue">=</span>"<span style="color: blue">1.0.0</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Title</span><span style="color: blue">&gt;</span>Notify
Property Changed Property<span style="color: blue">&lt;/</span><span style="color: #a31515">Title</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;</span>npc<span style="color: blue">&lt;/</span><span style="color: #a31515">Shortcut</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Description</span><span style="color: blue">&gt;</span>Code
snippet for creating a property with a backing variable that calls NotifyPropertyChanged<span style="color: blue">&lt;/</span><span style="color: #a31515">Description</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Author</span><span style="color: blue">&gt;</span>Andy
Schwam<span style="color: blue">&lt;/</span><span style="color: #a31515">Author</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">SnippetTypes</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">SnippetType</span><span style="color: blue">&gt;</span>Expansion<span style="color: blue">&lt;/</span><span style="color: #a31515">SnippetType</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">SnippetTypes</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Header</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Declarations</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;</span>type<span style="color: blue">&lt;/</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">ToolTip</span><span style="color: blue">&gt;</span>The
Type for the Property<span style="color: blue">&lt;/</span><span style="color: #a31515">ToolTip</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;</span>TYPE<span style="color: blue">&lt;/</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;</span>fieldname<span style="color: blue">&lt;/</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">ToolTip</span><span style="color: blue">&gt;</span>The
Type for the field<span style="color: blue">&lt;/</span><span style="color: #a31515">ToolTip</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;</span>FIELD<span style="color: blue">&lt;/</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;</span>propertyname<span style="color: blue">&lt;/</span><span style="color: #a31515">ID</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">ToolTip</span><span style="color: blue">&gt;</span>Parameter
name<span style="color: blue">&lt;/</span><span style="color: #a31515">ToolTip</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;</span>PROPERTY<span style="color: blue">&lt;/</span><span style="color: #a31515">Default</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Literal</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Declarations</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Code </span><span style="color: red">Language</span><span style="color: blue">=</span>"<span style="color: blue">csharp</span>"<span style="color: blue">&gt;
&lt;![CDATA[ </span><span style="color: gray">public $type$ $propertyname$ { get {
return $fieldname$; } set { if ($fieldname$ != value) { $fieldname$ = value; NotifyPropertyChanged("$propertyname$");
} } } private $type$ $fieldname$; $end$</span><span style="color: blue">]]&gt; &lt;/</span><span style="color: #a31515">Code</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">Snippet</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">CodeSnippet</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">CodeSnippets</span><span style="color: blue">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Unfortunately, there isn’t a Code Snippet Editor included in VS.  There is some
kind of plug-in available for creating VB snippets but I’ve never used it.  I’m
sure you can find it with your favorite search engine.  Still it is pretty easy
to write these yourself.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=4c8239ab-3d77-4be2-a652-3ffa3eb2f2f8" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Using MSBuild to Check if a Subversion Tag Exists</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Using+MSBuild+To+Check+If+A+Subversion+Tag+Exists.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,64b679c6-65c5-44d1-8e54-3a756526a5eb.aspx</id>
    <published>2010-04-17T14:49:50.0863878-04:00</published>
    <updated>2010-04-17T14:49:50.0863878-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="MSBuild" label="MSBuild" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,MSBuild.aspx" />
    <category term="Subversion" label="Subversion" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Subversion.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’ve been working on continuous integration and automated builds/deployment lately. 
I put together a <a href="http://msdn.microsoft.com/en-us/library/wea2sca5.aspx" target="_blank">MSBuild</a> script
that does a whole bunch of typical stuff including getting code from my repository,
compiling, and running tests.  I also want to create a <a href="http://svnbook.red-bean.com/en/1.1/ch04s06.html" target="_blank">Tag</a> in
my <a href="http://subversion.apache.org/" target="_blank">Subversion</a> repository
so I know which code was deployed.  That wasn’t too hard to do either. 
The tricky part was that I want my build to fail if I try to make a Tag that already
exists!
</p>
        <p>
I fumbled around with this for a while trying a wide variety of ideas.  In the
end, it turned out to be pretty simple.  There may be better ways to accomplish
this, but here is how I am doing it…
</p>
        <p>
I’m using the SvnClient task (it’s part of the <a href="http://msbuildtasks.tigris.org/" target="_blank">MSBuild.Community.Tasks</a> download)
and executing a command:  “ls $(SvnRepository)/tags/$(SvnTagName)”.  Of
course, my MSBuild script already has the variables $(SvnRepository) which is the
path to my repository and $(SvnTagName) which is the name of the Tag I hope to create. 
You may have to tweak this stuff depending on the layout of your SVN repository. 
The command “ls” will actually list the contents of my Tag.  Since I know my
tag just has a few folders in it (it isn’t a recursive task), it isn’t too harmful
if it succeeds.  If the Tag does not exist in my repository, the command actually
fails.  Oddly, I actually want this task to fail!  Yes, in my case, I am
hoping the task does not find the Tag with the specified name.  When the task
is executed, it returns an ExitCode which is 0 for success or 1 for failure. 
Next I just check the returned code and if it is successful I generate an error. 
Sure this feels a bit odd.  I don’t love using this kind of logic, hoping for
an error to occur. I’d much rather have some subversion command I could execute to
see if the Tag exists but I couldn’t find anything like that.  Here is the actual
xml from my Build file to check for the Tag:
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">Target </span>
          <span style="color: red">Name</span>
          <span style="color: blue">=</span>"<span style="color: blue">CheckSvn</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Message </span><span style="color: red">Text</span><span style="color: blue">=</span>"<span style="color: blue">***
Checking to see if the tag already exists</span>"<span style="color: blue">/&gt;
&lt;!-- </span><span style="color: green">this task will look up the tag supplied.
If it exists it will return a code of 0 Technically this is a success but for this
instance, it is a failure because we don't want to find it! </span><span style="color: blue">--&gt;
&lt;</span><span style="color: #a31515">SvnClient </span><span style="color: red">Command</span><span style="color: blue">=</span>"<span style="color: blue">ls
$(SvnRepository)/tags/$(SvnTagName)</span>" <span style="color: red">Username</span><span style="color: blue">=</span>"<span style="color: blue">xxx</span>" <span style="color: red">password</span><span style="color: blue">=</span>"<span style="color: blue">xxx</span>" <span style="color: red">ContinueOnError</span><span style="color: blue">=</span>"<span style="color: blue">true</span>"<span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Output </span><span style="color: red">TaskParameter</span><span style="color: blue">=</span>"<span style="color: blue">ExitCode</span>" <span style="color: red">PropertyName</span><span style="color: blue">=</span>"<span style="color: blue">CommandResult</span>"<span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">SvnClient</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">Message </span><span style="color: red">Text</span><span style="color: blue">=</span>"<span style="color: blue">***
ExitCode = $(<span style="color: blue">CommandResult</span>)</span>"<span style="color: blue">/&gt;
&lt;</span><span style="color: #a31515">Error </span><span style="color: red">Condition</span><span style="color: blue">=</span>"<span style="color: blue">$(<span style="color: blue">CommandResult</span>)=='0'</span>" <span style="color: red">Text</span><span style="color: blue">=</span>"<span style="color: blue">This
Tag already exists!</span>"<span style="color: blue">/&gt; &lt;</span><span style="color: #a31515">Message </span><span style="color: red">Text</span><span style="color: blue">=</span>"<span style="color: blue">***
Finished Checking to see if the tag already exists</span>"<span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">Target</span><span style="color: blue">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Working with MSBuild is becoming a love/hate relationship for me.  I really love
the power of what you can do to automate tasks.  But it can be a real pain to
write, test, and debug these scripts.  I hope my sharing this code will make
your life a little easier.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=64b679c6-65c5-44d1-8e54-3a756526a5eb" />
      </div>
    </content>
  </entry>
  <entry>
    <title>LINQ to String?</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/LINQ+To+String.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,ef0d330b-be08-452c-a43c-85041c1e7817.aspx</id>
    <published>2010-04-15T21:53:45.4576582-04:00</published>
    <updated>2010-04-15T21:53:45.4576582-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="LINQ" label="LINQ" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,LINQ.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
You’ve heard about LINQ to SQL, LINQ to Objects and LINQ to XML right?  Well
how about LINQ to String?  Well technically there is no such thing.  But
you use LINQ on a string!  You may not have realized this before but it works. 
LINQ can be used to query IEnumerable&lt;T&gt; and a string is actually a collection
of chars.  So yup, you can actually query the string itself.  Here are a
few simple examples but you certainly do a lot more than this…
</p>
        <pre class="code">
          <span style="color: blue">public void </span>LinqToStringSample()
{ <span style="color: blue">string </span>letters = <span style="color: #a31515">"keuiwierqewefqwfwefcjlkjkl"</span>; <span style="color: green">//count
all 'e' </span><span style="color: blue">int </span>count = letters.Count(l =&gt;
l == <span style="color: #a31515">'e'</span>); <span style="color: green">//order </span><span style="color: blue">var </span>ordered
= letters.OrderBy(l =&gt; l); }</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=ef0d330b-be08-452c-a43c-85041c1e7817" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Introduction to LINQ &amp;ndash; Watch it online!</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Introduction+To+LINQ+Ndash+Watch+It+Online.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,5709210c-8328-4be4-826f-a4c0ae8b561c.aspx</id>
    <published>2010-04-15T21:22:16.9632678-04:00</published>
    <updated>2010-04-15T21:22:16.9632678-04:00</updated>
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I recently gave an “Introduction To LINQ” presentation at the Microsoft event: 
Data Access Firestarter.  That session was recorded and posted on MSDN’s Channel
9.  Here is the <a href="http://channel9.msdn.com/posts/egibson/MSDN-Simulcast-All-DataAll-Day-Dive-into-NET-Data-Access-Introduction-to-LINQ/" target="_blank">link</a> so
you can check it out.  If you want the content, you can get it from <a href="http://www.blog.ingenuitynow.net/Introduction+To+LINQ+Demo.aspx" target="_blank">this
post</a>.  I’m really excited to be on Channel 9, it is such a great source of
content!
</p>
        <p>
I also gave the same talk at Philly.Net Code Camp this past Saturday.  That talk
was also recorded and is available via Live Meeting replay.  I think it will
be on Channel 9 too.  Both sessions went pretty well.  
</p>
        <p>
I do feel the need to make a correction though!  In the presentation at Code
Camp I answered a question incorrectly.  It was a subtle error, but one just
the same.  The question was “Can you use LINQ to SQL against other databases?”. 
I’m not really sure why I gave the wrong answer because I have been doing this stuff
for a long time and I really do know the right answer.  My incorrect answer was
that you can do LINQ to SQL as long as you have a provider to the other database. 
This is completely wrong!  You can certainly do LINQ to a wide variety of data
sources, including Oracle, but that isn’t “LINQ to SQL”.  LINQ to SQL is specifically
an implementation of LINQ to a SQL Server Database.  I hope that makes sense.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=5709210c-8328-4be4-826f-a4c0ae8b561c" />
      </div>
    </content>
  </entry>
  <entry>
    <title>My new Coding Mantra:  Slow Down</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/My+New+Coding+Mantra+Slow+Down.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,a2ee61f0-9c18-43c9-8fd9-1f50ca07dbf9.aspx</id>
    <published>2010-03-27T13:58:20.180856-04:00</published>
    <updated>2010-03-27T13:58:20.180856-04:00</updated>
    <category term="General" label="General" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,General.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
I remember a conversation with a client about certain coding practices.  We were
discussing things like code reviews, paired programming and similar concepts. 
These were concepts that we both believed in but yet at certain times in the project
we scrapped these ideas.  Why?  Why else, we were behind schedule and we
didn’t have time.  Thinking about it, I said to him that this was precisely the
time we should NOT ignore these practices.  Why is this?  Because when you
are close to a deadline and running out of time, you don’t have time for mistakes. 
And paired programming is a great way to minimize mistakes.  I’m not trying to
say here that Pairing is the only way to prevent issues.  The point is that we
shouldn’t rush our work when we have limited time.  I know that while we were
in our frantic pace to the finish we made some mistakes.  We then had to fix
those mistakes before we released anyway.  If we had taken our time a bit more,
we would have saved resources in the long run because it is faster to write better
code than fix bugs.  
</p>
        <p>
I thought of this conversation today.  I was listening to Studio 360 on NPR and
heard a great quote.  The show had guest host Alec Baldwin interviewing actress
Laura Linney. Laura Linney was recalling a quote by some other woman, I don’t recall
her name, who was a Broadway director, if I recall correctly.  In any case, this
woman used a phrase all the time that really expresses my thoughts from above. 
She used to say “Ok people, we are almost out of time, we better slow down”. 
Isn’t that perfect?  I’m not positive that I’m quoting it correctly, word for
word, but I am close enough and the point is clear.
</p>
        <p>
I hope to remember this quote and repeat it a lot. 
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=a2ee61f0-9c18-43c9-8fd9-1f50ca07dbf9" />
      </div>
    </content>
  </entry>
  <entry>
    <title>Introduction To LINQ Demo</title>
    <link rel="alternate" type="text/html" href="http://www.blog.ingenuitynow.net/Introduction+To+LINQ+Demo.aspx" />
    <id>http://www.blog.ingenuitynow.net/PermaLink,guid,1f6eee0d-d263-4724-857b-a0847bee229b.aspx</id>
    <published>2010-03-26T20:08:01.4978903-04:00</published>
    <updated>2010-03-26T20:08:01.4978903-04:00</updated>
    <category term=".Net" label=".Net" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,.Net.aspx" />
    <category term="Code Camp" label="Code Camp" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Code%2BCamp.aspx" />
    <category term="LINQ" label="LINQ" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,LINQ.aspx" />
    <category term="Microsoft" label="Microsoft" scheme="http://www.blog.ingenuitynow.net/CategoryView,category,Microsoft.aspx" />
    <content type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml">
        <p>
Tomorrow I’m presenting “Introduction to LINQ” at the Microsoft Event: Data Access
Firestarter.  I’m trying to be proactive and actually post my samples and slides
in advance!  This way, they’ll be there if anyone wants to take a look. 
I’ll be doing the same demo at <a href="http://codecamp.phillydotnet.org/2010-1/default.aspx" target="_blank">Philly.Net
Code Camp</a> on April 10 too.
</p>
        <p>
Click here to download the samples: <a href="http://blog.ingenuitynow.net/content/binary/IntroductionToLinq.zip" target="_blank">Demo
and Slides</a> 
</p>
        <p>
If you attended the Firestarter, I hope you enjoyed my talk.  If not, I hope
to see you at Code Camp.  And if you just happened along and are interested in
learning LINQ, enjoy the samples.  Email me if you have any questions.
</p>
        <img width="0" height="0" src="http://www.blog.ingenuitynow.net/aggbug.ashx?id=1f6eee0d-d263-4724-857b-a0847bee229b" />
      </div>
    </content>
  </entry>
</feed>