The Bitter Coder Tutorials, Binsor Style IV: Switching Configs

Wed, Jul 16, 2008 2-minute read
Previous posts in the series: In part four of his series, the Bitter Coder (Alex) shows how easy it is to switch configurations using Windsor.  Alex moves his config into a separate file and then comments out whichever configuration is not needed.  I am gonna take a different approach with Binsor, since we have the ability to use logic in the configuration. Presuming we have 2 environments, dev and prod, and we want to deploy to both without manually switching configuration items.  (Note:  The big idea in this post is to show the logic, not the best practice.  I realize that keeping dev config info in a production file is icky.)  Anyway, let's create a class that has a connection string, the ubiquitous configuraiton item:

public class ConfigDepot


public string ConnectionString{ get;set;}


So, we need something that is different between the development and production boxes to let us know which connect string to use.  For the purposes of this article, I am choosing the hostname.  Here is the Binsor:


if machineName.ToUpper() == "DEVBOX":




component "class.conn", ConfigDepot:

ConnectionString = connectionString

And the program....

static void Main(string[] args)


ConfigDepot cl = container.Resolve<ConfigDepot>();



When you run the program (which, um, defaults to the production conn string. Yikes!) and the name of your dev box is 'DEVBOX' then you'll see "connStringDEV".  Otherwise, you'll see "connStringPROD". It'd be easy to add a slew of connection parameters here, but you should be wary of a couple of Booisms.  First, the if block is determined by the indention level.  So, everything that is indented the same amount is part of the block. (Check this out for code conventions.)  Also, the 'if' line and the 'else' line end in a colon ( : ) which is a bit odd. time we have Configuration Parameters.