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



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

Explained:

  • “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
Explained:

  • “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



Comments

dave thieben said:

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.

dave

# April 25, 2007 4:06 PM

Derik Whittaker said:

Dave,

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.

Derik

# April 25, 2007 6:03 PM

Joshua Flanagan said:

Try:

StructureMapConfiguration.ScanAssemblies().IncludeTheCallingAssembly();

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

# April 27, 2007 5:38 PM

Derik Whittaker said:

Joshua.

Thanks,  I will try this over the weekend.

derik

# April 27, 2007 5:57 PM

VusCode - Coding dreams since 1998! said:

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

# March 3, 2008 4:59 AM

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