How to inject AutoMapper IMappingEngine with StructureMap

Posted by Jay Walker on Stack Overflow See other posts from Stack Overflow or by Jay Walker
Published on 2012-09-26T15:11:52Z Indexed on 2012/09/26 15:37 UTC
Read the original article Hit count: 1286

Most of the examples I've found for Automapper use the static Mapper object for managing type mappings. For my project, I need to inject an IMapperEngine as part of object construction using StructureMap so that we can mock the mapper in unit tests so we can't use the static mapper. I also need to support configuring AutoMapper Profiles.

My question is how can I configure the StructureMap registry so that it can supply an instance of IMappingEngine when an instance of MyService is constructed.

Here is the Service constructor signature:

public MyService(IMappingEngine mapper, IMyRepository myRepository, ILogger logger)

And here is the StructureMap Registry

public class MyRegistry : StructureMap.Configuration.DSL.Registry
{
    public MyRegistry()
    {
        For<IMyRepository>().Use<MyRepository>();
        For<ILogger>().Use<Logger>();
        //what to do for IMappingEngine?
    }
}

And the profile I want to load

public class MyAutoMapperProfile : AutoMapper.Profile
{
    protected override void Configure()
    {
        this.CreateMap<MyModel, MyDTO>();
    }
}

© Stack Overflow or respective owner

Related posts about dependency-injection

Related posts about automapper