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
- Open Visual Studio
- 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)
- 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)
- Replace the contents of Program.cs with the code at the bottom of this post
- Run the application to see the English greeting.
Notes
- The call to ObjectFactory.GetInstance in the Main method demonstrates how you get an object from StructureMap. Notice that it only specifies the interface for the type you'd like to retrieve so the caller is decoupled from the actual implementation that is used.
- The ConfigureDependencies method is where I tell StructureMap which concrete implementations I would like to use for my interfaces. You can accomplish this same goal by decorating your interfaces and classes with attributes, or by using a configuration file.
- Aside from the two bullet items above in the Program class, none of the rest of the code has any reference to or knowledge of StructureMap.
- Notice that I only ever ask for an IAppEngine. I never had to explicitly ask for an IGreeter or IOutputDisplay. StructureMap was smart enough to recognize these dependencies of AppEngine and injected them automatically.
- Change the second line in ConfigureDependencies so that TheDefaultIsConcreteType<FrenchGreeter> and run the application again. Notice that you now see the French greeting. You have changed the implementation that is used by AppEngine, without changing any of the application code (if you agree that the Program class is just your launching point, and not really part of the application logic). If you were using a config file to configure StructureMap, you wouldn't have had to change any code at all to see the different behavior.
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
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
Thanks
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