且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET MVC控制器中使用Automapper配置

更新时间:2023-02-10 08:27:13

 公共静态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;
}

构建映射器,并将其注册到应用程序使用的依赖容器中。 / p>

global.asax

  MapperConfiguration配置= AutoMapperConfig.Configure();; 

//构建映射器
IMapper mapper = config.CreateMapper();

// ..使用您的应用程序使用的依赖容器注册映射器。

myContainer.Register< IMapper>(映射器); // ...这只是一个过度简化的示例

更新您的控制器以通过以下方式显式依赖于映射器构造函数注入

 私有只读IMapper映射器; 

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

// ...
}

然后调用

  // ... 

注意模型=映射器。 Map< Note>(noteVM);

// ...


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?

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);

//...