Framework Madness!

And other adventures C# and …

StructureMap 2.6.1 – Looking for Documentation? Here are a few helpful tips.

with 7 comments

I have started to use StructureMap in a new project I’m incubating here at home. Version 2.6x is VERY easy to use, but it is not documented. I think Jeremy has done a very nice job making StructureMap much more approachable. But there has been so much change that most of the documentation and community helps you’ll find are now obsolete. I have only one complaint, and that is that the new version is not 3.0. With this many changes it should be.

So, back to the main point. The only documentation I’ve found is by looking over Jeremy Miller’s recent blog posts, a few video presentations he’s made, and the Google news group. Here is what I have found:

1) Usage should be scoped to ‘ObjectFactory.Container’. This will become convention moving forward. 
(See Figure 1)

2) If you use the ‘Initialize’ method you wipe the container. So go with ‘Configure’ if you want to progressively set up you IOC mappings.
(See Figure 1, 2)

3) Most mappings can now be made using the ‘For’ and ‘Use’ expressions. ‘Use’ is very powerful and simple with it’s overloads, with them you can specify a return type, return an object instance, and more.
(See Figure 1, 2)

4) You can create a scoped container. This creates an instance copy of ‘ObjectFactory.Container’ and applies a Profile mapping to it. So there are no worries about caching scenarios, HttpContext use, etc.  And the scoped container implements IDisposable so you can leverage a ‘Using’ block to force it to clean up.  
(See Figure 3)

5) The syntax for creating a Profile is a bit cleaner.
(See Figure 2)


Code Snippet
  1. ObjectFactory.Container.Configure(c => c.For<IModulesContext>().Use<ModulesContext>());

Figure 1

Code Snippet
  1. ObjectFactory
  2.     .Container.Configure(i =>
  3.   {
  4.       i.Profile(_StorageProfile, p => p.For<IStorageContext>().Use(new StorageContext("~/_Storage")));
  5.   });

Figure 2

Code Snippet
  1. using (IContainer Factory = ObjectFactory.Container.GetNestedContainer(routeProfile.ToString()))
  2. {
  3.     return Factory.GetInstance(controllerType) as IController;
  4. }

Figure 3


Written by Lynn Eriksen

February 21, 2010 at 9:13 pm

Posted in Uncategorized

Tagged with ,

7 Responses

Subscribe to comments with RSS.

  1. Is there *any* documentation on the 2.6 syntax? I am trying to learn StructureMap and it is very confusing to have legacy documentation that is not directly applicable to the 2.6 release. I’ve been banging my head against the wall all day trying to understand the correlation.


    April 26, 2010 at 8:00 pm

  2. Hey Dark,

    Jeremy does maintain an active monitor on a google group.

    Also, if you have any questions I’ll try to help.

    lynn eriksen

    April 26, 2010 at 8:10 pm

  3. Working with documentation is easier than constantly asking…


    June 14, 2011 at 4:06 pm

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: