Geeks With Blogs
Lee Brandt's Blog You're only as smart as your last line of code

I've been using StructureMap for my dependency injection container for a little while now and I wanted to discover all the ways (I could find) to register my dependencies. Here's is what I came up with (If you know of more or see something wrong drop me a comment):

I've got a project with a console app, a project for my dependencies (memento and concrete types), and a project that does my dependency registration (when I do it in code). My console app just gets the default concrete type first, then gets a named instance. Each implement the IInjectable interface.

   1:  public static void Main(string[] args) {
   2:   
   3:      //Registrar.RegisterByAddingARegistry();
   4:      //Registrar.RegisterBySearchingAnAssembly();
   5:   
   6:      // get the default concrete type
   7:      var defaultInjector = ObjectFactory.GetInstance<IInjectable>();
   8:   
   9:      // get a named concrete type
  10:      var namedInjector = ObjectFactory.GetNamedInstance<IInjectable>("Other");
  11:   
  12:      // output the types so we know we got the right ones
  13:      Console.WriteLine(defaultInjector.WhatsMyType());
  14:      Console.WriteLine(namedInjector.WhatsMyType());
  15:   
  16:      // pause so we can see
  17:      Console.WriteLine("press any key to continue...");
  18:      Console.ReadKey();
  19:  }

Output

Once the program runs, I should see (no matter which registration method I am using) a console output that looks like the screen shot to the right.

 

 

 

 

First, is the good, old-fashioned StructureMap.config file. This is probably the simplest solution and the one most people will use. It looks like:

   1:  <?xml version="1.0" encoding="utf-8" ?>
   2:  <StructureMap>
   3:    <Assembly Name="ConsoleTester" />
   4:    <PluginFamily Assembly="Dependencies.Project" Type="Dependencies.Project.IInjectable" DefaultKey="Default">
   5:      <Plugin Assembly="Dependencies.Project" Type="Dependencies.Project.InjectorOne" ConcreteKey="Default"/>
   6:      <Plugin Assembly="Dependencies.Project" Type="Dependencies.Project.InjectorTwo" ConcreteKey="Other"/>
   7:    </PluginFamily>
   8:  </StructureMap> 

Put this config file in any project that needs to have dependencies injected, and make sure to change the "Copy to Output Directory" property of the file to "Copy if newer" or "Copy always". This ensures the project needing dependency injection can use the config file.

Second, I can add a Registry to my configuration using the following code. It will override using the XML configuration file and to call AddRegistry Method. (notice the first line of the configure() override: I reset the plug in graph no need to do this if you take the StructureMap.config XML file out.):

   1:  public static void RegisterByAddingARegistry()
   2:  {
   3:      ObjectFactory.Reset();
   4:      StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
   5:      StructureMapConfiguration.AddRegistry(new InjectableRegistry());
   6:  }

where my InjectableRegistry class looks like:

   1:  public class InjectableRegistry : Registry
   2:  {
   3:      protected override void configure() {
   4:          ObjectFactory.Reset();
   5:          RegisterDefault<IInjectable, InjectorOne>();
   6:          Register<IInjectable, InjectorTwo>("Other");
   7:      }
   8:   
   9:      private static void RegisterDefault<MementoType, ConcreteType>() where ConcreteType : MementoType
  10:      {
  11:          StructureMapConfiguration
  12:              .ForRequestedType<MementoType>()
  13:              .TheDefaultIsConcreteType<ConcreteType>()
  14:              .CacheBy(InstanceScope.Singleton);
  15:      }
  16:      private static void Register<MementoType, ConcreteType>(string instanceName) where ConcreteType : 
  17:          MementoType
  18:      {
  19:          StructureMapConfiguration
  20:              .ForRequestedType<MementoType>()
  21:              .AddConcreteType<ConcreteType>(instanceName)
  22:              .CacheBy(InstanceScope.Singleton);
  23:      }
  24:  }

The Registry type is in the StructureMap.Configuration.DSL assembly. It basically allows the fluent interface to the StructureMapConfiguration class. This setup should receive the exact same screen output as before once that is done.

Lastly, I can tell StructureMap to set up all plug in families and plug ins by searching an assembly. StructureMap uses Attributes on the classes to create the configuration. Like so:

   1:  [PluginFamily("Default", IsSingleton = true)]
   2:  public interface IInjectable {
   3:      string WhatsMyType();
   4:  }
   5:   
   6:  [Pluggable("Default")]
   7:  public class InjectorOne : IInjectable {
   8:      public string WhatsMyType()
   9:      {
  10:          return "I am InjectorOne";
  11:      }
  12:  }
  13:   
  14:  [Pluggable("Other")]
  15:  public class InjectorTwo :IInjectable {
  16:      public string WhatsMyType()
  17:      {
  18:          return "I am InjectorTwo";
  19:      }
  20:  }

 

The Registrar method looks like:
   1:  public static void RegisterBySearchingAnAssembly()
   2:  {
   3:      ObjectFactory.Reset();
   4:      StructureMapConfiguration.UseDefaultStructureMapConfigFile = false;
   5:      StructureMapConfiguration.ScanAssemblies();
   6:  }

 David Hayden's post helped me a lot with this.

Hope this helps someone. It will probably help me when I forget one of these! :0)

Download the code here.

 

~Lee

Posted on Sunday, August 10, 2008 4:26 AM Dependency Injection , Learning | Back to top


Comments on this post: Three Ways to Register Dependencies with StructureMap 2.4.9

# re: Three Ways to Register Dependencies with StructureMap 2.4.9
Requesting Gravatar...
Thank you! Needed a complete, simple example!
Left by Mr_Needed_This on Aug 19, 2009 11:13 AM

# re: Three Ways to Register Dependencies with StructureMap 2.4.9
Requesting Gravatar...
You're welcome! I'm glad it helped.
Left by Lee Brandt on Aug 19, 2009 11:15 AM

# re: Three Ways to Register Dependencies with StructureMap 2.4.9
Requesting Gravatar...
Great post.What version of StructureMap is this post referencing? It would help readers if all bloggers would add the versions of tools they are working with in their posts.FYI, part of the text in this post is being cut off on the right side of page. I had to make the browser well over 1600 pixels wide (in IE 7 and FF 2) before I could read all the words.
Left by gioco del bingo on May 07, 2010 7:27 AM

Your comment:
 (will show your gravatar)


Copyright © Lee Brandt | Powered by: GeeksWithBlogs.net