Using StructureMap 4 in a Console App

If you’re using an older version of StructureMap or want more of an introduction, please read A Gentle Introduction to StructureMap, which includes a console application for StructureMap 2.x.

StructureMap 4 is currently the latest version (v4.0.1.318) and is what I’m using for this example. The old ObjectFactory type is no more; instead you create instances of Container as needed. For a simple console application, you should only need one such instance. Also, the preferred means of wiring up dependencies within a container is to use one or more Registry classes. My preferred approach is to use a single Registry class per project in my solution. For a single-project console app, that means a single Registry class. This also has the advantage of keeping the Main() method clean without the need for helper methods (e.g. InitIoC()).

Here is the complete example showing how I would configure a simple Console Application to use dependency injection with StructureMap 4.

I like to use the WithDefaultConventions() extension for StructureMap, because it greatly reduces how many lines of registration code you need to write. With this in place, any interface in the scanned assembly will automatically use an instance of the type with the same name, minus the “I” prefix, if it exists. Thus, if you have an interface IFoo and a class Foo, there’s no need to explicitly register them – StructureMap will use Foo wherever an IFoo is needed automatically. In the example above, I’m using this to wire up Writer to IWriter. However, since I didn’t follow this convention for my ILog implementation (I named the implementation class ConsoleLogger), I have to explicitly tell StructureMap how to resolve the ILog type. This is done on line 29:

If you’re just getting started with dependency injection or the Dependency Inversion Principle, this is a good way to start learning how it works. Just copy the above code into a new console application’s Program.cs file and try changing the types. Add an interface of your own, perhaps to change the “Hello World” line to a custom greeting. Maybe you can make it say “Good morning” or “Good afternoon” as appropriate. The nice thing about this style of programming is that it makes testing very easy. Your implementation of an IGreeting interface shouldn’t have any dependencies, so if you want to test that it produces the right greeting at the right time of day, you should be able to do so without being blocked.

Next: Configure NLog Using StructureMap (for some more advanced StructureMap usage)

  • Michael Lennon

    Hi Steve, I have been looking at using structuremap 4 to register multiple types against a single interface in a project. Its a CQRS type deal with a class library project including all commandhandlers and commanddispatchers. Existing examples out there of command wire up using StructureMap use StructureMap 2 and the ObjectFactory. Seeing as the ObjectFactory is no longer available in SM4, I am intrigued with your suggestion of using registry class per project. In an MVC solution for example, if I add a registry class to a class library project (where the commands/dispatchers/handers live), can I use a registry class in that class library project to hook the DI back to the container instance instantiated in the Web project?

    • ardalis

      Yes, that should do what you want. Obviously you can’t reference the container instance in the web project from your class library, but the registry class makes it so you don’t have to do so. You can tell StructureMap to scan the class library assembly and load all registries it finds there, which sounds like it will do what you want.

  • Soulice

    Do you happen to have any tuts on StructureMap and caliburn in WPF?

  • Sarath Chandra

    Thanks for explanation Steve. This is greater kick starter for SM4+. Your explanation here and also in pluralsight courses are really detailed.