The Bitter Coder Tutorials, Binsor Style IV: Switching Configs

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:

machineName=System.Environment.MachineName

if machineName.ToUpper() == “DEVBOX”:

connectionString=”connStringDEV”

else:

connectionString=”connStringPROD”

component “class.conn”, ConfigDepot:

ConnectionString = connectionString

And the program….

static void Main(string[] args)

{

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

Console.WriteLine(cl.ConnectionString);

}

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.

Onward…next time we have Configuration Parameters.

Advertisements

About Ruprict

I am a nerd that is a Nerd Wannabe. I have more kids than should be allowed by law, a lovely wife, and a different sense of humor than most. I work in the field of GIS, where I am still trying to find myself on the map. View all posts by Ruprict

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: