<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Alkampfer's Place</title>
	
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Fri, 01 Jun 2012 14:47:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/AlkampferEng" /><feedburner:info uri="alkampfereng" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>43.44754459758882</geo:lat><geo:long>12.84752368927002</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by-sa/3.0/</creativeCommons:license><image><url>http://www.feedburner.com/fb/images/pub/fb_pwrd.gif</url></image><feedburner:emailServiceId>AlkampferEng</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>Visual Studio 2012 and TFS 2012 RC is finally out</title>
		<link>http://feedproxy.google.com/~r/AlkampferEng/~3/0W3f5isGMmc/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/06/01/visual-studio-2012-and-tfs-2012-rc-is-finally-out/#comments</comments>
		<pubDate>Fri, 01 Jun 2012 14:47:48 +0000</pubDate>
		<dc:creator>Ricci Gian Maria</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[TFS2012]]></category>
		<category><![CDATA[VS2012]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/?p=3236</guid>
		<description><![CDATA[Yesterday the Release Candidate of TFS2012 and Visual Studio 2012 were released by the Visual Studio team, you can download directly from Microsoft, and yes, the name is now officially Visual Studio 2012 and TFS 2012 Visual Studio 2012 RC: http://www.microsoft.com/visualstudio/11/en-us Team Foundation Server 2012 RC: http://www.microsoft.com/en-us/download/details.aspx?id=29918 If you are interested in the changes from [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday<strong> the Release Candidate of TFS2012 and Visual Studio 2012 were released by the Visual Studio team</strong>, you can download directly from Microsoft, and yes, the name is now officially Visual Studio 2012 and TFS 2012 <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Visual Studio 2012 RC</strong>: <a target="_blank" href="http://www.microsoft.com/visualstudio/11/en-us">http://www.microsoft.com/visualstudio/11/en-us</a></p>
<p><strong>Team Foundation Server 2012 RC</strong>: <a target="_blank" title="http://www.microsoft.com/en-us/download/details.aspx?id=29918" href="http://www.microsoft.com/en-us/download/details.aspx?id=29918">http://www.microsoft.com/en-us/download/details.aspx?id=29918</a></p>
<p>If you are interested in the changes from the Beta you can <a target="_blank" href="http://msdn.microsoft.com/en-us/library/bb386063(v=vs.110)">check this MSDN Page</a>, you can find also a lot of information from the Visual Studio ALM Rangers, that <a target="_blank" href="http://blogs.msdn.com/b/visualstudioalm/archive/2012/05/31/welcome-to-visual-studio-2012-alm-rangers-readiness-rc-wave.aspx">released the Readiness RC Wave</a> full of important information. We have a good improvement on the upgrade process as <a target="_blank" href="http://blogs.msdn.com/b/visualstudioalm/archive/2012/05/31/deep-dive-on-configure-features.aspx">described in This post,</a> that explains the new upgrade procedure and how the wizard can avoid breaking something if you have customized a standard TFS Process. </p>
<p><strong>You have also a brand new Developer Tools Blog at this url</strong>: <a target="_blank" title="http://blogs.msdn.com/b/developer-tools/" href="http://blogs.msdn.com/b/developer-tools/">http://blogs.msdn.com/b/developer-tools/</a></p>
<p>Happy Programming.</p>
<p>Gian Maria</p>

<p><a href="http://feedads.g.doubleclick.net/~a/IOxsp_OCL7xJAw5FkFjD643Z0UE/0/da"><img src="http://feedads.g.doubleclick.net/~a/IOxsp_OCL7xJAw5FkFjD643Z0UE/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/IOxsp_OCL7xJAw5FkFjD643Z0UE/1/da"><img src="http://feedads.g.doubleclick.net/~a/IOxsp_OCL7xJAw5FkFjD643Z0UE/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=0W3f5isGMmc:o4Qf23GZTdI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=0W3f5isGMmc:o4Qf23GZTdI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=0W3f5isGMmc:o4Qf23GZTdI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?i=0W3f5isGMmc:o4Qf23GZTdI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=0W3f5isGMmc:o4Qf23GZTdI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=0W3f5isGMmc:o4Qf23GZTdI:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/0W3f5isGMmc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/06/01/visual-studio-2012-and-tfs-2012-rc-is-finally-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codewrecks.com/blog/index.php/2012/06/01/visual-studio-2012-and-tfs-2012-rc-is-finally-out/</feedburner:origLink></item>
		<item>
		<title>Troubleshoot logout from TFS Service when using multiple live Id</title>
		<link>http://feedproxy.google.com/~r/AlkampferEng/~3/fF13kJ-Ksj0/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/05/28/troubleshoot-logout-from-tfs-service-when-using-multiple-live-id/#comments</comments>
		<pubDate>Mon, 28 May 2012 18:00:00 +0000</pubDate>
		<dc:creator>Ricci Gian Maria</dc:creator>
				<category><![CDATA[Team Foundation Server]]></category>
		<category><![CDATA[Tfs service]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/05/28/troubleshoot-logout-from-tfs-service-when-using-multiple-live-id/</guid>
		<description><![CDATA[If you are using multiple live id to connect to the same instance of TFS Preview (I’m, using a couple of different ID to demo authentication feature), you can find difficulties to logout from inside Visual Studio. In Figure 1 you can visualize the standard Connect to Team Project page, and in the bottom left [...]]]></description>
			<content:encoded><![CDATA[<p>If you are using <strong><em>multiple live id to connect to the same instance of </em></strong><a target="_blank" href="http://www.tfspreview.com/" target="_blank"><strong><em>TFS Preview</em></strong></a><strong><em> (I’m, using a couple of different ID to demo authentication feature), you can find difficulties to logout from inside Visual Studio</em></strong>. In <strong>Figure 1</strong> you can visualize the standard Connect to Team Project page, and in the bottom left corner you find information about the user currently logged to TFS.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/05/SNAGHTML2ef1ce.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="SNAGHTML2ef1ce" border="0" alt="SNAGHTML2ef1ce" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/05/SNAGHTML2ef1ce_thumb.png" width="639" height="398" /></a></p>
<p><strong>Figure 1: </strong><em>Standard form to connect to a TFS team project, after the installation of the hotfix to connect to TFS Preview</em></p>
<p>Now if you press “Sign Out” the standard login form of TFS Preview appears and it should prompt you for a new login, but sometimes it happens that the form automatically logins you again so you are not able to logout. This happens sometimes when You check the “log me automatically” checkbox, but now you are unable to change the user you are using to login to TFS Preview.</p>
<p>A simple workaround is <strong>open a Web Browser window (in menu View-&gt;other windows-&gt;Web Browser&quot;), navigate to your TFS Preview web site</strong> xxxx.TfsPreview.com and you should be already logged with the same user you see in the “Connect to Team Project” window. <strong>Now you can simply logout from the site</strong></p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/05/image1.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/05/image_thumb.png" width="604" height="340" /></a>&#160;</p>
<p><strong>Figure 2: </strong><em>Signout the current user from TFS Service web side from a Web Browser inside Visual Studio.</em></p>
<p>Now you are logged out and even if you are redirected to a page that tells you “Sign-out isn’t complete” you can just close all instances of Visual Studio, then, if you open again the “Connect to Team Project” dialog, you should now be prompted for login and you are finally able to change the TFS Preview Logged User</p>
<p>Gian Maria.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/9Iajal24lVkq-j9GlfaXlbevAQc/0/da"><img src="http://feedads.g.doubleclick.net/~a/9Iajal24lVkq-j9GlfaXlbevAQc/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/9Iajal24lVkq-j9GlfaXlbevAQc/1/da"><img src="http://feedads.g.doubleclick.net/~a/9Iajal24lVkq-j9GlfaXlbevAQc/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fF13kJ-Ksj0:PohQ2XwkfkA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fF13kJ-Ksj0:PohQ2XwkfkA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fF13kJ-Ksj0:PohQ2XwkfkA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?i=fF13kJ-Ksj0:PohQ2XwkfkA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fF13kJ-Ksj0:PohQ2XwkfkA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fF13kJ-Ksj0:PohQ2XwkfkA:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/fF13kJ-Ksj0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/05/28/troubleshoot-logout-from-tfs-service-when-using-multiple-live-id/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codewrecks.com/blog/index.php/2012/05/28/troubleshoot-logout-from-tfs-service-when-using-multiple-live-id/</feedburner:origLink></item>
		<item>
		<title>Query untyped object inside a Mongo Db (log4net mongoappender)</title>
		<link>http://feedproxy.google.com/~r/AlkampferEng/~3/ldTfp9Clvsk/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/05/25/query-untyped-object-inside-a-mongo-db-log4net-mongoappender/#comments</comments>
		<pubDate>Fri, 25 May 2012 17:22:38 +0000</pubDate>
		<dc:creator>Ricci Gian Maria</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/05/25/how-to-run-test-in-new-vs11-beta/</guid>
		<description><![CDATA[I’ve blogged about using MongoDb to store log4net logs, but the original appender has a little problem, it stores a BsonDocument inside the MongoCollection, and does not use any C# object and when it is time to query data you cannot use the new LINQ Mongo provider that is included in newest C# drivers (1.4), [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve blogged about <strong>using MongoDb to store log4net logs</strong>, but the original appender has a little problem, it stores a BsonDocument inside the MongoCollection, and does not use any C# object and when it is time to query data you cannot use the new LINQ Mongo provider that is included in <a target="_blank" href="http://www.mongodb.org/display/DOCS/CSharp+Language+Center" target="_blank">newest C# drivers (1.4</a>), because data is completely untyped.</p>
<p>This is usually not a big problem, because you can query a collection using <strong>simple JSON-like query</strong>, but if you are used to standard LINQ provider, probably you will get a little bit lost on how to create the JSON query to retrieve the data you need. <em>This is the scenario I need to solve</em>: I have a stupid Winform that is able to show some information from a standard Log4Net Sql database (using the adonetappender) and I want to be able to use the very same interface to load data from mongo database. First of all I need to <em>load</em> <em>all distinct value for the property <strong>level</strong>&#160; and <strong>loggerName</strong></em>, because I have a combo where the user can filter for Severity (ERROR, WARN, INFO, Etc.) and a list of checkboxes used to filter for loggerName. Luckily enough, mongo offer such a functionality out of the box.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b6d5ead4-6988-4035-a435-919263794ac5" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; pad-line-numbers: true; title: ; notranslate">
var db = server.GetDatabase(bsDatabase.Current as string);
var collection = db.GetCollection(bsCollections.CurrencyManager.Current as string);
var allLevel = collection.Distinct(&quot;level&quot;);
</pre>
</pre>
</div>
<p>The code is really simple, I create a connection to the database and then retrieve a reference to collection that was selected in the interface from the user, then <em>I use the <strong>Distinct() </strong>method of MongoCollection object, passing the name of the property you need, and Mongo gave you the distinct list of every value of that property</em>. This permits me to populate the user interface with few lines of code.</p>
<p>Now it is time to think <strong>how to create a query to retrieve all the document with a certain level of logging and belonging to a list of possible loggerName</strong>. In my software I usually use Castle Log4net Integration, this means that loggerName property is equals to the name of the class that issue the log and usually the user wants to see logs belonging to one or more class, something like: <em>all ERROR from classa or classb and severity ERROR</em>. Mongo has a QueryBuilder helper class that makes easy to create such a query with a <strong>little help from intellisense and without the need to dirty your hand directly with JSON</strong></p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:5a24360d-2095-414c-9316-cd20a2732dcb" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; pad-line-numbers: true; title: ; notranslate">
var query = Query.And(
                    Query.EQ(&quot;level&quot;, &quot;ERROR&quot;),
                    Query.Or(
                        Query.EQ(&quot;loggerName&quot;, &quot;classa&quot;),
                        Query.EQ(&quot;loggerName&quot;, &quot;classb&quot;)
                    )
                );
</pre>
</pre>
</div>
<p>Now I need to make this code dynamic, because I need to create a query that retrieve logs <strong>belonging to an unknown number of loggerName</strong>,<strong> such as “classa”, “classb” and “classc”</strong>, because user interface contains a CheckBoxList of every loggerName present into the database, and the user can choose any number of elements to search from, so I need to dynamically create the list of condition to create a dynamic query.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:0a8724bb-abe5-4b3d-94b5-59ca94fa09b5" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; title: ; notranslate">
List&lt;IMongoQuery&gt; mainQueries = new List&lt;IMongoQuery&gt;();
if (!String.IsNullOrWhiteSpace(cmbLevel.Text))
{
    mainQueries.Add(Query.EQ(&quot;level&quot;, cmbLevel.Text));
}
if (cbList.CheckedIndices.Count &gt; 0)
{
    List&lt;IMongoQuery&gt; listOfLoggerNameQueries = new List&lt;IMongoQuery&gt;();

    foreach (Int32 index in cbList.CheckedIndices)
    {
        String value = cbList.Items[index].ToString();
        listOfLoggerNameQueries.Add(Query.EQ(&quot;loggerName&quot;, value));
    }
    mainQueries.Add(Query.Or(listOfLoggerNameQueries.ToArray()));
}
var finalQuery = Query.And(mainQueries.ToArray());
</pre>
</pre>
</div>
<p>Code is really simple, because <strong>it simply create a list of IMongoQuery object that contains all the first-level condition that will be combined</strong> <strong>in the last instruction with the Query.And() helper</strong>. Since I can select more than one loggerName from the checkboxList I can simply <strong>iterate through all CheckedIndices and create a Query.EQ(“loggerName”, value) condition for every checked name in the UI, then I can combine all these condition with Query.Or() to produce a single IMongoQuery that is added to the main list</strong>. </p>
<p>After you have the query you can use to retrieve records.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:c5556166-ea1f-4ee1-a7ec-053eca62c18c" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; title: ; notranslate">
var cursor = collection.Find(finalQuery);
Int32 limit;
if (!Int32.TryParse(txtLogNum.Text, out limit))
{
    limit = 50;
}
cursor.SetFields(&quot;level&quot;, &quot;loggerName&quot;, &quot;message&quot;, &quot;exception&quot;, &quot;customproperties&quot;, &quot;timestamp&quot;);
cursor.SetSortOrder(SortBy.Descending(&quot;timestamp&quot;));
cursor.Limit = limit;
</pre>
</pre>
</div>
<p>Finally the method <strong>MongoCollection.Find() </strong>returns a cursor that actually does not contains any data,&#160; you can now add Sorting, pagination and specify all the properties you want to return directly on the curso, and when you iterate through all element with a foreach data will be retrieved from the database. This is really similar to a LINQ query, where no data is retrieved if you call Where(), Select() etc, but only when you iterate the query or you call a not deferred operator like List().</p>
<p>With this simple code I’m able to build a simple form to have a quick visualization of all the logs stored inside a Mongo Database even if the appender store untyped objects.</p>
<p>Gian Maria.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/HINmBlsg6DlYAzfD0fC_zJ89xe8/0/da"><img src="http://feedads.g.doubleclick.net/~a/HINmBlsg6DlYAzfD0fC_zJ89xe8/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/HINmBlsg6DlYAzfD0fC_zJ89xe8/1/da"><img src="http://feedads.g.doubleclick.net/~a/HINmBlsg6DlYAzfD0fC_zJ89xe8/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=ldTfp9Clvsk:ZVFwyg-47eM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=ldTfp9Clvsk:ZVFwyg-47eM:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=ldTfp9Clvsk:ZVFwyg-47eM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?i=ldTfp9Clvsk:ZVFwyg-47eM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=ldTfp9Clvsk:ZVFwyg-47eM:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=ldTfp9Clvsk:ZVFwyg-47eM:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/ldTfp9Clvsk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/05/25/query-untyped-object-inside-a-mongo-db-log4net-mongoappender/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.codewrecks.com/blog/index.php/2012/05/25/query-untyped-object-inside-a-mongo-db-log4net-mongoappender/</feedburner:origLink></item>
		<item>
		<title>Use Capped collection on MongoAppender for Log4Net</title>
		<link>http://feedproxy.google.com/~r/AlkampferEng/~3/zh32pJTnAIk/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/05/22/use-capped-collection-on-mongoappender-for-log4net/#comments</comments>
		<pubDate>Tue, 22 May 2012 18:02:49 +0000</pubDate>
		<dc:creator>Ricci Gian Maria</dc:creator>
				<category><![CDATA[NoSql]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[MongoDb]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/05/22/use-capped-collection-on-mongoappender-for-log4net/</guid>
		<description><![CDATA[One of the coolest feature of Mongo is the concept of Capped Collection, or “fixed size” collection. They are based on a FIFO queue where the first record to be discharded is the first inserted, and this is exceptional to create a log-collection that automatically purge all old logs without any user intervention. To be [...]]]></description>
			<content:encoded><![CDATA[<p>One of the coolest feature of Mongo is the concept of <a target="_blank" href="http://www.mongodb.org/display/DOCS/Capped+Collections" target="_blank"><strong>Capped Collection</strong></a><strong>, </strong>or “fixed size” collection. They are based on a FIFO queue where the first record to be discharded is the first inserted, and this is exceptional to create a log-collection that automatically purge all old logs <strong>without any user intervention</strong>.</p>
<p>To be able to automatically enable this feature on the Log4Net <a href="http://www.codewrecks.com/blog/index.php/2012/03/19/using-mongo-database-to-store-log4net-logs/" target="_blank">Mongo appender</a> you need to do a little modification to the code, this is because the original code simply gets a reference to the collection with this code.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:df9bd402-bed8-4b9f-8fd8-69418cde11a3" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; pad-line-numbers: true; title: ; notranslate">
connection = MongoServer.Create(mongoConnectionString.ToString());
connection.Connect();
var db = connection.GetDatabase(DatabaseName);
collection = db.GetCollection(CollectionName);
</pre>
</pre>
</div>
<p>C# drivers for Mongo <strong>automatically creates a collection if it is not present</strong>, this means that when you call db.GetCollection if the collection is not present it will be automatically created, but it is not capped. To solve this problem you can modify the initialization code with this code.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:d167b4f0-e395-4176-86fd-2c80f03da0e7" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; title: ; notranslate">
if (!db.CollectionExists(collectionName)) {
    var options =  CollectionOptions
        .SetCapped(true)
        .SetMaxSize(CappedSize);
    db.CreateCollection(collectionName, options);
}
collection = db.GetCollection(CollectionName);
</pre>
</pre>
</div>
<p>MongoDb C# drivers has a class called <a target="_blank" title="CollectionOptions class MongoDb API" href="http://api.mongodb.org/csharp/1.2/html/a06528f4-772e-f4f4-890d-ca3b55ac8d92.htm" target="_blank"><strong>CollectionOptions</strong></a><strong> used to setup options to create a new MongoCollection</strong> and it can be accessed with a really easy Fluent-Interface, in my example I call SetCapped(true) to enable a capped collection and <a target="_blank" href="http://api.mongodb.org/csharp/1.2/html/1fd7ba87-3d7b-a9ae-97c3-6c0d9dbe8c00.htm" target="_blank">SetMaxSize</a>() to setup the maximum size in bytes. The size of the capped-collection is stored in the appender property called CappedSize, the default is 500MB, but you can setup any size you likes in standard log4Net configuration.</p>
<p>Gian Maria.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/dnylUVL_U2omh4ZilXhu0YR5joQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/dnylUVL_U2omh4ZilXhu0YR5joQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/dnylUVL_U2omh4ZilXhu0YR5joQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/dnylUVL_U2omh4ZilXhu0YR5joQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=zh32pJTnAIk:96EhD7mE7y4:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=zh32pJTnAIk:96EhD7mE7y4:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=zh32pJTnAIk:96EhD7mE7y4:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?i=zh32pJTnAIk:96EhD7mE7y4:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=zh32pJTnAIk:96EhD7mE7y4:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=zh32pJTnAIk:96EhD7mE7y4:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/zh32pJTnAIk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/05/22/use-capped-collection-on-mongoappender-for-log4net/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://www.codewrecks.com/blog/index.php/2012/05/22/use-capped-collection-on-mongoappender-for-log4net/</feedburner:origLink></item>
		<item>
		<title>Troubleshoot WCF The underlying connection was closed: A connection that was expected to be kept alive was closed by the server</title>
		<link>http://feedproxy.google.com/~r/AlkampferEng/~3/AByRp3RIn40/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/05/16/troubleshoot-wcf-the-underlying-connection-was-closed-a-connection-that-was-expected-to-be-kept-alive-was-closed-by-the-server/#comments</comments>
		<pubDate>Wed, 16 May 2012 20:46:00 +0000</pubDate>
		<dc:creator>Ricci Gian Maria</dc:creator>
				<category><![CDATA[Nhibernate]]></category>
		<category><![CDATA[Wcf]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/05/16/troubleshoot-wcf-the-underlying-connection-was-closed-a-connection-that-was-expected-to-be-kept-alive-was-closed-by-the-server/</guid>
		<description><![CDATA[I’ve already blogged in the past on how to easily troubleshoot WCF Exception and that suggestion is valid for every exception you encounter in WCF. Today I have a function that gave the error System.ServiceModel.CommunicationException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. &#8212;&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve already blogged in the past on how to <a href="http://www.codewrecks.com/blog/index.php/2012/04/23/troubleshoot-wcf-exception/" target="_blank">easily troubleshoot WCF Exception</a> and that suggestion is valid for every exception you encounter in WCF. Today I have a function that gave the error</p>
<blockquote><p>System.ServiceModel.CommunicationException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. &#8212;&gt; System.Net.WebException: The underlying connection was closed: A connection that was expected to be kept alive was closed by the server. &#8212;&gt; System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. &#8212;&gt; System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host</p>
</blockquote>
<p>This problem is usually caused by some error in the communication channel, but even in such a situation, enabling the trace listener immediately gave to you an idea of the problem.</p>
<p>&#160;</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/05/SNAGHTML20daa9b1.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="SNAGHTML20daa9b" border="0" alt="SNAGHTML20daa9b" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/05/SNAGHTML20daa9b_thumb.png" width="1039" height="318" /></a></p>
<p><strong>Figure 1: </strong><em>How to identify the problem thanks to WCF Trace Listener</em></p>
<p>In my situation the error is ClientProxy …. is not expected and this is due to the fact that I’m returning from WCF a couple of classes that are mapped with NHIbernate, the entity returned has a property of type Customer that is a lazy property, thus at runtime it is represented by a dynamic proxy that is not known to WCF. The solution is trivial, just issue a Fetch(obj =&gt; obj.Client) in the LINQ query to fetch Client and avoiding NH to create a proxy, but the key point is that with WCF trace listener, finding problems in WCF is matter of few seconds.</p>
<p>Gian MAria.</p>
<p><font style="background-color: #f0f0e0">&#160;</font></p>

<p><a href="http://feedads.g.doubleclick.net/~a/KmlQ1kZGii1OX3wCYJmjxjRNNsI/0/da"><img src="http://feedads.g.doubleclick.net/~a/KmlQ1kZGii1OX3wCYJmjxjRNNsI/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/KmlQ1kZGii1OX3wCYJmjxjRNNsI/1/da"><img src="http://feedads.g.doubleclick.net/~a/KmlQ1kZGii1OX3wCYJmjxjRNNsI/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=AByRp3RIn40:Lw9i-k9nUcI:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=AByRp3RIn40:Lw9i-k9nUcI:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=AByRp3RIn40:Lw9i-k9nUcI:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?i=AByRp3RIn40:Lw9i-k9nUcI:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=AByRp3RIn40:Lw9i-k9nUcI:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=AByRp3RIn40:Lw9i-k9nUcI:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/AByRp3RIn40" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/05/16/troubleshoot-wcf-the-underlying-connection-was-closed-a-connection-that-was-expected-to-be-kept-alive-was-closed-by-the-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.codewrecks.com/blog/index.php/2012/05/16/troubleshoot-wcf-the-underlying-connection-was-closed-a-connection-that-was-expected-to-be-kept-alive-was-closed-by-the-server/</feedburner:origLink></item>
		<item>
		<title>Misusing an ORM</title>
		<link>http://feedproxy.google.com/~r/AlkampferEng/~3/aflBnx3LLl0/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/05/12/misusing-an-orm/#comments</comments>
		<pubDate>Sat, 12 May 2012 07:44:49 +0000</pubDate>
		<dc:creator>Ricci Gian Maria</dc:creator>
				<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[NoSql]]></category>
		<category><![CDATA[ORM]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/05/12/misusing-an-orm/</guid>
		<description><![CDATA[I’ve blogged some time ago that I’m starting to consider ORM an Antipattern, and recently Mr Fowler posted similar thoughts in his bliki, moreover I have the pleasure to be one of the organizer of the first RavenDB official Course in Italy, with my dear friend Mauro as teacher. Since I’m strongly convinced that in [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve blogged some time ago that I’m starting to <a target="_blank" href="http://www.codewrecks.com/blog/index.php/2011/10/27/square-peg-in-a-round-hole/" target="_blank">consider ORM an Antipattern</a>, and recently Mr Fowler <a href="http://martinfowler.com/bliki/OrmHate.html" target="_blank">posted similar thoughts</a> in his bliki, moreover I have the pleasure to be one of the organizer of the <a target="_blank" href="http://ravendb.net/events/82/3-days-ravendb-training-in-italy" target="_blank">first RavenDB official Course in Italy</a>, with my dear friend <a target="_blank" href="http://milestone.topics.it/" target="_blank">Mauro</a> as teacher. </p>
<p>Since I’m strongly convinced that in a full OOP approach to problem<strong> objects should not have nor setter nor getter</strong>, most of the work and complexities of an ORM is simply not needed, because you usually retrieve objects from the storage with only one function <strong>GetById</strong> and nothing else. In my long experience with NHibernate, I verified that most of the problem arise when you need to <strong>show data in UI in specific format and you start to write complex Query</strong> in HQL or ICRiteria or LINQ, then you need to spend time with NHProfiler to understand if the queries are good enough to run on production system and when objects changes a little bit <strong>you need to rewrite a lot of code to suite the new Object Model. </strong>This last point is the real pain point in DDD, where you usually should create Object Model that will be manipulated a lot before reaching a good point, after all the main value of DDD approach is being able to create a dialog with a DOMAIN EXPERT and it is impossible to find a good Object Models at the first tentative. If refactoring a model become painful, you are not allowed to modify it with easy, you are going away from DDD approach.</p>
<p>This is where <a target="_blank" href="http://codebetter.com/gregyoung/2010/02/13/cqrs-and-event-sourcing/" target="_blank">CQRS</a> can help you, for all objects belonging to the domain you need only to Save, LoadById, Update and delete, because every read model should be defined somewhere else. In such a scenario an ORM is really useful, because if you need to store objects inside Relational Database you can leave the ORM all the work to satisfy the CRUD part, where the R is the method GetById. <strong>To start easily with this approach you can create SQL View or stored procedures for all the Read Models</strong> you need; this imply that whenever the structure of the Domain Model changes, you need only to change all affected Read Models, some view and some stored procedure, but you have no need to refactor the code.</p>
<p>In this situation the ORM can really helps you, because if you change the Domain Model, you should only change the mapping, or let some Mapping by convention do this for you (<a target="_blank" href="http://fabiomaulo.blogspot.it/2010/02/conform-nhibernate-un-mapping.html" target="_blank">ConfORM</a> for NH is an example), regenerate the database and update only affected Read Models. If your domain is really anemic, if you expose properties from objects, even only with getters, whenever you change a domain class you should answer the question “If I change this property, what other domain objects will be affected? How many service class will be affected? How many query issued from Views will be affected?”. If you are not able to create a Read Model with SQL View or stored procedure, you can write a denormalizer that listens for DOMAIN EVENTS and populate the Read Model accordingly. In my opinion this is the scenario where an ORM can really helps you.</p>
<p>In such a situation a NoSql database can dramatically simplify your life, because you do not need an ORM anymore, cause you are able to save object graps into the storage directly, and you can create Read Models with Map/Reduce or with denormalizers.</p>
<p>But sadly enough, ORM are primarily used to avoid writing SQL and persist completely anemic domain, where all the logic reside on services. In such a scenario it is <strong>easy to abuse an ORM </strong>and probably in the long term the ORM could become much more a pain than a real help.</p>
<p>Gian Maria.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/YARCdO6lxqdhsu631dm1_-w2ZFQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/YARCdO6lxqdhsu631dm1_-w2ZFQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/YARCdO6lxqdhsu631dm1_-w2ZFQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/YARCdO6lxqdhsu631dm1_-w2ZFQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=aflBnx3LLl0:VW5dNu1K3AU:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=aflBnx3LLl0:VW5dNu1K3AU:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=aflBnx3LLl0:VW5dNu1K3AU:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?i=aflBnx3LLl0:VW5dNu1K3AU:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=aflBnx3LLl0:VW5dNu1K3AU:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=aflBnx3LLl0:VW5dNu1K3AU:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/aflBnx3LLl0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/05/12/misusing-an-orm/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.codewrecks.com/blog/index.php/2012/05/12/misusing-an-orm/</feedburner:origLink></item>
		<item>
		<title>Shim and InstanceBehavior fallthrough to isolate part of the SUT</title>
		<link>http://feedproxy.google.com/~r/AlkampferEng/~3/fUesRaVxiqA/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/05/10/shim-and-instancebehavior-fallthrough-to-isolate-part-of-the-sut/#comments</comments>
		<pubDate>Thu, 10 May 2012 17:11:26 +0000</pubDate>
		<dc:creator>Ricci Gian Maria</dc:creator>
				<category><![CDATA[Testing]]></category>
		<category><![CDATA[shim]]></category>
		<category><![CDATA[VS11]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/05/10/shim-and-instancebehavior-fallthrough-to-isolate-part-of-the-sut/</guid>
		<description><![CDATA[I’ve dealt in a previous post with the new Shim library in Vs11 that permits you to test “difficult to test code” and I showed a really simple example on how to use Shim to isolate the call to DateTime.Now to simulate passing time in a Unit Test. Now I want to change a little [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve dealt in a previous post with the new <a href="http://www.codewrecks.com/blog/index.php/2012/04/27/using-shims-in-visual-studio-11-to-test-untestable-code/" target="_blank">Shim library in Vs11</a> that permits you to test “difficult to test code” and <strong>I showed a really simple example on how to use Shim to isolate the call to DateTime.Now</strong> to simulate passing time in a Unit Test. Now I want to change a little bit the perspective of the test, in the test showed in previous post I simply exercise the sut calling Execute() a couple of time, simulating the time that pass between the two calls. Here is the test</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:a8138ad7-d6ee-42c8-98e9-8d92e9a19f2c" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; pad-line-numbers: true; title: ; notranslate">
[Fact]
public void Verify_do_not_execute_task_if_interval_is_not_elapsed()
{
    using (ShimsContext.Create())
    {
        Int32 callCount = 0;
        PerformHeavyTask sut = new PerformHeavyTask(10, () =&gt; callCount++);
        DateTime startDate = new DateTime(2012, 1, 1, 12, 00, 00);
        ShimDateTime.NowGet = () =&gt;  startDate;
        sut.Execute();
        ShimDateTime.NowGet = () =&gt; startDate.AddMinutes(9);
        sut.Execute();
        Assert.Equal(1, callCount);
    }
}
</pre>
</pre>
</div>
<p>I can also change the point of view and<strong> write a test that uses a shim to isolate the SUT</strong>, this is a less common scenario but it can be also really interesting, because it shows you how you can write simple White box Unit Tests isolating part of the <a target="_blank" href="http://xunitpatterns.com/SUT.html" target="_blank">SUT</a>. The term <strong>White Box is used because this kind of Unit Test are created with a full knowledge of the internal structure of the <a target="_blank" href="http://xunitpatterns.com/SUT.html" target="_blank">SUT</a></strong>, in my situation I have a private method called CanExecute() that return true/false based on the interval of time passed from the last execution and since it is private it makes difficult for me to test the <a target="_blank" href="http://xunitpatterns.com/SUT.html" target="_blank">SUT</a>.</p>
<p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:8b25e446-2fcb-4fd3-878a-ec36d2559adb" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; title: ; notranslate">
private Boolean CanExecute() {

    return DateTime.Now.Subtract(lastExecutionTime)
        .TotalMinutes &gt;= intervalInMinutes;
}
</pre>
</pre>
</div>
<p>But I can create a Shime on the SUT and isolate calls to the CanExecute(), making it return the value I need for the test, here is an example</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:b8f4c9f6-f11d-419a-a39e-55308e02a5ec" class="wlWriterEditableSmartContent">
<pre style=white-space:normal>
<pre class="brush: csharp; title: ; notranslate">
[Fact]
public void Verify_can_execute_is_honored()
{
    using (ShimsContext.Create())
    {
        Int32 callCount = 0;
        PerformHeavyTask sut = new PerformHeavyTask(10, () =&gt; callCount++);
        ShimPerformHeavyTask shimSut = new ShimPerformHeavyTask(sut);
        shimSut.InstanceBehavior = ShimBehaviors.Fallthrough;
        shimSut.CanExecute = () =&gt; false;
        sut.Execute();
        Assert.Equal(0, callCount);
    }
}
</pre>
</pre>
</div>
<p>To write this test I’ve added another fake assembly on the assembly that contains the PerformHeavyTask class to create shim for the SUT. This test basically create a <strong>ShimPerformHeavyTask</strong> (a shim of my SUT) passing an existing instance to the SUT to the constructor of the shim, then I set the InstanceBehavior to <strong><em>ShimBehaviors.Fallthrough</em></strong> to indicate to the Shim Library to call original <a target="_blank" href="http://xunitpatterns.com/SUT.html" target="_blank">SUT</a> method if the method was not isolated. At this point I can simply <strong>isolate the call to the CanExecute() private and non-virtual method</strong>, specifying to the shim to return the value false, then I call the Execute() method and verify that the heavy task is not executed. </p>
<p><strong>This test shows how to create a shim of the SUT to isolate calls to its private methods, thanks to the Fallthrough behavior</strong>; if you forget to change the InstanceBehavior the test will fail with an exception of type <em>ShimNotImplementedException</em>, because the default behavior for a Shim is to throw an exception for any method that is not intercepted. Thanks to shim library you can simply isolate every part of the SUT, making easier to write Unit Test for classes written with no TDD and no Unit Testing in mind.</p>
<p>Gian Maria.</p>

<p><a href="http://feedads.g.doubleclick.net/~a/vFUbhz-7ENjVbf3mPBQgXBOZDeQ/0/da"><img src="http://feedads.g.doubleclick.net/~a/vFUbhz-7ENjVbf3mPBQgXBOZDeQ/0/di" border="0" ismap="true"></img></a><br/>
<a href="http://feedads.g.doubleclick.net/~a/vFUbhz-7ENjVbf3mPBQgXBOZDeQ/1/da"><img src="http://feedads.g.doubleclick.net/~a/vFUbhz-7ENjVbf3mPBQgXBOZDeQ/1/di" border="0" ismap="true"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fUesRaVxiqA:lep9RB_10AA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fUesRaVxiqA:lep9RB_10AA:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=7Q72WNTAKBA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fUesRaVxiqA:lep9RB_10AA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?i=fUesRaVxiqA:lep9RB_10AA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fUesRaVxiqA:lep9RB_10AA:qj6IDK7rITs"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=qj6IDK7rITs" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/AlkampferEng?a=fUesRaVxiqA:lep9RB_10AA:TzevzKxY174"><img src="http://feeds.feedburner.com/~ff/AlkampferEng?d=TzevzKxY174" border="0"></img></a>
</div><img src="http://feeds.feedburner.com/~r/AlkampferEng/~4/fUesRaVxiqA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/05/10/shim-and-instancebehavior-fallthrough-to-isolate-part-of-the-sut/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://www.codewrecks.com/blog/index.php/2012/05/10/shim-and-instancebehavior-fallthrough-to-isolate-part-of-the-sut/</feedburner:origLink></item>
	</channel>
</rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: basic (Requested URI is rejected)
Object Caching 524/573 objects using disk: basic

Served from: www.codewrecks.com @ 2012-06-01 15:48:41 -->

