Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Using your app.config (web.config) to store your StructureMap settings

With the most recent drop of StrcutreMap (2.4.9 -- aka 2.5) you now have the ability to put your wiring information inside the app.config file.  Prior to this you needed to either use the StructureMap.config file or wire your dependencies up in code. So today I thought I would simply review how to use this new feature and show how simple and easy it is.

***** Disclaimer *****
Yes, I know that some people think that using attributes for IoC wiring is evil, but I like it.  Also, because most of the stuff I work on is 'API' like, I tend to not have a single entry point so using the config files is the path with the least amount of friction.
***** End Disclaimer *****

If you have ever used the StructureMap.config file you know that a sample layout would look something like this:

<StructureMap>
<Assembly Name="Assembly_NAME_HERE" />
</StructureMap>

Well, the good news is that adding your config to an app.config (or web.config) is almost 100% the same.  Below you will find a simple app.config that has my StructureMap information inside it

<configuration>
<configSections>
<section name="StructureMap" type="StructureMap.Configuration.StructureMapConfigurationSection, StructureMap"/>
</configSections>
<StructureMap>
<Assembly Name="Assembly_NAME_HERE" />
<Assembly Name="Assembly_NAME_HERE" />
</StructureMap>

</configuration>

Just so you know, in order to use the app.config as your source, you do need to add on line of code to your application.

StructureMapConfiguration.PullConfigurationFromAppConfig = true;

So there you go, moving from the StructureMap.config to an app.config is easy and should be fairly painless.

Hope this helps.

Till next time,

[----- Remember to check out DimeCasts.Net -----]



Comments

Chrissie1 said:

I like the attributes very much. So keep using them. Allthough I like that new feature where it wires up the interface and implementation automagically when interface and class have the same name (apart from th I). But that is my lazy side ;-).

# July 24, 2008 2:56 PM

Brian Johnston said:

I was curious if you think you could do a 'hello world' on structure maps sometime...I keep reading about it on here and the site for it just gets you straight down in the weeds...I have one of those personalities of 'show me a simple demo' and from that simple demo I can determine whether it's a good fit for our team or not, versus doing 'real' research (pathetic I know).  I live for 'hello words' - it's how I decided I wanted to learn .NET instead of Java...the hello world made more sense.

You seem to enjoy them so I figured I'd ask - I'm sure I'm not the only one with a passing curiosity that isn't strong enough to go digging through the weeds to find out more...or if you have 'hello world' links you could pass along?  I mean conceptually - yeah, no biggie, potentially very help framework - but I want to see a simple walk-through of getting something going from scratch to sell me.

# July 24, 2008 8:24 PM

Joshua Flanagan said:

Brian - hope this helps:

flimflan.com/.../HelloStructureMap.aspx

# July 25, 2008 10:18 AM

Chad Myers' Blog said:

I&#39;m trying to wrap up the &quot;StructureMap: Advanced Scenarios Usage&quot; post. In the meantime

# July 26, 2008 1:54 PM

Dale Smith said:

@Brian - I swiped some of Joshua's code and cobbled together a sample app using StructureMap 2.0 instead of the 2.4.9 preview version.  Here it is:

creedcultcode.blogspot.com/.../boba-fett-greedo-and-structuremap.html

# August 17, 2008 3:52 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News