Article Index

Hello StructureMap

In a comment on Derik Whittaker's post about using app.config with StructureMap, Brian Johnston asks for a simple "hello world" example. I figured I could re-create the app that I used when I was first learning StructureMap. This is a very simple demonstration of how to make an application use StructureMap. It does not get into the whys and whens for different scenarios. It is simply a piece of code that will let you see StructureMap "work" so that you can play with it. For more introductory details, I would highly recommend reading Chad Myers' posts that cover Basic and Medium-level usage scenarios for StructureMap.

Create the app

  1. Open Visual Studio
  2. Go to File | New | Console Application (hopefully you have Tools | Options | Projects and Solutions | "Save new projects when created" UNCHECKED so you aren't forced to choose a path for this throwaway code)
  3. Add a reference to StructureMap.dll (agonize over how slow the Add Reference dialog box is to open because it needs to populate the list on the .NET tab which will go unused when we switch to the Browse tab)
  4. Replace the contents of Program.cs with the code at the bottom of this post
  5. Run the application to see the English greeting.

Notes

Code

Update: I've created a gist that shows the original example, but using the cleaner syntax introduced in more recent versions of StructureMap.

The original code from this post that used older StructureMap syntax:

using System; using StructureMap; namespace ConsoleApplication1 { class Program { private static void Main(string[] args) { ConfigureDependencies(); IAppEngine appEngine = ObjectFactory.GetInstance<IAppEngine>(); appEngine.Run(); } private static void ConfigureDependencies() { StructureMapConfiguration.ForRequestedType<IAppEngine>().TheDefaultIsConcreteType<AppEngine>(); StructureMapConfiguration.ForRequestedType<IGreeter>().TheDefaultIsConcreteType<EnglishGreeter>(); StructureMapConfiguration.ForRequestedType<IOutputDisplay>().TheDefaultIsConcreteType<ConsoleOutputDisplay>(); } } public class AppEngine : IAppEngine { private readonly IGreeter greeter; private readonly IOutputDisplay outputDisplay; public AppEngine(IGreeter greeter, IOutputDisplay outputDisplay) { this.greeter = greeter; this.outputDisplay = outputDisplay; } public void Run() { outputDisplay.Show(greeter.GetGreeting()); } } public interface IAppEngine { void Run(); } public interface IGreeter { string GetGreeting(); } public class EnglishGreeter : IGreeter { public string GetGreeting() { return "Hello"; } } public class FrenchGreeter : IGreeter { public string GetGreeting() { return "Bonjour"; } } public interface IOutputDisplay { void Show(string message); } public class ConsoleOutputDisplay : IOutputDisplay { public void Show(string message) { Console.WriteLine(message); } } }

Comments

Thanks for the step-by-step example and full code.

2 steps that IMHO are missing and would help me with this example:

- what version(s) of StructureMap is this example using and is it backward compatible with older versions of SM?
- how do you get the StructureMap.dll?

Thanks
Darren - August 08, 2008 09:02am
Also, how can you find out the version the StructureMap.dll is? For instance, if I am looking at an older existing app that has StructureMap.dll in it, how do I know what vesion of SM is being used?

Thanks
Darren - August 08, 2008 09:34am
Hi Joshua,

Love the post. In fact, I loved it so much I stole some of your code and wrote my own little StructureMap sample app, but I used the 2.0 version of StructureMap. You can get it here if you want to have a look:

http://creedcultcode.blogspot.com/2008/08/boba-fett-greedo-and-structuremap.html
Dale Smith - August 17, 2008 12:31pm