<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Agile .NET</title>
	<atom:link href="http://vladhorby.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://vladhorby.wordpress.com</link>
	<description>Ideas &#38; Gotchas</description>
	<lastBuildDate>Thu, 01 Dec 2011 17:38:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='vladhorby.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Agile .NET</title>
		<link>http://vladhorby.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://vladhorby.wordpress.com/osd.xml" title="Agile .NET" />
	<atom:link rel='hub' href='http://vladhorby.wordpress.com/?pushpress=hub'/>
		<item>
		<title>New blog home</title>
		<link>http://vladhorby.wordpress.com/2009/12/11/new-blog-home/</link>
		<comments>http://vladhorby.wordpress.com/2009/12/11/new-blog-home/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 19:52:58 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[Agile]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/?p=41</guid>
		<description><![CDATA[I moved the blog to my own domain &#8211; you can find it here: http://www.vladhorby.com/blog.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=41&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I moved the blog to my own domain &#8211; you can find it here: <a href="http://www.vladhorby.com/blog">http://www.vladhorby.com/blog</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=41&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2009/12/11/new-blog-home/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>
	</item>
		<item>
		<title>A simple and tool-free way for database versioning (I)</title>
		<link>http://vladhorby.wordpress.com/2009/05/26/a-simple-and-tool-free-way-for-database-versioning-i/</link>
		<comments>http://vladhorby.wordpress.com/2009/05/26/a-simple-and-tool-free-way-for-database-versioning-i/#comments</comments>
		<pubDate>Wed, 27 May 2009 01:37:50 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[Continuous Integration]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/?p=31</guid>
		<description><![CDATA[One common problem in projects that involve a database is managing its versions. Basically, there are two needs to be fulfilled: - to be able to see a history of changes for a database object in a similar way to seeing the changes for a code file - to be able to update / revert [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=31&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One common problem in projects that involve a database is managing its versions.<br />
Basically, there are two needs to be fulfilled:<br />
- to be able to see a history of changes for a database object in a similar way to seeing the changes for a code file<br />
- to be able to update / revert the database to a specific version without losing the data.</p>
<p>For the first one, a simple solution is to just script all objects in the database to a folder and add it to the source code repository (one file per object &#8211; in MS SQL Server can be done with a few clicks from SQL Server Management Studio). When there are changes, all you need to do is to repeat this process and the files will be updated. You can then review changes to database objects in the same way you review them for code files.</p>
<p>The second one is a little trickier. Idealy you want scripts that can be <strong>automatically</strong> used to:<br />
- update <strong>developer </strong>database copies<br />
- update the <strong>continuous integration</strong> database (database used for the continuous integration tests)<br />
- update the <strong>test</strong> database<br />
- update the <strong>production</strong> database.</p>
<p>There are tools that give you &#8220;diff&#8221; scripts that can be used to update a database, but, apart from the price problem, they are difficult to use for several-times-a-day automatic updates like the ones needed to update the developer copies. A second problem is that, because the developers have full freedom in their database copy, some &#8220;garbage&#8221; or unwanted changes might be picked up and included in the diff script.<br />
The solution below is free, the updates can be run automatically whenever is necessary on any database (developer, test and even production) and ensures that the changes were really meant to be included in the update. All you need is a little discipline from the developers.</p>
<p><span id="more-31"></span>We&#8217;ll use a table in the database to store the current version. This script create this table and sets the version to 1.</p>
<pre>-- CREATE Table [dbo].[Version]</pre>
<pre>SET ANSI_NULLS ON</pre>
<pre>GO</pre>
<pre>SET QUOTED_IDENTIFIER ON</pre>
<pre>GO</pre>
<pre>SET XACT_ABORT ON</pre>
<pre>GO</pre>
<pre>BEGIN TRAN</pre>
<pre>IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Version]') AND type in (N'U'))</pre>
<pre>BEGIN</pre>
<pre>DECLARE @actual int</pre>
<pre>SELECT @actual = ISNULL(MAX(VersionNumber),0) FROM Version</pre>
<pre>RAISERROR('Update to version %d cannot be run - actual version is %d', 1, 127, 1, @actual) WITH LOG</pre>
<pre>END</pre>
<pre>CREATE TABLE [dbo].[Version](</pre>
<pre>[VersionNumber] [int] NOT NULL,</pre>
<pre>CONSTRAINT [PK_Version] PRIMARY KEY CLUSTERED</pre>
<pre>(</pre>
<pre>[VersionNumber] ASC</pre>
<pre>)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]</pre>
<pre>) ON [PRIMARY]</pre>
<pre>INSERT INTO [dbo].[Version]</pre>
<pre>([VersionNumber])</pre>
<pre>VALUES</pre>
<pre>(1)</pre>
<pre>GO</pre>
<pre>COMMIT</pre>
<p>The next step is to define a template for an update:</p>
<pre>-- VERSION DESCRIPTION HERE!</pre>
<pre>SET ANSI_NULLS ON</pre>
<pre>GO</pre>
<pre>SET QUOTED_IDENTIFIER ON</pre>
<pre>GO</pre>
<pre>SET XACT_ABORT ON</pre>
<pre>GO</pre>
<pre>BEGIN TRAN</pre>
<pre>DECLARE @newVersion int</pre>
<pre>SET @newVersion = -- version number here</pre>
<pre>IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Version]') AND type in (N'U'))</pre>
<pre>RAISERROR('Update to version %d cannot be run - actual version is 0', 1, 127, @newVersion) WITH LOG</pre>
<pre>DECLARE @actualVersion int</pre>
<pre>SELECT @actualVersion = ISNULL(MAX(VersionNumber),0) FROM Version</pre>
<pre>IF @actualVersion &lt;&gt; @newVersion - 1</pre>
<pre>RAISERROR('Update to version %d cannot be run - actual version is %d', 1, 127, @newVersion, @actualVersion) WITH LOG</pre>
<pre>UPDATE [dbo].[Version]</pre>
<pre>SET [VersionNumber] = @newVersion</pre>
<pre>RAISERROR('Updating to version %d...', 1, 1, @newVersion) WITH LOG</pre>
<pre>GO</pre>
<pre>-- START UPDATE</pre>
<pre>-- END UPDATE</pre>
<pre>GO</pre>
<pre>COMMIT</pre>
<p>What this does is that it checks that the actual version in the database is the one preceding the @newVersion we want to apply. If there is no Version table or it has a different version, the script raises an error and aborts the update. If everything is ok, it applies the changes found between &#8212; START UPDATE and &#8212; END UPDATE and updates the version number.</p>
<p>The developers can now create update scripts using this template and save them in a &#8220;Change Scripts&#8221; folder using a naming convention like v0001.sql, v0002.sql, etc. The naming is important because we&#8217;ll rely on it to automatically run the scripts in order. The folder should be added to the code repository so everyone can easily get the latest changes.<br />
Also, it is important that the developers take into account that the scripts will be run in production &#8211; no data loss is allowed! So no Drop and Create Table &#8211; use Alter instead!</p>
<p>In order to run the scripts automatically you can use the following batch files or write your own using your preferred shell.<br />
First, a helper script &#8211; runOneSqlScript.bat &#8211; receives (in order) servername, database, sql file, output log file and a flag wether to show the output to console and calls sqlcmd passing those parameters.</p>
<pre>rem @echo off</pre>
<pre>sqlcmd -S %1 -d %2 -i %3 -o %4 -X</pre>
<pre>IF "%5" == "N" GOTO :END</pre>
<pre>type %4</pre>
<pre>:END</pre>
<pre>rem @echo on</pre>
<p>This second script uses the first to run all the available updates. You can pass parameters if you need to use another server or database name than the defaults.</p>
<pre>set DBSERVER=localhost</pre>
<pre>set DBNAME=MyDB</pre>
<pre>IF "%1" == "" GOTO :STARTSQL</pre>
<pre>SET DBSERVER=%1</pre>
<pre>IF "%2" == "" GOTO :STARTSQL</pre>
<pre>SET DBNAME=%2</pre>
<pre>:STARTSQL</pre>
<pre>for %%f in ("Change Scripts\"v*.sql) do call runOneSqlScript.bat %DBSERVER% %DBNAME% "%%f" "%%f.log"</pre>
<p>Using these, the developers can automatically apply updates to their copy of the database. The script can also be run as part of the Continuous Integration build and update the CI database copy before running the tests. And finally, the same scripts can be use to update the test and production databases. Because the developers have to take the time to create the update script, the chance of adding &#8220;garbage&#8221; changes is greatly reduced.</p>
<p>For the developer and test databases another important issue is the demo data &#8211; how to maintain it up to date and not have it turn into garbage. In the next part I&#8217;ll propose a solution that builds on the automatic updates presented above to mitigate this problem.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=31&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2009/05/26/a-simple-and-tool-free-way-for-database-versioning-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>
	</item>
		<item>
		<title>ASP.NET &#8211; formatting bound fields</title>
		<link>http://vladhorby.wordpress.com/2007/10/25/aspnet-formatting-bound-fields/</link>
		<comments>http://vladhorby.wordpress.com/2007/10/25/aspnet-formatting-bound-fields/#comments</comments>
		<pubDate>Thu, 25 Oct 2007 21:29:10 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[WCSF]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/10/25/aspnet-formatting-bound-fields/</guid>
		<description><![CDATA[If you try something like this in a GridView or DetailsView you might end up frustrated: &#60;asp:BoundField DataField=&#8221;Percentage&#8221; DataFormatString=&#8221;{0:f2}%&#8221; HeaderText=&#8221; Percentage&#8221; ReadOnly=&#8221;True&#8221; SortExpression=&#8221;Percentage&#8221; /&#62; That&#8217;s because no matter what you put in the DataFormatString property, the text is formatted the same way (although the literal characters from outside the brackets do appear, so DataFormatString it&#8217;s [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=30&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://picasaweb.google.com/vladhorby/20071018SandaWeiglInMTY/photo#5124668147519002946"><img src="http://lh3.google.com/vladhorby/Rx56h0yhGUI/AAAAAAAAC2U/-Wm16kCM4Js/s400/DSC_2024.JPG" /></a><br />
If you try something like this in a GridView or DetailsView you might end up frustrated:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">&lt;</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">asp</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">:</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">BoundField</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:red;">DataField</span><span style="color:blue;">=&#8221;Percentage&#8221;</span> <span style="color:red;">DataFormatString</span><span style="color:blue;">=&#8221;{0:f2}%&#8221;</span> <span style="color:red;">HeaderText</span><span style="color:blue;">=&#8221; Percentage&#8221;</span> <span style="color:red;">ReadOnly</span><span style="color:blue;">=&#8221;True&#8221;</span> <span style="color:red;">SortExpression</span><span style="color:blue;">=&#8221;Percentage&#8221;</span> <span style="color:red;"></span><span style="color:blue;">/&gt;</span></span></p>
<p> That&#8217;s because no matter what you put in the <span style="font-size:10pt;font-family:'Courier New';"><span style="color:red;">DataFormatString</span><span style="color:blue;"> </span></span>property, the text is formatted the same way (although the literal characters from outside the brackets do appear, so <span style="font-size:10pt;font-family:'Courier New';"><span style="color:red;">DataFormatString</span><span style="color:blue;"> </span></span>it&#8217;s not completely ignored).</p>
<p>The solution:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">&lt;</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">asp</span><span style="font-size:10pt;font-family:'Courier New';color:blue;">:</span><span style="font-size:10pt;font-family:'Courier New';color:#a31515;">BoundField</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:red;">DataField</span><span style="color:blue;">=&#8221;Percentage&#8221;</span> <span style="color:red;">DataFormatString</span><span style="color:blue;">=&#8221;{0:f2}%&#8221;</span> <span style="color:red;">HeaderText</span><span style="color:blue;">=&#8221; Percentage&#8221;</span> <span style="color:red;">ReadOnly</span><span style="color:blue;">=&#8221;True&#8221;</span> <span style="color:red;">SortExpression</span><span style="color:blue;">=&#8221;Percentage&#8221;</span> <strong><span style="color:red;">HtmlEncode</span><span style="color:blue;">=&#8221;false&#8221;</span></strong> <span style="color:blue;">/&gt;</span></span></p>
<p> Don&#8217;t know why&#8230;</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/30/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/30/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=30&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/10/25/aspnet-formatting-bound-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://lh3.google.com/vladhorby/Rx56h0yhGUI/AAAAAAAAC2U/-Wm16kCM4Js/s400/DSC_2024.JPG" medium="image" />
	</item>
		<item>
		<title>CenterParent for non-modal forms</title>
		<link>http://vladhorby.wordpress.com/2007/10/16/centerparent-for-non-modal-forms/</link>
		<comments>http://vladhorby.wordpress.com/2007/10/16/centerparent-for-non-modal-forms/#comments</comments>
		<pubDate>Tue, 16 Oct 2007 22:09:56 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/10/16/centerparent-for-non-modal-forms/</guid>
		<description><![CDATA[As it turns out, if you want to show a non-modal form centered in the boundaries of its parent, in .NET 2.0, you&#8217;ll have to set its position manually. Although the System.Windows.Forms.FormStartPosition.CenterParent works fine if you use form.ShowDialog(owner); (modal form), it doesn’t seem to make a difference when using form.Show (owner); for a non-modal form. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=29&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://picasaweb.google.com/vladhorby/Interesting/photo#5122046404596809762"><img src="http://lh6.google.com/vladhorby/RxUqETUPQCI/AAAAAAAAC0k/0DRlIAqOK_g/s400/DSC_1653.JPG" /></a></p>
<p class="MsoNormal">As it turns out, if you want to show a non-modal form centered in the boundaries of its parent, in .NET 2.0, you&#8217;ll have to set its position manually.</p>
<p class="MsoNormal">Although the <span style="font-size:10pt;font-family:'Courier New';">System.Windows.Forms.<span style="color:#2b91af;">FormStartPosition</span>.CenterParent </span>works fine if you use <span style="font-size:10pt;font-family:'Courier New';">form.ShowDialog(owner); </span>(modal form), it doesn’t seem to make a difference when using <span style="font-size:10pt;font-family:'Courier New';">form.Show (owner); </span>for a non-modal form. I suspect a bug, because I didn’t find any mention of this behavior in help.</p>
<p class="MsoNormal">The solution:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">form.StartPosition = <span style="color:#2b91af;">FormStartPosition</span>.Manual;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">form.Location = <span style="color:blue;">new</span> System.Drawing.<span style="color:#2b91af;">Point</span>(form.Owner.Location.X + (form.Owner.Width &#8211; form.Width) / 2, form.Owner.Location.Y + (form.Owner.Height &#8211; form.Height) / 2);</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/29/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/29/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=29&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/10/16/centerparent-for-non-modal-forms/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://lh6.google.com/vladhorby/RxUqETUPQCI/AAAAAAAAC0k/0DRlIAqOK_g/s400/DSC_1653.JPG" medium="image" />
	</item>
		<item>
		<title>SQL – Select ranges of records using variables</title>
		<link>http://vladhorby.wordpress.com/2007/10/09/sql-%e2%80%93-select-ranges-of-records-using-variables/</link>
		<comments>http://vladhorby.wordpress.com/2007/10/09/sql-%e2%80%93-select-ranges-of-records-using-variables/#comments</comments>
		<pubDate>Tue, 09 Oct 2007 22:37:25 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/10/09/sql-%e2%80%93-select-ranges-of-records-using-variables/</guid>
		<description><![CDATA[One annoying thing in SQL 2000 was that you couldn’t select TOP @n records using a variable. The options were to construct the SQL query dynamically or (if you wanted to use stored procedures) to use SET ROWCOUNT @n before the select. And of course not to forget to set it back to 0 as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=28&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://picasaweb.google.com/vladhorby/Interesting/photo#5119469334024896418"><img src="http://lh4.google.com/vladhorby/RwwCPDUPP6I/AAAAAAAACzY/WG9TudseMHE/s400/DSC_0824.JPG" align="right" /></a></p>
<p class="MsoNormal">One annoying thing in SQL 2000 was that you couldn’t select TOP @n records using a variable. The options were to construct the SQL query dynamically or (if you wanted to use stored procedures) to use SET ROWCOUNT @n before the select. And of course not to forget to set it back to 0 as soon as you’re done with it.</p>
<p class="MsoNormal">SQL 2005 solves this problem. You can now use variables in the TOP count:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">CREATE</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">PROCEDURE</span> GetLatestAnnouncements<br />
<span>      </span>@topCount <span style="color:blue;">int</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">AS<br />
BEGIN</span><span style="font-size:10pt;font-family:'Courier New';"><br />
<span>      </span><span style="color:blue;">SELECT</span> <span style="color:blue;">TOP</span> <span style="color:gray;">(</span>@topCount<span style="color:gray;">)</span> <span style="color:gray;">*</span> <span style="color:blue;">FROM</span> Announcements</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;"><span>      </span><span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> <font color="#333300">DateAdded </font><span style="color:blue;">DESC</span><br />
END</span></p>
<p class="MsoNormal">Another handy addition is the <span style="font-size:10pt;font-family:'Courier New';color:fuchsia;">Row_Number</span><span style="font-size:10pt;font-family:'Courier New';color:gray;">() </span>function – it basically gives you the sequential number for a row in a result set. You can use it to get “pages” of records just like those you normally need in a paging DataGrid:</p>
<p class="MsoNormal"><span id="more-28"></span><span style="font-size:10pt;font-family:'Courier New';color:blue;">CREATE</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">PROCEDURE</span> GetAnnouncementRangeByDate<br />
<span>      </span>@startIndex <span style="color:blue;">int</span><span>,<br />
</span>@pageSize <span style="color:blue;">int</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">AS<br />
BEGIN</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">SELECT</span> Title<span style="color:gray;">,</span> [Text]<span style="color:gray;">,</span> DateAdded <span>FROM<br />
</span><span style="color:gray;">(</span><span style="color:blue;">SELECT</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:fuchsia;">Row_Number</span><span style="color:gray;">()</span> <span style="color:blue;">OVER</span> <span style="color:gray;">(</span><span style="color:blue;">ORDER</span> <span style="color:blue;">BY</span> DateAdded <span style="color:blue;">DESC</span><span style="color:gray;">)</span> <span style="color:blue;">as</span> RowPosition<span>,<br />
</span>Title<span style="color:gray;">,</span> [Text]<span style="color:gray;">,</span> DateAdded <span style="color:blue;">FROM</span> Announcements<br />
<span>      </span><span style="color:gray;">)</span> tempselect<br />
<span>      </span><span style="color:blue;">WHERE</span> tempselect<span style="color:gray;">.</span>rowposition <span style="color:gray;">BETWEEN</span> @startIndex <span style="color:gray;">AND</span> @startIndex <span style="color:gray;">+</span> @pageSize</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">END</span></p>
<p class="MsoNormal">&nbsp;</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/28/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/28/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=28&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/10/09/sql-%e2%80%93-select-ranges-of-records-using-variables/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://lh4.google.com/vladhorby/RwwCPDUPP6I/AAAAAAAACzY/WG9TudseMHE/s400/DSC_0824.JPG" medium="image" />
	</item>
		<item>
		<title>WCSF – Using Session State in Unit Tests</title>
		<link>http://vladhorby.wordpress.com/2007/09/28/wcsf-%e2%80%93-using-session-state-in-unit-tests/</link>
		<comments>http://vladhorby.wordpress.com/2007/09/28/wcsf-%e2%80%93-using-session-state-in-unit-tests/#comments</comments>
		<pubDate>Fri, 28 Sep 2007 21:18:39 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[patterns & practices]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[WCSF]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/09/28/wcsf-%e2%80%93-using-session-state-in-unit-tests/</guid>
		<description><![CDATA[The Web Client Software Factory provides a handy class, StateValue&#60;T&#62;, to use for storing values in Session. From the WCSF help: This class supports code that runs both in a Web server environment (the session is available, and data is stored there) and outside of a Web server environment (no session is available, such as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=27&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://picasaweb.google.com/vladhorby/Interesting/photo#5115366862868135490"><img src="http://lh4.google.com/vladhorby/Rv1vDzUPPkI/AAAAAAAACvI/PFUr03FTH80/s400/100_4227.JPG" align="right" /></a></p>
<p class="MsoNormal">The <a href="http://codeplex.com/websf" target="_blank">Web Client Software Factory</a> provides a handy class, <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">StateValue</span><span style="font-size:10pt;font-family:'Courier New';">&lt;<span style="color:#2b91af;">T</span>&gt;</span>, to use for storing values in Session. From the WCSF help:</p>
<p class="MsoNormal"><em>This class supports code that runs both in a Web server environment (the session is available, and data is stored there) and outside of a Web server environment (no session is available, such as when you run your unit tests, and the data is stored in memory).</em></p>
<p class="MsoNormal">To use it, just declare a public field of <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">StateValue</span><span style="font-size:10pt;font-family:'Courier New';">&lt;<span style="color:#2b91af;">T</span>&gt; </span>type and the Object Builder injects the object:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:#2b91af;">StateValue</span>&lt;<span style="color:#2b91af;">MyType</span>&gt; MyStoredValue;</span></p>
<p class="MsoNormal"> To access it, just use the <span style="font-size:10pt;font-family:'Courier New';">Value </span>property:<br />
<span style="font-size:10pt;font-family:'Courier New';"></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">MyStoredValue.Value = <span style="color:blue;">new</span> <span style="color:#2b91af;">MyType</span>();</span></p>
<p class="MsoNormal">This works fine when running the application in the web server. When unit testing however, you’ll notice that the field remains null, because the object builder used for the <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">MockCompositionContainer</span> lacks the strategy that deals with the <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">StateValue</span><span style="font-size:10pt;font-family:'Courier New';">&lt;<span style="color:#2b91af;">T</span>&gt; </span>fields. Unfortunately, the Reference Implementation that comes with WCSF doesn’t make use of these type of fields so there is no quick example on how to deal with them in tests. You can use the tests covering the builder strategies from the CompositeWeb application block tests, or you can use the following quick reference and save yourself the digging <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
<p class="MsoNormal"><span id="more-27"></span></p>
<p class="MsoNormal">The <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">StateValue</span><span style="font-size:10pt;font-family:'Courier New';">&lt;<span style="color:#2b91af;">T</span>&gt;</span> fields are injected by the <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">SessionStateBindingStrategy</span>. This in turn uses the <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">ISessionStateLocatorService</span> service to obtain an <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">IHttpSessionState</span> object that will act as a repository.</p>
<p class="MsoNormal">So the first step is to implement a <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">MockHttpSessionState</span> class:</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">class</span> <span style="color:#2b91af;">MockHttpSessionState</span> : <span style="color:#2b91af;">IHttpSessionState</span><br />
{<br />
<span></span><span style="color:blue;">    public</span> <span style="color:#2b91af;">Hashtable</span> Values = <span style="color:blue;">new</span> <span style="color:#2b91af;">Hashtable</span>();<br />
<span>    </span><span style="color:blue;">public</span> <span style="color:blue;">object</span> <span style="color:blue;">this</span>[<span style="color:blue;">string</span> name]<br />
<span>    </span>{<br />
<span>        </span><span>get<br />
</span>        {<br />
<span></span><span style="color:blue;">            return</span> Values[name];<br />
<span></span>        }<br />
<span></span><span>        set<br />
</span>        {<br />
<span></span>            Values[name] = <span style="color:blue;">value</span>;<br />
<span></span>        }<br />
<span></span>    }<br />
<span></span><span style="color:green;">    // other IHttpSessionState members</span><br />
}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal">We can now write a <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">MockSessionStateLocatorService</span>:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">class</span> <span style="color:#2b91af;">MockSessionStateLocatorService</span> : <span style="color:#2b91af;">ISessionStateLocatorService</span><br />
{<br />
<span></span><span style="color:blue;">    public</span> <span style="color:#2b91af;">MockHttpSessionState</span> SessionState = <span style="color:blue;">new</span> <span style="color:#2b91af;">MockHttpSessionState</span>();<br />
<span></span><span style="color:blue;">    public</span> System.Web.SessionState.<span style="color:#2b91af;">IHttpSessionState</span> GetSessionState()<br />
<span></span>    {<br />
<span></span><span style="color:blue;">        return</span> SessionState;<br />
<span></span>    }<br />
}</span></p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal">The <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">MockCompositionContainer</span> initializes the builder with our mock service and adds the <span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">SessionStateBindingStrategy</span> strategy:</p>
<p class="MsoNormal">&nbsp;</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">class</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:#2b91af;">MockCompositionContainer</span> : <span style="color:#2b91af;">CompositionContainer</span><br />
{<br />
<span></span><span style="color:blue;">    public</span> <span style="color:#2b91af;">WCSFBuilder</span> CreatedBuilder;<br />
<span></span><span style="color:blue;">    public</span> MockCompositionContainer()<br />
<span></span>    {<br />
<span></span>        InitializeRootContainer(CreateBuilder());<br />
<span></span><span style="color:blue;">        this</span>.Services.AddNew&lt;<span style="color:#2b91af;">MockSessionStateLocatorService</span>, <span style="color:#2b91af;">ISessionStateLocatorService</span>&gt;();<br />
<span></span><span></span>        CreatedBuilder.Strategies.AddNew&lt;<span style="color:#2b91af;">SessionStateBindingStrategy</span>&gt;(<span style="color:#2b91af;">BuilderStage</span>.Initialization);<br />
<span></span>    }<br />
<span></span><span style="color:blue;">    private</span> <span style="color:#2b91af;">WCSFBuilder</span> CreateBuilder()<br />
<span></span>    {<br />
<span></span><span style="color:#2b91af;">        WCSFBuilder</span> builder = <span style="color:blue;">new</span> <span style="color:#2b91af;">WCSFBuilder</span>();<br />
<span></span>        CreatedBuilder = builder;<br />
<span></span>        builder.Policies.SetDefault&lt;<span style="color:#2b91af;">ISingletonPolicy</span>&gt;(<span style="color:blue;">new</span> <span style="color:#2b91af;">SingletonPolicy</span>(<span style="color:blue;">true</span>));<br />
<span></span><span style="color:blue;">        return</span> builder;<br />
<span></span>    }<br />
}</span></p>
<p class="MsoNormal"><span> </span></p>
<p class="MsoNormal"><span>Now the </span><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">StateValue</span><span style="font-size:10pt;font-family:'Courier New';">&lt;<span style="color:#2b91af;">T</span>&gt; </span><span>field gets injected:</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">[<span style="color:#2b91af;">Test</span>]<br />
public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">void</span> ProvidesAnnouncementToAdd()<br />
{<br />
<span></span><span style="color:#2b91af;">    MockCompositionContainer</span> rootContainer = <span style="color:blue;">new</span> <span style="color:#2b91af;">MockCompositionContainer</span>();<br />
<span></span><span style="color:#2b91af;">    CompositionContainer</span> moduleContainer = rootContainer.Containers.AddNew&lt;<span style="color:#2b91af;">CompositionContainer</span>&gt;();<br />
<span></span><span style="color:#2b91af;">    MyPresenter</span> presenter = moduleContainer.BuildNewItem(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">MyPresenter</span>)) <span style="color:blue;">as</span> <span style="color:#2b91af;">MyPresenter</span>;<span style="color:#2b91af;"></span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span style="color:#2b91af;">    Assert</span>.IsNotNull(presenter.MyStateField);<br />
}</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/27/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/27/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/27/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/27/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/27/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=27&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/09/28/wcsf-%e2%80%93-using-session-state-in-unit-tests/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://lh4.google.com/vladhorby/Rv1vDzUPPkI/AAAAAAAACvI/PFUr03FTH80/s400/100_4227.JPG" medium="image" />
	</item>
		<item>
		<title>TDD – MockRepositories for WCF Service Factory</title>
		<link>http://vladhorby.wordpress.com/2007/09/21/tdd-%e2%80%93-mockrepositories-for-wcf-service-factory/</link>
		<comments>http://vladhorby.wordpress.com/2007/09/21/tdd-%e2%80%93-mockrepositories-for-wcf-service-factory/#comments</comments>
		<pubDate>Fri, 21 Sep 2007 22:15:27 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[patterns & practices]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/09/21/tdd-%e2%80%93-mockrepositories-for-wcf-service-factory/</guid>
		<description><![CDATA[When testing the business logic layer you need to fake the data access layer so you have to worry about the database or the connection speed. The following example focuses on the Repository pattern used in the WCF Service Factory, but it can be adapted to other data access layer architectures. What do you need [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=26&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal"><a href="http://vladhorby.files.wordpress.com/2007/09/classdiagram1.jpg" title="MockRepository Class Diagram"><img src="http://vladhorby.files.wordpress.com/2007/09/classdiagram1.jpg" alt="MockRepository Class Diagram" align="right" /></a>When testing the business logic layer you need to fake the data access layer so you have to worry about the database or the connection speed. The following example focuses on the Repository pattern used in the <a href="http://codeplex.com/servicefactory" target="_blank">WCF Service Factory</a>, but it can be adapted to other data access layer architectures.</p>
<p class="MsoNormal">What do you need from a MockRepository?</p>
<p class="MsoNormal" style="text-indent:0.25in;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span>·<span style="font-family:'Times New Roman';font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">         </span></span></span><!--[endif]-->To know if certain methods have been called.</p>
<p class="MsoNormal" style="text-indent:0.25in;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span>·<span style="font-family:'Times New Roman';font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">         </span></span></span><!--[endif]-->To know what parameters were passed to those methods.</p>
<p class="MsoNormal" style="text-indent:0.25in;"><!--[if !supportLists]--><span style="font-family:Symbol;"><span>·<span style="font-family:'Times New Roman';font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">         </span></span></span><!--[endif]-->To know what (fake) results were returned.</p>
<p class="MsoNormal">The base Repository class that the Data Access Guidance Package helps you create has 6 types of methods: SearchOne, SearchMany, SearchAll, Insert, Update and Delete. The derived Repository classes use these to accomplish various tasks (SearchById, SearchByIDParent, etc). For the tests, we’ll use the same model: a base MockRepository class that will be inherited by the actual MockRepositories. In order to be able to use them instead of the real ones, we’ll extract the interfaces from the real repositories, have the MockRepositories implement them, and have the business actions work with these interfaces instead.</p>
<p class="MsoNormal"><span id="more-26"></span>The base MockRepository class manages an internal list to simulate a repository:</p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">class</span> <span style="color:#2b91af;">MockRepository</span>&lt;TDomainObject&gt;<br />
{<br />
<span>      </span><span style="color:blue;">private</span> <span style="color:#2b91af;">List</span>&lt;TDomainObject&gt; _internalList;<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:#2b91af;">List</span>&lt;TDomainObject&gt; InternalList<br />
<span>      </span>{<br />
<span>            </span><span style="color:blue;">get</span><br />
<span>            </span>{<br />
<span>                  </span><span style="color:blue;">return</span> _internalList;<br />
<span>            </span>}<br />
<span>            </span><span style="color:blue;">set</span> { _internalList = <span style="color:blue;">value</span>; }<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> MockRepository()<br />
<span>      </span>{<br />
<span>            </span>InternalList = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;TDomainObject&gt;();<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">int</span> CCAdd;<br />
<span>      </span><span style="color:blue;">public</span> TDomainObject LastAdded = <span style="color:blue;">default</span>(TDomainObject);<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">virtual</span> <span style="color:blue;">void</span> Add(TDomainObject toAdd)<br />
<span>      </span>{<br />
<span>            </span>CCAdd++;<br />
<span>            </span>InternalList.Add(toAdd);<br />
<span>            </span>LastAdded = toAdd;<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">int</span> CCRemove;<br />
<span>      </span><span style="color:blue;">public</span> TDomainObject LastRemoved = <span style="color:blue;">default</span>(TDomainObject);<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">virtual</span> <span style="color:blue;">void</span> Remove(TDomainObject toRemove)<br />
<span>      </span>{<br />
<span>            </span>CCRemove++;<br />
<span>            </span>InternalList.Remove(toRemove);<br />
<span>            </span>LastRemoved = toRemove;<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">virtual</span> <span style="color:blue;">void</span> Remove(<span style="color:#2b91af;">Predicate</span>&lt;TDomainObject&gt; match)<br />
<span>      </span>{<br />
<span>            </span>CCRemove++;<br />
<span>            </span>LastRemoved = InternalList.Find(match);<br />
<span>            </span>InternalList.Remove(LastRemoved);<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">int</span> CCSearchOne;<br />
<span>      </span><span style="color:blue;">public</span> TDomainObject LastSearchOne = <span style="color:blue;">default</span>(TDomainObject);<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">virtual</span> TDomainObject SearchOne(<span style="color:#2b91af;">Predicate</span>&lt;TDomainObject&gt; match)<br />
<span>      </span>{<br />
<span>            </span>CCSearchOne++;<br />
<span>            </span>LastSearchOne = InternalList.Find(match);<br />
<span>            </span><span style="color:blue;">return</span> LastSearchOne;<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">int</span> CCSave;<br />
<span>      </span><span style="color:blue;">public</span> TDomainObject LastSaveOriginal = <span style="color:blue;">default</span>(TDomainObject);<br />
<span>      </span><span style="color:blue;">public</span> TDomainObject LastSaveNew = <span style="color:blue;">default</span>(TDomainObject);<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">virtual</span> <span style="color:blue;">void</span> Save(TDomainObject toSave, <span style="color:#2b91af;">Predicate</span>&lt;TDomainObject&gt; match)<br />
<span>      </span>{<br />
<span>            </span>CCSave++;<br />
<span>            </span>LastSaveOriginal = InternalList.Find(match);<br />
<span>            </span><span style="color:blue;">if</span> (LastSaveOriginal.Equals(<span style="color:blue;">default</span>(TDomainObject)))<br />
<span>                  </span><span style="color:blue;">throw</span> <span style="color:blue;">new</span> <span style="color:#2b91af;">InvalidOperationException</span>(<span style="color:#a31515;">&#8220;The object to be saved could not be found.&#8221;</span>);<br />
<span>            </span>InternalList[InternalList.IndexOf(LastSaveOriginal)] = toSave;<br />
<span>            </span>LastSaveNew = toSave;<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">int</span> CCSearchMany;<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:#2b91af;">List</span>&lt;TDomainObject&gt; LastSearchMany = <span style="color:blue;">null</span>;<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">virtual</span> <span style="color:#2b91af;">List</span>&lt;TDomainObject&gt; SearchMany(<span style="color:#2b91af;">Predicate</span>&lt;TDomainObject&gt; match)<br />
<span>      </span>{<br />
<span>            </span>CCSearchMany++;<br />
<span>            </span>LastSearchMany = InternalList.FindAll(match);<br />
<span>            </span><span style="color:blue;">return</span> LastSearchMany;<br />
<span>      </span>}<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">int</span> CCSearchAll;<br />
<span>      </span><span style="color:blue;">public</span> <span style="color:blue;">virtual</span> <span style="color:#2b91af;">List</span>&lt;TDomainObject&gt; SearchAll()<br />
<span>      </span>{<br />
<span>            </span>CCSearchAll++;<br />
<span>            </span><span style="color:blue;">return</span> InternalList;<br />
<span>      </span>}<br />
}</span></p>
<p class="MsoNormal"><span>The derived MockRepositories can implement the methods from the real repository’s interface:</span></p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">public</span> <span style="color:#2b91af;">Question</span> GetQuestionByID(<span style="color:#2b91af;">Guid</span> iD)<br />
<span>      </span>{<br />
<span>            </span><span style="color:blue;">return</span> <span style="color:blue;">base</span>.SearchOne(<span style="color:blue;">delegate</span>(<span style="color:#2b91af;">Question</span> target) { <span style="color:blue;">return</span> target.ID == iD; });<br />
<span>      </span>}</span></p>
<p class="MsoNormal"><span>If necessary, they can add other “flag fields” to be used in tests.</span></p>
<p class="MsoNormal"><span>In order to use them in tests, just pass them to the business action instead of the real ones and validate the operation using the Call Count fields (CCMethodName) and/or the Last____ fields for parameters and return values.</span></p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">MyAction</span><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;"> action = <span style="color:blue;">new</span> <span style="color:#2b91af;">MyAction</span>();<br />
MockQuestionRepository</span><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;"> questionRep = <span style="color:blue;">new</span> <span style="color:#2b91af;">MockQuestionRepository</span>();<br />
MockResultRepository</span><span style="font-size:10pt;font-family:'Courier New';"> resultRep = <span style="color:blue;">new</span> <span style="color:#2b91af;">MockResultRepository</span>();<br />
action.QuestionRepository = questionRep;</span></p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';">action.ResultRepository = resultRep;</span></p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">Result</span><span style="font-size:10pt;font-family:'Courier New';"> result = action.Execute(); </span></p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">Assert</span><span style="font-size:10pt;font-family:'Courier New';">.IsNotNull(result);</span></p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">Assert</span><span style="font-size:10pt;font-family:'Courier New';">.AreEqual(resultRep.LastSearchOne, result);</span></p>
<p class="MsoNormal" style="text-indent:0;"><span style="font-size:10pt;font-family:'Courier New';color:#2b91af;">Assert</span><span style="font-size:10pt;font-family:'Courier New';">.AreEqual(1, questionRep.CCAdd);</span></p>
<p class="MsoNormal"><span> </span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/26/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/26/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=26&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/09/21/tdd-%e2%80%93-mockrepositories-for-wcf-service-factory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://vladhorby.files.wordpress.com/2007/09/classdiagram1.jpg" medium="image">
			<media:title type="html">MockRepository Class Diagram</media:title>
		</media:content>
	</item>
		<item>
		<title>SCSF – Loading modules into child WorkItems</title>
		<link>http://vladhorby.wordpress.com/2007/09/20/scsf-%e2%80%93-loading-modules-into-child-workitems/</link>
		<comments>http://vladhorby.wordpress.com/2007/09/20/scsf-%e2%80%93-loading-modules-into-child-workitems/#comments</comments>
		<pubDate>Thu, 20 Sep 2007 20:25:54 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[patterns & practices]]></category>
		<category><![CDATA[SCSF]]></category>
		<category><![CDATA[Smart Client]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/09/20/scsf-%e2%80%93-loading-modules-into-child-workitems/</guid>
		<description><![CDATA[The Composite UI Application Block (CAB) used in the Smart Client Software Factory (SCSF) allows you to specify which modules will be loaded using an XML file or a web service. The problem is that all the modules you specify get loaded into the root WorkItem. If you have a services module, for example, that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=24&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://picasaweb.google.com/vladhorby/Interesting/photo#5112386284774450978"><img src="http://lh3.google.com/vladhorby/RvLYPUqPZyI/AAAAAAAACuk/kpWhYstdAuw/s400/DSC_0276.JPG" align="right" height="302" width="202" /></a></p>
<p class="MsoNormal">The Composite UI Application Block (CAB) used in the Smart Client Software Factory (SCSF) allows you to specify which modules will be loaded using an XML file or a web service. The problem is that all the modules you specify get loaded into the root WorkItem. If you have a services module, for example, that is used only by one WorkItem, you have to choose between loading it in the root WorkItem and explicitly loading it in the “AddServices” method of your WorkItemController.</p>
<p class="MsoNormal">The first option can generate EventBroker problems – all events published by the service will be visible to all WorkItems and vice versa, so you might get some “surprise” event calls.</p>
<p class="MsoNormal">In the second option you get a tight coupling between the module and its services (not always a real problem, but if you have the service in a separate assembly it could be because you are trying to isolate change between them).</p>
<p class="MsoNormal">The solution we want is to use the same mechanism as the root WorkItem and load the modules defined in an XML file. One way to do this is to create a service that will be available to child WorkItems.</p>
<p class="MsoNormal"><span id="more-24"></span>Add the service interface to the Infrastructure.Inteface project:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">interface</span> <span style="color:#2b91af;">IModulesFromFileLoaderService</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">void</span> LoadModules(<span style="color:#2b91af;">WorkItem</span> workItem, <span style="color:blue;">string</span> catalogFilePath);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">}</span></p>
<p class="MsoNormal"><span>Then add the service implementation to the Interface.Library project:</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">class</span> <span style="color:#2b91af;">ModulesFromFileLoaderService</span> : <span style="color:#2b91af;">IModulesFromFileLoaderService</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">private</span> <span style="color:#2b91af;">IModuleLoaderService</span> _moduleLoaderService;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">public</span> ModulesFromFileLoaderService([<span style="color:#2b91af;">ServiceDependency</span>] <span style="color:#2b91af;">IModuleLoaderService</span> moduleLoaderService)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>_moduleLoaderService = moduleLoaderService;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;"><span>      </span>#region</span><span style="font-size:10pt;font-family:'Courier New';"> IModulesFromFileLoaderService Members</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span></span></p>
<p class="MsoNormal" style="text-indent:0.5in;"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">void</span> LoadModules(<span style="color:#2b91af;">WorkItem</span> workItem, <span style="color:blue;">string</span> catalogFilePath)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:#2b91af;">XmlStreamDependentModuleEnumerator</span> modEnumerator = <span style="color:blue;">new</span> <span style="color:#2b91af;">XmlStreamDependentModuleEnumerator</span>();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:#2b91af;">ProfileCatalogModuleInfoStore</span> infoStore = <span style="color:blue;">new</span> <span style="color:#2b91af;">ProfileCatalogModuleInfoStore</span>();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>infoStore.CatalogFilePath = catalogFilePath;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>modEnumerator.ModuleInfoStore = infoStore;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:blue;">if</span> (modEnumerator != <span style="color:blue;">null</span>)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span>_moduleLoaderService.Load(workItem, modEnumerator.EnumerateModules());</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;"><span>      </span>#endregion</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">}</span></p>
<p class="MsoNormal">Register the service:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">abstract</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">SmartClientApplication</span>&lt;TWorkItem, TShell&gt; : Microsoft.Practices.CompositeUI.WPF.<span style="color:#2b91af;">WPFFormShellApplication</span>&lt;TWorkItem, TShell&gt;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">where</span> TWorkItem : <span style="color:#2b91af;">WorkItem</span>, <span style="color:blue;">new</span>()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">where</span> TShell : <span style="color:#2b91af;">Form</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">protected</span> <span style="color:blue;">override</span> <span style="color:blue;">void</span> AddServices()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:blue;">base</span>.AddServices();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> <span>           </span>RootWorkItem.Services.AddNew&lt;<span style="color:#2b91af;">ProfileCatalogModuleInfoStore</span>, <span style="color:#2b91af;">IModuleInfoStore</span>&gt;();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            // &#8230;</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><strong>RootWorkItem.Services.AddNew&lt;<span style="color:#2b91af;">ModulesFromFileLoaderService</span>, <span style="color:#2b91af;">IModulesFromFileLoaderService</span>&gt;();</strong></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">}</span></p>
<p class="MsoNormal">And use it in your module initialization:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">WorkItem.Services.Get&lt;<span style="color:#2b91af;">IModulesFromFileLoaderService</span>&gt;().LoadModules(WorkItem, <span style="color:#a31515;">&#8220;ModuleProfileCatalog.xml&#8221;</span>);</span></p>
<p class="MsoNormal">Another way to implement LoadModules (<a href="http://staff.southworks.net/blogs/matiaswoloski/archive/2006/02/26/CABModulesOnDemand.aspx" target="_blank">more details here</a>) is to use FileCatalogModuleEnumerator from Microsoft.Practices.CompositeUI.Services namespace – its simpler, but is works only with V1 schema files (no sections and dependencies).</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">void</span> LoadModules(<span style="color:#2b91af;">WorkItem</span> workItem, <span style="color:blue;">string</span> catalogFilePath)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:#2b91af;">FileCatalogModuleEnumerator</span> modEnumerator = <span style="color:blue;">new</span> <span style="color:#2b91af;">FileCatalogModuleEnumerator</span>(catalogFilePath);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">if</span> (modEnumerator != <span style="color:blue;">null</span>)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>_moduleLoaderService.Load(workItem, modEnumerator.EnumerateModules());</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">}</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=24&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/09/20/scsf-%e2%80%93-loading-modules-into-child-workitems/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://lh3.google.com/vladhorby/RvLYPUqPZyI/AAAAAAAACuk/kpWhYstdAuw/s400/DSC_0276.JPG" medium="image" />
	</item>
		<item>
		<title>TDD &#8211; Test similar objects automatically</title>
		<link>http://vladhorby.wordpress.com/2007/09/18/tdd-test-similar-objects-automatically/</link>
		<comments>http://vladhorby.wordpress.com/2007/09/18/tdd-test-similar-objects-automatically/#comments</comments>
		<pubDate>Tue, 18 Sep 2007 22:01:32 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[patterns & practices]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/09/18/tdd-test-similar-objects-automatically/</guid>
		<description><![CDATA[When testing you get to spend lots of time writing the setup and validation parts of the unit test. I talked in this post about a way to speed up the setup phase &#8211; generate objects that have random data in their value-type properties. Something similar can be applied to the validation part. Let&#8217;s take [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=23&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://picasaweb.google.com/vladhorby/Interesting/photo#5111668805888706882"><img src="http://lh4.google.com/vladhorby/RvBLsjhxCUI/AAAAAAAACt8/1Xoj3L7XRB8/s400/DSC_0208.JPG" align="left" /></a><br />
When testing you get to spend lots of time writing the setup and validation parts of the unit test. I talked in <a href="http://vladhorby.wordpress.com/2007/09/13/tdd-create-test-objects-automatically/">this post</a> about a way to speed up the setup phase &#8211; generate objects that have random data in their value-type properties.</p>
<p>Something similar can be applied to the validation part. Let&#8217;s take an example of a service implementation with WCF Service Factory. Normally you receive a request containing Data Contract objects, you translate those into Business Entities and pass them to a Business Action. This may return a result also expressed as Business Entities that the service implementation must translate into Data Contract objects and forward them to the client.</p>
<p>Luckily, you have a recipe for creating entity translators. But if you want to test the service implementation thoroughly you have to make sure that every property gets properly translated and forwarded from Data Contracts to Business Entities and back. This means lots of &#8220;Assert.AreEqual(source.MyProperty, destination.MyProperty)&#8221;. More, if you add or remove properties you have to remember to add them to the asserts list, or they will not be tested.</p>
<p>One way to do this automatically (at least for the value-type properties) is to compare the properties that have the same name and type in both objects &#8211; taking advantage of the fact that most of the time they are identical. The following method accomplishes this for non-array value or string properties. It can be extended for arrays and indexer properties or even to drill down into reference types, but i find it difficult to follow what exactly gets tested or not. It can also be used for objects of the same type as a quick property-by-property equality check (or you can use it inside an overridden Equals(),  but you have to remove the &#8220;<span style="font-size:10pt;font-family:'Courier New';">obj1.Equals(obj2)</span>&#8221; from the first if).</p>
<p class="MsoNormal"><span id="more-23"></span><span style="font-size:10pt;font-family:'Courier New';color:blue;">public</span><span style="font-size:10pt;font-family:'Courier New';"> <span style="color:blue;">static</span> <span style="color:blue;">void</span> EqualByPropertyNames(<span style="color:blue;">object</span> obj1, <span style="color:blue;">object</span> obj2, <span style="color:blue;">params</span> <span style="color:blue;">string</span>[] excludeProperties) </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">if</span> (obj1 == obj2 || obj1.Equals(obj2))</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:blue;">return</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:#2b91af;">Assert</span>.IsNotNull(obj1);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:#2b91af;">Assert</span>.IsNotNull(obj2);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:#2b91af;">Type</span> obj1Type = obj1.GetType();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:#2b91af;">Type</span> obj2Type = obj2.GetType();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:#2b91af;">PropertyInfo</span>[] properties1 = obj1Type.GetProperties();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt; excludedList = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;(excludeProperties);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> <span>     </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">PropertyInfo</span>&gt; properties2 = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">PropertyInfo</span>&gt;(obj2Type.GetProperties());</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">foreach</span> (<span style="color:#2b91af;">PropertyInfo</span> prop <span style="color:blue;">in</span> properties1)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:blue;">if</span> (prop.CanRead &amp;&amp;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                </span>(prop.PropertyType.IsValueType || prop.PropertyType == <span style="color:blue;">typeof</span>(System.<span style="color:#2b91af;">String</span>))      </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">                 &amp;&amp;<span> </span>!excludedList.Contains(prop.Name)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                 </span>&amp;&amp; prop.GetIndexParameters().Length == 0)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:#2b91af;">PropertyInfo</span> prop2 = properties2.Find(<span style="color:blue;">delegate</span>(<span style="color:#2b91af;">PropertyInfo</span> target)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                                       </span>{ <span style="color:blue;">return</span> target.Name == prop.Name &amp;&amp; target.PropertyType == prop.PropertyType; });</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">if</span> (prop2 != <span style="color:blue;">null</span>)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:#2b91af;">Assert</span>.AreEqual(prop.GetValue(obj1, <span style="color:blue;">null</span>), prop2.GetValue(obj2, <span style="color:blue;">null</span>), <span style="color:#a31515;">&#8220;Mismatch in objects {1} and {2} &#8211; property {0}.&#8221;</span>, prop.Name, obj1Type, obj2Type);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';">}</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=23&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/09/18/tdd-test-similar-objects-automatically/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://lh4.google.com/vladhorby/RvBLsjhxCUI/AAAAAAAACt8/1Xoj3L7XRB8/s400/DSC_0208.JPG" medium="image" />
	</item>
		<item>
		<title>TDD &#8211; Create test objects automatically</title>
		<link>http://vladhorby.wordpress.com/2007/09/13/tdd-create-test-objects-automatically/</link>
		<comments>http://vladhorby.wordpress.com/2007/09/13/tdd-create-test-objects-automatically/#comments</comments>
		<pubDate>Thu, 13 Sep 2007 22:52:35 +0000</pubDate>
		<dc:creator>Vlad</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Extreme Programming]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://vladhorby.wordpress.com/2007/09/13/tdd-create-test-objects-automatically/</guid>
		<description><![CDATA[Unit tests normally have three sections: setup, action and validation. In the setup part you often have to create and initialize objects that will be required by the action and that you can use in the validation part to decide whether the code works or not. The code might look like this:       [Test]       [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=22&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://picasaweb.google.com/vladhorby/Interesting/photo#5109826728775190834"><img src="http://lh5.google.com/vladhorby/RunAVjhxCTI/AAAAAAAACtQ/Qy-qT906PG0/s400/DSC_0304.JPG" align="right" /></a><br />
Unit tests normally have three sections: setup, action and validation. In the setup part you often have to create and initialize objects that will be required by the action and that you can use in the validation part to decide whether the code works or not.</p>
<p>The code might look like this:</p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[<span style="color:#2b91af;">Test</span>]</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> DoSomethingWorksFine()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>MyObject obj = <span style="color:blue;">new</span> MyObject();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>obj.Text = <span style="color:#a31515;">&#8220;test text&#8221;</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>obj.ID = <span style="color:#2b91af;">Guid</span>.NewGuid();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>obj.Date = <span style="color:#2b91af;">DateTime</span>.Now;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:green;">// etc.</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>testedObject.DoSomething(obj);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:green;">// asserts</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>}</span></p>
<p>Although normally one or two initialized properties are enough, I consider it a good practice to check that all the fields pass well through the test (especially when you&#8217;re testing database repositories or web service calls). The problem is that if you use the default values you won&#8217;t know if the test passed just because the result has the same initial values.</p>
<p>My solution is a handy utility class that fills the objects properties with random data:<br />
<span id="more-22"></span></p>
<pre>
<span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">class</span> <span style="color:#2b91af;">TestObjectCreator</span></span></pre>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:blue;">static</span> <span style="color:#2b91af;">Random</span> rand = <span style="color:blue;">new</span> <span style="color:#2b91af;">Random</span>();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> <span>            </span><span style="color:blue;">public</span> <span style="color:blue;">static</span> <span style="color:blue;">void</span> FillTestObject(<span style="color:blue;">object</span> obj, <span style="color:blue;">params</span> <span style="color:blue;">string</span>[] excludeProperties)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:#2b91af;">Type</span> objType = obj.GetType();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:#2b91af;">PropertyInfo</span>[] properties = objType.GetProperties();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt; excludedList = <span style="color:blue;">new</span> <span style="color:#2b91af;">List</span>&lt;<span style="color:blue;">string</span>&gt;(excludeProperties);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">foreach</span> (<span style="color:#2b91af;">PropertyInfo</span> prop <span style="color:blue;">in</span> properties)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:#2b91af;">Object</span> val = GetTestPropertyValue(prop);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:blue;">if</span> (prop.CanWrite &amp;&amp; val != <span style="color:blue;">null</span> &amp;&amp; !excludedList.Contains(prop.Name))</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                              </span>prop.SetValue(obj, val, <span style="color:blue;">null</span>);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> <span>            </span><span style="color:blue;">public</span> <span style="color:blue;">static</span> T CreateAndFillTestObject&lt;T&gt;(<span style="color:blue;">params</span> <span style="color:blue;">string</span>[] excludeProperties) <span style="color:blue;">where</span> T: <span style="color:blue;">new</span>()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span>T obj = <span style="color:blue;">new</span> T();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span>FillTestObject(obj, excludeProperties);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">return</span> obj;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"> </span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:blue;">private</span> <span style="color:blue;">static</span> <span style="color:blue;">object</span> GetTestPropertyValue(<span style="color:#2b91af;">PropertyInfo</span> propInfo)</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">if</span> (propInfo.PropertyType == <span style="color:blue;">typeof</span>(System.<span style="color:#2b91af;">Int32</span>))</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:blue;">return</span> rand.Next(999999);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">if</span> (propInfo.PropertyType == <span style="color:blue;">typeof</span>(System.<span style="color:#2b91af;">String</span>))</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:blue;">return</span> <span style="color:blue;">string</span>.Format(<span style="color:#a31515;">&#8220;{0} &#8211; {1}&#8221;</span>, propInfo.Name, rand.Next(999999));</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">if</span> (propInfo.PropertyType == <span style="color:blue;">typeof</span>(System.<span style="color:#2b91af;">DateTime</span>))</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:blue;">return</span> <span style="color:#2b91af;">DateTime</span>.Now.AddDays(rand.Next(200) &#8211; 100);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">if</span> (propInfo.PropertyType == <span style="color:blue;">typeof</span>(System.<span style="color:#2b91af;">TimeSpan</span>))</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:blue;">return</span> <span style="color:#2b91af;">TimeSpan</span>.FromMinutes(rand.Next(999999));</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">if</span> (propInfo.PropertyType == <span style="color:blue;">typeof</span>(System.<span style="color:#2b91af;">Guid</span>))</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                        </span><span style="color:blue;">return</span> <span style="color:#2b91af;">Guid</span>.NewGuid();<span>  </span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>                  </span><span style="color:blue;">return</span> <span style="color:blue;">null</span>;</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>}</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>}</span></p>
<p>It basically has methods to fill an existing object or to create and fill a new one, optionally skipping some properties if it&#8217;s necessary. And to use it:</p>
<pre>
<span style="font-size:10pt;font-family:'Courier New';"><span>      </span>[<span style="color:#2b91af;">Test</span>]</span></pre>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span><span style="color:blue;">public</span> <span style="color:blue;">void</span> DoSomethingWorksFine()</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>{</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>MyObject obj = <span style="color:#2b91af;">TestObjectCreator</span>.CreateAndFillTestObject&lt;MyObject&gt;();</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span>testedObject.DoSomething(obj);</span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>            </span><span style="color:green;">// asserts</span></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:'Courier New';"><span>      </span>}</span></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/vladhorby.wordpress.com/22/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/vladhorby.wordpress.com/22/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/vladhorby.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/vladhorby.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/vladhorby.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/vladhorby.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/vladhorby.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/vladhorby.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/vladhorby.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/vladhorby.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/vladhorby.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/vladhorby.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/vladhorby.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/vladhorby.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/vladhorby.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/vladhorby.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=vladhorby.wordpress.com&amp;blog=1572812&amp;post=22&amp;subd=vladhorby&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://vladhorby.wordpress.com/2007/09/13/tdd-create-test-objects-automatically/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/912a4309d7f962491f50799e154c22e8?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Vlad</media:title>
		</media:content>

		<media:content url="http://lh5.google.com/vladhorby/RunAVjhxCTI/AAAAAAAACtQ/Qy-qT906PG0/s400/DSC_0304.JPG" medium="image" />
	</item>
	</channel>
</rss>
