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 3608968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T21:34:03+00:00 2026-05-18T21:34:03+00:00

I am using ASP.NET MVC 3 and Autofac for my dependency injection. I am

  • 0

I am using ASP.NET MVC 3 and Autofac for my dependency injection. I am using AutoMapper for my mapping.

I have an IMapper class that I use for all my model view mappings. So any any of my mapping classes can implement this interface. In the controller below the constructor receives an IMapper instance, and in my User controller it might receive a different instance, maybe userMapper. Getting back to the code below, I have a class called NewsMapper and it implements IMapper. How do I setup dependency injection so that this controller must receive an instance of NewsMapper? Please bear in mind that I might have another mapper called UserMapper.

I have the following controller:

public class NewsController
{
   private INewsService newsService;
   private IMapper newsMapper;

   public NewsController(INewsService newsService, IMapper newsMapper)
   {
      if (newsService == null)
      {
         throw new ArgumentNullException("newsService");
      }

      if (newsMapper == null)
      {
         throw new ArgumentNullException("newsMapper");
      }

      this.newsService = newsService;
      this.newsMapper = newsMapper;
   }
}

I have the following configuration in my global.asax.cs:

var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterType<NewsService>().As<INewsService>();
builder.RegisterType<NewsRepository>().As<INewsRepository>();

UPDATED:

My IMapper interface:

public interface IMapper
{
   object Map(object source, Type sourceType, Type destinationType);
}

My NewsMapper class:

public class NewsMapper : IMapper
{
   static NewsMapper()
   {
      Mapper.CreateMap<News, NewsViewModel>();
      Mapper.CreateMap<NewsViewModel, News>();
   }

   public object Map(object source, Type sourceType, Type destinationType)
   {
      return Mapper.Map(source, sourceType, destinationType);
   }
}

My controller action method where I do the mappings:

[HttpPost]
public ActionResult Create(NewsViewModel newsViewModel)
{
   // Check model state
   if (!ModelState.IsValid)
   {
      return View("Create", newsViewModel);
   }

   // Do mapping
   var news = (News)newsMapper.Map(newsViewModel, typeof(NewsViewModel), typeof(News));

   // Add to database via news service

   // Redirect to list view
   return RedirectToAction("List", "News");
}
  • 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-05-18T21:34:04+00:00Added an answer on May 18, 2026 at 9:34 pm

    The problem here is the broadness of the IMapper contract. It is too general – the NewsController wants to map something like News to NewsViewModel but IMapper just says “maps something to something.”

    Instead, have a look at creating a generic variant such as IMapper<TFrom,TTo>. Then, you can set up container so that the NewsController receives an IMapper<News,NewsModel> which is unambiguous and should uniquely match the NewsMapper component (however you decide to set that up.)

    Edit

    For examples/variants on the generic mapper theme see:

    • http://consultingblogs.emc.com/owainwragg/archive/2010/12/15/automapper-profiles.aspx
    • http://lucisferre.net/2009/12/31/graphite-update-the-automapfilter-for-model-e280933e-viewmodel-mapping/
    • Specifying a default Unity type mapping for a generic interface and class pair
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Autofac with ASP.NET MVC so all my controllers have their dependencies resolved
Using ASP.NET MVC 2.0, I have an actionlink that is used for comments on
using ASP.NET MVC 1.0 and I have a action that returns a JsonResult and
Using ASP.NET MVC & Spark, I have a view that is listing a number
I have some questions regarding a bootstrapper class. I am using ASP.NET MVC 3
I am using asp.net MVC to develop an application that will have ajax interactions.
using ASP.NET MVC, I have a Model, to which I'm attaching attributes so that
I am using Autofac with ASP.NET MVC integration, all my controllers receive dependencies and
Using Asp.net MVC, I have one view that's strongly bind to List , and
I'm using ASP.NET MVC 3 and have my custom model binder. public class NewsModelBinder

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.