Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9235957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:10:31+00:00 2026-06-18T07:10:31+00:00

Does anyone know of a comprehensive guide to setting up Automapper with Autofac. I’m

  • 0

Does anyone know of a comprehensive guide to setting up Automapper with Autofac. I’m new to both but I have played around with the static Mapper class however I want to be able to mock and inject IMappingEngine and create a configuration that sets up all my mappings. All the guides I have looked at so far don’t really explain what is going on and I can’t quite work it out. Also I am using Autofac 3.0 which seems to have some differences in the ContainerBuilder methods which doesn’t help (the reason I’m using it is that Autofac.mvc4 depends on it).

Update:

OK, the simplest solution seems to work well enough, however I had not seen it anywhere on the internet and that maybe for a good reason that I don’t know? The simplest thing to do is just to Register the static Mapper.Engine as IMappingEngine and still use the static Mapper.CreateMap to configure in the first place.

var builder = new ContainerBuilder();
builder.Register<IMappingEngine>(c => Mapper.Engine);

Now Autofac can inject the IMappingEngine into your constructors. This does mean that Mapper will handle the IMappingEngine singleton rather than Autofac and Autofac is just acting as a wrapper for it. I would like Autofac to handle the IMappingEngine instance but I’m not sure how?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T07:10:32+00:00Added an answer on June 18, 2026 at 7:10 am

    Your simple solution is OK provided that you don’t want to mock the mapper in unit tests or create mappers with modified configurations for nested lifetime scopes (the latter one looks a bit weird to me, but who knows).

    If you need that, you can pick up some pieces of code from the Mapper class and register components like this:

    builder.Register(ctx => new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers()))
           .AsImplementedInterfaces()
           .SingleInstance();
    
    builder.RegisterType<MappingEngine>()
           .As<IMappingEngine>();
    

    I’m not sure if you really need to make IMappingEngine a singleton. It should be quite lightweight to create per dependency.

    Now you can use it like this:

    // in a bootstrapper:
    var mapperConfig = ctx.Resolve<IConfiguration>();
    mapperConfig.CreateMap<A, B>();
    
    // later on:
    public class X{
        IMappingEngine _mapper;
    
        public X(IMappingEngine mapper){
            _mapper = mapper;
        }
    
        public B DoSmth(){
            return _mapper.Map<B>(new A());
        }
    }
    

    You can also set up automatic profiles registration like this:

    builder.Register(ctx => new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers()))
           .AsImplementedInterfaces()
           .SingleInstance()
           .OnActivating(x => {
               foreach (var profile in x.Context.Resolve<IEnumerable<Profile>>()){
                   x.Instance.AddProfile(profile);
               }
           });
    

    Then just register a Profile implementation anywhere in Autofac configuration or in a module to get it hooked up to the configuration.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know why UsernameExists wont return True. I must have my syntax messed
Does anyone know of a site giving a comprehensive list of APIs for retrieving
Does anyone know how to implement tls using boost sockets ? I have implemented
Does anyone know if it is possible to collect system information using the new
Does anyone know how many options a drop down list can have? Is it
Does anyone know if there is a way to generate different code in the
Does anyone know of a free tool, similar to what is built into Visual
Does anyone know of a mechanism in Sybase ASA 9 / Sybase SQL Anywhere
Does anyone know of any websites, or (preferably) downloadable packages that you can use
Does anyone know of a way to contain a nonbreaking space in an html

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.