<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>notepuddle</title>
	<atom:link href="http://notepuddle.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://notepuddle.com</link>
	<description>a blog</description>
	<lastBuildDate>Wed, 08 Sep 2010 23:16:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>MSBuild &#8211; More ways to generate a cross-product</title>
		<link>http://notepuddle.com/2010/03/16/msbuild-more-ways-to-generate-a-cross-product/</link>
		<comments>http://notepuddle.com/2010/03/16/msbuild-more-ways-to-generate-a-cross-product/#comments</comments>
		<pubDate>Wed, 17 Mar 2010 03:23:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MsBuild]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=477</guid>
		<description><![CDATA[I&#8217;ve been using MsBuild a lot over the last 6 months as the company I&#8217;ve been working with migrates from TFS to TeamCity/Subversion. One problem that arises is the need to join item collections. I found two ways to do this and decided to look around to see how other people have been handling this. [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using MsBuild a lot over the last 6 months as the company I&#8217;ve been working with migrates from TFS to TeamCity/Subversion. One problem that arises is the need to join item collections. I found two ways to do this and decided to look around to see how other people have been handling this. Aaron Hallberg <a href="http://blogs.msdn.com/aaronhallberg/archive/2006/09/05/msbuild-batching-generating-a-cross-product.aspx">wrote</a> about this a few years back and another example can be found <a href="http://ljusberg.se/blogs/smorakning/archive/2007/02/02/generating-a-cross-product-with-msbuild.aspx">here</a>. </p>
<p>One example I&#8217;ve found to work is similar to the second example:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Project </span><span style="color: red">DefaultTargets</span><span style="color: blue">=</span>&quot;<span style="color: blue">PrintFooAndBar</span>&quot;</pre>
<pre class="code">     <span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">http://schemas.microsoft.com/developer/msbuild/2003</span>&quot; <span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Foo </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">foo1</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;</span>1<span style="color: blue">&lt;/</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Foo</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Foo </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">foo2</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;</span>2<span style="color: blue">&lt;/</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Foo</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Bar </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">bar1</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;</span>a<span style="color: blue">&lt;/</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Bar</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Bar </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">bar2</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;</span>b<span style="color: blue">&lt;/</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Bar</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">Target </span><span style="color: red">Name</span><span style="color: blue">=</span>&quot;<span style="color: blue">Merge</span>&quot; <span style="color: red">Outputs</span><span style="color: blue">=</span>&quot; <span style="color: blue">%(Bar.Identity) </span>&quot;<span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">_Foo </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">@(Foo)</span>&quot;<span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;</span>%(Bar.BarMetadata)<span style="color: blue">&lt;/</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;
      &lt;/</span><span style="color: #a31515">_Foo</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">Target</span><span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">Target </span><span style="color: red">Name</span><span style="color: blue">=</span>&quot;<span style="color: blue">PrintFooAndBar</span>&quot; <span style="color: red">DependsOnTargets</span><span style="color: blue">=</span>&quot;<span style="color: blue">Merge</span>&quot;<span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Message </span><span style="color: red">Importance</span><span style="color: blue">=</span>&quot;<span style="color: blue">high</span>&quot;</pre>
<pre class="code">             <span style="color: red">Text</span><span style="color: blue">=</span>&quot;<span style="color: blue">FooMetadata=%(_Foo.FooMetadata), BarMetadata=%(_Foo.BarMetadata)</span>&quot; <span style="color: blue">/&gt;
  &lt;/</span><span style="color: #a31515">Target</span><span style="color: blue">&gt;

&lt;/</span><span style="color: #a31515">Project</span><span style="color: blue">&gt;</span></pre>
<p></p>
<p>This produces the following output:</p>
<pre>Project &quot;E:\Projects\MsBuild\cross_product_1.build&quot; on node 0 (default target(s)).
  FooMetadata=1, BarMetadata=a
  FooMetadata=2, BarMetadata=a
  FooMetadata=1, BarMetadata=b
  FooMetadata=2, BarMetadata=b
Done Building Project &quot;E:\Projects\MsBuild\cross_product_1.build&quot; (default target(s)).</pre>
<p></p>
<p>The other example I&#8217;ve used is similar although it doesn’t require an intermediate task to build a temporary list:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">Project </span><span style="color: red">DefaultTargets</span><span style="color: blue">=</span>&quot;<span style="color: blue">PrintFooAndBar</span>&quot;
         <span style="color: red">xmlns</span><span style="color: blue">=</span>&quot;<span style="color: blue">http://schemas.microsoft.com/developer/msbuild/2003</span>&quot; <span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Foo </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">foo1</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;</span>1<span style="color: blue">&lt;/</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Foo</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Foo </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">foo2</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;</span>2<span style="color: blue">&lt;/</span><span style="color: #a31515">FooMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Foo</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Bar </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">@(Foo)</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;</span>a<span style="color: blue">&lt;/</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Bar</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Bar </span><span style="color: red">Include</span><span style="color: blue">=</span>&quot;<span style="color: blue">@(Foo)</span>&quot;<span style="color: blue">&gt;
      &lt;</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;</span>b<span style="color: blue">&lt;/</span><span style="color: #a31515">BarMetadata</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">Bar</span><span style="color: blue">&gt;
  &lt;/</span><span style="color: #a31515">ItemGroup</span><span style="color: blue">&gt;

  &lt;</span><span style="color: #a31515">Target </span><span style="color: red">Name</span><span style="color: blue">=</span>&quot;<span style="color: blue">PrintFooAndBar</span>&quot;<span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">Message </span><span style="color: red">Importance</span><span style="color: blue">=</span>&quot;<span style="color: blue">high</span>&quot;
             <span style="color: red">Text</span><span style="color: blue">=</span>&quot;<span style="color: blue">FooMetadata=%(Bar.FooMetadata), BarMetadata=%(Bar.BarMetadata)</span>&quot; <span style="color: blue">/&gt;
  &lt;/</span><span style="color: #a31515">Target</span><span style="color: blue">&gt;

&lt;/</span><span style="color: #a31515">Project</span><span style="color: blue">&gt;</span></pre>
<p></p>
<p>Which produces the following output:</p>
<pre>Project &quot;E:\Projects\MsBuild\cross_product_2.build&quot; on node 0 (default targets).
  FooMetadata=1, BarMetadata=a
  FooMetadata=2, BarMetadata=a
  FooMetadata=1, BarMetadata=b
  FooMetadata=2, BarMetadata=b
Done Building Project &quot;E:\Projects\MsBuild\cross_product_2.build&quot; (default targets).</pre>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2010/03/16/msbuild-more-ways-to-generate-a-cross-product/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Limit Exceptions in CS2009</title>
		<link>http://notepuddle.com/2009/03/26/limit-exceptions-in-cs2009/</link>
		<comments>http://notepuddle.com/2009/03/26/limit-exceptions-in-cs2009/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 04:18:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CommerceServer]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=404</guid>
		<description><![CDATA[One flaw with the current design of Commerce Server 2009 is how exceptions are trapped. CS2009 wraps the 2007 object model in a service boundary, logging all exceptions that occur within that boundary and raising a generic FaultException with the following message: The operation service has encountered an error while processing the request. The error [...]]]></description>
			<content:encoded><![CDATA[<p>One flaw with the current design of Commerce Server 2009 is how exceptions are trapped. CS2009 wraps the 2007 object model in a service boundary, logging all exceptions that occur within that boundary and raising a generic <a href="http://msdn.microsoft.com/en-us/library/ms576199.aspx">FaultException</a> with the following message:</p>
<pre>The operation service has encountered an error while processing the request. The error details
have been logged by the service.</pre>
<p><br/></p>
<p>This removes the ability to recover from specific exceptions, e.g. <a href="http://msdn.microsoft.com/en-us/library/aa528934.aspx">ConfiguredLimitExceededException</a> when a user adds too many line items.</p>
<p>A decent workaround for this is to use the other error reporting mechanism that exists in Commerce Server, tunneling via weak properties, e.g. <a href="http://msdn.microsoft.com/en-us/library/aa544643.aspx">_Basket_Errors</a>. To do this we need two things:</p>
<ol>
<li>Access to the <a href="http://msdn.microsoft.com/en-us/library/ms960085.aspx">limits element</a> within the Commerce Server section of web.config </li>
<li>A custom <a href="http://msdn.microsoft.com/en-us/library/dd451104.aspx">OperationSequenceComponent</a> that will check for limit violations and add a custom status code property to the returned order form. </li>
</ol>
<h2></h2>
<h3>Accessing the limits element</h3>
<p>Commerce Server 2007 uses an internal class, <code>CommerceOrdersConfiguration</code>, to read attributes of the limits element. You can get an instance of this object from the <code>OrderContext</code> using reflection and further use reflection to extract the limit properties needed. See the <code>OrderLimitSettings</code> class in the example code.</p>
<h3>Creating a custom OperationSequenceComponent</h3>
<p>When new items are added to a basket a call is issued to <code>BasketLineItemsProcessor.CreateRelatedItems</code>. This is part of the <code>CommerceUpdateOperation_Basket</code> operation defined within the channel configuration file. This method receives a collection of line items to create, so we can extend this component to check for limit violations. There’s actually not a lot of code for this:</p>
<pre class="prettyprint"><code>protected override void CreateRelatedItems(
    List&#60;CommerceCreateRelatedItem&#62; createRelatedItemOperations) {

    var orderForm = CachedOrderGroup.GetDefaultOrderForm();
    if ((orderForm.LineItems.Count + createRelatedItemOperations.Count) >
         Limits.LineItemCountPerOrderFormLimit) {
        orderForm[StatusCodeKey] = LineItemLimitExceeded;
        return;
    }

    base.CreateRelatedItems(createRelatedItemOperations);
}</code></pre>
<p><strong>Note:</strong> For this example it’s all or nothing when adding a batch of line items. If adding a new batch will exceed the configured limit, a status code is returned to indicate error preventing the entire batch from being created. Alternatively you could extend <code>BasketLineItemsProcessor.CreateRelatedItem</code> allowing individual line items to be inserted until one violates a limit.</p>
<p>Once the component is complete we need to register it in the <code>CommerceUpdateOperation_Basket</code> operation, within the channel configuration file, by modifying the existing <em>Line Items processor</em> to point to the new class.</p>
<p>You can download an example <a href='http://notepuddle.com/wp-content/uploads/2009/03/limitexceptions.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/03/26/limit-exceptions-in-cs2009/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Vista Restarts</title>
		<link>http://notepuddle.com/2009/03/03/vista-restarts/</link>
		<comments>http://notepuddle.com/2009/03/03/vista-restarts/#comments</comments>
		<pubDate>Wed, 04 Mar 2009 01:11:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://notepuddle.com/2009/03/03/vista-restarts/</guid>
		<description><![CDATA[Seems to occur much more often than it did in XP.]]></description>
			<content:encoded><![CDATA[<p><a href="http://notepuddle.com/wp-content/uploads/2009/03/restart.png"><img title="restart" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="183" alt="restart" src="http://notepuddle.com/wp-content/uploads/2009/03/restart-thumb.png" width="536" border="0" /></a></p>
<p>Seems to occur much more often than it did in XP.</p>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/03/03/vista-restarts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Commerce Server 2009 outside of a Web Application</title>
		<link>http://notepuddle.com/2009/02/10/using-commerce-server-2009-outside-of-a-web-application/</link>
		<comments>http://notepuddle.com/2009/02/10/using-commerce-server-2009-outside-of-a-web-application/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 04:05:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CommerceServer]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=327</guid>
		<description><![CDATA[If you try running Commerce Server 2009 outside of a web application you&#8217;ll likely encounter a few exceptions along the way. The hurdles are setting up configuration settings and initializing the context objects. Setting up a Console Application I created an example to query a catalog product, using the following steps for setting up a [...]]]></description>
			<content:encoded><![CDATA[<p>If you try running Commerce Server 2009 outside of a web application you&#8217;ll likely encounter a few exceptions along the way. The hurdles are setting up configuration settings and initializing the context objects.</p>
<h3>Setting up a Console Application</h3>
<p>I created an example to query a catalog product, using the following steps for setting up a Visual Studio 2008 Console Application with Commerce Server 2009 RC1:</p>
<ol>
<li>Unpup <code>C:\Program Files\Microsoft Commerce Server 2007\Microsoft Commerce Server 2009\Site\MicrosoftCommerceDefaultSiteWithSampleData.pup</code> to a site named <code>ConsoleSite</code>.</li>
<li>Add the assemblies located at <code>C:\Program Files\Microsoft Commerce Server 2007\Microsoft Commerce Server 2009\Assemblies</code> as references to the project.</li>
<li>Copy <code>ChannelConfiguration.config</code> (editing siteName) and <code>MetadataDefinitions.xml</code> from <code><br />
C:\Program Files\Microsoft Commerce Server 2007\Microsoft Commerce Server 2009\Sdk\Samples</code> to the solution directory and add them to the solution.</li>
<li>Unpack <code>CommerceSharePointExtensibilityKit.zip</code> from <code>C:\Program Files\Microsoft Commerce Server 2007\Microsoft Commerce Server 2009\Sdk\Samples</code> to <code>C:\CommerceSharePointExtensibilityKit\</code>.</li>
<li>Piece together an <code>app.config</code> file using the configuration fragment files in the unpacked extensibility kit located at <code>C:\CommerceSharePointExtensibilityKit\SharePointCommon\FeatureActivation\DefaultConfigs\</code>.</li>
</ol>
<h3>Initializing Context Objects</h3>
<p>In Commerce Server 2007 you have to manually create each subsystem context during your application initialization process, when executing outside of a web site. In Commerce Server 2009 context initialization is controlled by the <code>useSharedCommerceContexts</code> attribute in the channel configuration file:</p>
<p><code>&lt;ServiceConfiguration useSharedCommerceContexts="true"&gt;<br />
...<br />
&lt;/ServiceConfiguration&gt;</code></p>
<p>The default value for <strong>useSharedCommerceContexts</strong> is <strong>true</strong>. If you try running your non-web application with this setting, you will see the following error in the event log:</p>
<p><code>...<br />
Microsoft.Commerce.Providers.Exceptions.ContextCreationException, ...<br />
Message : Configuration specifies useSharedCommerceContexts=true however this is only valid for a Commerce Server enabled web site runtime environment.<br />
...<br />
</code></p>
<p>You need to set this value to <strong>false</strong> and add some additional configuration elements.</p>
<p>Searching the CS2009 documentation for <strong>useSharedCommerceContexts</strong> reveals one result explaining a work around when specifying <code>useSharedCommerceContexts="false"</code>. It outlines additional configuration settings to include in the channel configuration file and displays the following warning regarding <strong>useSharedCommerceContexts</strong>:</p>
<blockquote><p>Currently only &#8220;true&#8221; is supported, meaning that Commerce Server 2009 is running as a shared Commerce Server resource</p></blockquote>
<p>FWIW, I&#8217;ve been using this technique for automated unit testing on my current project without any issues.</p>
<p>The example I created queries the Adventure Works catalog for a product and displays a few of its properties. Here&#8217;s what the output looks like:</p>
<p><img src="http://notepuddle.com/wp-content/uploads/2009/02/cs2009-console-output.gif" alt="Console Output" title="Console Output" width="672" height="347" class="aligncenter size-full wp-image-359" /></p>
<p>You can download the example application <a href='http://notepuddle.com/wp-content/uploads/2009/02/cs2009_console.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/02/10/using-commerce-server-2009-outside-of-a-web-application/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>TDD and Commerce Server 2007: Custom Rollback</title>
		<link>http://notepuddle.com/2009/01/28/tdd-and-commerce-server-2007-custom-rollback/</link>
		<comments>http://notepuddle.com/2009/01/28/tdd-and-commerce-server-2007-custom-rollback/#comments</comments>
		<pubDate>Thu, 29 Jan 2009 01:58:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CommerceServer]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=102</guid>
		<description><![CDATA[In my previous post I mentioned using a TransactionScope to rollback Commerce Server database updates, when unit testing, may not always work. Turns out it doesn&#8217;t work with the profile subsystem. To verify this, I coded an example similar to the address example on MSDN, resulting in the following test: [Fact] public void Should_save_address_changes() { [...]]]></description>
			<content:encoded><![CDATA[<p>In my previous <a href="http://notepuddle.com/2009/01/22/tdd-and-commerce-server-2007-getting-started/">post</a> I mentioned using a <code>TransactionScope</code> to rollback Commerce Server database updates, when unit testing, may not always work. Turns out it doesn&#8217;t work with the profile subsystem.</p>
<p>To verify this, I coded an example similar to the address example on <a href="http://msdn.microsoft.com/en-us/library/ms865038.aspx">MSDN</a>, resulting in the following test:</p>
<pre class="prettyprint"><code>[Fact]
public void Should_save_address_changes() {
    var addressId = Guid.NewGuid();
    var addressService = new AddressService(TestProfileContext);
    var address = createNewAddress(addressService, addressId);

    addressService.Save(address);

    var savedAddress = addressService.Find(addressId);
    Assert.Equal(address.Line1, savedAddress.Line1);
    Assert.Equal(address.City, savedAddress.City);
    Assert.Equal(address.State, savedAddress.State);
    Assert.Equal(address.ZipCode, savedAddress.ZipCode);
}</code></pre>
<p>For this test, I extended the <code>CommerceFixture</code> class (created in my previous post) to create a <code>TestProfileContext</code> instance. If this test is executed, it will always save a new address to the Commerce Server Profiles Database. If we try adding an <code>AutoRollback</code> attribute to the test, the new address doesn&#8217;t get rolled back.</p>
<p>To fix this we can call <code>TestProfileContext.DeleteProfile</code> when the test completes. An easy way to do this, while making the code exception safe, is to create a scope class that implements <code>IDisposable</code>. This allows us to wrap the test in a <code>using</code> block, reducing the amount of code we need to type. Here&#8217;s an example implementation:</p>
<pre class="prettyprint"><code>public class ProfileScope : IDisposable {
    private readonly ProfileContext _profileContext;
    private readonly string _keyValue;
    private readonly string _profileType;

    public ProfileScope(ProfileContext context, string keyValue, string profileType) {
        _profileContext = context;
        _keyValue = keyValue;
        _profileType = profileType;
    }

    public void Dispose() {
        _profileContext.DeleteProfile(_keyValue, _profileType);
    }
}</code></pre>
<p>Now we can update the test, adding a using block with our scope object:</p>
<pre class="prettyprint"><code>[Fact]
public void Should_save_address_changes() {
    //arrange

    using (new ProfileScope(TestProfileContext, addressId.ToString(),
        AddressService.ProfileName)) {

        addressService.Save(address);

        //assert
    }
}</code></pre>
<p>We can trim the code down even further by adding a helper method to the base test class, <code>CommerceFixture</code>:</p>
<pre class="prettyprint"><code>public class CommerceFixture {
    ...
    public static ProfileScope ProfileScope(string keyValue, string profileType){
        return new ProfileScope(TestProfileContext, keyValue, profileType);
    }
    ...
}</code></pre>
<p>Which gives us our final version:</p>
<pre class="prettyprint"><code>[Fact]
public void Should_save_address_changes() {
    //arrange

    using (ProfileScope(addressId.ToString(), AddressService.ProfileName)) {
        addressService.Save(address);

        //assert
    }
}</code></pre>
<p>You can download the code for this example <a href='http://notepuddle.com/wp-content/uploads/2009/01/cs2007_customrollback.zip'>here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/01/28/tdd-and-commerce-server-2007-custom-rollback/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>One tough pest</title>
		<link>http://notepuddle.com/2009/01/26/one-tough-pest/</link>
		<comments>http://notepuddle.com/2009/01/26/one-tough-pest/#comments</comments>
		<pubDate>Tue, 27 Jan 2009 02:15:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Vista]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=217</guid>
		<description><![CDATA[The saga of moving back to a single Vista64 boot from a dual with ArchLinux&#8230; First treatment Vista System Recovery Options -&#62; Startup Repair -&#62; Success -&#62; Reboot -&#62; GRUB Loading stage 1.5. GRUB loading, please wait... Error 22 Second treatment Vista System Recovery Options -&#62; Command Prompt -&#62; bootrec /fixmbr -&#62; The operation completed [...]]]></description>
			<content:encoded><![CDATA[<p>The saga of moving back to a single Vista64 boot from a dual with ArchLinux&#8230;</p>
<h4>First treatment</h4>
<p>Vista System Recovery Options<br />
-&gt; Startup Repair -&gt; <em>Success</em><br />
-&gt; Reboot -&gt;</p>
<pre class="text-plain"><code>GRUB Loading stage 1.5.

GRUB loading, please wait...
Error 22</code></pre>
<h4>Second treatment</h4>
<p>Vista System Recovery Options<br />
-&gt; Command Prompt<br />
-&gt; <code>bootrec /fixmbr</code> -&gt; <em>The operation completed successfully</em><br />
-&gt; Reboot -&gt;</p>
<pre class="text-plain"><code>GRUB Loading stage 1.5.

GRUB loading, please wait...
Error 22</code></pre>
<h4>Third treatment</h4>
<p>Vista System Recovery Options<br />
-&gt; Command Prompt<br />
-&gt; <code>bootrec /fixmbr</code> -&gt; <em>The operation completed successfully</em><br />
-&gt; <code>bootrec /fixboot</code> -&gt; <em>Element not found</em> -&gt; <strong><em>sonofa#?*</em></strong><br />
-&gt; Reboot -&gt;</p>
<pre class="text-plain"><code>GRUB Loading stage 1.5.

GRUB loading, please wait...
Error 22</code></pre>
<p>Panic setting in&#8230;</p>
<p>&#8230; a light of hope <a href="http://www.sysint.no/nedlasting/mbrfix.htm">surfaces</a>&#8230;</p>
<p>Vista System Recovery Options -&gt; Command Prompt<br />
-&gt; copy <a href="http://www.sysint.no/nedlasting/mbrfix.htm">MbrFix </a>from thumb drive to hard drive (64 bit version of course)<br />
-&gt; <strong><em>cross fingers</em></strong><br />
-&gt; <code>MbrFix /drive 0 savembr Backup_MBR.bin</code><br />
-&gt; <code>MbrFix /drive 0 fixmbr /vista /yes</code><br />
-&gt; Reboot -&gt;</p>
<p><strong>SUCCESS!</strong></p>
<p>Wondering what <code>bootrec /fixmbr</code> is useful for?</p>
<p>Wondering why this <a href="http://www.sysint.no/nedlasting/mbrfix.htm">page</a> doesn&#8217;t have a donation button?</p>
<p>Time wasted: ~4 hours</p>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/01/26/one-tough-pest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TDD and Commerce Server 2007: Getting Started</title>
		<link>http://notepuddle.com/2009/01/22/tdd-and-commerce-server-2007-getting-started/</link>
		<comments>http://notepuddle.com/2009/01/22/tdd-and-commerce-server-2007-getting-started/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 04:12:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[CommerceServer]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=47</guid>
		<description><![CDATA[Mixing Commerce Server 2007 with unit testing frameworks is a little tricky. The few examples I&#8217;ve seen use MsTest specific features to route tests through ASP.NET or IIS when executing, providing the following drawbacks: Increased test execution time (sucks worse when running on a VPC) Increased cost for creating tests Techniques like dependency injection can [...]]]></description>
			<content:encoded><![CDATA[<p>Mixing Commerce Server 2007 with unit testing frameworks is a little tricky. The few examples I&#8217;ve seen use MsTest specific features to route tests through ASP.NET or IIS when executing, providing the following drawbacks:</p>
<ol>
<li>Increased test execution time (sucks worse when running on a VPC)</li>
<li>Increased cost for creating tests</li>
</ol>
<p>Techniques like dependency injection can be used to significantly reduce the number of tests that require a web host for execution. I&#8217;m using the Orders subsystem here, but the same techniques can be used with other subsystems as well.</p>
<h3>Example Code</h3>
<p>For this example I <a href="http://msdn.microsoft.com/en-us/library/aa544681.aspx">unpacked the CSharp.pup</a> file to create a new site named <em>CommerceSample</em>. You can download the example code <a href="http://notepuddle.com/wp-content/uploads/2009/01/starting_tdd_cs2007.zip">here</a>.</p>
<h3>Breaking Dependencies</h3>
<p>Commerce Server is tightly coupled to ASP.NET and this causes problems when trying to execute Commerce Server API code outside of a web application. Suppose we try to execute the following code within a unit testing framework:</p>
<pre class="prettyprint"><code>var userId = Guid.NewGuid();
var basket = OrderContext.Current.GetBasket(userId);
</code></pre>
<p>This produces a <code>NullReferenceException</code> due to an uninitialized singleton, <code>OrderContext.Current</code>. In a web application scenario this context is initialized by the <code>HttpModule <a href="http://msdn.microsoft.com/en-us/library/microsoft.commerceserver.runtime.orders.commerceordermodule.commerceordermodule.aspx">CommerceOrderModule</a></code>.</p>
<h3>Establishing a Test Context</h3>
<p>To execute code like this within a testing framework, we need to create an instance of <code>OrderContext</code>. One approach is to create a custom base class for tests requiring the use of an <code>OrderContext</code>, as shown below:</p>
<pre class="prettyprint"><code>public class CommerceFixture {
    public const string SiteName = "CommerceSample";

    public static readonly OrderContext TestOrderContext =
        OrderContext.Create(SiteName);
}</code></pre>
<p>If I have a test that requires an <code>OrderContext</code>, my test class can now extend this class and have access to the <code>TestOrderContext</code> instance. <strong>One thing to point out</strong>: <code>OrderContext.Create</code> will also initialize <code>OrderContext.Current</code>, so technically you don&#8217;t need to store the reference in an accessible property.</p>
<h3>Wrapping and Injecting Context Objects</h3>
<p>When working with context objects, I like to create wrappper types and inject the context into the wrapper. This allows for the following benefits:</p>
<ol>
<li>The ability to add new behavior to context objects (the usual benefit of wrapper classes)</li>
<li>No dependence on singletons (a lot of material has been written about this already)</li>
<li>Narrowing the API. Sometimes context objects can be a dumping ground for anything and everything. Wrapper classes provide a nice way to offer a subset of this functionality specific to my application.</li>
</ol>
<h3>Rolling Back Updates</h3>
<p>If a test updates a database, we want to make sure the updates are removed upon test completion, preventing unwanted side effects. Most mature testing frameworks provide some form of a rollback attribute, which wraps each test in a <code>TransactionScope</code> instance (if you&#8217;re stuck with MsTest, you can wrap your test code in a <code>using TransactionScope</code> block).</p>
<p><strong>Note</strong>: Originally I was told using a <code>TransactionScope</code> might not work with Commerce Server API&#8217;s due to legacy COM code, so I&#8217;ve actually never used this during full project development. I didn&#8217;t realize this worked until playing with this example while creating this post. One context type it might not work with is the <code>ProfileContext</code>. That said, there&#8217;s more than one way to rollback changes. I&#8217;ll discuss the rollback approach I&#8217;ve used before in a subsequent post.</p>
<h3>An Example</h3>
<p>Lets polish the <a href="http://msdn.microsoft.com/en-us/library/aa545295.aspx">recurring order</a> example from MSDN. I&#8217;d like to have a method that accepts a user id and a template name (basket name) and returns a basket that is already populated with items from the saved template:</p>
<pre class="prettyprint"><code>var cartService = new ShoppingCartService(orderContext);
var cart = cartService.CreateFromRecurringOrder(userId, templateName);</code></pre>
<p><code>ShoppingCartService</code> is an example of a thin wrapper, where the context is injected in.</p>
<p>To setup a test I need to create a recurring order template in the database, so my service class can find it. This is the update I want rolled back upon test completion. My final test looks like this:</p>
<pre class="prettyprint"><code>[Fact, AutoRollback]
public void Should_create_new_order_from_recurring_order_for_user() {
    var userId = Guid.NewGuid();
    var templateName = "MonthlyOrder";
    var cartService = new ShoppingCartService(TestOrderContext);

    var recurringOrder = buildRecurringOrder(cartService, userId, templateName);
    var cart = cartService.CreateFromRecurringOrder(userId, templateName);

    Assert.Equal(recurringOrder.LineItems.Count, cart.LineItems.Count);
}</code></pre>
<p>Where <code>buildRecurringOrder</code> is a helper function that creates an order template in the database. For this test I used <a href="http://www.codeplex.com/xunit">xUnit.net</a>. When the test completes, the order I saved will no longer exist. You can use the <a href="http://msdn.microsoft.com/en-us/library/ms915815.aspx">Customer and Orders Manager</a> to verify that your tests are not leaking baskets and orders.</p>
<h3>Future Topics</h3>
<p>In subsequent posts I&#8217;ll discuss the following topics:</p>
<ol>
<li>Using custom scope objects for situations where TransactionScope might not work</li>
<li>How to handle pipeline execution with testing frameworks</li>
<li>Testing with Commerce Server 2009</li>
</ol>
<h3>Related Information</h3>
<ol>
<li><a href="http://martinfowler.com/articles/injection.html">Dependency Injection</a></li>
<li>Misko Hevery discussing <a href="http://misko.hevery.com/2009/01/04/interfacing-with-hard-to-test-third-party-code/">hard to test third party code</a></li>
<li>Context object pain. See Misko <a href="http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/">here</a></li>
<li>Singleton pain. See Misko <a href="http://misko.hevery.com/code-reviewers-guide/flaw-brittle-global-state-singletons/">again</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/01/22/tdd-and-commerce-server-2007-getting-started/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Always Start When Debugging</title>
		<link>http://notepuddle.com/2009/01/12/always-start-when-debugging/</link>
		<comments>http://notepuddle.com/2009/01/12/always-start-when-debugging/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 01:24:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[VS2008]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=19</guid>
		<description><![CDATA[On my current project I&#8217;m working with MsTest and ASP.NET in Visual Studio 2008. I&#8217;ve spent the last few weeks working on building models to support some user controls and most of my time has been spent flushing out models in a test project. Typically I run tests using the Test View window in release [...]]]></description>
			<content:encoded><![CDATA[<p>On my current project I&#8217;m working with MsTest and ASP.NET in Visual Studio 2008. I&#8217;ve spent the last few weeks working on building models to support some user controls and most of my time has been spent flushing out models in a test project. Typically I run tests using the <em>Test View</em> window in release mode. I ended up running a specific test in debug mode today when I noticed this:</p>
<p><img src="http://notepuddle.com/wp-content/uploads/2009/01/dev_server_starting.png" alt="Developer Web Server starting" title="Developer Web Server starting" width="241" height="90" class="aligncenter size-full wp-image-20" /></p>
<p>Hmmm. Why would the <em>Developer Web Server</em> start when I&#8217;m running tests? Maybe there&#8217;s an option in <em>Property Pages > Start Options</em> for the site project&#8230;nope.</p>
<p>After some digging around I realized I forgot about the other property settings interface, the <em>Properties Window</em>:</p>
<p><img src="http://notepuddle.com/wp-content/uploads/2009/01/always_start_props.png" alt="VS2008 Properties Window" title="VS2008 Properties Window" width="437" height="238" class="aligncenter size-full wp-image-25" /></p>
<p><strong>Changing this to False</strong> fixes the problem. It seems odd that the default is True&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/01/12/always-start-when-debugging/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting started</title>
		<link>http://notepuddle.com/2009/01/10/getting-started/</link>
		<comments>http://notepuddle.com/2009/01/10/getting-started/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 23:07:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[About]]></category>

		<guid isPermaLink="false">http://notepuddle.com/?p=4</guid>
		<description><![CDATA[To set this site up I used some feedback from stackoverflow. I&#8217;m using DreamHost for hosting and WordPress for publishing. For a base theme, I chose Journalist. As I make more customizations, I&#8217;ll try to keep this page updated with what I&#8217;m using.]]></description>
			<content:encoded><![CDATA[<p>To set this site up I used some feedback from <a href="http://stackoverflow.com/questions/9051/what-is-best-blogging-host-for-programmerscode-formatting">stackoverflow</a>. I&#8217;m using <a href="http://www.dreamhost.com/">DreamHost</a> for hosting and <a href="http://wordpress.org/">WordPress</a> for publishing. For a base theme, I chose <a href="http://wordpress.org/extend/themes/journalist">Journalist</a>.</p>
<p>As I make more customizations, I&#8217;ll try to keep this page updated with what I&#8217;m using.</p>
]]></content:encoded>
			<wfw:commentRss>http://notepuddle.com/2009/01/10/getting-started/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.393 seconds -->

