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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:08:44+00:00 2026-05-24T10:08:44+00:00

The service layer of my ASP.NET MVC 3 application uses AutoMapper to map view

  • 0

The service layer of my ASP.NET MVC 3 application uses AutoMapper to map view models to business objects. I implemented type converters to convert object IDs submitted in post/get request to business objects.Type converters look up entities in the database by IDs and rebuild business from those. For example:

class UserViewModel {
   // user fields
   Guid? Manager; // this is an ID of another user in the system
}

A service will call AutoMapper:

var userModel = Mapper.Map<UserViewModel, UserModel>(viewModel);

to map to this class:

class UserModel {
    // other fields...
    UserModel Manager;
}

with the help of type converter that looked up another user in the database by Manager GUID from view model. Type converters are injected with dependencies using Ninject and everything works fine in the web application.

I’m trying to write unit-test that will use mock repositories with type converters. AutoMapper can be configured to construct services using a user provided function:

Mapper.Initialize(cfg =>
{
    cfg.ConstructServicesUsing(type => Kernel.GetService(type));
});

In web application Kernel is Ninject, for tests I decided to provided my own method that returns instances of requested types from a dictionary:

Dictionary<Type, object> typeDict = new Dictionary<Type, object>()
{
    { typeof(IRepository), new MockRepository() }
};

...
Mapper.Initialize(cfg =>
{
    cfg.ConstructServicesUsing(type => { return typeDict[type]; } );
});

The idea was that when AutoMapper is configured to convert a Guid to an object like this:

Mapper.CreateMap<Guid?, TDropDown>()
       .ConvertUsing<GuidToSelectListValueConverter<TDropDown>>();

and needs to create an new instance of GuidToSelectListValueConverter and inject it with a repository it will request the IRepository type using method configured in ConstructServicesUsing.

Instead AutoMapper actually tries to obtain an instance of GuidToSelectListValueConverter type. Which means I become responsible for creating a new instance of type converter and injecting it with dependencies. My typeDict should look like this instead:

Dictionary<Type, object> typeDict = new Dictionary<Type, object>()
{
    { typeof(GuidToSelectListValueConverter), new GuidToSelectListValueConverter(
               new MockRepository()) }
};

I do have a lot of type converters and manually write resolver functions for those doesn’t seem feasible. So that got me thinking. Would it be wrong to simply configure Ninject for to resolve these dependencies in my unit test? I could bind IRepository to MockRepository and then when I need an instance of service simply call Kernel.GetService(typeof(MyService)) and have Ninject inject it with MockRepository. The added benefit is that AutoMapper will also use Ninject to get instances of type converters and value resolvers:

// configure Ninject
Kernel.Bind<IRepository>().To<MockRepository>();
// initialize AutoMapper
Mapper.Initialize(cfg => 
{ 
    cfg.ConstructServicesUsing(t => Kernel.GetService(t)); 
});
// create an instance of service to test
var service = Kernel.GetService(IMyService);
// do work
var result = service.DoWork();
// analyze result

Now when DoWork method calls Mapper.Map type converters are created using Ninject.

Any suggestions, ideas and best practices advices are welcome.

Thank you!

  • 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-24T10:08:45+00:00Added an answer on May 24, 2026 at 10:08 am

    Check out this post on using IoC with AutoMapper.

    This helped me get going in the right direction by using IMappingEngine instead of Mapper in my controllers.

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

Sidebar

Related Questions

I have this sort of format asp.net MVC View -> Service Layer -> Repository.
I'm developing a ASP.NET MVC application with 3 layer classic architecture 1. data access
I'm developing an application using asp.net mvc, NHibernate and DDD. I have a service
Say you have an ASP.NET MVC project and are using a service layer, such
I have an asp.net mvc application with three layers: - data layer with entities
I've got a generic ASP.NET (MVC) application, that uses NHibernate as the model persistence
My ASP.NET MVC 2 controllers are currently instantiating service objects in their constructors by
In my ASP.NET MVC project, my actions typically call a Service layer to get
I'm using AutoMapper in an ASP.NET MVC application. I was told that I should
In my ASP.NET MVC application, I have a project that contains all the business

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.