AutoMapper StructureMap Profile

Get rid of that static Mapper reference and inject a mapper into your services with StructureMap.

Below is an updated StructureMap registry to wire up all the bits of AutoMapper 2.0 for dependency injection:

public class AutomapperRegistry : Registry
    public AutomapperRegistry()
        For<IConfigurationProvider>().Use(ctx => ctx.GetInstance<ConfigurationStore>());
        For<IConfiguration>().Use(ctx => ctx.GetInstance<ConfigurationStore>());

        Scan(scan =>

When you need an AutoMapper mapper, just add a constructor dependency of type IMappingEngine.

You'll need to configure when your application starts. Here's my bootstrapper:

public static class AutoMapperBootstrapper
    private static readonly IConfiguration cfg = ObjectFactory.GetInstance<IConfiguration>();

    public static void Initialize()

    public class MyAutoMapperProfile : Profile
        protected override void Configure()
            CreateMap<SomeEntity, SomeViewModel>();

Then we just call AutoMapperBootstrapper.Initialize() in global.asax.


Ben Foster

About Me

I'm a software engineer and aspiring entrepreneur with 15 years experience in tech and have worked with companies of all sizes in areas such as healthcare, agriculture, recruitment, e-commerce and finance. I founded my first startup Fabrik in 2011.

I now head up the engineering team at If you're interested in working in an exciting and innovative fin-tech company, drop me a message.

Creative Commons Licence