Derik Whittaker



Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at
Howto: StructureMap configuration using Attributes
Since the new version of StructureMap came out a few weeks ago I have been playing around with it to see what it can do.  So far I have been very impressed and have been very happy with the support I have received from Jeremy (the author) when I had questions/issues.

During my exploring I have been looking at the different ways to configure StrucutreMap.  There are 3 different ways to setup PluginGraph 1) Inline in code, 2) Via Attributes and 3) using the configuration file supported by the tool.  In this post I will go over how to configure your PluginGraph via attributes.  I plan on posting on the other ones as well in the future.

Getting started

**** NOTE **** One thing, although we are using attributes in this example I still had to create a config file for the actual assembly to be scanned.  I hope to figure out a way around this, but at this time I don’t know of one.
**** UPDATE **** Thanks to Joshua i was able to get this to work without the use of any config file.  You will need to call StructureMapConfiguration.ScanAssemblies().IncludeTheCallingAssembly() during the startup of your application

Basic Configuration file (StructureMap.config)

Basic setup to use the configuration file

PluginFamily Attribute Usage


  • “ShoppingCartItem” – is the key given on the Pluggable attribute.  This can be anything.  Here I made it represent the concrete type name
  • “SouceType” – here I used MemoryMentoSource as the default.
  • “IsSingleton” – here I want this object to NOT be a singleton, but I could set this to true to have the ObjectBuilder return me the same instance very time.

Pluggable Attribute Usage

  • “ShoppingCartItem” – is the key for the class.  This can be any thing, here I made it represent the concrete type name.  This is the key that the PluginFamily attribute class will look form.

Calling the object builder and building your instance

There you have it, hopefully a simple example on how to use StructorMap with Attributes

Posted 04-25-2007 9:15 AM by Derik Whittaker



dave thieben wrote re: Howto: StructureMap configuration using Attributes
on 04-25-2007 4:06 PM

Thanks for the post, Derek.  I was trying to figure out how to use Attributes but did not realize you still needed a config file.  I was trying to use the ScanAssemblies() method on StructureMapConfiguration, but it wasn't working.  This should work better.


Derik Whittaker wrote re: Howto: StructureMap configuration using Attributes
on 04-25-2007 6:03 PM


I tried the ScanAssemblies as well, but that did not work.  I am going to spend some time tonight hopefully (if my son will let me) to see if there is a way around using the config file at all.

Jeremy, if you see this can you chime in.


Joshua Flanagan wrote re: Howto: StructureMap configuration using Attributes
on 04-27-2007 5:38 PM



ScanAssemblies is just the beginning of the sentence... you need to tell it what to scan.

Derik Whittaker wrote re: Howto: StructureMap configuration using Attributes
on 04-27-2007 5:57 PM


Thanks,  I will try this over the weekend.


VusCode - Coding dreams since 1998! wrote Design for testability - Structure map (Part 6)
on 03-03-2008 4:59 AM

On my quest to design for testability, I've already covered: Part 1 - Separation of concerns (SOC

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)