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

<channel>
	<title>.Net Head</title>
	<atom:link href="http://blog.baltrinic.com/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.baltrinic.com</link>
	<description>Agile and .Net development from the trenches</description>
	<lastBuildDate>Sun, 20 May 2012 14:19:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Integrating ASP.NET SQL Registration with Entity Framework Code First</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/integrating-asp-net-reg-sql-and-ef-code-first</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/integrating-asp-net-reg-sql-and-ef-code-first#comments</comments>
		<pubDate>Sun, 20 May 2012 02:19:10 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=207</guid>
		<description><![CDATA[I am using Microsoft&#8217;s Entity Framework 4.1&#8242;s Code First approach to rapidly develop a basic MVC 3 application for a friend of mine. The app is also leveraging the standard MVC Internet Website template and its build-in membership management functionality. Hence it uses the SQL Membership Provider which adds a bunch of tables to the [...]]]></description>
			<content:encoded><![CDATA[<p>I am using Microsoft&#8217;s Entity Framework 4.1&#8242;s Code First approach to rapidly develop a basic MVC 3 application for a friend of mine.  The app is also leveraging the standard MVC Internet Website template and its build-in membership management functionality.  Hence it uses the SQL Membership Provider which adds a bunch of tables to the database by means of the <a href="http://msdn.microsoft.com/en-us/library/ms229862(v=vs.100).aspx" target="_blank">aspnet_regsql.exe</a> utility.  Thus not all database access is via the EF.</p>
<p>To get it all working together, my initial, naive approach was to use the utility to generate the SQL script needed to build out the database and then execute that script as part of the Seed() method of my database initializer.  (I was subclassing both  DropCreateDatabaseIfModelChanges for my website and DropCreateDatabaseAlways for my automated tests.)</p>
<p>The method to actually execute the script looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> ExecuteSqlSript<span style="color: #008000;color: #000000;">&#40;</span>Database database, <span style="color: #6666cc; font-weight: bold;color: #000080;">string</span> scriptPath<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    var conn <span style="color: #008000;color: #000000;">=</span> database<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Connection</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;color: #000000;">==</span> ConnectionState<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Closed</span><span style="color: #008000;color: #000000;">&#41;</span>
        conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    var fullScript <span style="color: #008000;color: #000000;">=</span> File<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;color: #000000;">&#40;</span>
        scriptPath<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">foreach</span> <span style="color: #008000;color: #000000;">&#40;</span>var command <span style="color: #0600FF; font-weight: bold;color: #000000;">in</span> Regex<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;color: #000000;">&#40;</span>fullScript, <span style="color: #666666;">@&quot;\bGO\b&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        var cmd <span style="color: #008000;color: #000000;">=</span> conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CreateCommand</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        cmd<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CommandText</span> <span style="color: #008000;color: #000000;">=</span> command<span style="color: #008000;color: #000000;">;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">try</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            cmd<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">catch</span> <span style="color: #008000;color: #000000;">&#40;</span>Exception e<span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;color: #000000;">throw</span> <span style="color: #008000;color: #000000;">new</span> ApplicationException<span style="color: #008000;color: #000000;">&#40;</span>e<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Message</span> <span style="color: #008000;color: #000000;">+</span> 
                <span style="color: #666666;color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>Command: &quot;</span> <span style="color: #008000;color: #000000;">+</span> command, e<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>This worked great for my first few iterations, until I needed to add a model to the framework to represent some of the data on the aspnet_User table.  </p>
<p>Mapping my new User entity to the existing table name was easy.  It just required adding the following to my DbContext implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">protected</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">override</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> OnModelCreating<span style="color: #008000;color: #000000;">&#40;</span>DbModelBuilder modelBuilder<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">base</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">OnModelCreating</span><span style="color: #008000;color: #000000;">&#40;</span>modelBuilder<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    modelBuilder<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;color: #000000;">&lt;</span>User<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ToTable</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;aspnet_Users&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    modelBuilder<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Entity</span><span style="color: #008000;color: #000000;">&lt;</span>User<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property</span><span style="color: #008000;color: #000000;">&#40;</span>u <span style="color: #008000;color: #000000;">=&gt;</span> u<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Name</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">HasColumnName</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;UserName&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>This lead to the aspnet_regsql generated script failing because EF now was creating the aspNet_User table before the script ran.  So my next step was to hack the script by adding the following just before the code to create the table:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;color: #000080;"><span style="color: #993333; font-weight: bold;color: #000000;">IF</span> <span style="color: #993333; font-weight: bold;color: #000000;">EXISTS</span> <span style="color: #66cc66;color: #000000;">&#40;</span><span style="color: #993333; font-weight: bold;color: #000000;">SELECT</span> name
                <span style="color: #993333; font-weight: bold;color: #000000;">FROM</span> sysobjects
                <span style="color: #993333; font-weight: bold;color: #000000;">WHERE</span> <span style="color: #66cc66;color: #000000;">&#40;</span>name <span style="color: #66cc66;color: #000000;">=</span> N<span style="color: #ff0000;color: #800000;">'aspnet_Users'</span><span style="color: #66cc66;color: #000000;">&#41;</span>
                  <span style="color: #993333; font-weight: bold;color: #000000;">AND</span> <span style="color: #66cc66;color: #000000;">&#40;</span><span style="color: #993333; font-weight: bold;color: #000000;">TYPE</span> <span style="color: #66cc66;color: #000000;">=</span> <span style="color: #ff0000;color: #800000;">'U'</span><span style="color: #66cc66;color: #000000;">&#41;</span><span style="color: #66cc66;color: #000000;">&#41;</span>
   <span style="color: #993333; font-weight: bold;color: #000000;">AND</span> <span style="color: #993333; font-weight: bold;color: #000000;">NOT</span> <span style="color: #993333; font-weight: bold;color: #000000;">EXISTS</span><span style="color: #66cc66;color: #000000;">&#40;</span><span style="color: #993333; font-weight: bold;color: #000000;">SELECT</span> <span style="color: #66cc66;color: #000000;">*</span> <span style="color: #993333; font-weight: bold;color: #000000;">FROM</span> sys<span style="color: #66cc66;color: #000000;">.</span><span style="color: #993333; font-weight: bold;color: #000000;">COLUMNS</span> 
                  <span style="color: #993333; font-weight: bold;color: #000000;">WHERE</span> Name <span style="color: #66cc66;color: #000000;">=</span> N<span style="color: #ff0000;color: #800000;">'ApplicationId'</span>  
                    <span style="color: #993333; font-weight: bold;color: #000000;">AND</span> Object_ID <span style="color: #66cc66;color: #000000;">=</span> Object_ID<span style="color: #66cc66;color: #000000;">&#40;</span>N<span style="color: #ff0000;color: #800000;">'aspnet_Users'</span><span style="color: #66cc66;color: #000000;">&#41;</span><span style="color: #66cc66;color: #000000;">&#41;</span>
<span style="color: #993333; font-weight: bold;color: #000000;">BEGIN</span>
	<span style="color: #808080; font-style: italic;color: #008000;">--This indicates the presense of a the table </span>
        <span style="color: #808080; font-style: italic;color: #008000;">--as created by the Entity Framework Code First</span>
	<span style="color: #808080; font-style: italic;color: #008000;">--My EF schema does no use/create the full table </span>
        <span style="color: #808080; font-style: italic;color: #008000;">--so just drop it so that the script below will do its job.</span>
    <span style="color: #993333; font-weight: bold;color: #000000;">DROP</span> <span style="color: #993333; font-weight: bold;color: #000000;">TABLE</span> <span style="color: #66cc66;color: #000000;">&#91;</span>dbo<span style="color: #66cc66;color: #000000;">&#93;</span><span style="color: #66cc66;color: #000000;">.</span>aspnet_Users
<span style="color: #993333; font-weight: bold;color: #000000;">END</span>
&nbsp;
<span style="color: #993333; font-weight: bold;color: #000000;">IF</span> <span style="color: #66cc66;color: #000000;">&#40;</span><span style="color: #993333; font-weight: bold;color: #000000;">NOT</span> <span style="color: #993333; font-weight: bold;color: #000000;">EXISTS</span> <span style="color: #66cc66;color: #000000;">&#40;</span><span style="color: #993333; font-weight: bold;color: #000000;">SELECT</span> name
                <span style="color: #993333; font-weight: bold;color: #000000;">FROM</span> sysobjects
                <span style="color: #993333; font-weight: bold;color: #000000;">WHERE</span> <span style="color: #66cc66;color: #000000;">&#40;</span>name <span style="color: #66cc66;color: #000000;">=</span> N<span style="color: #ff0000;color: #800000;">'aspnet_Users'</span><span style="color: #66cc66;color: #000000;">&#41;</span>
                  <span style="color: #993333; font-weight: bold;color: #000000;">AND</span> <span style="color: #66cc66;color: #000000;">&#40;</span><span style="color: #993333; font-weight: bold;color: #000000;">TYPE</span> <span style="color: #66cc66;color: #000000;">=</span> <span style="color: #ff0000;color: #800000;">'U'</span><span style="color: #66cc66;color: #000000;">&#41;</span><span style="color: #66cc66;color: #000000;">&#41;</span><span style="color: #66cc66;color: #000000;">&#41;</span>
<span style="color: #993333; font-weight: bold;color: #000000;">BEGIN</span>
  PRINT <span style="color: #ff0000;color: #800000;">'Creating the aspnet_Users table...'</span>
<span style="color: #808080; font-style: italic;color: #008000;">/* Actual CREATE TABLE statement omitted for brevity...</span></pre></div></div>

<p>This was a hack, but this was rapid development of the &#8220;just get it working&#8221; sort.  And it worked great, until I added a new model that referenced a user, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> Record
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">int</span> RecordId <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> set<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> Guid UserId <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> set<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008000;color: #000000;">&#91;</span>Required<span style="color: #008000;color: #000000;">&#93;</span>
    <span style="color: #008000;color: #000000;">&#91;</span>ForeignKey<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;UserId&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">virtual</span> User Recorder <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> set<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008000;color: #000000;">&#91;</span>MaxLength<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #800000;">4000</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">string</span> Notes <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> set<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>This busted my hack because I could no longer just drop the table.  There was now a foreign key constraint that had to be deleted first.  While I could certainly patch my hack to drop the key too, with more foreign keys on the horizon, it was obvious that this approach was going to quickly become unmanageable.</p>
<p>The problem was that, especially for my automated tests, I wanted to drop and recreate the database regularly.  The right solution, therefore was to somehow execute the aspnet_regsql script after EF created the database itself, but before EF built out any tables.  If I could do that, EF was smart enough to just use the existing aspnet_User table.    </p>
<p>In my search for a solution, I found <a href="http://blogs.microsoft.co.il/blogs/gilf/archive/2011/05/30/creating-a-code-first-database-initializer-strategy.aspx" title="Creating a Code First Database Initializer Strategy" target="_blank">this article</a> which described how to create a database initializer that would just drop all the table and recreate them w/o dropping and recreating the database itself.  This seems promising but its approach to dropping all the tables didn&#8217;t account for foreign keys or any other objects that might need dropped too.  Modifying it to do so might be possible but could also become a real maintenance headache.  My concern was how complex the code would become to find and drop all dependencies which my code might create against the aspnet tables w/o dropping those that the aspnet_regsql script itself created.  It might be easy; it might not.  I still felt that simply dropping the whole database and rebuilding it was the best approach.  So I set out to find a way to inject some functionality between EF&#8217;s database creation and schema build out actions.  I <a href="http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/b8ea43cd-dee2-4e00-b16d-f38d4a417d9a" title=" Code First: How to Inject script between DB Creation and Model Build-out" target="_blank">posted on the EF forum</a> and got a suggestion to try migrations.  That wasn&#8217;t a bad idea, but still seemed more complicated than I wanted.</p>
<p>Going back to the blog on custom initialization strategies, I looked harder at the provided code, and googled around for some additional examples of custom strategies and in the end was able to come up with a custom initializer that did what I wanted.  It </p>
<ol>
<li>drops the database (conditionally or always, base on a constructor arg).</li>
<li>creates the database itself.</li>
<li>runs the aspnet_regsql script.</li>
<li>uses EF to build out the remaining schema.</li>
</ol>
<p>Without further adue, here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Data</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Data.Entity</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Data.Entity.Infrastructure</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Data.SqlClient</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Text.RegularExpressions</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Transactions</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> CreateDatabaseWithAspNetRegSql<span style="color: #008000;color: #000000;">&lt;</span>TContext<span style="color: #008000;color: #000000;">&gt;</span> 
    <span style="color: #008000;color: #000000;">:</span> IDatabaseInitializer<span style="color: #008000;color: #000000;">&lt;</span>TContext<span style="color: #008000;color: #000000;">&gt;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">where</span> TContext <span style="color: #008000;color: #000000;">:</span> DbContext
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">enum</span> CreationStrategy <span style="color: #008000;color: #000000;">&#123;</span> AlwaysCreate, CreateIfModelChanged <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">readonly</span> CreationStrategy _creationStrategy<span style="color: #008000;color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> CreateDatabaseWithAspNetRegSql<span style="color: #008000;color: #000000;">&#40;</span>
        CreationStrategy creationStrategy<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        _creationStrategy <span style="color: #008000;color: #000000;">=</span> creationStrategy<span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#region IDatabaseInitializer&lt;Context&gt; Members</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> InitializeDatabase<span style="color: #008000;color: #000000;">&#40;</span>TContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;color: #000080;">bool</span> dbExists<span style="color: #008000;color: #000000;">;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">new</span> TransactionScope<span style="color: #008000;color: #000000;">&#40;</span>TransactionScopeOption<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Suppress</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            dbExists <span style="color: #008000;color: #000000;">=</span> context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Exists</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>dbExists<span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>_creationStrategy <span style="color: #008000;color: #000000;">==</span> CreationStrategy<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CreateIfModelChanged</span>
                <span style="color: #008000;color: #000000;">&amp;&amp;</span> context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CompatibleWithModel</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">false</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span> 
                <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
            context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Delete</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
        CreateDatabase<span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        DbInitializer<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">DoAspNetRegSql</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        CreateTablesForModels<span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        Seed<span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SaveChanges</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region Private/Protected Methods</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> CreateDatabase<span style="color: #008000;color: #000000;">&#40;</span>TContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        var masterDbConnString <span style="color: #008000;color: #000000;">=</span> context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Connection</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ConnectionString</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Connection</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span>, <span style="color: #666666;color: #800000;">&quot;master&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;color: #008000;">//TODO: Find way to create db in an agnostic way.</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008000;color: #000000;">&#40;</span>var conn <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> SqlConnection<span style="color: #008000;color: #000000;">&#40;</span>masterDbConnString<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008000;color: #000000;">&#40;</span>var cmd <span style="color: #008000;color: #000000;">=</span> conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CreateCommand</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">&#123;</span>
                cmd<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CommandText</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">string</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;CREATE DATABASE [{0}]&quot;</span>, 
                    context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Connection</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
                cmd<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
            <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> DoAspNetRegSql<span style="color: #008000;color: #000000;">&#40;</span>Database database<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;color: #008000;">//TODO: This file name reference is a hack, </span>
        <span style="color: #008080; font-style: italic;color: #008000;">//need a better way of handling this!</span>
        ExecuteSqlSript<span style="color: #008000;color: #000000;">&#40;</span>database, <span style="color: #666666;">@&quot;C:\Users\Ken\Documents\Visual Studio 2010\Projects\MVCSandbox\_Resources\aspnet_regsql.sql&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">protected</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> ExecuteSqlSript
        <span style="color: #008000;color: #000000;">&#40;</span>Database database, <span style="color: #6666cc; font-weight: bold;color: #000080;">string</span> scriptPath<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        var conn <span style="color: #008000;color: #000000;">=</span> database<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Connection</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">State</span> <span style="color: #008000;color: #000000;">==</span> ConnectionState<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Closed</span><span style="color: #008000;color: #000000;">&#41;</span>
            conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        var fullScript <span style="color: #008000;color: #000000;">=</span> File<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;color: #000000;">&#40;</span>
            scriptPath<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;color: #000000;">foreach</span> <span style="color: #008000;color: #000000;">&#40;</span>var command <span style="color: #0600FF; font-weight: bold;color: #000000;">in</span> Regex<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;color: #000000;">&#40;</span>fullScript, <span style="color: #666666;">@&quot;\bGO\b&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            var cmd <span style="color: #008000;color: #000000;">=</span> conn<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CreateCommand</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
            cmd<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CommandText</span> <span style="color: #008000;color: #000000;">=</span> command<span style="color: #008000;color: #000000;">;</span>
            <span style="color: #0600FF; font-weight: bold;color: #000000;">try</span>
            <span style="color: #008000;color: #000000;">&#123;</span>
                cmd<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ExecuteNonQuery</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
            <span style="color: #008000;color: #000000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;color: #000000;">catch</span> <span style="color: #008000;color: #000000;">&#40;</span>Exception e<span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">&#123;</span>
                <span style="color: #0600FF; font-weight: bold;color: #000000;">throw</span> <span style="color: #008000;color: #000000;">new</span> ApplicationException<span style="color: #008000;color: #000000;">&#40;</span>e<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Message</span> <span style="color: #008000;color: #000000;">+</span> 
                    <span style="color: #666666;color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span><span style="color: #008080; font-weight: bold;">\r</span><span style="color: #008080; font-weight: bold;">\n</span>Command: &quot;</span> <span style="color: #008000;color: #000000;">+</span> command, e<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
            <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> CreateTablesForModels<span style="color: #008000;color: #000000;">&#40;</span>TContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        var modelBuildoutScript <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>IObjectContextAdapter<span style="color: #008000;color: #000000;">&#41;</span>context<span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ObjectContext</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CreateDatabaseScript</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        RemoveTableCreationCommandsForTablesCreatedByAspNetRegSql<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">ref</span> modelBuildoutScript<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Database</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ExecuteSqlCommand</span><span style="color: #008000;color: #000000;">&#40;</span>modelBuildoutScript<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">readonly</span> Regex __aspNetCreateTableCommandFinder 
        <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> Regex<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;">@&quot;create table \[dbo\]\.\[aspnet_\w+\][^;]*;&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> RemoveTableCreationCommandsForTablesCreatedByAspNetRegSql
        <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">ref</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">string</span> script<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        script <span style="color: #008000;color: #000000;">=</span> __aspNetCreateTableCommandFinder<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Replace</span><span style="color: #008000;color: #000000;">&#40;</span>script, <span style="color: #6666cc; font-weight: bold;color: #000080;">string</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
&nbsp;
    <span style="color: #008080;">#region Public Methods</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">protected</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">virtual</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> Seed<span style="color: #008000;color: #000000;">&#40;</span>TContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/integrating-asp-net-reg-sql-and-ef-code-first/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rebasing can fail with an updated .gitignore file.</title>
		<link>http://blog.baltrinic.com/software-development/rebasing-can-fail-with-an-updated-gitignore-file</link>
		<comments>http://blog.baltrinic.com/software-development/rebasing-can-fail-with-an-updated-gitignore-file#comments</comments>
		<pubDate>Wed, 07 Dec 2011 05:43:32 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Git]]></category>
		<category><![CDATA[rebase]]></category>
		<category><![CDATA[Version Control]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=190</guid>
		<description><![CDATA[When using version control and working on a branch other than the master, one should periodically pull any changes made to master into the working branch. If you are using Git, the preferred way to do this is with the rebase command rather than merge. (Here is a good explanation of the difference between the [...]]]></description>
			<content:encoded><![CDATA[<p>When using version control and working on a branch other than the master, one should periodically pull any changes made to master into the working branch.  If you are using Git, the preferred way to do this is with the rebase command rather than merge.  (<a href="http://gitguru.com/2009/02/03/rebase-v-merge-in-git/" title="GitGuru: Rebase v Merge in Git" target="_blank">Here is a good explanation</a> of the difference between the two and why rebase is usually preferred.)</p>
<p>However, rebase sometimes does not work as you would like.  I hit a good example of this this evening and wanted to document it.  The scenario is that of working on my own working branch of a rails project where we had just started using RubyMine.  I had created my working branch just prior to switching to RubyMine.  To my chagrin, I discovered that RubyMine had created a bunch of files to manage its view of the project in a folder named .idea, and I had committed them in my branch.</p>
<p>In the meantime another developer discovered the same issue and coincidentally added the .idea directory to .gitignore on master.  The natural thing to do, I thought, was to rebase my branch on master.  I was promptly told that I could not do this.  I failed to capture the exact error message, but basically the problem was that rebase was attempting to replay the addition of files to the .idea directory on top of a git repository that now called for the .idea directory to be ignored.</p>
<p>Because I only had a few commits on my branch, I decided the quick solution would be to create a new working branch based on the current master and then cherry-pick my commits from the old working branch onto the new one.</p>
<p>My first cherry-pick command resulted in a &#8220;commit not found error.&#8221;  I thought this was very odd, but in examining the contents of the commit I was attempting to cherry-pick, it turns out that it only contained files in the .idea directory. So it seems a better error message would have been &#8220;nothing found in the commit.&#8221;  </p>
<p>Seeing as I could safely omit that commit, I went on to cherry-pick the remaining ones.  Each failed initially due to merge errors, because each attempted to modify files under .idea.  To solve this I had to execute a git rm -f &lt;file&gt; command for each such file to resolve the conflict.</p>
<p>After resolving the conflict, I followed the onscreen instructions that were given at the point that the cherry-pick failed.  Namely to run the commit -c &lt;hash&gt; command specifying the hash of the commit I had cherry-picked.  </p>
<p>The -c option creates a new commit with the same message and author as the one referenced by the hash.  I had not seen this option before and as it turns out I really wanted to use the capitalized -C version.  Both do the same thing but the lowercase version throws you into vi to edit the commit message where as the uppercase one uses the commit message as-is.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/rebasing-can-fail-with-an-updated-gitignore-file/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fluent APIs Violate the Open/Closed Principle</title>
		<link>http://blog.baltrinic.com/software-development/fluent-apis-violate-the-openclosed-principle</link>
		<comments>http://blog.baltrinic.com/software-development/fluent-apis-violate-the-openclosed-principle#comments</comments>
		<pubDate>Sun, 20 Nov 2011 17:40:36 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Fluent Interfaces]]></category>
		<category><![CDATA[OCP]]></category>
		<category><![CDATA[Open Closed Principle]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=138</guid>
		<description><![CDATA[In a recent design session, we were discussing the API for an event bus that supports asynchronous RPC. A colleague of mine proposed a fluent interface. A simplified version of it looked roughly like this: bus.for&#40;event&#41;.replyTo&#40;handler&#41;.withATimeOutOf&#40;500&#41;.Send&#40;&#41;; The proposal lead to some debate because some members of the team liked the idea and others preferred the [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent design session, we were discussing the API for an event bus that supports asynchronous RPC. A colleague of mine proposed a fluent interface.  A simplified version of it looked roughly like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;">bus<span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">for</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">event</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">replyTo</span><span style="color: #008000;color: #000000;">&#40;</span>handler<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">withATimeOutOf</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #800000;">500</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span></pre></div></div>

<p>The proposal lead to some debate because some members of the team liked the idea and others preferred the more traditional approach of using overloaded methods as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;">bus<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">event</span>, replyHandler<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
bus<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">event</span>, replyHandler, timeout<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span></pre></div></div>

<p>In the end it was agreed that the traditional API was essential, and if some of the team wanted a fluent API, one could be written as a wrapper around it.  </p>
<p>I think this was the right call and the point of this post is to explain why.  One of the reasons that was given against the fluent interface was that they are very hard to extend if you do not have the ability to modify the original source.  In short, they violate the <a href="/software-development/agile-practices/do-one-thing-is-prerequisite-for-open-closed-principle" title="My previous of OCP.">Open/Closed Principle</a>, one of the core principles of <a href="http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod" title="Robert C. Martin's article on the SOLID principles." target="_blank">SOLID</a> object oriented design.</p>
<p>Lets take a look at why.  </p>
<p>Here is a pseudo implementation of the fluent API.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> Builder
<span style="color: #008000;color: #000000;">&#123;</span>
    Builder <span style="color: #0600FF; font-weight: bold;color: #000000;">for</span><span style="color: #008000;color: #000000;">&#40;</span>IEvent <span style="color: #0600FF; font-weight: bold;color: #000000;">event</span><span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #008000;color: #000000;">&#125;</span>
    Builder withReplyTo<span style="color: #008000;color: #000000;">&#40;</span>IHandler handler<span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #008000;color: #000000;">&#125;</span>
    Builder withATimeOutOf<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #6666cc; font-weight: bold;color: #000080;">int</span> milliseconds<span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> send<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>Omitted is the operative code that would collect the various arguments and ultimately invoke the base API in Send().  What remains is just what is needed to do the fluent call chaining, namely returning an instance of the builder itself.  It is this return value that violates OCP. </p>
<p>Suppose a third party wants to add a second event handler to be invoked in case of a timeout.  We will presume the base Bus class is otherwise well factored and adheres to OCP.  Because of this, adding a new Send overload to the traditional API can be achieved by subclassing the base implementation.  Invoking the new method will look like this.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;">bus<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Send</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">event</span>, replyHanlder, timeout, timeoutHandler<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span></pre></div></div>

<p>But what happens if we try to extend our fluent API by subclassing the Builder?</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> NewBuilder <span style="color: #008000;color: #000000;">:</span> Builder 
<span style="color: #008000;color: #000000;">&#123;</span>
    NewBuilder andHandleTimeoutsWith<span style="color: #008000;color: #000000;">&#40;</span>ITimeoutHanlder handler<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span> 
        <span style="color: #008000;color: #000000;">...</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">this</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>Now we see the problem with the base class methods returning Builder.  From our subclass, even if the return value is actually an instance of MyBuilder, it is returned by the inherited members as Builder.  In a statically typed language, the new method is not accessible without a very un-fluent explicit cast.</p>
<h2>A Generic Solution?</h2>
<p>In a simple API, if the original designers are prescient enough, one might get around this by using generics.  Suppose the orignal builder had been written generically.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> Builder<span style="color: #008000;color: #000000;">&lt;</span>T<span style="color: #008000;color: #000000;">&gt;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">where</span> T <span style="color: #008000;color: #000000;">:</span> Builder
<span style="color: #008000;color: #000000;">&#123;</span>
    T <span style="color: #0600FF; font-weight: bold;color: #000000;">for</span><span style="color: #008000;color: #000000;">&#40;</span>IEvent <span style="color: #0600FF; font-weight: bold;color: #000000;">event</span><span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #008000;color: #000000;">&#125;</span>
    T andReplyTo<span style="color: #008000;color: #000000;">&#40;</span>IHandler handler<span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #008000;color: #000000;">&#125;</span>
    T <span style="color: #0600FF; font-weight: bold;color: #000000;">for</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #6666cc; font-weight: bold;color: #000080;">int</span> milliseconds<span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> send<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&#123;</span> <span style="color: #008000;color: #000000;">...</span> <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>As can be seen, we still need the non-generic Builder so that the generic one can be instantiated in terms of it.  If we would normally access the fluent API like so: bus.GetFluentBus&lt;Builder&gt;(), we could then access our extended API like this: bus.GetFluentBus&lt;MyBuilder&gt;().  </p>
<p>This approach gives us a builder that complies with OCP.  The down side is we have muddied up our GetFluentBus method with a generic argument just to allow for what is likely to be an edge case.  But supporting edge cases is what extensibility is all about.</p>
<p>A bigger problem is that the generic approach only works well for a simple, single-builder API. Most fluent interfaces use a set of builder classes and/or interfaces in order to limit the methods available at any point in the call chain to those that make sense.  In this scenario, genericizing the API gets ugly.  Every builder will need to be generic not only with respect to itself but with respect to every builder that it returns directly or indirectly, and our GetFluentBus method is going need generic arguments for all of them.  Consequently, generics are not a good general purpose solution.</p>
<h2>Conclusion</h2>
<p>Fluent APIs do have an appeal and a legitimate place in software design.  However, they are not extensible and therefore should never be the only means of doing something. Rather they need to be seen as an augmentation and approached with the understanding that their purpose is to make the common use cases fluent for those who want the option.   </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/fluent-apis-violate-the-openclosed-principle/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Change your App.Config Location file at Run Time</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/how-to-change-your-app-config-location-file-at-run-time</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/how-to-change-your-app-config-location-file-at-run-time#comments</comments>
		<pubDate>Thu, 04 Aug 2011 00:43:28 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[AppConfig]]></category>
		<category><![CDATA[Automated Testing]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=132</guid>
		<description><![CDATA[Need to change which .config file your application is using on the fly?  Here is how.  Great for automated integration testing.]]></description>
			<content:encoded><![CDATA[<p>This is one of those pieces of functionality which I have been in search of for a long time.  When writing integration tests and other automated tests, you frequently want to test with different application configurations. Unfortunately, switching App.Config files on the fly is not something that the .Net framework supports.  There are some alternative approaches to this problem which rely on dependency injection or using AppDomains.  Depending on what you are trying to accomplish, these are not always simple or satisfactory techniques.  This is especially the case when performing integration tests with third party components that rely on app.config settings and you want to test out alternatives of those settings.</p>
<p>But today, I found <a href="http://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime" target="_blank" title="Change default app.config at runtime">this great solution</a> provided by Daniel Hilgarth on StackOverflow.  It is something of a hack in that it relies on mucking with the internals of how AppDomains work.  I don&#8217;t think I would recommend it for production code, but for automated testing, its just what the doctor ordered.  </p>
<p>Here is my slightly modified version of the code, as well as a usage example.  Please take a look at the example in the article as well, you may like it better.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Configuration</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Reflection</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> AlternateAppConfig <span style="color: #008000;color: #000000;">:</span> IDisposable
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">readonly</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">string</span> oldConfig <span style="color: #008000;color: #000000;">=</span>
        AppDomain<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CurrentDomain</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetData</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;APP_CONFIG_FILE&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">bool</span> disposedValue<span style="color: #008000;color: #000000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> AlternateAppConfig<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #6666cc; font-weight: bold;color: #000080;">string</span> path<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        AppDomain<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CurrentDomain</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SetData</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;APP_CONFIG_FILE&quot;</span>, path<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        ResetConfigMechanism<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> Dispose<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">!</span>disposedValue<span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            AppDomain<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">CurrentDomain</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SetData</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;APP_CONFIG_FILE&quot;</span>,oldConfig<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
            ResetConfigMechanism<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
&nbsp;
            disposedValue <span style="color: #008000;color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">true</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
        GC<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SuppressFinalize</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">this</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> ResetConfigMechanism<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #008000;color: #000000;">typeof</span><span style="color: #008000;color: #000000;">&#40;</span>ConfigurationManager<span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;s_initState&quot;</span>, BindingFlags<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;color: #000000;">|</span>
                                        BindingFlags<span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">Static</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">null</span>, <span style="color: #FF0000;color: #800000;">0</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        <span style="color: #008000;color: #000000;">typeof</span><span style="color: #008000;color: #000000;">&#40;</span>ConfigurationManager<span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;s_configSystem&quot;</span>, BindingFlags<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;color: #000000;">|</span>
                                        BindingFlags<span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">Static</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">null</span>, <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        <span style="color: #008000;color: #000000;">typeof</span><span style="color: #008000;color: #000000;">&#40;</span>ConfigurationManager<span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Assembly</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetTypes</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">Where</span><span style="color: #008000;color: #000000;">&#40;</span>x <span style="color: #008000;color: #000000;">=&gt;</span> x<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">FullName</span> <span style="color: #008000;color: #000000;">==</span>
                        <span style="color: #666666;color: #800000;">&quot;System.Configuration.ClientConfigPaths&quot;</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;s_current&quot;</span>, BindingFlags<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;color: #000000;">|</span>
                                    BindingFlags<span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">Static</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SetValue</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">null</span>, <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>And the usage:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #008000;color: #000000;">&#91;</span>Test<span style="color: #008000;color: #000000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> Application_should_behave_differently_with_alternate_config<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">new</span> AlternateAppConfig<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;">@&quot;Path\to\Alternate.App.Config&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;color: #008000;">//Your test goes here</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008080; font-style: italic;color: #008000;">//The original configuration is restored upon disposal of the alternate.</span></pre></div></div>

<p>Happy testing!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/how-to-change-your-app-config-location-file-at-run-time/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Log4Net Integration with Unity IoC Container</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/log4net-integration-with-unity-ioc-container</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/log4net-integration-with-unity-ioc-container#comments</comments>
		<pubDate>Fri, 29 Jul 2011 01:18:53 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Castle Windsor]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Log4Net]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=128</guid>
		<description><![CDATA[How to configure Microsoft's Unity 2.0 IoC Container to inject Log4Net loggers that are initialized with the type of the class into which they are being injected.  Also supports direct resolution of loggers.]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.castleproject.org/" target="_blank" title="Castle Project Home Page">Castle Project</a> has a great <a href="http://docs.castleproject.org/Windsor.Logging-Facility.ashx" target="_blank" title="Castle Logging Integration Facility Page">logging integration facility</a> that lets you integrate <a href="http://logging.apache.org/log4net/" target="_blank" title="Log4Net Project Home Page">Log4Net</a> (or <a href="http://nlog-project.org/" target="_blank" title="NLog Project Home Page">NLog</a>) into your dependency resolution such that if you declare a dependency on ILogger (Castles wrapper around Log4Net.ILog), the dependancy that gets injected is initialized with the type into which it is being injected.  If you understand how Log4Net logger names work, you&#8217;ll know why this is important.</p>
<p>But on my current project, I am not using Castle; I am using Microsoft&#8217;s Unity IoC container.  Figuring out how to do the same thing in Unity took me most of a day and I didn&#8217;t like my solution, so I <a href="http://stackoverflow.com/questions/6846342/how-to-inject-log4net-ilog-implementations-using-unity-2-0" target="_blank">posted a question on Stack Overflow</a> and got a response that pointed me to exactly what I wanted.  The links were to a <a href="http://davidkeaveny.blogspot.com/2011/03/unity-and-log4net.html" title="Unity and log4net" target="_blank">blog post</a> and ultimately to <a href="http://unity.codeplex.com/discussions/203744" target="_blank">an obscure thread</a> on the Unity &#8211; Patern&#8217;s and Practices forum.  I find that forums are notorious for not being there for the long haul, and the blog did not provide any details on its own, so I am re-posting it it all here in a slightly cleaned up form for posterity&#8217;s sake.</p>
<p>Implementing this in Unity requires three steps.</p>
<ol>
<li>Creating a Build Tracking Extension</li>
<li>Creating a Logger Creation Extension</li>
<li>Wire the extensions into Unity</li>
</ol>
<h2>The Build Tracking Extension</h2>
<p>The first step is to a create Unity extension that installs a build tracking strategy.  This strategy uses a policy to keep track of the stack of dependencies being built as part of handling the current resolution request.  This is needed so that when resolving the logger dependency, we can consult the stack to see what type the dependency is being resolved for.</p>
<p>Here is the code, pretty much taken straight from the aforementioned post.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Collections.Generic</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">Microsoft.Practices.ObjectBuilder2</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">Microsoft.Practices.Unity</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">Microsoft.Practices.Unity.ObjectBuilder</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> BuildTracking <span style="color: #008000;color: #000000;">:</span> UnityContainerExtension
<span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">protected</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">override</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> Initialize<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        Context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Strategies</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">AddNew</span><span style="color: #008000;color: #000000;">&lt;</span>BuildTrackingStrategy<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span>UnityBuildStage<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">TypeMapping</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> IBuildTrackingPolicy GetPolicy<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Policies</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;color: #000000;">&lt;</span>IBuildTrackingPolicy<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKey</span>, <span style="color: #0600FF; font-weight: bold;color: #000000;">true</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> IBuildTrackingPolicy SetPolicy<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        IBuildTrackingPolicy policy <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> BuildTrackingPolicy<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Policies</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SetDefault</span><span style="color: #008000;color: #000000;">&#40;</span>policy<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> policy<span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> BuildTrackingStrategy <span style="color: #008000;color: #000000;">:</span> BuilderStrategy
<span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">override</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> PreBuildUp<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        var policy <span style="color: #008000;color: #000000;">=</span> BuildTracking<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetPolicy</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">??</span> BuildTracking<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">SetPolicy</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
        policy<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKeys</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Push</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKey</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">override</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> PostBuildUp<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        IBuildTrackingPolicy policy <span style="color: #008000;color: #000000;">=</span> BuildTracking<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetPolicy</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>policy <span style="color: #008000;color: #000000;">!=</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span><span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&amp;&amp;</span> <span style="color: #008000;color: #000000;">&#40;</span>policy<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKeys</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;color: #000000;">&gt;</span> <span style="color: #FF0000;color: #800000;">0</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            policy<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKeys</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Pop</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">interface</span> IBuildTrackingPolicy <span style="color: #008000;color: #000000;">:</span> IBuilderPolicy
<span style="color: #008000;color: #000000;">&#123;</span>
    Stack<span style="color: #008000;color: #000000;">&lt;</span><span style="color: #6666cc; font-weight: bold;color: #000080;">object</span><span style="color: #008000;color: #000000;">&gt;</span> BuildKeys <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> BuildTrackingPolicy <span style="color: #008000;color: #000000;">:</span> IBuildTrackingPolicy
<span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> BuildTrackingPolicy<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        BuildKeys <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> Stack<span style="color: #008000;color: #000000;">&lt;</span><span style="color: #6666cc; font-weight: bold;color: #000080;">object</span><span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> Stack<span style="color: #008000;color: #000000;">&lt;</span><span style="color: #6666cc; font-weight: bold;color: #000080;">object</span><span style="color: #008000;color: #000000;">&gt;</span> BuildKeys <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> set<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<h2>The Logger Creation Extension</h2>
<p>With the build tracking extension in place, it is now possible to create a logger creation extension that will use it to create an instance of ILog.  (This implemenation is not generic like the Castle implemenation but specific to Log4Net).</p>
<p>Again here is the code, pretty much straight from the forum thread.  All I have done is incorporated the patch (also from the thread) to handle direct resolution requests for an ILog instance.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Diagnostics</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">System.Linq</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">Microsoft.Practices.ObjectBuilder2</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">Microsoft.Practices.Unity</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">Microsoft.Practices.Unity.ObjectBuilder</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">using</span> <span style="color: #008080;">Log4Net</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> LogCreation <span style="color: #008000;color: #000000;">:</span> UnityContainerExtension
<span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">protected</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">override</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> Initialize<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        Context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Strategies</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">AddNew</span><span style="color: #008000;color: #000000;">&lt;</span>LogCreationStrategy<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span>UnityBuildStage<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">PreCreation</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> LogCreationStrategy <span style="color: #008000;color: #000000;">:</span> BuilderStrategy
<span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">bool</span> IsPolicySet <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> set<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">override</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> PreBuildUp<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        Type typeToBuild <span style="color: #008000;color: #000000;">=</span> context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKey</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Type</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">typeof</span><span style="color: #008000;color: #000000;">&#40;</span>ILog<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Equals</span><span style="color: #008000;color: #000000;">&#40;</span>typeToBuild<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Policies</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Get</span><span style="color: #008000;color: #000000;">&lt;</span>IBuildPlanPolicy<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKey</span><span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">==</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">&#123;</span>
                Type typeForLog <span style="color: #008000;color: #000000;">=</span> LogCreationStrategy<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetLogType</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
                IBuildPlanPolicy policy <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> LogBuildPlanPolicy<span style="color: #008000;color: #000000;">&#40;</span>typeForLog<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
                context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Policies</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Set</span><span style="color: #008000;color: #000000;">&lt;</span>IBuildPlanPolicy<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span>policy, context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKey</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
                IsPolicySet <span style="color: #008000;color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">true</span><span style="color: #008000;color: #000000;">;</span>
            <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">override</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> PostBuildUp<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>IsPolicySet<span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Policies</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Clear</span><span style="color: #008000;color: #000000;">&lt;</span>IBuildPlanPolicy<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKey</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
            IsPolicySet <span style="color: #008000;color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">false</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> Type GetLogType<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        Type logType <span style="color: #008000;color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span> <span style="color: #008000;color: #000000;">;</span>  
        IBuildTrackingPolicy buildTrackingPolicy <span style="color: #008000;color: #000000;">=</span> BuildTracking<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetPolicy</span><span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>buildTrackingPolicy <span style="color: #008000;color: #000000;">!=</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span><span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">&amp;&amp;</span> <span style="color: #008000;color: #000000;">&#40;</span>buildTrackingPolicy<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKeys</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Count</span> <span style="color: #008000;color: #000000;">&gt;=</span> <span style="color: #FF0000;color: #800000;">2</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            logType <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>NamedTypeBuildKey<span style="color: #008000;color: #000000;">&#41;</span>buildTrackingPolicy<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">BuildKeys</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">ElementAt</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #FF0000;color: #800000;">1</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Type</span><span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">else</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            StackTrace stackTrace <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> StackTrace<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
            <span style="color: #008080; font-style: italic;color: #008000;">//first two are in the log creation strategy, can skip over them</span>
            <span style="color: #0600FF; font-weight: bold;color: #000000;">for</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #6666cc; font-weight: bold;color: #000080;">int</span> i <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">2</span><span style="color: #008000;color: #000000;">;</span> i <span style="color: #008000;color: #000000;">&lt;</span> stackTrace<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">FrameCount</span><span style="color: #008000;color: #000000;">;</span> i<span style="color: #008000;color: #000000;">++</span><span style="color: #008000;color: #000000;">&#41;</span>
            <span style="color: #008000;color: #000000;">&#123;</span>
                StackFrame frame <span style="color: #008000;color: #000000;">=</span> stackTrace<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetFrame</span><span style="color: #008000;color: #000000;">&#40;</span>i<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
                logType <span style="color: #008000;color: #000000;">=</span> frame<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetMethod</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">DeclaringType</span><span style="color: #008000;color: #000000;">;</span>
                <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">!</span>logType<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">FullName</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">StartsWith</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #666666;color: #800000;">&quot;Microsoft.Practices&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span>
                <span style="color: #008000;color: #000000;">&#123;</span>
                    <span style="color: #0600FF; font-weight: bold;color: #000000;">break</span><span style="color: #008000;color: #000000;">;</span>
                <span style="color: #008000;color: #000000;">&#125;</span>
            <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> logType<span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">class</span> LogBuildPlanPolicy <span style="color: #008000;color: #000000;">:</span> IBuildPlanPolicy
<span style="color: #008000;color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> LogBuildPlanPolicy<span style="color: #008000;color: #000000;">&#40;</span>Type logType<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        LogType <span style="color: #008000;color: #000000;">=</span> logType<span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> Type LogType <span style="color: #008000;color: #000000;">&#123;</span> get<span style="color: #008000;color: #000000;">;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> set<span style="color: #008000;color: #000000;">;</span> <span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> BuildUp<span style="color: #008000;color: #000000;">&#40;</span>IBuilderContext context<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Existing</span> <span style="color: #008000;color: #000000;">==</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">&#123;</span>
            ILog log <span style="color: #008000;color: #000000;">=</span> LogManager<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetLogger</span><span style="color: #008000;color: #000000;">&#40;</span>LogType<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
            context<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Existing</span> <span style="color: #008000;color: #000000;">=</span> log<span style="color: #008000;color: #000000;">;</span>
        <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<h2>Wiring It All Up</h2>
<p>Lastly, to wire this up to your container all you need is the following (also stolen) code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;">var container <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> UnityContainer<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">AddNewExtension</span><span style="color: #008000;color: #000000;">&lt;</span>BuildTracking<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">AddNewExtension</span><span style="color: #008000;color: #000000;">&lt;</span>LogCreation<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Register</span><span style="color: #008000;color: #000000;">&lt;</span>MyClassWithLoggerDependency<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
var myClass <span style="color: #008000;color: #000000;">=</span> container<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Resolve</span><span style="color: #008000;color: #000000;">&lt;</span>MyClassWithLoggerDependency<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;color: #008000;">//Or</span>
var myLogger <span style="color: #008000;color: #000000;">=</span> container<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Resolve</span><span style="color: #008000;color: #000000;">&lt;</span>ILog<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>  <span style="color: #008080; font-style: italic;color: #008000;">//return logger with a name of this.GetType().Fullname</span></pre></div></div>

<p>Hope this code helps you as much as it did me.  Many thanks to <a href="http://stackoverflow.com/users/125439/wageoghe" target="_blank" title="wageoghe's Stack Overflow profile.">wageoghe</a>, <a href="http://davidkeaveny.blogspot.com/" target="_blank" title="David Keaveny's blog.">David Keaveny</a>, <a href="http://www.codeplex.com/site/users/view/marcoerni" target="_blank" title="Marco's Codeplex profile.">Marco</a> and <a href="http://www.codeplex.com/site/users/view/ThunderEagle" target="_blank" title="Scott's Codeplex profile.">Scott</a>!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/log4net-integration-with-unity-ioc-container/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Automatically Selecting an Available TCP Port</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/auto-select-available-tcp-port</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/auto-select-available-tcp-port#comments</comments>
		<pubDate>Wed, 20 Jul 2011 01:39:50 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[TCP]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=121</guid>
		<description><![CDATA[Code snippet for automatically selecting an unused local TCP port.]]></description>
			<content:encoded><![CDATA[<p>If you are doing low level TCP networking in .Net and in particular are using a TcpListener on a client to listen for callbacks form a server, you need to select a port on which to listen.  Typically, in this scenario, you don&#8217;t want to have to specify a port in the .config file.  You just want to pick any open port (well actually, any open port, in the IANA private port range).</p>
<p>After looking around the net, I found some almost correct but not quite code for this, so I though I should post my improved version here.</p>
<p>The below function will return the first open port in the IANA port range that is not currently being used on the local machine.  </p>
<p>Be careful however.  Just because the port is not used at the point the code is called, doesn&#8217;t mean it won&#8217;t be in use when you go to open your listener.  Just incase some other process grabbed the port in the interim, you should always open your listener in a try catch block and if the call fails because the port is in use, grab another available port and try again.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">private</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">int</span> SelectAvailablePort<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">const</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">int</span> START_OF_IANA_PRIVATE_PORT_RANGE <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">49152</span><span style="color: #008000;color: #000000;">;</span>
    var ipGlobalProperties <span style="color: #008000;color: #000000;">=</span> IPGlobalProperties<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetIPGlobalProperties</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    var tcpListeners <span style="color: #008000;color: #000000;">=</span> ipGlobalProperties<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetActiveTcpListeners</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    var tcpConnections <span style="color: #008000;color: #000000;">=</span> ipGlobalProperties<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GetActiveTcpConnections</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    var allInUseTcpPorts <span style="color: #008000;color: #000000;">=</span> tcpListeners<span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">Select</span><span style="color: #008000;color: #000000;">&#40;</span>tcpl <span style="color: #008000;color: #000000;">=&gt;</span> tcpl<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Port</span><span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Union</span><span style="color: #008000;color: #000000;">&#40;</span>tcpConnections<span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">Select</span><span style="color: #008000;color: #000000;">&#40;</span>tcpi <span style="color: #008000;color: #000000;">=&gt;</span> tcpi<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">LocalEndPoint</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Port</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    var orderedListOfPrivateInUseTcpPorts <span style="color: #008000;color: #000000;">=</span> allInUseTcpPorts
        <span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">Where</span><span style="color: #008000;color: #000000;">&#40;</span>p <span style="color: #008000;color: #000000;">=&gt;</span> p <span style="color: #008000;color: #000000;">&gt;=</span> START_OF_IANA_PRIVATE_PORT_RANGE<span style="color: #008000;color: #000000;">&#41;</span>
        <span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">OrderBy</span><span style="color: #008000;color: #000000;">&#40;</span>p <span style="color: #008000;color: #000000;">=&gt;</span> p<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
&nbsp;
    var candidatePort <span style="color: #008000;color: #000000;">=</span> START_OF_IANA_PRIVATE_PORT_RANGE<span style="color: #008000;color: #000000;">;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">foreach</span> <span style="color: #008000;color: #000000;">&#40;</span>var usedPort <span style="color: #0600FF; font-weight: bold;color: #000000;">in</span> orderedListOfPrivateInUseTcpPorts<span style="color: #008000;color: #000000;">&#41;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span> <span style="color: #008000;color: #000000;">&#40;</span>usedPort <span style="color: #008000;color: #000000;">!=</span> candidatePort<span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">break</span><span style="color: #008000;color: #000000;">;</span>
        candidatePort<span style="color: #008000;color: #000000;">++;</span>
    <span style="color: #008000;color: #000000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> candidatePort<span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>Disclaimer: I am by no means an network programming guru.  If you find issues with this snippet or have a better one, please let me know.  </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/auto-select-available-tcp-port/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing for Memory Leaks</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/unit-testing-for-memory-leaks</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/unit-testing-for-memory-leaks#comments</comments>
		<pubDate>Sat, 11 Jun 2011 03:10:36 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[Automated Testing]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Unit Tests]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=119</guid>
		<description><![CDATA[ Beautifully simply solution for asserting that a class doesn't hold a reference to an object that it should not.  ]]></description>
			<content:encoded><![CDATA[<p>I just found this gem in <a href="http://kozmic.pl/2009/01/27/castle-dynamic-proxy-tutorial-part-iv-breaking-hard-dependencies" target="_blank">a post on another subject</a> on Krzysztof Koźmic&#8217;s blog and thought it merited its own post.  Beautifully simply solution for asserting that a class doesn&#8217;t hold a reference to an object that it should not.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #008000;color: #000000;">&#91;</span>Fact<span style="color: #008000;color: #000000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> Freezable_should_not_hold_any_reference_to_created_objects<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    var pet <span style="color: #008000;color: #000000;">=</span> Freezable<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">MakeFreezable</span><span style="color: #008000;color: #000000;">&lt;</span>Pet<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    var petWeakReference <span style="color: #008000;color: #000000;">=</span> <span style="color: #008000;color: #000000;">new</span> WeakReference<span style="color: #008000;color: #000000;">&#40;</span>pet, <span style="color: #0600FF; font-weight: bold;color: #000000;">false</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    pet <span style="color: #008000;color: #000000;">=</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">null</span><span style="color: #008000;color: #000000;">;</span>
    GC<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Collect</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
    Assert<span style="color: #008000;color: #000000;">.</span><span style="color: #0600FF; font-weight: bold;color: #000000;">False</span><span style="color: #008000;color: #000000;">&#40;</span>petWeakReference<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">IsAlive</span>, <span style="color: #666666;color: #800000;">&quot;Object should have been collected&quot;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/unit-testing-for-memory-leaks/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Add Decorator Chaining Support to Unity IoC</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/add-decorator-chaining-support-to-unity-ioc</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/add-decorator-chaining-support-to-unity-ioc#comments</comments>
		<pubDate>Sat, 02 Apr 2011 12:26:06 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[Castle Windsor]]></category>
		<category><![CDATA[Decorator Pattern]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Unity]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=130</guid>
		<description><![CDATA[This is just a quick plug for a post on Jim Christopher&#8217;s blog, beefycode.com, that explains how to create a Unity Extension to support IoC Chaining in much the same way as Castle Windsor does. I am going to need this someday and don&#8217;t want to loose the link. Thanks Jim.]]></description>
			<content:encoded><![CDATA[<p>This is just a quick plug for a <a href="http://www.beefycode.com/post/Decorator-Unity-Container-Extension.aspx" target="_blank" title="decorator unity container extension">post</a> on Jim Christopher&#8217;s blog, <a href="http://www.beefycode." target="_blank">beefycode.com</a>, that explains how to create a Unity Extension to support IoC Chaining in much the same way as Castle Windsor does.  I am going to need this someday and don&#8217;t want to loose the link.  Thanks Jim.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/add-decorator-chaining-support-to-unity-ioc/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Extension Methods and Lambda Expressions to create some Syntactical Sugar</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/using-extension-methods-and-lambda-expressions-to-create-some-syntactical-sugar</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/using-extension-methods-and-lambda-expressions-to-create-some-syntactical-sugar#comments</comments>
		<pubDate>Thu, 24 Mar 2011 22:23:18 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Lambda Expressions]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=126</guid>
		<description><![CDATA[How to use extension methods on System.Object to create short cuts to common coding patterns.]]></description>
			<content:encoded><![CDATA[<p>This post grows out of <a href="/software-development/dotnet/c-equivalent-for-visual-basic-with-statement" target="_blank" title="A C# Equivalent to the Visual Basic With Statement">my previous post on creating a C# equivalent to the Visual Basic With statement</a>.  I have since come up with a hand full of other lambda-accepting extension methods that do other handy things.</p>
<h3>Improving on the With Statement</h3>
<p>First off, the original with function can be extended to return values if need be as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> TReturn With<span style="color: #008000;color: #000000;">&lt;</span>T,TReturn<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> T obj, Func<span style="color: #008000;color: #000000;">&lt;</span>T, TReturn<span style="color: #008000;color: #000000;">&gt;</span> action<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> action<span style="color: #008000;color: #000000;">&#40;</span>obj<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<h3>The <i>As</i> Method</h3>
<p>The following As methods are very handy when you have a variable that is of one type but you need to perform some actions on it in the context of another type which the it also implements.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">As</span><span style="color: #008000;color: #000000;">&lt;</span>T<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">object</span> obj, Action<span style="color: #008000;color: #000000;">&lt;</span>T<span style="color: #008000;color: #000000;">&gt;</span> action<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    action<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>T<span style="color: #008000;color: #000000;">&#41;</span> obj<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> TReturn <span style="color: #0600FF; font-weight: bold;color: #000000;">As</span><span style="color: #008000;color: #000000;">&lt;</span>T,TReturn<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">object</span> obj, Func<span style="color: #008000;color: #000000;">&lt;</span>T, TReturn<span style="color: #008000;color: #000000;">&gt;</span> action<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> action<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>T<span style="color: #008000;color: #000000;">&#41;</span> obj<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<h3><i>IfIs</i>, the Conditional form of <i>As</i></h3>
<p>What if you are not sure if the type you have can be cast to the other type?  The following IfIs methods are just like the As method but only execute the code if the cast can be made.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> IfIs<span style="color: #008000;color: #000000;">&lt;</span>T<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">object</span> obj, Action<span style="color: #008000;color: #000000;">&lt;</span>T<span style="color: #008000;color: #000000;">&gt;</span> action<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">if</span><span style="color: #008000;color: #000000;">&#40;</span>obj <span style="color: #008000;color: #000000;">is</span> T<span style="color: #008000;color: #000000;">&#41;</span>
        action<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>T<span style="color: #008000;color: #000000;">&#41;</span> obj<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> TReturn IfIs<span style="color: #008000;color: #000000;">&lt;</span>T,TReturn<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">object</span> obj, Func<span style="color: #008000;color: #000000;">&lt;</span>T, TReturn<span style="color: #008000;color: #000000;">&gt;</span> action<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;color: #000000;">return</span> obj <span style="color: #008000;color: #000000;">is</span> T <span style="color: #008000;color: #000000;">?</span> action<span style="color: #008000;color: #000000;">&#40;</span><span style="color: #008000;color: #000000;">&#40;</span>T<span style="color: #008000;color: #000000;">&#41;</span>obj<span style="color: #008000;color: #000000;">&#41;</span> <span style="color: #008000;color: #000000;">:</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">default</span><span style="color: #008000;color: #000000;">&#40;</span>TReturn<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>Again I can see that this sort of syntactical sugar may drive some people nuts. Others may just not see the need.  But for some of us, this sort of thing makes it easier to write expressive code.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/using-extension-methods-and-lambda-expressions-to-create-some-syntactical-sugar/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A C# Equivalent to the Visual Basic With Statement</title>
		<link>http://blog.baltrinic.com/software-development/dotnet/c-equivalent-for-visual-basic-with-statement</link>
		<comments>http://blog.baltrinic.com/software-development/dotnet/c-equivalent-for-visual-basic-with-statement#comments</comments>
		<pubDate>Mon, 27 Dec 2010 12:13:01 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Lambda Expressions]]></category>
		<category><![CDATA[Visual Basic]]></category>

		<guid isPermaLink="false">http://blog.baltrinic.com/?p=124</guid>
		<description><![CDATA[I moved from Visual Basic to C# when I migrated to .Net back on &#8217;04. I have never regretted this. C# is so much nicer a language in my opinion. One of the things I especially like about C# is its conciseness. I hate typing. The one and only thing I missed from VB was [...]]]></description>
			<content:encoded><![CDATA[<p>I moved from Visual Basic to C# when I migrated to .Net back on &#8217;04.  I have never regretted this.  C# is so much nicer a language in my opinion.  One of the things I especially like about C# is its conciseness.  I hate typing.  </p>
<p>The one and only thing I missed from VB was the WITH statement.  It drove me <em>nuts</em> that in C# for all its brevity, if I needed to do half a dozen things with  Object.Child.Grandchild.GreatGrandchild, I had to write that whole thing out or put GreatGrandchild in a local variable like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #6666cc; font-weight: bold;color: #000080;">Object</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Child</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Grandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GreatGrandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property1</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">1</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #6666cc; font-weight: bold;color: #000080;">Object</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Child</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Grandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GreatGrandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property2</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">2</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #6666cc; font-weight: bold;color: #000080;">Object</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Child</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Grandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GreatGrandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property3</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">3</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #6666cc; font-weight: bold;color: #000080;">Object</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Child</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Grandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GreatGrandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property4</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">4</span><span style="color: #008000;color: #000000;">;</span></pre></div></div>

<p>The advent of object initializers in C# 3.0 alleviated the primary use case for the With statement.  But object initializers don&#8217;t do much for the above case or any situation where you have a pre-existing instance.</p>
<p>I recently realized though that I can get 99% of the way there with a combination of two other C# 3.0 features, extension methods and lambda expressions.  Putting the two together, I came up with the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #0600FF; font-weight: bold;color: #000000;">public</span> <span style="color: #0600FF; font-weight: bold;color: #000000;">static</span> <span style="color: #6666cc; font-weight: bold;color: #000080;">void</span> With<span style="color: #008000;color: #000000;">&lt;</span>T<span style="color: #008000;color: #000000;">&gt;</span><span style="color: #008000;color: #000000;">&#40;</span><span style="color: #0600FF; font-weight: bold;color: #000000;">this</span> T obj, Action<span style="color: #008000;color: #000000;">&lt;</span>T<span style="color: #008000;color: #000000;">&gt;</span> action<span style="color: #008000;color: #000000;">&#41;</span>
<span style="color: #008000;color: #000000;">&#123;</span>
    action<span style="color: #008000;color: #000000;">&#40;</span>obj<span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span>
<span style="color: #008000;color: #000000;">&#125;</span></pre></div></div>

<p>Which lets me rewrite the example code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;color: #000080;"><span style="color: #6666cc; font-weight: bold;color: #000080;">Object</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Child</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Grandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">GreatGrandchild</span><span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">With</span><span style="color: #008000;color: #000000;">&#40;</span>x <span style="color: #008000;color: #000000;">=&gt;</span>
    <span style="color: #008000;color: #000000;">&#123;</span>
        x<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property1</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">1</span><span style="color: #008000;color: #000000;">;</span>
        x<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property2</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">2</span><span style="color: #008000;color: #000000;">;</span>
        x<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property3</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">3</span><span style="color: #008000;color: #000000;">;</span>
        x<span style="color: #008000;color: #000000;">.</span><span style="color: #0000FF;">Property4</span> <span style="color: #008000;color: #000000;">=</span> <span style="color: #FF0000;color: #800000;">4</span><span style="color: #008000;color: #000000;">;</span>
    <span style="color: #008000;color: #000000;">&#125;</span><span style="color: #008000;color: #000000;">&#41;</span><span style="color: #008000;color: #000000;">;</span></pre></div></div>

<p>Its not perfect and I can see that depending on your stylistic preferences this might drive you nuts, but I think its some very clean code.</p>
<h3>Author&#8217;s Update:</h3>
<p>I recently discovered that Anay Kamat <a href="http://anaykamat.com/2009/08/09/simple-equivalent-of-with-statement-in-c-sharp/" target="_blank">posted a different solution</a> on his blog a way back in &#8217;09.  His approach uses reflection and results in a different usage syntax.  I recommend you take a look and use whichever approach you like best.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.baltrinic.com/software-development/dotnet/c-equivalent-for-visual-basic-with-statement/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

