Framework Madness!

And other adventures C# and …

Posts Tagged ‘StructureMap

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 ,