Fork me on GitHub

A Gentle Quickstart Edit on GitHub


The first thing you should know is that StructureMap (and other IoC tools like it) are designed to make compositional and modular software designs easier to build by offloading the grubby mechanics of resolving dependencies, reading configuration data, and assembling object graphs to the IoC tool instead of cluttering up your application code.

Before you get started with StructureMap it's recommended that you first get comfortable with the Software Design Concepts of Dependency Injection and Inversion Of Control. It's important that you structure and build your application with these concepts in mind to fully utilize StructureMap's abilities.

Assuming that you're already familiar with those concepts, or you'd really rather skip the pedantry and jump right into concrete code, the first thing to do is, go Get StructureMap and jump into usage.

Main Usage

By and large, you really only do two kinds of things with StructureMap:

  1. Configure the container by registering the what and how StructureMap should build or find requested services based on a type and/or name.

  2. Resolve object instances of a service or dependency built out with all of its dependencies.

So let's say that you have a simple object model like so:


public interface IBar
{
}

public class Bar : IBar
{
}

public interface IFoo
{
}

public class Foo : IFoo
{
    public IBar Bar { get; private set; }

    public Foo(IBar bar)
    {
        Bar = bar;
    }
}

You could explicitly build a StructureMap Container object to build these types like this:


// Configure and build a brand new
// StructureMap Container object
            var container = new Container(_ =>
            {
                _.For<IFoo>().Use<Foo>();
                _.For<IBar>().Use<Bar>();
            });

// Now, resolve a new object instance of IFoo
            container.GetInstance<IFoo>()
                // should be type Foo
                .ShouldBeOfType<Foo>()

                // and the IBar dependency too
                .Bar.ShouldBeOfType<Bar>();


or utilize StructureMap's type scanning conventions to configure the relationships and do the same thing like this:


var container = new Container(_ =>
{
    _.Scan(x =>
    {
        x.TheCallingAssembly();
        x.WithDefaultConventions();
    });
});

container.GetInstance<IFoo>()
    .ShouldBeOfType<Foo>()
    .Bar.ShouldBeOfType<Bar>();

Integrating StructureMap within your application

At some point you will want to integrate StructureMap into your application. Whether you are using Windows Presentation Foundation (WPF), FubuMVC, ASP.NET WebForms, ASP.NET MVC or any other framework or technology, you will have to do some sort of plumbing and bootstrapping. Depending on the used technology or framework there can be important integration points that you will have to use to fully enable the power of StructureMap.

While StructureMap doesn't provide integration support out of the box for all of the frameworks and technologies out there, we do find it important to help you get started with integrating StructureMap into your application. That said, StructureMap does provide integration support for FubuMVC (a web framework, which is part of the same family as StructureMap).

What to do when things go wrong?

StructureMap, and any other IoC tool for that matter, is configuration intensive, which means that their will be problems in that configuration. We're all moving to more convention based type registration, so more stuff is happening off stage and out of your sight, making debugging the configuration even trickier. Not to worry (too much), StructureMap has some diagnostic abilities to help you solve configuration problems:

Need Help?