Stacy
Stacy

Reputation: 107

How to use Automapper configuration in ASP.NET MVC controllers

I am using AutoMapper to convert my models into view models. I have the configuration all setup, tested, and working. For reference, this is what my configure method looks like:

    public static MapperConfiguration Configure()
    {
            MapperConfiguration mapperConfiguration = new MapperConfiguration(cfg => {
                cfg.CreateMap<Ebill, EbillHarvesterTaskVM>()
                cfg.CreateMap<Note, NoteVM>();
                cfg.CreateMap<LoginItem, LoginCredentialVM>()
                cfg.CreateMap<Login, ProviderLoginVM>()
            });

            mapperConfiguration.CreateMapper();

            return mapperConfiguration;
     }

This is what my test looks like:

public void ValidConfigurationTest()
{
    var config = AutoMapperConfig.Configure();
    config.AssertConfigurationIsValid();
}

What I don't understand is how to access it to actually map one object to another from within my Controller. I know I can call this config method when my app starts up, I have an application configuration class that is called from global.asax that calls my automapper configuration method. I'm not sure how to access all of this from within the controller though. I've read things that say dependency injection, but I'm not familiar enough with what that means to know how to apply it.

I've used Automapper in the past, but I think I implemented the now unavailable static API. Where the config method looks like this:

public static void RegisterMappings()
    {
        AutoMapper.Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<ManagementCompany, ManagementCompanyViewModel>();
            cfg.CreateMap<ManagementCompanyViewModel, ManagementCompany>();

        });
    }

The configuration is called in Global.asax

AutoMapperConfig.RegisterMappings();

And where you can call this within a controller to utilize mapping:

AutoMapper.Mapper.Map(managementCompany, managementCompanyVM);

This way doesn't work anymore. When I type AutoMapperMapper there is no Map method to call. What do I need to do to be able to access my mappings and use them?

Upvotes: 3

Views: 8822

Answers (1)

Nkosi
Nkosi

Reputation: 247018

public static MapperConfiguration Configure() {
        MapperConfiguration mapperConfiguration = new MapperConfiguration(cfg => {
            cfg.CreateMap<Ebill, EbillHarvesterTaskVM>()
            cfg.CreateMap<Note, NoteVM>();
            cfg.CreateMap<LoginItem, LoginCredentialVM>()
            cfg.CreateMap<Login, ProviderLoginVM>()
        });

        return mapperConfiguration;
 }

build the mapper and register it with the dependency container used by your application.

global.asax

MapperConfiguration config = AutoMapperConfig.Configure();;

//build the mapper
IMapper mapper = config.CreateMapper();

//..register mapper with the dependency container used by your application.

myContainer.Register<IMapper>(mapper); //...this is just an oversimplified example

Update your controllers to explicitly depend on the mapper via constructor injection

private readonly IMapper mapper;

public MyController(IMapper mapper, ...) {
    this.mapper = mapper;

    //...
}

And call the mapper as needed in the controller actions.

//...

Note model = mapper.Map<Note>(noteVM);

//...

Upvotes: 1

Related Questions